Class: Fastlane::Actions::IncrementVersionNumberAction

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

Class Method Summary collapse

Methods inherited from Fastlane::Action

sh

Class Method Details

.authorObject



122
123
124
# File 'lib/fastlane/actions/increment_version_number.rb', line 122

def self.author
  "serluca"
end

.available_optionsObject



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

def self.available_options
  [
    ['build_number', 'specify specific build number (optional, omitting it increments by one)'],
    ['xcodeproj', 'optional, you must specify the path to your main Xcode project if it is not in the project root directory']
  ]
end

.descriptionObject



97
98
99
# File 'lib/fastlane/actions/increment_version_number.rb', line 97

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

.detailsObject



101
102
103
104
105
106
107
# File 'lib/fastlane/actions/increment_version_number.rb', line 101

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"
  ].join(' ')
end

.outputObject



116
117
118
119
120
# File 'lib/fastlane/actions/increment_version_number.rb', line 116

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

.run(params) ⇒ Object



10
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
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
# File 'lib/fastlane/actions/increment_version_number.rb', line 10

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

  begin
    first_param = (params.first rescue nil)
    folder = '.' #Current folder is the default folder

    case first_param
      when NilClass
        release_task = 'patch' #Patch is the default action
      when String
        release_task = first_param
      when Hash
        release_task = first_param[:release_task] ? first_param[:release_task] : "patch"
        folder = first_param[:xcodeproj] ? File.join('.', first_param[:xcodeproj], '..') : '.'
    end

    # Verify integrity
    case release_task
      when /\d.\d.\d/
        specific_version_number = release_task
        release_task = 'specific_version'
      when "patch"
        release_task = 'patch'
      when "minor"
        release_task = 'minor'
      when "major"
        release_task = "major"
      else
        raise 'Invalid parameter #{release_task}'
    end

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

    if Helper.test?
      version_array = [1,0,0]
    else
      current_version= `#{command_prefix} agvtool what-marketing-version -terse1`.split("\n").last
      raise 'Your current version does not respect the format A.B.C' unless current_version.match(/\d.\d.\d/)
      #Check if CFBundleShortVersionString is the same for each occurrence
      allBundles = `#{command_prefix} agvtool what-marketing-version -terse`.split("\n")
      allBundles.each do |bundle|
        raise 'Ensure all you CFBundleShortVersionString are equals in your project ' unless bundle.end_with? "=#{current_version}"
      end
      version_array = current_version.split(".").map(&:to_i)
    end

    case release_task
      when "patch"
        version_array[2] = version_array[2]+1
        next_version_number = version_array.join(".")
      when "minor"
        version_array[1] = version_array[1]+1
        version_array[2] = version_array[2]=0
        next_version_number = version_array.join(".")
      when "major"
        version_array[0] = version_array[0]+1
        version_array[1] = version_array[1]=0
        version_array[1] = version_array[2]=0
        next_version_number = version_array.join(".")
      when "specific_version"
        next_version_number = specific_version_number
    end

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

    if Helper.test?
      Actions.lane_context[SharedValues::VERSION_NUMBER] = command
    else
      Actions.sh command
      Actions.lane_context[SharedValues::VERSION_NUMBER] = next_version_number
    end

  rescue => ex
    Helper.log.error 'Make sure to to follow the steps to setup your Xcode project: https://developer.apple.com/library/ios/qa/qa1827/_index.html'.yellow
    raise ex
  end
end