Class: FastlaneCore::Configuration

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(available_options, values) ⇒ Configuration

Setting up



12
13
14
15
16
17
18
19
# File 'lib/fastlane_core/configuration.rb', line 12

def initialize(available_options, values)
  @available_options = available_options
  @values = values || {}

  verify_input_types      
  verify_value_exists
  verify_no_duplicates
end

Class Method Details

.create(available_options, values) ⇒ Object



6
7
8
# File 'lib/fastlane_core/configuration.rb', line 6

def self.create(available_options, values)
  Configuration.new(available_options, 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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/fastlane_core/configuration.rb', line 57

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 }}".red unless option

  # `if value == nil` instead of ||= because false is also a valid value

  value ||= @values[key]
  # TODO: configuration files

  if value == nil
    value = ENV[option.env_name]
    option.verify!(value) if value
  end

  value = option.default_value if value == nil
  value = false if (value == nil and not option.is_string) # by default boolean flags are false

  while value == nil and !option.optional
    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

#option_for_key(key) ⇒ Object

Returns the config_item object for a given key



106
107
108
# File 'lib/fastlane_core/configuration.rb', line 106

def option_for_key(key)
  @available_options.find { |o| o.key == key }
end

#set(key, value) ⇒ Object

Overwrites or sets a new value for a given key



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/fastlane_core/configuration.rb', line 91

def set(key, value)
  raise "Key '#{key}' must be a symbol. Example :app_id.".red unless key.kind_of?Symbol
  option = option_for_key(key)
  
  unless option
    raise "Could not find available option '#{key}' in the list of !available options #{@available_options.collect { |a| a.key }}".red
  end

  option.verify!(value)

  @values[key] = value
  true
end

#verify_input_typesObject



21
22
23
24
25
26
27
# File 'lib/fastlane_core/configuration.rb', line 21

def verify_input_types
  raise "available_options parameter must be an array of ConfigItems".red unless @available_options.kind_of?Array
  @available_options.each do |item|
    raise "available_options parameter must be an array of ConfigItems".red unless item.kind_of?ConfigItem
  end
  raise "values parameter must be a hash".red unless @values.kind_of?Hash
end

#verify_no_duplicatesObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fastlane_core/configuration.rb', line 41

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_existsObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fastlane_core/configuration.rb', line 29

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 available option '#{key}' in the list of available options #{@available_options.collect { |a| a.key }}".red
    end
  end
end