Class: FastlaneCore::ConfigurationFile

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

Overview

Responsible for loading configuration files

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, path, block_for_missing) ⇒ ConfigurationFile

Returns a new instance of ConfigurationFile.

Parameters:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fastlane_core/configuration/configuration_file.rb', line 9

def initialize(config, path, block_for_missing)
  self.config = config
  @block_for_missing = block_for_missing
  content = File.read(path)

  # From https://github.com/orta/danger/blob/master/lib/danger/Dangerfile.rb
  if content.tr!('“”‘’‛', %(""'''))
    UI.error("Your #{File.basename(path)} has had smart quotes sanitised. " \
              'To avoid issues in the future, you should not use ' \
              'TextEdit for editing it. If you are not using TextEdit, ' \
              'you should turn off smart quotes in your editor of choice.')
  end

  begin
    # rubocop:disable Lint/Eval
    eval(content) # this is okay in this case
    # rubocop:enable Lint/Eval

    print_resulting_config_values(path) # only on success
  rescue SyntaxError => ex
    line = ex.to_s.match(/\(eval\):(\d+)/)[1]
    UI.user_error!("Syntax error in your configuration file '#{path}' on line #{line}: #{ex}")
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/fastlane_core/configuration/configuration_file.rb', line 58

def method_missing(method_sym, *arguments, &block)
  # First, check if the key is actually available
  if self.config.all_keys.include?(method_sym)
    # This silently prevents a value from having its value set more than once.
    return unless self.config._values[method_sym].to_s.empty?

    value = arguments.first
    value = yield if value.nil? && block_given?

    return if value.nil?
    self.modified_values[method_sym] = value
    self.config[method_sym] = value
  else
    # We can't set this value, maybe the tool using this configuration system has its own
    # way of handling this block, as this might be a special block (e.g. ipa block) that's only
    # executed on demand
    if @block_for_missing
      @block_for_missing.call(method_sym, arguments, block)
    else
      self.config[method_sym] = '' # important, since this will raise a good exception for free
    end
  end
end

Instance Attribute Details

#configObject

A reference to the actual configuration



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

def config
  @config
end

Instance Method Details

#modified_valuesObject

This is used to display only the values that have changed in the summary table



54
55
56
# File 'lib/fastlane_core/configuration/configuration_file.rb', line 54

def modified_values
  @modified_values ||= {}
end


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fastlane_core/configuration/configuration_file.rb', line 34

def print_resulting_config_values(path)
  require 'terminal-table'
  UI.success("Successfully loaded '#{File.expand_path(path)}' 📄")

  # Show message when self.modified_values is empty
  if self.modified_values.empty?
    UI.important("No values defined in '#{path}'")
    return
  end

  rows = self.modified_values.collect do |key, value|
    [key, value] if value.to_s.length > 0
  end.compact

  puts ""
  puts Terminal::Table.new(rows: rows, title: "Detected Values from '#{path}'")
  puts ""
end