diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d2b8142e..8966d50c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,8 @@ _None_ ### New Features -_None_ +- Add `ios_get_build_number` action to get the current build number from an `xcconfig` file. [#458] + ### Bug Fixes diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_get_build_number.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_get_build_number.rb new file mode 100644 index 000000000..d6f3aa11a --- /dev/null +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_get_build_number.rb @@ -0,0 +1,54 @@ +module Fastlane + module Actions + class IosGetBuildNumberAction < Action + def self.run(params) + require_relative '../../helper/ios/ios_version_helper' + + xcconfig_file_path = params[:xcconfig_file_path] + Fastlane::Helper::Ios::VersionHelper.read_build_number_from_config_file(xcconfig_file_path) + end + + ##################################################### + # @!group Documentation + ##################################################### + + def self.description + 'Gets the build number of the app' + end + + def self.details + 'Gets the build number of the app' + end + + def self.available_options + [ + FastlaneCore::ConfigItem.new( + key: :xcconfig_file_path, + env_name: 'FL_IOS_XCCONFIG_FILE_PATH', + description: 'Path to the .xcconfig file containing the build number', + type: String, + optional: false + ), + ] + end + + def self.output + # Define the shared values you are going to provide + end + + def self.return_value + # If you method provides a return value, you can describe here what it does + 'Return the build number of the app' + end + + def self.authors + # So no one will ever forget your contribution to fastlane :) You are awesome btw! + ['Automattic'] + end + + def self.is_supported?(platform) + [:ios, :mac].include?(platform) + end + end + end +end diff --git a/spec/ios_get_build_number_spec.rb b/spec/ios_get_build_number_spec.rb new file mode 100644 index 000000000..17482b3e6 --- /dev/null +++ b/spec/ios_get_build_number_spec.rb @@ -0,0 +1,64 @@ +require 'spec_helper' + +describe Fastlane::Actions::IosGetBuildNumberAction do + describe 'getting the build number from the provided .xcconfig file' do + it 'parses an xcconfig file with keys without spacing and returns the correct build number' do + xcconfig_mock_content = <<~CONTENT + // a comment + VERSION_SHORT=6 + VERSION_LONG=6.30.0 + BUILD_NUMBER=1940 + CONTENT + + expect_build_number(xcconfig_mock_content: xcconfig_mock_content, expected_build_number: '1940') + end + + it 'parses an xcconfig file with keys with spaces and returns a nil build number' do + xcconfig_mock_content = <<~CONTENT + VERSION_SHORT = 6 + VERSION_LONG = 6.30.1 + BUILD_NUMBER = 1940 + CONTENT + + expect_build_number(xcconfig_mock_content: xcconfig_mock_content, expected_build_number: nil) + end + + it 'parses an xcconfig file with an invalid format and returns a nil build number' do + xcconfig_mock_content = <<~CONTENT + VERSION_SHORT = 6 + VERSION_LONG = 6.30.1 + BUILD_NUMBER 1940 + CONTENT + + expect_build_number(xcconfig_mock_content: xcconfig_mock_content, expected_build_number: nil) + end + + it 'parses an xcconfig file with no build number and returns a nil build number' do + xcconfig_mock_content = <<~CONTENT + VERSION_SHORT = 6 + // a comment + CONTENT + + expect_build_number(xcconfig_mock_content: xcconfig_mock_content, expected_build_number: nil) + end + + it 'throws an error when the xcconfig file does not exist' do + expect do + run_described_fastlane_action( + xcconfig_file_path: 'file/not/found' + ) + # Ruby error for 'No such file or directory': https://ruby-doc.org/core-2.7.4/SystemCallError.html + end.to raise_error(Errno::ENOENT) + end + + def expect_build_number(xcconfig_mock_content:, expected_build_number:) + with_tmp_file(named: 'mock_xcconfig.xcconfig', content: xcconfig_mock_content) do |tmp_file_path| + build_number_result = run_described_fastlane_action( + xcconfig_file_path: tmp_file_path + ) + + expect(build_number_result).to eq(expected_build_number) + end + end + end +end