Skip to content

Commit d050d84

Browse files
Merge pull request #512 from wordpress-mobile/add/version-model
Add Version Models and Formatter/Calculator Classes
2 parents 6e3d2e9 + b3efb93 commit d050d84

37 files changed

+1279
-8
lines changed

CHANGELOG.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@ _None_
1010

1111
### New Features
1212

13-
- Renamed `addbranchprotection` to `set_branch_protection`, and allow it to provide additional optional protection
13+
- Adds `AppVersion` and `BuildCode` models that can be used by version actions. [#512]
14+
- Adds version calculator and formatter classes that can be used with the `AppVersion` and `BuildCode` models. [#512]
15+
- Adds methods for writing values to `xcconfig` files using the `Xcodeproj` gem. [#512]
16+
- Renamed `addbranchprotection` to `set_branch_protection`, and allow it to provide additional optional protection
1417
settings to set/update on the target branch (like `lock_branch`, `required_ci_checks`, etc).
1518
The `addbranchprotection` action name still exists for backward compatibility for now (with a deprecation notice),
1619
but it will be removed in a future major release. [#513]
17-
- Renamed `removebranchprotection` to `remove_branch_protection`.
20+
- Renamed `removebranchprotection` to `remove_branch_protection`.
1821
The `removebranchprotection` action name still exists for now for backward compatibility (with a deprecation notice),
1922
but it will be removed in a future major release. [#513]
20-
- Added `copy_branch_protection` action to replicate the branch protection settings of one branch onto another. [#513]
23+
- Added `copy_branch_protection` action to replicate the branch protection settings of one branch onto another. [#513]
2124

2225
### Bug Fixes
2326

Gemfile

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ source('https://rubygems.org')
22

33
gemspec
44

5+
gem 'buildkite-test_collector', '~> 2.3'
6+
gem 'codecov', require: false
57
gem 'danger', '~> 9.3'
68
gem 'danger-rubocop', '~> 0.6'
7-
8-
gem 'codecov', require: false
99
gem 'webmock', require: false
1010
gem 'yard'
11-
12-
gem 'buildkite-test_collector', '~> 2.3'

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ PATH
99
fastlane (~> 2.213)
1010
git (~> 1.3)
1111
google-cloud-storage (~> 1.31)
12+
java-properties (~> 0.3.0)
1213
nokogiri (~> 1.11)
1314
octokit (~> 6.1)
1415
parallel (~> 1.14)
@@ -264,6 +265,7 @@ GEM
264265
httpclient (2.8.3)
265266
i18n (1.12.0)
266267
concurrent-ruby (~> 1.0)
268+
java-properties (0.3.0)
267269
jmespath (1.6.2)
268270
json (2.6.3)
269271
jwt (2.7.0)

fastlane-plugin-wpmreleasetoolkit.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Gem::Specification.new do |spec|
3030
spec.add_dependency 'diffy', '~> 3.3'
3131
spec.add_dependency 'fastlane', '~> 2.213'
3232
spec.add_dependency 'git', '~> 1.3'
33+
spec.add_dependency 'java-properties', '~> 0.3.0'
3334
spec.add_dependency 'nokogiri', '~> 1.11' # Needed for AndroidLocalizeHelper
3435
spec.add_dependency 'octokit', '~> 6.1'
3536
spec.add_dependency 'parallel', '~> 1.14'

lib/fastlane/plugin/wpmreleasetoolkit.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Fastlane
44
module Wpmreleasetoolkit
55
# Return all .rb files inside the "actions", "helper" and "models" directories
66
def self.all_classes
7-
Dir[File.expand_path('**/{actions,helper,models}/**/*.rb', File.dirname(__FILE__))]
7+
Dir[File.expand_path('**/{actions,helper,models,versioning}/**/*.rb', File.dirname(__FILE__))]
88
end
99
end
1010
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module Fastlane
2+
module Models
3+
# The AppVersion model represents a version of an app with major, minor, patch, and build number components.
4+
class AppVersion
5+
attr_accessor :major, :minor, :patch, :build_number
6+
7+
# Initializes a new AppVersion instance.
8+
#
9+
# @param [Integer] major The major version number.
10+
# @param [Integer] minor The minor version number.
11+
# @param [Integer] patch The patch version number.
12+
# @param [Integer] build_number The build number.
13+
#
14+
def initialize(major, minor, patch = 0, build_number = 0)
15+
# Validate that the major and minor version numbers are not nil
16+
UI.user_error!('Major version cannot be nil') if major.nil?
17+
UI.user_error!('Minor version cannot be nil') if minor.nil?
18+
19+
@major = major
20+
@minor = minor
21+
@patch = patch
22+
@build_number = build_number
23+
end
24+
25+
# Converts the AppVersion object to a string representation.
26+
# This should only be used for internal debugging/testing purposes, not to write versions in version files
27+
# In order to format an `AppVersion` into a `String`, you should use the appropriate `VersionFormatter` for your project instead.
28+
#
29+
# @return [String] a string in the format "major.minor.patch.build_number".
30+
#
31+
def to_s
32+
"#{@major}.#{@minor}.#{@patch}.#{@build_number}"
33+
end
34+
end
35+
end
36+
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module Fastlane
2+
module Models
3+
# The `BuildCode` model represents a build code for an app. This could be the Version Code for an Android app or
4+
# the VERSION_LONG/BUILD_NUMBER for an iOS/Mac app.
5+
class BuildCode
6+
attr_accessor :build_code
7+
8+
# Initializes a new BuildCode instance with the provided build code value.
9+
#
10+
# @param build_code [String] The build code value.
11+
#
12+
def initialize(build_code)
13+
UI.user_error!('Build code cannot be nil') if build_code.nil?
14+
15+
@build_code = build_code
16+
end
17+
18+
# Returns the build code as a string.
19+
#
20+
# @return [String] The build code represented as a string.
21+
#
22+
def to_s
23+
@build_code.to_s
24+
end
25+
end
26+
end
27+
end
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
module Fastlane
2+
module Wpmreleasetoolkit
3+
module Versioning
4+
# The `AbstractVersionCalculator` class is responsible for performing version calculations and transformations. It can be used
5+
# as a base class for version calculations that use different versioning schemes. It contains calculation and
6+
# transformation methods that are shared by all platforms. It has the abstract suffix because it should not be
7+
# instantiated directly.
8+
class AbstractVersionCalculator
9+
# This method increments the major version component and resets minor, patch, and build number
10+
# components to zero.
11+
#
12+
# @param version [AppVersion] The version to calculate the next major version for.
13+
#
14+
# @return [AppVersion] The next major version.
15+
#
16+
def next_major_version(version:)
17+
new_version = version.dup
18+
new_version.major += 1
19+
new_version.minor = 0
20+
new_version.patch = 0
21+
new_version.build_number = 0
22+
23+
new_version
24+
end
25+
26+
# This method increments the minor version component and resets patch and build number components
27+
# to zero.
28+
#
29+
# @param version [AppVersion] The version to calculate the next minor version for.
30+
#
31+
# @return [AppVersion] The next minor version.
32+
#
33+
def next_minor_version(version:)
34+
new_version = version.dup
35+
new_version.minor += 1
36+
new_version.patch = 0
37+
new_version.build_number = 0
38+
39+
new_version
40+
end
41+
42+
# This method increments the patch version component and resets the build number component to zero.
43+
#
44+
# @param version [AppVersion] The version to calculate the next patch version for.
45+
#
46+
# @return [AppVersion] The next patch version.
47+
#
48+
def next_patch_version(version:)
49+
new_version = version.dup
50+
new_version.patch += 1
51+
new_version.build_number = 0
52+
53+
new_version
54+
end
55+
56+
# This method increments the build number component.
57+
#
58+
# @param version [AppVersion] The version to calculate the next build number for.
59+
#
60+
# @return [AppVersion] The next version with an incremented build number.
61+
#
62+
def next_build_number(version:)
63+
new_version = version.dup
64+
new_version.build_number += 1
65+
66+
new_version
67+
end
68+
69+
# Calculate the previous patch version by decrementing the patch version if it's not zero.
70+
#
71+
# @param [AppVersion] version The version to calculate the previous patch version for.
72+
#
73+
# @return [AppVersion] The previous patch version.
74+
#
75+
def previous_patch_version(version:)
76+
new_version = version.dup
77+
new_version.patch -= 1 unless version.patch.zero?
78+
new_version.build_number = 0
79+
80+
new_version
81+
end
82+
end
83+
end
84+
end
85+
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module Fastlane
2+
module Wpmreleasetoolkit
3+
module Versioning
4+
# The `DateBuildCodeCalculator` class is a build code calculator for apps that use date-based
5+
# build codes.
6+
class DateBuildCodeCalculator
7+
# Calculate the next internal build code by setting the build number to the current date.
8+
#
9+
# @param [AppVersion] version The version to calculate the next internal version for.
10+
#
11+
# @return [AppVersion] The next version with the build number set to the current date.
12+
#
13+
def next_build_code(version:)
14+
new_version = version.dup
15+
new_version.build_number = today_date
16+
17+
new_version
18+
end
19+
20+
private
21+
22+
# Get the current date in the format 'YYYYMMDD'.
23+
#
24+
# @return [String] The current date in 'YYYYMMDD' format.
25+
#
26+
def today_date
27+
DateTime.now.strftime('%Y%m%d')
28+
end
29+
end
30+
end
31+
end
32+
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require_relative 'abstract_version_calculator'
2+
3+
module Fastlane
4+
module Wpmreleasetoolkit
5+
module Versioning
6+
# The `DateVersionCalculator` class is a specialized version calculator for date-based versions
7+
# of an app, extending the `AbstractVersionCalculator` class.
8+
class DateVersionCalculator < AbstractVersionCalculator
9+
# Calculate the next date-based release version.
10+
#
11+
# If the current month is December, the method prompts the user to determine if the next
12+
# release will be the first release of the next year. If so, it increments the major version
13+
# and sets the minor version to 1, resetting the patch and build number components to zero.
14+
# Otherwise, it calculates the next minor version.
15+
#
16+
# @param [AppVersion] version The version to calculate the next date-based release version for.
17+
#
18+
# @return [AppVersion] The next date-based release version.
19+
#
20+
def next_release_version(version:)
21+
new_version = version.dup
22+
first_release_of_year = FastlaneCore::UI.confirm('Is this release the first release of next year?') if Time.now.month == 12
23+
if first_release_of_year
24+
new_version.major += 1
25+
new_version.minor = 1
26+
new_version.patch = 0
27+
new_version.build_number = 0
28+
29+
new_version
30+
else
31+
next_minor_version(version: version)
32+
end
33+
end
34+
end
35+
end
36+
end
37+
end

0 commit comments

Comments
 (0)