Class: Fastlane::FastFile

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/fast_file.rb

Constant Summary collapse

SharedValues =
Fastlane::Actions::SharedValues

Instance Attribute Summary collapse

DSL collapse

Other things collapse

Overwriting Ruby methods collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Object

Returns The runner which can be executed to trigger the given actions.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fastlane/fast_file.rb', line 13

def initialize(path = nil)
  return unless (path || '').length > 0
  UI.user_error!("Could not find Fastfile at path '#{path}'") unless File.exist?(path)
  @path = File.expand_path(path)
  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

  parse(content, @path)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Is used to look if the method is implemented as an action



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/fastlane/fast_file.rb', line 129

def method_missing(method_sym, *arguments, &_block)
  method_str = method_sym.to_s
  method_str.delete!('?') # as a `?` could be at the end of the method name

  # First, check if there is a predefined method in the actions folder
  class_name = method_str.fastlane_class + 'Action'
  class_ref = nil
  begin
    class_ref = Fastlane::Actions.const_get(class_name)
  rescue NameError
    # Action not found
    # Is there a lane under this name?
    return self.runner.try_switch_to_lane(method_sym, arguments)
  end

  # It's important to *not* have this code inside the rescue block
  # otherwise all NameErrors will be catched and the error message is
  # confusing
  if class_ref && class_ref.respond_to?(:run)
    # Action is available, now execute it
    return self.runner.execute_action(method_sym, class_ref, arguments)
  else
    UI.user_error!("Action '#{method_sym}' of class '#{class_name}' was found, but has no `run` method.")
  end
end

Instance Attribute Details

#current_platformObject

the platform in which we’re currently in when parsing the Fastfile This is used to identify the platform in which the lane is in



8
9
10
# File 'lib/fastlane/fast_file.rb', line 8

def current_platform
  @current_platform
end

#runnerObject

Stores all relevant information from the currently running process



4
5
6
# File 'lib/fastlane/fast_file.rb', line 4

def runner
  @runner
end

Instance Method Details

#actions_path(path) ⇒ Object



173
174
175
176
177
# File 'lib/fastlane/fast_file.rb', line 173

def actions_path(path)
  UI.crash!("Path '#{path}' not found!") unless File.directory?(path)

  Actions.load_external_actions(path)
end

#after_all(&block) ⇒ Object

Is executed after each test run



119
120
121
# File 'lib/fastlane/fast_file.rb', line 119

def after_all(&block)
  @runner.set_after_all(@current_platform, block)
end

#before_all(&block) ⇒ Object

Is executed before each test run



114
115
116
# File 'lib/fastlane/fast_file.rb', line 114

def before_all(&block)
  @runner.set_before_all(@current_platform, block)
end

#collectorObject



159
160
161
# File 'lib/fastlane/fast_file.rb', line 159

def collector
  runner.collector
end

#desc(string) ⇒ Object



186
187
188
# File 'lib/fastlane/fast_file.rb', line 186

def desc(string)
  desc_collection << string
end

#desc_collectionObject



190
191
192
# File 'lib/fastlane/fast_file.rb', line 190

def desc_collection
  @desc_collection ||= []
end

#error(&block) ⇒ Object

Is executed if an error occured during fastlane execution



124
125
126
# File 'lib/fastlane/fast_file.rb', line 124

def error(&block)
  @runner.set_error(@current_platform, block)
end

#import(path = nil) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/fastlane/fast_file.rb', line 194

def import(path = nil)
  UI.user_error!("Please pass a path to the `import` action") unless path

  path = path.dup.gsub("~", Dir.home)
  unless Pathname.new(path).absolute? # unless an absolute path
    path = File.join(File.expand_path('..', @path), path)
  end

  UI.user_error!("Could not find Fastfile at path '#{path}'") unless File.exist?(path)

  collector.did_launch_action(:import)
  parse(File.read(path), path)

  # Check if we can also import local actions which are in the same directory as the Fastfile
  actions_path = File.join(File.expand_path("..", path), 'actions')
  Fastlane::Actions.load_external_actions(actions_path) if File.directory?(actions_path)
end

#import_from_git(url: nil, branch: 'HEAD', path: 'fastlane/Fastfile') ⇒ Object

Parameters:

  • url (String) (defaults to: nil)

    The git URL to clone the repository from

  • branch (String) (defaults to: 'HEAD')

    The branch to checkout in the repository

  • path (String) (defaults to: 'fastlane/Fastfile')

    The path to the Fastfile



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/fastlane/fast_file.rb', line 215

def import_from_git(url: nil, branch: 'HEAD', path: 'fastlane/Fastfile')
  UI.user_error!("Please pass a path to the `import_from_git` action") if url.to_s.length == 0

  Actions.execute_action('import_from_git') do
    require 'tmpdir'

    collector.did_launch_action(:import_from_git)

    # Checkout the repo
    repo_name = url.split("/").last

    tmp_path = Dir.mktmpdir("fl_clone")
    clone_folder = File.join(tmp_path, repo_name)

    branch_option = ""
    branch_option = "--branch #{branch}" if branch != 'HEAD'

    clone_command = "git clone '#{url}' '#{clone_folder}' --depth 1 -n #{branch_option}"

    UI.message "Cloning remote git repo..."
    Actions.sh(clone_command)

    Actions.sh("cd '#{clone_folder}' && git checkout #{branch} '#{path}'")

    # We also want to check out all the local actions of this fastlane setup
    containing = path.split(File::SEPARATOR)[0..-2]
    containing = "." if containing.count == 0
    actions_folder = File.join(containing, "actions")
    begin
      Actions.sh("cd '#{clone_folder}' && git checkout #{branch} '#{actions_folder}'")
    rescue
      # We don't care about a failure here, as local actions are optional
    end

    import(File.join(clone_folder, path))

    if Dir.exist?(clone_folder)
      # We want to re-clone if the folder already exists
      UI.message "Clearing the git repo..."
      Actions.sh("rm -rf '#{tmp_path}'")
    end
  end
end

#is_platform_block?(key) ⇒ Boolean

Is the given key a platform block or a lane?

Returns:

  • (Boolean)


164
165
166
167
168
169
170
171
# File 'lib/fastlane/fast_file.rb', line 164

def is_platform_block?(key)
  UI.crash!('No key given') unless key

  return false if self.runner.lanes.fetch(nil, {}).fetch(key.to_sym, nil)
  return true if self.runner.lanes[key.to_sym].kind_of? Hash

  UI.user_error!("Could not find '#{key}'. Available lanes: #{self.runner.available_lanes.join(', ')}")
end

#lane(lane_name, &block) ⇒ Object

User defines a new lane



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fastlane/fast_file.rb', line 64

def lane(lane_name, &block)
  UI.user_error!("You have to pass a block using 'do' for lane '#{lane_name}'. Make sure you read the docs on GitHub.") unless block

  self.runner.add_lane(Lane.new(platform: self.current_platform,
                                   block: block,
                             description: desc_collection,
                                    name: lane_name,
                              is_private: false))

  @desc_collection = nil # reset the collected description again for the next lane
end

#override_lane(lane_name, &block) ⇒ Object

User defines a lane that can overwrite existing lanes. Useful when importing a Fastfile



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/fastlane/fast_file.rb', line 90

def override_lane(lane_name, &block)
  UI.user_error!("You have to pass a block using 'do' for lane '#{lane_name}'. Make sure you read the docs on GitHub.") unless block

  self.runner.add_lane(Lane.new(platform: self.current_platform,
                                   block: block,
                             description: desc_collection,
                                    name: lane_name,
                              is_private: false), true)

  @desc_collection = nil # reset the collected description again for the next lane
end

#parse(data, path = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fastlane/fast_file.rb', line 34

def parse(data, path = nil)
  @runner ||= Runner.new

  Dir.chdir(Fastlane::FastlaneFolder.path || Dir.pwd) do # context: fastlane subfolder
    # create nice path that we want to print in case of some problem
    relative_path = path.nil? ? '(eval)' : Pathname.new(path).relative_path_from(Pathname.new(Dir.pwd)).to_s

    begin
      # We have to use #get_binding method, because some test files defines method called `path` (for example SwitcherFastfile)
      # and local variable has higher priority, so it causes to remove content of original Fastfile for example. With #get_binding
      # is this always clear and safe to declare any local variables we want, because the eval function uses the instance scope
      # instead of local.

      # rubocop:disable Lint/Eval
      eval(data, parsing_binding, relative_path) # using eval is ok for this case
      # rubocop:enable Lint/Eval
    rescue SyntaxError => ex
      line = ex.to_s.match(/#{Regexp.escape(relative_path)}:(\d+)/)[1]
      UI.user_error!("Syntax error in your Fastfile on line #{line}: #{ex}")
    end
  end

  self
end

#parsing_bindingObject



30
31
32
# File 'lib/fastlane/fast_file.rb', line 30

def parsing_binding
  binding
end

#platform(platform_name) ⇒ Object

User defines a platform block



103
104
105
106
107
108
109
110
111
# File 'lib/fastlane/fast_file.rb', line 103

def platform(platform_name)
  SupportedPlatforms.verify!(platform_name)

  self.current_platform = platform_name

  yield

  self.current_platform = nil
end

#private_lane(lane_name, &block) ⇒ Object

User defines a new private lane, which can’t be called from the CLI



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fastlane/fast_file.rb', line 77

def private_lane(lane_name, &block)
  UI.user_error!("You have to pass a block using 'do' for lane '#{lane_name}'. Make sure you read the docs on GitHub.") unless block

  self.runner.add_lane(Lane.new(platform: self.current_platform,
                                   block: block,
                             description: desc_collection,
                                    name: lane_name,
                              is_private: true))

  @desc_collection = nil # reset the collected description again for the next lane
end

#puts(value) ⇒ Object



273
274
275
276
277
278
# File 'lib/fastlane/fast_file.rb', line 273

def puts(value)
  # Overwrite this, since there is already a 'puts' method defined in the Ruby standard library
  value ||= yield
  collector.did_launch_action(:puts)
  Fastlane::Actions::PutsAction.run([value])
end

#say(value) ⇒ Object

Speak out loud



264
265
266
267
268
269
270
271
# File 'lib/fastlane/fast_file.rb', line 264

def say(value)
  # Overwrite this, since there is already a 'say' method defined in the Ruby standard library
  value ||= yield
  Actions.execute_action('say') do
    collector.did_launch_action(:say)
    Fastlane::Actions::SayAction.run([value])
  end
end

#sh(command) ⇒ Object

Execute shell command



180
181
182
183
184
# File 'lib/fastlane/fast_file.rb', line 180

def sh(command)
  Actions.execute_action(command) do
    Actions.sh_no_action(command)
  end
end