Class: Fastlane::Actions::IncrementBuildNumberInXcodeprojAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/versioning/actions/increment_build_number_in_xcodeproj.rb

Documentation collapse

Class Method Summary collapse

Class Method Details

.authorsObject



128
129
130
# File 'lib/fastlane/plugin/versioning/actions/increment_build_number_in_xcodeproj.rb', line 128

def self.authors
  ["jdouglas-nz", "neilb01"]
end

.available_optionsObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fastlane/plugin/versioning/actions/increment_build_number_in_xcodeproj.rb', line 94

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :build_number,
                                env_name: "FL_BUILD_NUMBER_BUILD_NUMBER",
                                description: "Change to a specific build number",
                                optional: true),
    FastlaneCore::ConfigItem.new(key: :xcodeproj,
                                   env_name: "FL_VERSION_NUMBER_PROJECT",
                                   description: "Optional, you must specify the path to your main Xcode project if it is not in the project root directory or if you have multiple *.xcodeproj's in the root directory",
                                   optional: true,
                                   verify_block: proc do |value|
                                     UI.user_error!("Please pass the path to the project, not the workspace") if value.end_with? ".xcworkspace"
                                     UI.user_error!("Could not find Xcode project at path '#{File.expand_path(value)}'") if !File.exist?(value) and !Helper.is_test?
                                   end),
    FastlaneCore::ConfigItem.new(key: :target,
                                   env_name: "FL_VERSION_NUMBER_TARGET",
                                   optional: true,
                                   conflicting_options: [:scheme],
                                   description: "Specify a specific target if you have multiple per project, optional"),
    FastlaneCore::ConfigItem.new(key: :scheme,
                                   env_name: "FL_VERSION_NUMBER_SCHEME",
                                   optional: true,
                                   conflicting_options: [:target],
                                   description: "Specify a specific scheme if you have multiple per project, optional"),
    FastlaneCore::ConfigItem.new(key: :build_configuration_name,
                                   optional: true,
                                   description: "Specify a specific build configuration if you have different build settings for each configuration"),
    FastlaneCore::ConfigItem.new(key: :skip_package_dependencies_resolution,
                                   description: "Skips resolution of Swift Package Manager dependencies",
                                   type: Boolean,
                                   default_value: false)
  ]
end

.descriptionObject



90
91
92
# File 'lib/fastlane/plugin/versioning/actions/increment_build_number_in_xcodeproj.rb', line 90

def self.description
  "Increment build number in xcodeproj"
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/fastlane/plugin/versioning/actions/increment_build_number_in_xcodeproj.rb', line 132

def self.is_supported?(platform)
  %i[ios mac android].include? platform
end

.run(params) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fastlane/plugin/versioning/actions/increment_build_number_in_xcodeproj.rb', line 4

def self.run(params)
  unless params[:xcodeproj]
    if Helper.test?
      params[:xcodeproj] = "/tmp/fastlane/tests/fastlane/xcodeproj/versioning_fixture_project.xcodeproj"
    else
      params[:xcodeproj] = Dir["*.xcodeproj"][0] unless params[:xcodeproj]
    end
  end

  if params[:build_number]
    next_build_number = params[:build_number]
  else
    current_build_number = GetBuildNumberFromXcodeprojAction.run(params)
    build_array = current_build_number.split(".").map(&:to_i)
    build_array[-1] = build_array[-1] + 1
    next_build_number = build_array.join(".")
  end

  if params[:target]
    set_build_number_using_target(params, next_build_number)
  elsif params[:build_configuration_name] && params[:scheme]
    set_build_number_using_scheme(params, next_build_number)
  else
    set_all_xcodeproj_build_numbers(params, next_build_number)
  end
  Actions.lane_context[SharedValues::BUILD_NUMBER] = next_build_number
  next_build_number
end

.select_build_configuration_predicate(name, configuration) ⇒ Object



43
44
45
46
47
# File 'lib/fastlane/plugin/versioning/actions/increment_build_number_in_xcodeproj.rb', line 43

def self.select_build_configuration_predicate(name, configuration)
  is_build_valid_configuration = configuration.isa == "XCBuildConfiguration" && !configuration.resolve_build_setting('PRODUCT_BUNDLE_IDENTIFIER').nil?
  is_build_valid_configuration &&= configuration.name == name unless name.nil?
  return is_build_valid_configuration
end

.set_all_xcodeproj_build_numbers(params, next_build_number) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/fastlane/plugin/versioning/actions/increment_build_number_in_xcodeproj.rb', line 33

def self.set_all_xcodeproj_build_numbers(params, next_build_number)
  project = Xcodeproj::Project.open(params[:xcodeproj])
  configs = project.objects.select { |obj| select_build_configuration_predicate(nil, obj) }
  configs.each do |config|
    config.build_settings["CURRENT_PROJECT_VERSION"] = next_build_number
  end
  project.save
end

.set_build_number_using_scheme(params, next_build_number) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/fastlane/plugin/versioning/actions/increment_build_number_in_xcodeproj.rb', line 76

def self.set_build_number_using_scheme(params, next_build_number)
  project = Xcodeproj::Project.open(params[:xcodeproj])
  configs = project.objects.select { |obj| select_build_configuration_predicate(params[:build_configuration_name], obj) }
  configs.each do |config|
    config.build_settings["CURRENT_PROJECT_VERSION"] = next_build_number
  end
  
  project.save
end

.set_build_number_using_target(params, next_build_number) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fastlane/plugin/versioning/actions/increment_build_number_in_xcodeproj.rb', line 49

def self.set_build_number_using_target(params, next_build_number)
  project = Xcodeproj::Project.open(params[:xcodeproj])
  if params[:target]
    target = project.targets.detect { |t| t.name == params[:target] }
  else
    # firstly we are trying to find modern application target
    target = project.targets.detect do |t|
      t.kind_of?(Xcodeproj::Project::Object::PBXNativeTarget) &&
        t.product_type == 'com.apple.product-type.application'
    end
    target = project.targets[0] if target.nil?
  end

  if params[:build_configuration_name]
    config = target.build_configurations.detect { |c| c.name == params[:build_configuration_name]}
    UI.message "updating #{config.name} to build #{next_build_number}"
    config.build_settings["CURRENT_PROJECT_VERSION"] = next_build_number
  else
    target.build_configurations.each do |config|
      UI.message "updating #{config.name} to build #{next_build_number}"
      config.build_settings["CURRENT_PROJECT_VERSION"] = next_build_number
    end unless target.nil?
  end

  project.save
end