Class: FastlaneCore::ConfigItem

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key: nil, env_name: nil, description: nil, short_option: nil, default_value: nil, verify_block: nil, is_string: true, optional: false) ⇒ ConfigItem

Creates a new option

Parameters:

  • key (Symbol) (defaults to: nil)

    the key which is used as command paramters or key in the fastlane tools

  • env_name (String) (defaults to: nil)

    the name of the environment variable, which is only used if no other values were found

  • description (String) (defaults to: nil)

    A description shown to the user

  • short_option (String) (defaults to: nil)

    A string of length 1 which is used for the command parameters (e.g. -f)

  • default_value (defaults to: nil)

    the value which is used if there was no given values and no environment values

  • verify_block (defaults to: nil)

    an optional block which is called when a new value is set. Check value is valid. This could be type checks or if a folder/file exists You have to raise a specific exception if something goes wrong. Append .red after the string

  • is_string (String) (defaults to: true)

    is that parameter a string? Defaults to true. If it’s true, the type string will be verified.

  • optional (Boolean) (defaults to: false)

    is false by default. If set to true, also string values will not be asked to the user



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fastlane_core/configuration/config_item.rb', line 16

def initialize(key: nil, env_name: nil, description: nil, short_option: nil, default_value: nil, verify_block: nil, is_string: true, optional: false)
  raise "key must be a symbol" unless key.kind_of? Symbol
  raise "env_name must be a String" unless (env_name || '').kind_of? String
  if short_option
    raise "short_option must be a String of length 1" unless short_option.kind_of? String and short_option.delete('-').length == 1
  end
  if description
    raise "Do not let descriptions end with a '.', since it's used for user inputs as well".red if (description[-1] == '.')
  end

  @key = key
  @env_name = env_name
  @description = description
  @short_option = short_option
  @default_value = default_value
  @verify_block = verify_block
  @is_string = is_string
  @optional = optional
end

Instance Attribute Details

#default_valueObject

Returns the value of attribute default_value.



3
4
5
# File 'lib/fastlane_core/configuration/config_item.rb', line 3

def default_value
  @default_value
end

#descriptionObject

Returns the value of attribute description.



3
4
5
# File 'lib/fastlane_core/configuration/config_item.rb', line 3

def description
  @description
end

#env_nameObject

Returns the value of attribute env_name.



3
4
5
# File 'lib/fastlane_core/configuration/config_item.rb', line 3

def env_name
  @env_name
end

#is_stringObject

Returns the value of attribute is_string.



3
4
5
# File 'lib/fastlane_core/configuration/config_item.rb', line 3

def is_string
  @is_string
end

#keyObject

Returns the value of attribute key.



3
4
5
# File 'lib/fastlane_core/configuration/config_item.rb', line 3

def key
  @key
end

#optionalObject

Returns the value of attribute optional.



3
4
5
# File 'lib/fastlane_core/configuration/config_item.rb', line 3

def optional
  @optional
end

#short_optionObject

Returns the value of attribute short_option.



3
4
5
# File 'lib/fastlane_core/configuration/config_item.rb', line 3

def short_option
  @short_option
end

#verify_blockObject

Returns the value of attribute verify_block.



3
4
5
# File 'lib/fastlane_core/configuration/config_item.rb', line 3

def verify_block
  @verify_block
end

Instance Method Details

#auto_convert_value(value) ⇒ Object

Returns an updated value type (if necessary)



65
66
67
68
69
70
71
72
73
74
# File 'lib/fastlane_core/configuration/config_item.rb', line 65

def auto_convert_value(value)
  # Special treatment if the user specififed true, false or YES, NO
  if %w(YES yes true).include?(value)
    value = true
  elsif %w(NO no false).include?(value)
    value = false
  end

  return value
end

#to_sObject



76
77
78
# File 'lib/fastlane_core/configuration/config_item.rb', line 76

def to_s
  [@key, @description].join(": ")
end

#valid?(value) ⇒ Boolean

Make sure, the value is valid (based on the verify block) Returns false if that’s not the case

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fastlane_core/configuration/config_item.rb', line 44

def valid?(value)
  # we also allow nil values, which do not have to be verified.
  if value
    if @is_string
      raise "'#{self.key}' value must be a String! Found #{value.class} instead.".red unless value.kind_of? String
    end

    if @verify_block
      begin
        @verify_block.call(value)
      rescue => ex
        Helper.log.fatal "Error setting value '#{value}' for option '#{@key}'".red
        raise ex
      end
    end
  end

  true
end

#verify!(value) ⇒ Object

This will raise an exception if the value is not valid



37
38
39
40
# File 'lib/fastlane_core/configuration/config_item.rb', line 37

def verify!(value)
  raise "Invalid value '#{value}' for option '#{self}'".red unless valid? value
  true
end