Class: Fastlane::Actions::GetBuildNumberFromXcodeprojAction

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

Documentation collapse

Class Method Summary collapse

Class Method Details

.authorsObject



127
128
129
# File 'lib/fastlane/plugin/versioning/actions/get_build_number_from_xcodeproj.rb', line 127

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

.available_optionsObject



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
# File 'lib/fastlane/plugin/versioning/actions/get_build_number_from_xcodeproj.rb', line 97

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :xcodeproj,
                                 env_name: "FL_BUILD_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_BUILD_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_BUILD_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



89
90
91
# File 'lib/fastlane/plugin/versioning/actions/get_build_number_from_xcodeproj.rb', line 89

def self.description
  "Get the build number of your project"
end

.detailsObject



93
94
95
# File 'lib/fastlane/plugin/versioning/actions/get_build_number_from_xcodeproj.rb', line 93

def self.details
  'Gets the $(CURRENT_PROJECT_VERSION) build setting using the specified parameters, or the first if not enough parameters are passed.'
end

.get_build_number_using_scheme(params) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fastlane/plugin/versioning/actions/get_build_number_from_xcodeproj.rb', line 70

def self.get_build_number_using_scheme(params)
  config = {
    project: params[:xcodeproj],
    scheme: params[:scheme],
    configuration: params[:build_configuration_name],
    skip_package_dependencies_resolution: params[:skip_package_dependencies_resolution]
  }
  project = FastlaneCore::Project.new(config)
  project.select_scheme

  build_number = project.build_settings(key: 'CURRENT_PROJECT_VERSION')
  UI.user_error! "Cannot resolve $(CURRENT_PROJECT_VERSION) in for the scheme #{config[:scheme]} with the name #{config[:configuration]}" if build_number.nil? || build_number.empty?
  build_number
end

.get_build_number_using_target(params) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fastlane/plugin/versioning/actions/get_build_number_from_xcodeproj.rb', line 42

def self.get_build_number_using_target(params)
  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

  build_number = target.resolved_build_setting('CURRENT_PROJECT_VERSION', true)
  UI.user_error! 'Cannot resolve build number build setting.' if build_number.nil? || build_number.empty?

  if !(build_configuration_name = params[:build_configuration_name]).nil?
    build_number = build_number[build_configuration_name]
    UI.user_error! "Cannot resolve $(CURRENT_PROJECT_VERSION) build setting for #{build_configuration_name}." if build_number.nil?
  else
    build_number = build_number.values.compact.uniq
    UI.user_error! 'Cannot accurately resolve $(CURRENT_PROJECT_VERSION) build setting, try specifying :build_configuration_name.' if build_number.count > 1
    build_number = build_number.first
  end

  build_number
end

.get_first_build_number_in_xcodeproj(params) ⇒ Object



29
30
31
32
33
# File 'lib/fastlane/plugin/versioning/actions/get_build_number_from_xcodeproj.rb', line 29

def self.get_first_build_number_in_xcodeproj(params)
  project = Xcodeproj::Project.open(params[:xcodeproj])
  configs = project.objects.select { |obj| select_build_configuration_predicate(nil, obj) }
  configs.first.build_settings["CURRENT_PROJECT_VERSION"]
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/fastlane/plugin/versioning/actions/get_build_number_from_xcodeproj.rb', line 131

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

.run(params) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fastlane/plugin/versioning/actions/get_build_number_from_xcodeproj.rb', line 7

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[:target]
    build_number = get_build_number_using_target(params)
  elsif params[:build_configuration_name] && params[:scheme]
    build_number = get_build_number_using_scheme(params)
  else
    UI.important "not enough information to pick a specific target or build configuration. taking the first one from the project"
    build_number = get_first_build_number_in_xcodeproj(params)
  end

  Actions.lane_context[SharedValues::BUILD_NUMBER] = build_number
  build_number
end

.select_build_configuration_predicate(name, configuration) ⇒ Object



36
37
38
39
40
# File 'lib/fastlane/plugin/versioning/actions/get_build_number_from_xcodeproj.rb', line 36

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