Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Fastlane actions from a ruby module

Tags:

ruby

fastlane

I'm trying to make a ruby module with some helper functions that I use in the Fastfile. It looks as follows:

lane :a do |options|
  Utils.foo
end

module Utils
  def self.foo
    get_info_plist_value(...)
  end
end

When I try to run the lane I get this error: undefined method 'get_info_plist_value' for Utils:Module.

I've tried the following ways to solve this problem:

  • adding extend Utils after the module definition
  • including Fastlane or Fastlane::Actions into the module

These didn't help me.

Are there any other ways to solve the problem?

like image 350
Ilya Kharabet Avatar asked Nov 02 '20 10:11

Ilya Kharabet


1 Answers

I've gotten a util like this to work using dependency injection:

lane :a do |options|
  Utils.call(self)
end

module Utils
  def initialize(lane)
     @lane = lane
  end

  def self.call(lane)
     new.execute(lane)
  end

  def execute
     @lane.get_info_plist_value(...)
  end
end

If you look at the anatomy of a declared "lane" (:a, for example), each action (like get_info_plist_value) is executed within Fastlane::Lane's block, which was instantiated by the Fastfile.

Although possible, writing utils that call Fastlane actions should be used sparingly. This seems fairly out of scope from intended usages of Fastlane. I think the "right" way to approach something like this is to actually write a custom action (a bit more verbose, but likely the more maintainable option).

As the saying goes, "Don't fight the framework!"

like image 189
dqlobo Avatar answered Oct 17 '22 02:10

dqlobo