Class: Fastlane::Actions::GetVersionNumberFromXcodeprojAction

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

Documentation collapse

Class Method Summary collapse

Class Method Details

.authorsObject



124
125
126
# File 'lib/fastlane/plugin/versioning/actions/get_version_number_from_xcodeproj.rb', line 124

def self.authors
  ["jdouglas-nz"]
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
# File 'lib/fastlane/plugin/versioning/actions/get_version_number_from_xcodeproj.rb', line 94

def self.available_options
  [
    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



86
87
88
# File 'lib/fastlane/plugin/versioning/actions/get_version_number_from_xcodeproj.rb', line 86

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

.detailsObject



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

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

.get_version_number_using_project(params) ⇒ Object



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

def self.get_version_number_using_project(params)
  project = Xcodeproj::Project.open(params[:xcodeproj])

  if params[:build_configuration_name] && !params[:build_configuration_name].empty?
    build_configuration_name = params[:build_configuration_name]
  else
    build_configuration_name = project.build_configurations.first.name
  end

  version_number = project.build_settings(build_configuration_name)["MARKETING_VERSION"]
end

.get_version_number_using_scheme(params) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fastlane/plugin/versioning/actions/get_version_number_from_xcodeproj.rb', line 55

def self.get_version_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: 'MARKETING_VERSION')
  UI.user_error! "Cannot resolve $(MARKETING_VERSION) in for the scheme #{config[:scheme]} with the name #{config[:configuration]}" if build_number.nil? || build_number.empty?
  build_number
end

.get_version_number_using_target(params) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fastlane/plugin/versioning/actions/get_version_number_from_xcodeproj.rb', line 27

def self.get_version_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

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

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

  version_number
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


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

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
# File 'lib/fastlane/plugin/versioning/actions/get_version_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]
    version_number = get_version_number_using_target(params)
  else
    version_number = get_version_number_using_project(params) if params[:scheme].nil?
    version_number = get_version_number_using_scheme(params) if version_number.nil?
  end

  Actions.lane_context[SharedValues::VERSION_NUMBER] = version_number
  version_number
end