Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom lane for fastlane to increment the build number for a single target in the project file.
### --------------------------------------------------------------------------------------------------
#
# The code below has been adotped and modified from https://stackoverflow.com/a/68022221/189292
# This allows the project to increment build numbers on a per-target basis, instead of updating the entire
# project for all targets when using the standard `increment_build_number` lane.
#
### --------------------------------------------------------------------------------------------------
# Sets the "BUILD" number for a target
#
# @param args [Hash] Key value arguments. All the values are Strings. List:
# - target: The name of the target (REQUIRED)
#
desc "Sets the \"VERSION\" and \"BUILD\" number for a target"
lane :increment_target_build_number do |args|
target_name = args[:target]
raise(StandardError, "Invalid ARGUMENTS for: \"increment_target_build_number\" LANE") unless (
target_name != nil
)
puts("Incrementing build number for target: #{target_name}")
project_url = find_xcode_project()
raise(StandardError, "Invalid Xcode project for: \"set_version_build_number\" LANE") unless project_url != nil
project = Xcodeproj::Project.open(project_url)
xc_target = project.targets.find { |target| target.name == target_name }
raise(StandardError, "Invalid target for: \"set_version_build_number\" LANE") unless xc_target != nil
xc_target.build_configurations.each { |build_configuration|
build_number = build_configuration.build_settings["CURRENT_PROJECT_VERSION"]
build_number = defined?(build_number) ? build_number : "0"
puts("\tUpdating: '#{build_configuration}'")
new_build = (build_number.to_i + 1).to_s
build_configuration.build_settings["CURRENT_PROJECT_VERSION"] = new_build
puts("\t\tbuild version #{build_number} -> #{new_build}")
}
project.save()
end
# Finds the location for an Xcode project within the current directory and all his parents
#
# @return [String] Full path to the Xcode project or nil if unsuccess
#
def find_xcode_project ()
absolute_dir_path = Dir.getwd
loop do
entries = Dir.entries(absolute_dir_path)
index = entries.find_index{ |entry| entry.include?(".xcodeproj") }
if index != nil
return "#{absolute_dir_path}/#{entries[index]}"
end
absolute_dir_path = File.dirname(absolute_dir_path)
if absolute_dir_path == nil || absolute_dir_path == "/"
break
end
end
return nil
end
@gdavis
Copy link
Author

gdavis commented Feb 23, 2022

The increment_build_number lane provided by default in Fastlane only allows updating the project build number for all targets. In a project that has multiple targets, such as for iOS, macOS, etc with differing version numbers, this lane becomes insufficient.

This script is an alternative to increment_build_number and allows a single target's build configuration to increment the CURRENT_PROJECT_VERSION.

Example usage from the command line, executed in the same directory as the project file:

fastlane increment_target_build_number target:MyProjectTarget

Example usage from within a fastlane file:

increment_target_build_number(target: 'MyProjectTarget')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment