Class: FastlaneCore::Configuration
- Inherits:
-
Object
- Object
- FastlaneCore::Configuration
- Defined in:
- lib/fastlane_core/configuration/configuration.rb
Actually using the class collapse
-
#all_keys ⇒ Array
readonly
An array of symbols which are all available keys.
-
#values ⇒ Object
Returns the value of attribute values.
Instance Attribute Summary collapse
-
#available_options ⇒ Object
Returns the value of attribute available_options.
-
#config_file_name ⇒ String
The name of the configuration file (not the path).
Setting up the configuration collapse
-
#initialize(available_options, values) ⇒ Configuration
constructor
A new instance of Configuration.
-
#load_configuration_file(config_file_name = nil, block_for_missing = nil) ⇒ Object
This method takes care of parsing and using the configuration file as values Call this once you know where the config file might be located Take a look at how ‘gym` uses this method.
-
#verify_default_value_matches_verify_block ⇒ Object
Verifies the default value is also valid.
- #verify_input_types ⇒ Object
- #verify_no_duplicates ⇒ Object
- #verify_value_exists ⇒ Object
Actually using the class collapse
-
#fetch(key) ⇒ Object
(also: #[])
Returns the value for a certain key.
-
#option_for_key(key) ⇒ Object
Returns the config_item object for a given key.
-
#set(key, value) ⇒ Object
(also: #[]=)
Overwrites or sets a new value for a given key.
Class Method Summary collapse
Constructor Details
#initialize(available_options, values) ⇒ Configuration
Returns a new instance of Configuration.
26 27 28 29 30 31 32 33 34 |
# File 'lib/fastlane_core/configuration/configuration.rb', line 26 def initialize(, values) self. = || [] self.values = values || {} verify_input_types verify_value_exists verify_no_duplicates verify_default_value_matches_verify_block end |
Instance Attribute Details
#all_keys ⇒ Array (readonly)
Returns An array of symbols which are all available keys.
13 14 15 |
# File 'lib/fastlane_core/configuration/configuration.rb', line 13 def all_keys @all_keys end |
#available_options ⇒ Object
Returns the value of attribute available_options.
8 9 10 |
# File 'lib/fastlane_core/configuration/configuration.rb', line 8 def @available_options end |
#config_file_name ⇒ String
Returns The name of the configuration file (not the path). Optional!.
16 17 18 |
# File 'lib/fastlane_core/configuration/configuration.rb', line 16 def config_file_name @config_file_name end |
#values ⇒ Object
Returns the value of attribute values.
10 11 12 |
# File 'lib/fastlane_core/configuration/configuration.rb', line 10 def values @values end |
Class Method Details
.create(available_options, values) ⇒ Object
18 19 20 |
# File 'lib/fastlane_core/configuration/configuration.rb', line 18 def self.create(, values) Configuration.new(, values) end |
Instance Method Details
#fetch(key) ⇒ Object Also known as: []
Returns the value for a certain key. fastlane_core tries to fetch the value from different sources
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/fastlane_core/configuration/configuration.rb', line 108 def fetch(key) raise "Key '#{key}' must be a symbol. Example :app_id.".red unless key.kind_of?Symbol option = option_for_key(key) raise "Could not find option for key :#{key}. Available keys: #{@available_options.collect { |a| a.key }.join(', ')}".red unless option value = @values[key] # `if value == nil` instead of ||= because false is also a valid value if value == nil and option.env_name and ENV[option.env_name] value = ENV[option.env_name].dup option.verify!(value) if value end value = option.default_value if value == nil value = nil if (value == nil and not option.is_string) # by default boolean flags are false return value unless value.nil? # we already have a value return value if option.optional # as this value is not required, just return what we have if Helper.is_test? or Helper.is_ci? # Since we don't want to be asked on tests, we'll just call the verify block with no value # to raise the exception that is shown when the user passes an invalid value set(key, '') # If this didn't raise an exception, just raise a default one raise "No value found for '#{key}'" end while value == nil Helper.log.info "To not be asked about this value, you can specify it using '#{option.key}'".yellow value = ask("#{option.description}: ") # Also store this value to use it from now on begin set(key, value) rescue Exception => ex puts ex value = nil end end value end |
#load_configuration_file(config_file_name = nil, block_for_missing = nil) ⇒ Object
This method takes care of parsing and using the configuration file as values Call this once you know where the config file might be located Take a look at how ‘gym` uses this method
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/fastlane_core/configuration/configuration.rb', line 90 def load_configuration_file(config_file_name = nil, block_for_missing = nil) return unless config_file_name self.config_file_name = config_file_name paths = Dir["./fastlane/#{self.config_file_name}"] + Dir["./#{self.config_file_name}"] paths = paths + Dir["./spec/fixtures/#{self.config_file_name}"] if Helper.is_test? return if paths.count == 0 path = paths.first ConfigurationFile.new(self, path, block_for_missing) end |
#option_for_key(key) ⇒ Object
Returns the config_item object for a given key
181 182 183 |
# File 'lib/fastlane_core/configuration/configuration.rb', line 181 def option_for_key(key) @available_options.find { |o| o.key == key } end |
#set(key, value) ⇒ Object Also known as: []=
Overwrites or sets a new value for a given key
154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/fastlane_core/configuration/configuration.rb', line 154 def set(key, value) raise "Key '#{key}' must be a symbol. Example :#{key}.".red unless key.kind_of?Symbol option = option_for_key(key) unless option raise "Could not find option '#{key}' in the list of available options: #{@available_options.collect { |a| a.key }.join(', ')}".red end option.verify!(value) @values[key] = value true end |
#verify_default_value_matches_verify_block ⇒ Object
Verifies the default value is also valid
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/fastlane_core/configuration/configuration.rb', line 70 def verify_default_value_matches_verify_block @available_options.each do |item| if item.verify_block and item.default_value begin item.verify_block.call(item.default_value) rescue => ex Helper.log.fatal ex raise "Invalid default value for #{item.key}, doesn't match verify_block".red end end end end |
#verify_input_types ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/fastlane_core/configuration/configuration.rb', line 36 def verify_input_types raise "available_options parameter must be an array of ConfigItems but is #{@available_options.class}".red unless @available_options.kind_of?Array @available_options.each do |item| raise "available_options parameter must be an array of ConfigItems. Found #{item.class}.".red unless item.kind_of?ConfigItem end raise "values parameter must be a hash".red unless @values.kind_of?Hash end |
#verify_no_duplicates ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/fastlane_core/configuration/configuration.rb', line 56 def verify_no_duplicates # Make sure a key was not used multiple times @available_options.each do |current| count = @available_options.select { |option| option.key == current.key }.count raise "Multiple entries for configuration key '#{current.key}' found!".red if count > 1 unless current.short_option.to_s.empty? count = @available_options.select { |option| option.short_option == current.short_option }.count raise "Multiple entries for short_option '#{current.short_option}' found!".red if count > 1 end end end |
#verify_value_exists ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/fastlane_core/configuration/configuration.rb', line 44 def verify_value_exists # Make sure the given value keys exist @values.each do |key, value| option = option_for_key(key) if option option.verify!(value) # Call the verify block for it too else raise "Could not find option '#{key}' in the list of available options: #{@available_options.collect { |a| a.key }.join(', ')}".red end end end |