Module: Fastlane::Helper::Android::VersionHelper

Defined in:
lib/fastlane/plugin/wpmreleasetoolkit/helper/android/android_version_helper.rb

Overview

A module containing helper methods to manipulate/extract/bump Android version strings in gradle files

Constant Summary collapse

VERSION_NAME =

The key used in internal version Hash objects to hold the versionName value

'name'.freeze
VERSION_CODE =

The key used in internal version Hash objects to hold the versionCode value

'code'.freeze
MAJOR_NUMBER =

The index for the major version number part

0
MINOR_NUMBER =

The index for the minor version number part

1
HOTFIX_NUMBER =

The index for the hotfix version number part

2
ALPHA_PREFIX =

The prefix used in front of the versionName for alpha versions

'alpha-'.freeze
RC_SUFFIX =

The suffix used in the versionName for RC (beta) versions

'-rc'.freeze

Class Method Summary collapse

Class Method Details

.calc_next_release_base_version(version) ⇒ Hash

Compute the next release version name for the given version, without incrementing the version code

- The version name sees its minor version part incremented by one (and carried to next major if it reaches 10)
- The version code is unchanged. This method is intended to be called internally by other methods taking care of the version code bump.

Parameters:

  • version (Hash)

    A version hash, with keys ‘“name”` and `“code”`, containing the version to increment

Returns:

  • (Hash)

    Hash containing the next release version name (“X.Y”) and code.



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/android/android_version_helper.rb', line 59

def self.calc_next_release_base_version(version)
  version_name = remove_beta_suffix(version[VERSION_NAME])
  vp = get_version_parts(version_name)
  vp[MINOR_NUMBER] += 1
  if vp[MINOR_NUMBER] == 10
    vp[MAJOR_NUMBER] += 1
    vp[MINOR_NUMBER] = 0
  end

  { VERSION_NAME => "#{vp[MAJOR_NUMBER]}.#{vp[MINOR_NUMBER]}", VERSION_CODE => version[VERSION_CODE] }
end

.calc_next_release_short_version(version) ⇒ String

Compute the version name to use for the next release (‘“X.Y”`).

Parameters:

  • version (String)

    The version name (string) to increment

Returns:

  • (String)

    The version name for the next release



45
46
47
48
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/android/android_version_helper.rb', line 45

def self.calc_next_release_short_version(version)
  v = calc_next_release_base_version(VERSION_NAME => version, VERSION_CODE => nil)
  v[VERSION_NAME]
end

.get_library_version_from_gradle_config(build_gradle_path:, import_key:) ⇒ String

Extract the value of a import key from build.gradle

Parameters:

  • import_key (String)

    The key to look for

Returns:

  • (String)

    The value of the key, or nil if not found



89
90
91
92
93
94
95
96
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/android/android_version_helper.rb', line 89

def self.get_library_version_from_gradle_config(build_gradle_path:, import_key:)
  return nil unless File.exist?(build_gradle_path)

  File.open(build_gradle_path, 'r') do |f|
    text = f.read
    text.match(/^\s*(?:\w*\.)?#{Regexp.escape(import_key)}\s*=\s*['"](.*?)["']/m)&.captures&.first
  end
end

.get_release_version(version_properties_path:, is_alpha: false) ⇒ Hash

Extract the version name and code from the release version of the app from ‘version.properties file`

Returns:

  • (Hash)

    A hash with 2 keys “name” and “code” containing the extracted version name and code, respectively



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/android/android_version_helper.rb', line 26

def self.get_release_version(version_properties_path:, is_alpha: false)
  return nil unless File.exist?(version_properties_path)

  version_name_key = is_alpha ? 'alpha.versionName' : 'versionName'
  version_code_key = is_alpha ? 'alpha.versionCode' : 'versionCode'

  text = File.read(version_properties_path)
  name = text.match(/#{version_name_key}=(\S*)/m)&.captures&.first
  code = text.match(/#{version_code_key}=(\S*)/m)&.captures&.first

  name.nil? || code.nil? ? nil : { VERSION_NAME => name, VERSION_CODE => code.to_i }
end

.get_version_parts(version) ⇒ Array<Int>

Split a version string into its individual integer parts

Parameters:

  • version (String)

    The version string to split, e.g. “1.2.3.4”

Returns:

  • (Array<Int>)

    An array of integers containing the individual integer parts of the version. Always contains 3 items at minimum (0 are added to the end if the original string contains less than 3 parts)



119
120
121
122
123
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/android/android_version_helper.rb', line 119

def self.get_version_parts(version)
  parts = version.split('.').map(&:to_i)
  parts.fill(0, parts.length...3) # add 0 if needed to ensure array has at least 3 components
  parts
end

.is_hotfix?(version) ⇒ Bool

Determines if a version name corresponds to a hotfix

Parameters:

  • version (String)

    The version number to test

Returns:

  • (Bool)

    True if the version number has a non-zero 3rd component, meaning that it is a hotfix version.



77
78
79
80
81
82
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/android/android_version_helper.rb', line 77

def self.is_hotfix?(version)
  return false if version[VERSION_NAME].start_with?(ALPHA_PREFIX)

  vp = get_version_parts(version[VERSION_NAME])
  (vp.length > 2) && (vp[HOTFIX_NUMBER] != 0)
end

.remove_beta_suffix(version) ⇒ String

Remove the beta suffix (part after the ‘-`) from a version string

Examples:

remove_beta_suffix(“1.2.3-rc.4”) => “1.2.3”

Parameters:

  • version (String)

    The version string to remove the suffix from

Returns:

  • (String)

    The part of the version string without the beta suffix, i.e. the part before the first dash.



108
109
110
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/android/android_version_helper.rb', line 108

def self.remove_beta_suffix(version)
  version.split('-')[0]
end