Ilya Kharabet
Ilya Kharabet

Reputation: 4621

Call Fastlane actions from a ruby module

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:

These didn't help me.

Are there any other ways to solve the problem?

Upvotes: 6

Views: 1795

Answers (1)

dqlobo
dqlobo

Reputation: 46

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!"

Upvotes: 3

Related Questions