Skip to content

Instantly share code, notes, and snippets.

@felginep
Created October 7, 2020 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felginep/3bb01c0d9a97110f6a8cf5c78157fd0a to your computer and use it in GitHub Desktop.
Save felginep/3bb01c0d9a97110f6a8cf5c78157fd0a to your computer and use it in GitHub Desktop.
Create lanes with non optional parameters and raises an error if we try to access a missing value from the `option` hash
###### BEFORE ######
lane :test do
build
end
lane :build do |options|
scheme = options[:scheme]
# ...
end
# => scheme is missing but execution continues (which can lead to problems later)
###### AFTER ######
lane :test do
build
end
secure_lane :build do |options|
scheme = options[:scheme]
# ...
end
# => scheme is missing but we get the exception: "Can't find value for key :scheme in lane :build"
class Fastlane::FastFile # from https://github.com/fastlane/fastlane/blob/master/fastlane/lib/fastlane/fast_file.rb
# Hash that raises an error when we access a missing value
class RequiredOptions < Hash
def initialize(options, lane)
options.each { |key, value| self[key] = value }
@lane = lane
end
def [](key)
value = super(key)
UI.user_error!("Can't find value for key :#{key} in lane :#{@lane}") if value.nil?
value
end
end
def secure_lane(name, &block)
lane(name) do |options|
options = RequiredOptions.new(options, name)
block.call(options)
end
end
def secure_private_lane(name, &block)
private_lane(name) do |options|
options = RequiredOptions.new(options, name)
block.call(options)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment