Class: Fastlane::Actions::AutoVersionNameAction

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

Class Method Summary collapse

Class Method Details

.authorsObject



147
148
149
# File 'lib/fastlane/plugin/auto_version_name/actions/auto_version_name_action.rb', line 147

def self.authors
  ["gileadeteixeira"]
end

.available_optionsObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/fastlane/plugin/auto_version_name/actions/auto_version_name_action.rb', line 160

def self.available_options
  [
    FastlaneCore::ConfigItem.new(
      key: :android_package_name,
      env_name: "ANDROID_PACKAGE_NAME",
      description: "The package_name of your android app",
      optional: true,
      type: String
    ),
    FastlaneCore::ConfigItem.new(
      key: :ios_app_identifier,
      env_name: "IOS_PACKAGE_NAME",
      description: "The bundle_identifier of your iOS app",
      optional: true,
      type: String
    ),
    FastlaneCore::ConfigItem.new(
      key: :minimal_version,
      env_name: "MINIMAL_VERSION_STRING", 
      description: "A minimal version to be set",
      optional: true,
      default_value: "1.0.0",
      type: String
    ),
    FastlaneCore::ConfigItem.new(
      key: :android_json_key_path,
      env_name: "ANDROID_JSON_KEY_PATH",
      description: "The path to a file containing service account JSON, used to authenticate with Google",
      optional: true,
      type: String
    ),
    FastlaneCore::ConfigItem.new(
      key: :ios_api_key,
      env_names: ["APPSTORE_BUILD_NUMBER_API_KEY", "APP_STORE_CONNECT_API_KEY"],
      description: "Your App Store Connect API Key information (https://docs.fastlane.tools/app-store-connect-api/#using-fastlane-api-key-hash-option)",
      type: Hash,
      default_value: Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::APP_STORE_CONNECT_API_KEY],
      default_value_dynamic: true,
      optional: true,
      sensitive: true,
      conflicting_options: [:api_key_path]
    ),
    FastlaneCore::ConfigItem.new(
      key: :ios_username,
      short_option: "-u",
      env_name: "ITUNESCONNECT_USER",
      description: "Your Apple ID Username",
      optional: true,
      type: String
    ),
    FastlaneCore::ConfigItem.new(
      key: :ios_team_id,
      short_option: "-k",
      env_name: "APPSTORE_BUILD_NUMBER_LIVE_TEAM_ID",
      description: "The ID of your App Store Connect team if you're in multiple teams",
      optional: true,
      skip_type_validation: true, # as we also allow integers, which we convert to strings anyway
      code_gen_sensitive: true,
      default_value: CredentialsManager::AppfileConfig.try_fetch_value(:itc_team_id),
      default_value_dynamic: true,
      verify_block: proc do |value|
        ENV["FASTLANE_ITC_TEAM_ID"] = value.to_s
      end
    ),
  ]
end

.descriptionObject



143
144
145
# File 'lib/fastlane/plugin/auto_version_name/actions/auto_version_name_action.rb', line 143

def self.description
  "Generate incremented version names from Apple and Google stores"
end

.detailsObject



155
156
157
158
# File 'lib/fastlane/plugin/auto_version_name/actions/auto_version_name_action.rb', line 155

def self.details
  # Optional:
  ""
end

.get_greater_version(minimal, ios, android) ⇒ Object



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
# File 'lib/fastlane/plugin/auto_version_name/actions/auto_version_name_action.rb', line 22

def self.get_greater_version(minimal, ios, android)
  greater_version = ""

  minimal = minimal == nil ? "" : minimal
  ios = ios == nil ? "" : ios
  android = android == nil ? "" : android

  is_minimal_greater = Gem::Version.new(minimal) >= Gem::Version.new(ios) && Gem::Version.new(minimal) >= Gem::Version.new(android)

  is_ios_greater = Gem::Version.new(ios) >= Gem::Version.new(minimal) && Gem::Version.new(ios) >= Gem::Version.new(android)

  is_android_greater = Gem::Version.new(android) >= Gem::Version.new(minimal) && Gem::Version.new(android) >= Gem::Version.new(ios)

  if (is_minimal_greater)
    greater_version = minimal
  elsif (is_ios_greater)
    greater_version = ios
  elsif (is_android_greater)
    greater_version = android
  end

  if (greater_version.empty? || greater_version == nil)
    greater_version = minimal_version_string
  end

  return greater_version
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


227
228
229
230
231
232
233
# File 'lib/fastlane/plugin/auto_version_name/actions/auto_version_name_action.rb', line 227

def self.is_supported?(platform)
  # Adjust this if your plugin only works for a particular platform (iOS vs. Android, for example)
  # See: https://docs.fastlane.tools/advanced/#control-configuration-by-lane-and-by-platform
  #
  # [:ios, :mac, :android].include?(platform)
  true
end

.return_valueObject



151
152
153
# File 'lib/fastlane/plugin/auto_version_name/actions/auto_version_name_action.rb', line 151

def self.return_value
  # If your method provides a return value, you can describe here what it does
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
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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/fastlane/plugin/auto_version_name/actions/auto_version_name_action.rb', line 7

def self.run(params)
  # INITIALIZE
  branch = Actions.git_branch()
  default_platform = lane_context[SharedValues::DEFAULT_PLATFORM]
  last_commit_message = Actions.last_git_commit_message()
  final_version_array = []
  final_version_string = ""
  minimal_version_string = params[:minimal_version]
  has_any_live_version = false

  # FUNCTIONS
  def self.version_string_to_array(version_string)
    return version_string.split('.').map { |value| value.to_i }
  end

  def self.get_greater_version(minimal, ios, android)
    greater_version = ""

    minimal = minimal == nil ? "" : minimal
    ios = ios == nil ? "" : ios
    android = android == nil ? "" : android

    is_minimal_greater = Gem::Version.new(minimal) >= Gem::Version.new(ios) && Gem::Version.new(minimal) >= Gem::Version.new(android)

    is_ios_greater = Gem::Version.new(ios) >= Gem::Version.new(minimal) && Gem::Version.new(ios) >= Gem::Version.new(android)

    is_android_greater = Gem::Version.new(android) >= Gem::Version.new(minimal) && Gem::Version.new(android) >= Gem::Version.new(ios)

    if (is_minimal_greater)
      greater_version = minimal
    elsif (is_ios_greater)
      greater_version = ios
    elsif (is_android_greater)
      greater_version = android
    end

    if (greater_version.empty? || greater_version == nil)
      greater_version = minimal_version_string
    end

    return greater_version
  end        
  
  # iOS
  begin
    AppStoreBuildNumberAction.run(
      api_key: params[:ios_api_key],
      app_identifier: params[:ios_app_identifier],
      username: params[:ios_username],
      team_id: params[:ios_team_id],
      platform: "ios",
      live: true,
    )
    ios_version_string = lane_context[SharedValues::LATEST_VERSION];
    has_any_live_version = true

  rescue => exception
    puts exception
    ios_version_string = minimal_version_string
  ensure 
    if (default_platform == :ios)
      platform = Spaceship::ConnectAPI::Platform.map("ios")
      app = Spaceship::ConnectAPI::App.find(params[:ios_app_identifier])
      latest_version = app.get_latest_app_store_version(platform: platform)

      v_state = latest_version.app_store_state
      v_name = latest_version.version_string
      v_name_array = v_name.split('.').map { |value| value.to_i }

      if ((v_state == "WAITING_FOR_REVIEW" || v_state == "IN_REVIEW") && v_name_array.last > 0)
        UI.error "App Store Connect is processing a hotfix! Version: #{v_name}; Status: #{v_state}"
        exit(false)
      end
    end
  end

  # ANDROID
  begin
    android_version_string = GooglePlayTrackReleaseNamesAction.run(
      json_key: params[:android_json_key_path],
      package_name: params[:android_package_name],
      track: "production",
    ).first
    has_any_live_version = true

  rescue => exception
    puts exception
    android_version_string = minimal_version_string
  end
  
  greater_version_string = get_greater_version(
    minimal_version_string, ios_version_string, android_version_string
  )

  are_all_equal = [minimal_version_string, ios_version_string, android_version_string].uniq.size <= 1
  
  if (!has_any_live_version || (!are_all_equal && greater_version_string == minimal_version_string))
    return minimal_version_string
  end

  final_version_array = greater_version_string.split('.').map { |value| value.to_i }

  # Upgrade final version array and return
  major = final_version_array[0]
  minor = final_version_array[1]
  patch = final_version_array[2]

  # Auto increment.
  # 1.0.1 => 1.0.1(+1) => 1.0.2 || 1.0.1 => 1.0(+1).1 => 1.1.0
  if ((branch.include? 'hotfix') || (last_commit_message.include? 'hotfix'))
    patch += 1
  else
    minor += 1
    patch = 0
  end

  # Auto replacement if it reaches the limit.
  # 1.999.1000 => 1.999(+1).0 => 1(+1).0.0 => 2.0.0
  if(patch > 999)
    patch = 0
    minor += 1
  end

  if(minor > 999)
    minor = 0
    major += 1
  end

  final_version_array[0] = major
  final_version_array[1] = minor
  final_version_array[2] = patch

  final_version_string = final_version_array.join('.') # [1, 0, 0] => "1.0.0"
  return final_version_string
end

.version_string_to_array(version_string) ⇒ Object

FUNCTIONS



18
19
20
# File 'lib/fastlane/plugin/auto_version_name/actions/auto_version_name_action.rb', line 18

def self.version_string_to_array(version_string)
  return version_string.split('.').map { |value| value.to_i }
end