-
Notifications
You must be signed in to change notification settings - Fork 8
Add version components digits configuration to DerivedBuildCodeFormatter #657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
09e71f9
Add custom number of digits for each version component
iangmaia 1c89711
Update CHANGELOG
iangmaia 5038b44
Merge branch 'iangmaia/derived-build-formatter-enhancements' into ian…
iangmaia 6e4033b
Decrease total digits to 8
iangmaia bf7d345
Remove dynamic format string interpolation
iangmaia f107df1
Remove comment referring to dynamic formatting that has been removed
iangmaia 31ec3a2
Add validation for version components values
iangmaia 369f423
Update code to deal with version components as an array
iangmaia baf1e69
Group prefix-related statements in initializer
iangmaia 189b256
Add digits range info to comment
iangmaia bafaf1a
Update CHANGELOG.md
iangmaia 903b7ec
Apply suggestions for unit tests
iangmaia 7dc1aca
Use constant for min / max digit count
iangmaia 767671f
Various unit tests improvements
iangmaia fdb9111
Apply suggestions for unit tests comments
iangmaia 0c8c5ec
Simplify @prefix assignment
iangmaia 5e40646
Add comments
iangmaia cef6996
Fix Rubocop violation
iangmaia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,23 +3,35 @@ | |
| module Fastlane | ||
| module Wpmreleasetoolkit | ||
| module Versioning | ||
| # Max total for `*_digits` params, not counting prefix | ||
| MAX_TOTAL_DIGITS = 8 | ||
| MIN_DIGIT_COUNT = 1 | ||
| MAX_DIGIT_COUNT = 3 | ||
|
|
||
| # The `DerivedBuildCodeFormatter` class is a specialized build code formatter for derived build codes. | ||
| # It takes in an AppVersion object and derives a build code from it. | ||
| class DerivedBuildCodeFormatter | ||
| # Initialize the formatter with a configurable prefix. | ||
| # Initialize the formatter with configurable prefix and digit counts. | ||
| # | ||
| # @param [String] prefix The prefix to use for the build code. Must be a single digit (0-9), or empty string / nil. | ||
| # @param [Integer] major_digits Number of digits for major version. Must be between 1–3. Defaults to 2. | ||
| # @param [Integer] minor_digits Number of digits for minor version. Must be between 1–3. Defaults to 2. | ||
| # @param [Integer] patch_digits Number of digits for patch version. Must be between 1–3. Defaults to 2. | ||
| # @param [Integer] build_digits Number of digits for build number. Must be between 1–3. Defaults to 2. | ||
| # | ||
| def initialize(prefix: nil) | ||
| prefix ||= '' | ||
| def initialize(prefix: nil, major_digits: 2, minor_digits: 2, patch_digits: 2, build_digits: 2) | ||
| validate_prefix!(prefix) | ||
| @prefix = prefix.to_s | ||
|
|
||
| @digit_counts = [major_digits, minor_digits, patch_digits, build_digits] | ||
| @digit_counts.each { |d| validate_digit_count!(d) } | ||
| validate_total_digits!(@digit_counts) | ||
| end | ||
|
|
||
| # Calculate the next derived build code. | ||
| # | ||
| # This method derives a new build code from the given AppVersion object by concatenating the configured prefix, | ||
| # the major version, the minor version, the patch version, and the build number. | ||
| # the major version, the minor version, the patch version, and the build number with configurable digit counts. | ||
| # | ||
| # @param [AppVersion] version The AppVersion object to derive the next build code from. | ||
| # | ||
|
|
@@ -28,19 +40,16 @@ def initialize(prefix: nil) | |
| # | ||
| # @return [String] The formatted build code string. | ||
| # | ||
| def build_code(build_code = nil, version:) | ||
| result = format( | ||
| # The prefix is configurable to allow for additional platforms or | ||
| # extensions that could use a different digit prefix such as 2, etc. | ||
| '%<prefix>s%<major>.2i%<minor>.2i%<patch>.2i%<build_number>.2i', | ||
| prefix: @prefix, | ||
| major: version.major, | ||
| minor: version.minor, | ||
| patch: version.patch, | ||
| build_number: version.build_number | ||
| ) | ||
|
|
||
| result.gsub(/^0+/, '') | ||
| def build_code(_build_code = nil, version:) | ||
| formatted_components = version.components.zip(@digit_counts).map do |value, width| | ||
| comp = value.to_s.rjust(width, '0') | ||
| if comp.length > width | ||
| UI.user_error!("Version component value (#{value}) exceeds maximum allowed width of #{width} characters. " \ | ||
| "Consider increasing the corresponding `*_digits` parameter of your `#{self.class.name}`") | ||
| end | ||
| comp | ||
| end | ||
| [@prefix, *formatted_components].join.gsub(/^0+/, '') | ||
| end | ||
|
|
||
| private | ||
|
|
@@ -52,6 +61,10 @@ def build_code(build_code = nil, version:) | |
| # @raise [StandardError] If the prefix is invalid | ||
| # | ||
| def validate_prefix!(prefix) | ||
| unless prefix.nil? || prefix.is_a?(String) || prefix.is_a?(Integer) | ||
| UI.user_error!("Prefix must be a string or integer, got: #{prefix.class}") | ||
| end | ||
|
|
||
| prefix_str = prefix.to_s | ||
|
Comment on lines
63
to
68
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Today I re-learned that This is why even if now you call |
||
|
|
||
| # Allow empty string | ||
|
|
@@ -67,6 +80,37 @@ def validate_prefix!(prefix) | |
|
|
||
| UI.user_error!("Prefix must be an integer digit (0-9) or empty string, got: '#{prefix_str}'") | ||
| end | ||
|
|
||
| # Validates that the digit count is a valid positive integer within reasonable limits. | ||
| # | ||
| # @param [Integer] digit_count The digit count to validate | ||
| # | ||
| # @raise [StandardError] If the digit count is invalid | ||
| # | ||
| def validate_digit_count!(digit_count) | ||
| # Check if it's an integer | ||
| unless digit_count.is_a?(Integer) | ||
| UI.user_error!("Digit count must be an integer, got: #{digit_count.class}") | ||
| end | ||
|
|
||
| return if digit_count.between?(MIN_DIGIT_COUNT, MAX_DIGIT_COUNT) | ||
|
|
||
| UI.user_error!("Digit count must be between #{MIN_DIGIT_COUNT} and #{MAX_DIGIT_COUNT} digits, got: #{digit_count}") | ||
| end | ||
|
|
||
| # Validates that the total number of digits (excluding prefix) doesn't exceed the maximum for multiplatform compatibility. | ||
| # | ||
iangmaia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Since Google Play's max versionCode is ≈ 2_000_000_000, we want to avoid being too close to the limit | ||
| # as this would then block us from submitting any updates for that app if we reached it. | ||
| def validate_total_digits!(digits_list) | ||
| total_digits = digits_list.sum | ||
|
|
||
| # Limit total digits to 8 (excluding prefix) | ||
| return if total_digits <= MAX_TOTAL_DIGITS | ||
|
|
||
| UI.user_error!("Total digit count (#{total_digits}) exceeds maximum allowed (#{MAX_TOTAL_DIGITS}). " \ | ||
| "Current config: major(#{digits_list[0]}) + minor(#{digits_list[1]}) + patch(#{digits_list[2]}) + build(#{digits_list[3]}) digits") | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.