Class: Fastlane::Actions::IncrementVersionNumberAction

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

Constant Summary

Constants inherited from Fastlane::Action

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

Class Method Summary collapse

Methods inherited from Fastlane::Action

action_name, authors, deprecated_notes, lane_context, method_missing, other_action, sample_return_value, shell_out_should_use_bundle_exec?, step_text

Class Method Details

.authorObject



181
182
183
# File 'fastlane/lib/fastlane/actions/increment_version_number.rb', line 181

def self.author
  "serluca"
end

.available_optionsObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'fastlane/lib/fastlane/actions/increment_version_number.rb', line 143

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :bump_type,
                                 env_name: "FL_VERSION_NUMBER_BUMP_TYPE",
                                 description: "The type of this version bump. Available: patch, minor, major",
                                 default_value: "bump",
                                 verify_block: proc do |value|
                                   UI.user_error!("Available values are 'patch', 'minor' and 'major'") unless ['bump', 'patch', 'minor', 'major'].include?(value)
                                 end),
    FastlaneCore::ConfigItem.new(key: :version_number,
                                 env_name: "FL_VERSION_NUMBER_VERSION_NUMBER",
                                 description: "Change to a specific version. This will replace the bump type value",
                                 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",
                                 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") unless File.exist?(value)
                                 end,
                                 optional: true)
  ]
end

.categoryObject



208
209
210
# File 'fastlane/lib/fastlane/actions/increment_version_number.rb', line 208

def self.category
  :project
end

.descriptionObject



132
133
134
# File 'fastlane/lib/fastlane/actions/increment_version_number.rb', line 132

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

.detailsObject



136
137
138
139
140
141
# File 'fastlane/lib/fastlane/actions/increment_version_number.rb', line 136

def self.details
  [
    "This action will increment the version number.",
    "You first have to set up your Xcode project, if you haven't done it already: [https://developer.apple.com/library/ios/qa/qa1827/_index.html](https://developer.apple.com/library/ios/qa/qa1827/_index.html)."
  ].join("\n")
end

.example_codeObject



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'fastlane/lib/fastlane/actions/increment_version_number.rb', line 185

def self.example_code
  [
    'increment_version_number # Automatically increment version number',
    'increment_version_number(
      bump_type: "patch" # Automatically increment patch version number
    )',
    'increment_version_number(
      bump_type: "minor" # Automatically increment minor version number
    )',
    'increment_version_number(
      bump_type: "major" # Automatically increment major version number
    )',
    'increment_version_number(
      version_number: "2.1.1" # Set a specific version number
    )',
    'increment_version_number(
      version_number: "2.1.1",                # specify specific version number (optional, omitting it increments patch version number)
      xcodeproj: "./path/to/MyApp.xcodeproj"  # (optional, you must specify the path to your main Xcode project if it is not in the project root directory)
    )',
    'version = increment_version_number'
  ]
end

.is_supported?(platform) ⇒ Boolean

Returns:



11
12
13
# File 'fastlane/lib/fastlane/actions/increment_version_number.rb', line 11

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

.outputObject



167
168
169
170
171
# File 'fastlane/lib/fastlane/actions/increment_version_number.rb', line 167

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

.return_typeObject



173
174
175
# File 'fastlane/lib/fastlane/actions/increment_version_number.rb', line 173

def self.return_type
  :string
end

.return_valueObject



177
178
179
# File 'fastlane/lib/fastlane/actions/increment_version_number.rb', line 177

def self.return_value
  "The new version number"
end

.run(params) ⇒ Object



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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'fastlane/lib/fastlane/actions/increment_version_number.rb', line 15

def self.run(params)
  # More information about how to set up your project and how it works:
  # https://developer.apple.com/library/ios/qa/qa1827/_index.html

  folder = params[:xcodeproj] ? File.join(params[:xcodeproj], '..') : '.'

  command_prefix = [
    'cd',
    File.expand_path(folder).shellescape,
    '&&'
  ].join(' ')

  version_number_build_setting = nil

  begin
    current_version = Actions
                      .sh("#{command_prefix} agvtool what-marketing-version -terse1", log: FastlaneCore::Globals.verbose?)
                      .split("\n")
                      .last
                      .strip

    if current_version =~ /\$\(([\w\-]+)\)/ || current_version =~ /\$\{([\w\-]+)\}/
      version_number_build_setting = $1
      UI.verbose("agvtool returned #{current_version}, resolving it...")
      current_version = GetVersionNumberAction.run(xcodeproj: params[:xcodeproj])
    end
  rescue
    current_version = ''
  end

  if params[:version_number]
    UI.verbose(version_format_error(current_version)) unless current_version =~ version_regex

    # Specific version
    next_version_number = params[:version_number]
  else
    UI.user_error!(version_format_error(current_version)) unless current_version =~ version_regex
    version_array = current_version.split(".").map(&:to_i)

    case params[:bump_type]
    when "bump"
      version_array[-1] = version_array[-1] + 1
      next_version_number = version_array.join(".")
    when "patch"
      UI.user_error!(version_token_error) if version_array.count < 3
      version_array[2] = version_array[2] + 1
      next_version_number = version_array.join(".")
    when "minor"
      UI.user_error!(version_token_error) if version_array.count < 2
      version_array[1] = version_array[1] + 1
      version_array[2] = 0 if version_array[2]
      next_version_number = version_array.join(".")
    when "major"
      UI.user_error!(version_token_error) if version_array.count == 0
      version_array[0] = version_array[0] + 1
      version_array[1] = 0 if version_array[1]
      version_array[2] = 0 if version_array[2]
      next_version_number = version_array.join(".")
    when "specific_version"
      next_version_number = specific_version_number
    end
  end

  command = [
    command_prefix,
    "agvtool new-marketing-version #{next_version_number.to_s.strip}"
  ].join(' ')

  if Helper.test?
    Actions.lane_context[SharedValues::VERSION_NUMBER] = command
  else
    Actions.sh(command)
    version_number_build_setting ||= "MARKETING_VERSION"
    update_project_version_build_setting(
      params[:xcodeproj],
      version_number_build_setting,
      next_version_number.to_s.strip
    )
    Actions.lane_context[SharedValues::VERSION_NUMBER] = next_version_number
  end

  return Actions.lane_context[SharedValues::VERSION_NUMBER]
rescue => ex
  UI.error('Before being able to increment and read the version number from your Xcode project, you first need to setup your project properly. Please follow the guide at https://developer.apple.com/library/content/qa/qa1827/_index.html')
  raise ex
end

.update_project_version_build_setting(xcodeproj_path_or_dir, build_setting, version_number) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'fastlane/lib/fastlane/actions/increment_version_number.rb', line 114

def self.update_project_version_build_setting(xcodeproj_path_or_dir, build_setting, version_number)
  project_path_or_dir = xcodeproj_path_or_dir
  if project_path_or_dir.nil?
    xcodeproj_paths = Dir.glob("./*.xcodeproj")
    return if xcodeproj_paths.empty?

    if xcodeproj_paths.size > 1
      return
    end

    project_path_or_dir = xcodeproj_paths.first
  end

  project = Fastlane::Helper::XcodeprojHelper.get_project!(project_path_or_dir)
  changed = Fastlane::Helper::XcodeprojHelper.update_project_build_setting(project, build_setting, version_number)
  project.save if changed[:project]
end

.version_format_error(version) ⇒ Object



106
107
108
# File 'fastlane/lib/fastlane/actions/increment_version_number.rb', line 106

def self.version_format_error(version)
  "Your current version (#{version}) does not respect the format A or A.B or A.B.C"
end

.version_regexObject



102
103
104
# File 'fastlane/lib/fastlane/actions/increment_version_number.rb', line 102

def self.version_regex
  /^\d+(\.\d+){0,2}$/
end

.version_token_errorObject



110
111
112
# File 'fastlane/lib/fastlane/actions/increment_version_number.rb', line 110

def self.version_token_error
  "Can't increment version"
end