Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastlane: How can I "increment_build_number" of one target only?

Tags:

ios

fastlane

I have two targets in my Xcode project:

  1. MyAwesomeApp (build 1)
  2. MyGreatApp (build 1)

After the increment_build_number action, they both turn to:

  1. MyAwesomeApp (build 2)
  2. MyGreatApp (build 2)

But what I expect is to apply it to only one target. So when I execute something like this one more time: increment_builder_number(scheme: "MyAwesomeAppScheme")

They turn to:

  1. MyAwesomeApp (build 3)
  2. MyGreatApp (build 2) <-- This build number should be unchanged

Is there a way to achieve it? Thanks!

like image 942
Rodrigo Recio Avatar asked Aug 05 '15 15:08

Rodrigo Recio


3 Answers

I have created my custom lane that by using "xcodeproj" is able to set both the version and the build number for a specific target name.

# Sets the "VERSION" and "BUILD" number for a target
#
# @param args [Hash] Key value arguments. All the values are Strings. List: 
#   - target_name: The name of the target (REQUIRED)
#   - version: The version number to be used (REQUIRED)
#   - build: The build number to be used (REQUIRED)
#
desc "Sets the \"VERSION\" and \"BUILD\" number for a target"
lane :set_version_build_number do |args|
    puts("\"set_version_build_number\" lane with ARGUMENTS: #{args}")

    target_name = args[:target_name]
    version = args[:version]
    build = args[:build]

    raise(StandardError, "Invalid ARGUMENTS for: \"set_version_build_number\" LANE") unless (
        target_name != nil &&
        version != nil &&
        build != nil
    )

    puts("Variables:")
    puts("\ttarget_name: #{target_name}")
    puts("\tversion: #{version}")
    puts("\tbuild: #{build}")
    
    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_configuration.build_settings["MARKETING_VERSION"] = version
        build_configuration.build_settings["CURRENT_PROJECT_VERSION"] = build
    }

    project.save()
end

Where:

# 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

It can easily be used:

bundle exec fastlane set_version_build_number target_name:TargetApp version:2.0 build:3

Of course don't forget to add this into the Fastlane file:

require "xcodeproj" 

and this into the Gemfile

gem "xcodeproj"
like image 185
Reimond Hill Avatar answered Sep 21 '22 14:09

Reimond Hill


Here is a plug-in to increment the build number.

This is what it looks like in the Fastfile:

increment_build_number_in_plist(        
   target:"MyAwesomeApp"
)
like image 6
Francois Nadeau Avatar answered Oct 23 '22 00:10

Francois Nadeau


I could easily add this option to the fastlane action, but would need the agvtool equivalent for doing that. The increment_build_number step is mostly a wrapper around agvtool.

You can find more information on Apple's Docs: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/agvtool.1.html

like image 5
KrauseFx Avatar answered Oct 22 '22 23:10

KrauseFx