Class: Fastlane::Actions::GetVersionNumberAction

Inherits:
Fastlane::Action show all
Defined in:
fastlane/lib/fastlane/actions/get_version_number.rb

Constant Summary

Constants inherited from Fastlane::Action

Fastlane::Action::AVAILABLE_CATEGORIES, Fastlane::Action::RETURN_TYPES

Documentation collapse

Class Method Summary collapse

Methods inherited from Fastlane::Action

action_name, author, deprecated_notes, lane_context, method_missing, other_action, return_value, sample_return_value, shell_out_should_use_bundle_exec?, step_text

Class Method Details

.authorsObject



175
176
177
# File 'fastlane/lib/fastlane/actions/get_version_number.rb', line 175

def self.authors
  ["Liquidsoul", "joshdholtz"]
end

.available_optionsObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'fastlane/lib/fastlane/actions/get_version_number.rb', line 147

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :xcodeproj,
                       env_name: "FL_VERSION_NUMBER_PROJECT",
                       description: "Path to the Xcode project to read version number from, or its containing directory, optional. If omitted, or if a directory is passed instead, it will use the first Xcode project found within the given directory, or the project root directory if none is passed",
                       optional: true,
                       verify_block: proc do |value|
                         UI.user_error!("Please pass the path to the project or its containing directory, not the workspace path") if value.end_with?(".xcworkspace")
                         UI.user_error!("Could not find file or directory at path '#{File.expand_path(value)}'") unless File.exist?(value)
                         UI.user_error!("Could not find Xcode project in directory at path '#{File.expand_path(value)}'") if File.extname(value) != ".xcodeproj" && Dir.glob("#{value}/*.xcodeproj").empty?
                       end),
    FastlaneCore::ConfigItem.new(key: :target,
                       env_name: "FL_VERSION_NUMBER_TARGET",
                       description: "Target name, optional. Will be needed if you have more than one non-test target to avoid being prompted to select one",
                       optional: true),
    FastlaneCore::ConfigItem.new(key: :configuration,
                       env_name: "FL_VERSION_NUMBER_CONFIGURATION",
                       description: "Configuration name, optional. Will be needed if you have altered the configurations from the default or your version number depends on the configuration selected",
                       optional: true)
  ]
end

.categoryObject



197
198
199
# File 'fastlane/lib/fastlane/actions/get_version_number.rb', line 197

def self.category
  :project
end

.descriptionObject



139
140
141
# File 'fastlane/lib/fastlane/actions/get_version_number.rb', line 139

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

.detailsObject



143
144
145
# File 'fastlane/lib/fastlane/actions/get_version_number.rb', line 143

def self.details
  "This action will return the current version number set on your project. It first looks in the plist and then for '$(MARKETING_VERSION)' in the build settings."
end

.example_codeObject



183
184
185
186
187
188
189
190
191
# File 'fastlane/lib/fastlane/actions/get_version_number.rb', line 183

def self.example_code
  [
    'version = get_version_number(xcodeproj: "Project.xcodeproj")',
    'version = get_version_number(
      xcodeproj: "Project.xcodeproj",
      target: "App"
    )'
  ]
end

.get_plist!(folder, target, configuration = nil) ⇒ Object



89
90
91
92
93
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
# File 'fastlane/lib/fastlane/actions/get_version_number.rb', line 89

def self.get_plist!(folder, target, configuration = nil)
  plist_files = target.resolved_build_setting("INFOPLIST_FILE", true)
  plist_files_count = plist_files.values.compact.uniq.count

  # Get plist file for specified configuration
  # Or: Prompt for configuration if plist has different files in each configurations
  # Else: Get first(only) plist value
  if configuration
    plist_file = plist_files[configuration]
  elsif plist_files_count > 1
    options = plist_files.keys
    selected = UI.select("What build configuration would you like to use?", options)
    plist_file = plist_files[selected]
  elsif plist_files_count > 0
    plist_file = plist_files.values.first
  else
    return nil
  end

  # $(SRCROOT) is the path of where the XcodeProject is
  # We can just set this as empty string since we join with `folder` below
  if plist_file.include?("$(SRCROOT)/")
    plist_file.gsub!("$(SRCROOT)/", "")
  end

  # plist_file can be `Relative` or `Absolute` path.
  # Make to `Absolute` path when plist_file is `Relative` path
  unless File.exist?(plist_file)
    plist_file = File.absolute_path(File.join(folder, plist_file))
  end

  UI.user_error!("Cannot find plist file: #{plist_file}") unless File.exist?(plist_file)

  plist_file
end

.get_project!(xcodeproj_path_or_dir) ⇒ Object



44
45
46
# File 'fastlane/lib/fastlane/actions/get_version_number.rb', line 44

def self.get_project!(xcodeproj_path_or_dir)
  Fastlane::Helper::XcodeprojHelper.get_project!(xcodeproj_path_or_dir)
end

.get_target!(project, target_name) ⇒ Object



48
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
75
76
# File 'fastlane/lib/fastlane/actions/get_version_number.rb', line 48

def self.get_target!(project, target_name)
  targets = project.targets

  # Prompt targets if no name
  unless target_name

    # Gets non-test targets
    non_test_targets = targets.reject do |t|
      # Not all targets respond to `test_target_type?`
      t.respond_to?(:test_target_type?) && t.test_target_type?
    end

    # Returns if only one non-test target
    if non_test_targets.count == 1
      return non_test_targets.first
    end

    options = targets.map(&:name)
    target_name = UI.select("What target would you like to use?", options)
  end

  # Find target
  target = targets.find do |t|
    t.name == target_name
  end
  UI.user_error!("Cannot find target named '#{target_name}'") unless target

  target
end

.get_version_number_from_build_settings!(target, variable, configuration = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'fastlane/lib/fastlane/actions/get_version_number.rb', line 78

def self.get_version_number_from_build_settings!(target, variable, configuration = nil)
  target.build_configurations.each do |config|
    if configuration.nil? || config.name == configuration
      value = config.resolve_build_setting(variable)
      return value if value
    end
  end

  return nil
end

.get_version_number_from_plist!(plist_file) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'fastlane/lib/fastlane/actions/get_version_number.rb', line 125

def self.get_version_number_from_plist!(plist_file)
  return '$(MARKETING_VERSION)' if plist_file.nil?

  plist = Xcodeproj::Plist.read_from_path(plist_file)
  UI.user_error!("Unable to read plist: #{plist_file}") unless plist

  return '${MARKETING_VERSION}' if plist["CFBundleShortVersionString"].nil?
  plist["CFBundleShortVersionString"]
end

.is_supported?(platform) ⇒ Boolean

Returns:



179
180
181
# File 'fastlane/lib/fastlane/actions/get_version_number.rb', line 179

def self.is_supported?(platform)
  [:ios, :mac].include?(platform)
end

.outputObject



169
170
171
172
173
# File 'fastlane/lib/fastlane/actions/get_version_number.rb', line 169

def self.output
  [
    ['VERSION_NUMBER', 'The version number']
  ]
end

.return_typeObject



193
194
195
# File 'fastlane/lib/fastlane/actions/get_version_number.rb', line 193

def self.return_type
  :string
end

.run(params) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'fastlane/lib/fastlane/actions/get_version_number.rb', line 11

def self.run(params)
  xcodeproj_path_or_dir = params[:xcodeproj] || '.'
  xcodeproj_dir = File.extname(xcodeproj_path_or_dir) == ".xcodeproj" ? File.dirname(xcodeproj_path_or_dir) : xcodeproj_path_or_dir
  target_name = params[:target]
  configuration = params[:configuration]

  # Get version_number
  project = get_project!(xcodeproj_path_or_dir)
  target = get_target!(project, target_name)
  plist_file = get_plist!(xcodeproj_dir, target, configuration)
  version_number = get_version_number_from_plist!(plist_file)

  # Get from build settings (or project settings) if needed (ex: $(MARKETING_VERSION) is default in Xcode 11)
  if version_number =~ /\$\(([\w\-]+)\)/
    version_number = get_version_number_from_build_settings!(target, $1, configuration) || get_version_number_from_build_settings!(project, $1, configuration)

  # ${MARKETING_VERSION} also works
  elsif version_number =~ /\$\{([\w\-]+)\}/
    version_number = get_version_number_from_build_settings!(target, $1, configuration) || get_version_number_from_build_settings!(project, $1, configuration)
  end

  # Error out if version_number is not set
  if version_number.nil?
    UI.user_error!("Unable to find Xcode build setting: #{$1}")
  end

  # Store the number in the shared hash
  Actions.lane_context[SharedValues::VERSION_NUMBER] = version_number

  # Return the version number because Swift might need this return value
  return version_number
end