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, type: nil, optional: nil, conflicting_options: nil, conflict_block: nil, deprecated: nil) ⇒ 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 (defaults to: true)

    *DEPRECATED: Use ‘type` instead* (Boolean) is that parameter a string? Defaults to true. If it’s true, the type string will be verified.

  • type (Class) (defaults to: nil)

    the data type of this config item. Takes precedence over ‘is_string`

  • optional (Boolean) (defaults to: nil)

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

  • conflicting_options ([]) (defaults to: nil)

    array of conflicting option keys(@param key). This allows to resolve conflicts intelligently

  • conflict_block (defaults to: nil)

    an optional block which is called when options conflict happens

  • deprecated (String) (defaults to: nil)

    Set if the option is deprecated. A deprecated option should be optional and is made optional if the parameter isn’t set, and fails otherwise



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fastlane_core/configuration/config_item.rb', line 20

def initialize(key: nil, env_name: nil, description: nil, short_option: nil, default_value: nil, verify_block: nil, is_string: true, type: nil, optional: nil, conflicting_options: nil, conflict_block: nil, deprecated: nil)
  UI.user_error!("key must be a symbol") unless key.kind_of? Symbol
  UI.user_error!("env_name must be a String") unless (env_name || '').kind_of? String

  if short_option
    UI.user_error!("short_option must be a String of length 1") unless short_option.kind_of? String and short_option.delete('-').length == 1
  end
  if description
    UI.user_error!("Do not let descriptions end with a '.', since it's used for user inputs as well") if description[-1] == '.'
  end

  if type.to_s.length > 0 and short_option.to_s.length == 0
    UI.user_error!("Type '#{type}' for key '#{key}' requires a short option")
  end

  if conflicting_options
    conflicting_options.each do |conflicting_option_key|
      UI.user_error!("Conflicting option key must be a symbol") unless conflicting_option_key.kind_of? Symbol
    end
  end
  if deprecated
    # deprecated options are automatically optional
    optional = true if optional.nil?
    UI.crash!("Deprecated option must be optional") unless optional
    # deprecated options are marked deprecated in their description
    description = "[DEPRECATED!] #{deprecated} - #{description}"
  end
  optional = false if optional.nil?

  @key = key
  @env_name = env_name
  @description = description
  @short_option = short_option
  @default_value = default_value
  @verify_block = verify_block
  @is_string = is_string
  @data_type = type
  @optional = optional
  @conflicting_options = conflicting_options
  @conflict_block = conflict_block
  @deprecated = deprecated
end

Instance Attribute Details

#conflict_blockObject

Returns the value of attribute conflict_block.



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

def conflict_block
  @conflict_block
end

#conflicting_optionsObject

Returns the value of attribute conflicting_options.



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

def conflicting_options
  @conflicting_options
end

#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

#deprecatedObject

Returns the value of attribute deprecated.



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

def deprecated
  @deprecated
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

#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)



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/fastlane_core/configuration/config_item.rb', line 93

def auto_convert_value(value)
  # Weird because of https://stackoverflow.com/questions/9537895/using-a-class-object-in-case-statement
  case
  when data_type == Array
    return value.split(',') if value.kind_of?(String)
  when data_type == Integer
    return value.to_i
  when data_type == Float
    return value.to_f
  else
    # Special treatment if the user specififed true, false or YES, NO
    # There is no boolean type, so we just do it here
    if %w(YES yes true TRUE).include?(value)
      return true
    elsif %w(NO no false FALSE).include?(value)
      return false
    end
  end

  return value # fallback to not doing anything
end

#data_typeObject

Determines the defined data type of this ConfigItem



116
117
118
119
120
121
122
# File 'lib/fastlane_core/configuration/config_item.rb', line 116

def data_type
  if @data_type
    @data_type
  else
    (@is_string ? String : nil)
  end
end

#string?Boolean

Replaces the attr_accessor, but maintains the same interface

Returns:

  • (Boolean)


125
126
127
# File 'lib/fastlane_core/configuration/config_item.rb', line 125

def string?
  data_type == String
end

#to_sObject



129
130
131
# File 'lib/fastlane_core/configuration/config_item.rb', line 129

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)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fastlane_core/configuration/config_item.rb', line 71

def valid?(value)
  # we also allow nil values, which do not have to be verified.
  if value
    # Verify that value is the type that we're expecting, if we are expecting a type
    if data_type && !value.kind_of?(data_type)
      UI.user_error!("'#{self.key}' value must be a #{data_type}! Found #{value.class} instead.")
    end

    if @verify_block
      begin
        @verify_block.call(value)
      rescue => ex
        UI.error "Error setting value '#{value}' for option '#{@key}'"
        raise Interface::FastlaneError.new, ex.to_s
      end
    end
  end

  true
end

#verify!(value) ⇒ Object

This will raise an exception if the value is not valid



64
65
66
67
# File 'lib/fastlane_core/configuration/config_item.rb', line 64

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