Class: AlpacaBuildTool::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/alpacabuildtool/configuration.rb

Overview

Cofniguration provides methods to retrieve from default, global and local configuration scopes and to save configuration into global or local scope

- default configuration (<GEM>/lib/data/.alpaca.conf)
- global configuration (~/.alpaca.conf)
- local configuration (<SOLUTION_FOLDER>/.alpaca.conf)

Constant Summary collapse

DATA_DIR =

Used to find default configuration

File.join(File.expand_path(File.dirname(__FILE__)), 'data')
DEFAULT_CONFIGURATION =

Default configuration file with absolute path

File.join(DATA_DIR, '.alpaca.conf')
GLOBAL_CONFIGURATION =

Global configuration file with absolute path

File.expand_path('~/.alpaca.conf')
LOCAL_CONFIGURATION_PROPERTY =

Use to define relative path to local configuration from solution

'local_configuration'
SOLUTION_NAME_VARIABLE =

Use in configuration values and in result it will be replaced with solution name

'#{solution_name}'
SOLUTION_DIR_VARIABLE =

Use in configuration values and in result it will be replaced with solution directory path

'#{solution_directory}'
CLEAN_CONFIGURATION_KEY =

Use this key to clean configuration entry

#>global configuration:
# MSBuild:
#   options:
#     target:                 ["Clean", "Rebuild"]
#     verbosity:              "minimal"
#     nologo:                 true

#>local configuration:
# MSBuild:
#   options:
#     clean_configuration:    true
#     nologo:                 true

#>results:
# MSBuild:
#   options:
#     nologo:                 true
'clean_configuration'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(solution) ⇒ Configuration

Creates instance of local configuration

solution

solution object to find configuration for



134
135
136
137
138
# File 'lib/alpacabuildtool/configuration.rb', line 134

def initialize(solution)
  local_config = load_local_configuration(solution.dir)
  @configuration = Configuration.merge(global_config, local_config)
  @configuration = detokenize(@configuration, solution)
end

Class Method Details

.clean(hash) ⇒ Object

Removes clean_configuration entry for the hash

hash

hash to be cleaned



117
118
119
120
# File 'lib/alpacabuildtool/configuration.rb', line 117

def self.clean(hash)
  hash.delete CLEAN_CONFIGURATION_KEY
  hash
end

.clean?(hash) ⇒ Boolean

Check if hash has clean_configuration entry

hash

hash to be verified

Returns:

  • (Boolean)


126
127
128
# File 'lib/alpacabuildtool/configuration.rb', line 126

def self.clean?(hash)
  hash.key? CLEAN_CONFIGURATION_KEY
end

.merge(first_config, second_config) ⇒ Object

Merges hashes recursively with higher priority for second hash Also second config can contain

clean_configuration: true

that will force to take only second configuration content for that configuration level

first_config

low priority configuration (hash)

second_config

high priority configuration



106
107
108
109
110
111
# File 'lib/alpacabuildtool/configuration.rb', line 106

def self.merge(first_config, second_config)
  return clean(second_config) if clean?(second_config)
  first_config.merge(second_config) do |_key, old_value, new_value|
    old_value.is_a?(Hash) ? merge(old_value, new_value) : new_value
  end
end

.set(properties) ⇒ Object

Saves list of properties into global scope configuration

properties

Array of properties ‘path.to.property=value’



65
66
67
68
69
70
71
# File 'lib/alpacabuildtool/configuration.rb', line 65

def self.set(properties)
  file = File.expand_path GLOBAL_CONFIGURATION
  File.new(file, 'w+').close unless File.exist? file
  config = YAML.load(File.open(file)) || {}
  properties.each { |property| set_property(config, property) }
  File.open(file, 'w+') { |f| f.write config.to_yaml }
end

.set_property(config, property) ⇒ Object

Set one property for existing configuration

config

hash to hold new property

property

property _node1.node2=value_

Configuration.set_propert(hash, 'node1.node2=value')
  # will make hash['node1']['node2'] == value
  # and create node1 or node2 if they do not exist there


82
83
84
85
86
87
88
89
90
91
92
# File 'lib/alpacabuildtool/configuration.rb', line 82

def self.set_property(config, property)
  name, value = property.split('=')
  nodes = name.split('.')
  tail = nodes.last
  current_node = config
  nodes.each do |node|
    current_node[node] ||= {}
    current_node[node] = value if node == tail
    current_node = current_node[node]
  end
end

Instance Method Details

#[](entry) ⇒ Object

Returns hash with configuration for entry

entry

entry name



157
158
159
# File 'lib/alpacabuildtool/configuration.rb', line 157

def [](entry)
  @configuration[entry]
end

#set(properties) ⇒ Object

Saves list of properties into local scope configuration

properties

Array of properties ‘path.to.property=value’



144
145
146
147
148
149
150
151
# File 'lib/alpacabuildtool/configuration.rb', line 144

def set(properties)
  File.new(@file, 'w+').close unless File.exist? @file
  config = YAML.load(File.open(@file)) || {}
  properties.each do |property|
    Configuration.set_property(config, property)
  end
  File.open(@file, 'w+') { |f| f.write config.to_yaml }
end