|
| 1 | +module Fastlane |
| 2 | + module Actions |
| 3 | + class VersionGetPodspecAction < Action |
| 4 | + def self.run(params) |
| 5 | + podspec_path = params[:path] |
| 6 | + |
| 7 | + UI.user_error!("Could not find podspec file at path '#{podspec_path}'") unless File.exist? podspec_path |
| 8 | + |
| 9 | + version_podspec_file = PodspecHelper.new(podspec_path) |
| 10 | + |
| 11 | + Actions.lane_context[SharedValues::PODSPEC_VERSION_NUMBER] = version_podspec_file.version_value |
| 12 | + end |
| 13 | + |
| 14 | + ##################################################### |
| 15 | + # @!group Documentation |
| 16 | + ##################################################### |
| 17 | + |
| 18 | + def self.description |
| 19 | + "Receive the version number from a podspec file" |
| 20 | + end |
| 21 | + |
| 22 | + def self.available_options |
| 23 | + [ |
| 24 | + FastlaneCore::ConfigItem.new(key: :path, |
| 25 | + env_name: "FL_VERSION_PODSPEC_PATH", |
| 26 | + description: "You must specify the path to the podspec file", |
| 27 | + is_string: true, |
| 28 | + default_value: Dir["*.podspec"].last, |
| 29 | + verify_block: proc do |value| |
| 30 | + UI.user_error!("Please pass a path to the `version_get_podspec` action") if value.length == 0 |
| 31 | + end) |
| 32 | + ] |
| 33 | + end |
| 34 | + |
| 35 | + def self.output |
| 36 | + [ |
| 37 | + ['PODSPEC_VERSION_NUMBER', 'The podspec version number'] |
| 38 | + ] |
| 39 | + end |
| 40 | + |
| 41 | + def self.authors |
| 42 | + ["Liquidsoul", "KrauseFx"] |
| 43 | + end |
| 44 | + |
| 45 | + def self.is_supported?(platform) |
| 46 | + true |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + class PodspecHelper |
| 51 | + attr_accessor :path |
| 52 | + attr_accessor :podspec_content |
| 53 | + attr_accessor :version_regex |
| 54 | + attr_accessor :version_match |
| 55 | + attr_accessor :version_value |
| 56 | + |
| 57 | + def initialize(path = nil) |
| 58 | + version_var_name = 'version' |
| 59 | + @version_regex = /^(?<begin>[^#]*#{version_var_name}\s*=\s*['"])(?<value>(?<major>[0-9]+)(\.(?<minor>[0-9]+))?(\.(?<patch>[0-9]+))?(\.(?<type>[a-z]+))?(\.(?<buildnumber>[0-9]+))?)(?<end>['"])/i |
| 60 | + |
| 61 | + return unless (path || '').length > 0 |
| 62 | + UI.user_error!("Could not find podspec file at path '#{path}'") unless File.exist?(path) |
| 63 | + |
| 64 | + @path = File.expand_path(path) |
| 65 | + podspec_content = File.read(path) |
| 66 | + |
| 67 | + parse(podspec_content) |
| 68 | + end |
| 69 | + |
| 70 | + def parse(podspec_content) |
| 71 | + @podspec_content = podspec_content |
| 72 | + @version_match = @version_regex.match(@podspec_content) |
| 73 | + UI.user_error!("AAAAAH!!! Could not find version in podspec content '#{@podspec_content}'") if @version_match.nil? |
| 74 | + @version_value = @version_match[:value] |
| 75 | + end |
| 76 | + |
| 77 | + def bump_version(bump_type) |
| 78 | + major = version_match[:major].to_i |
| 79 | + minor = version_match[:minor].to_i || 0 |
| 80 | + patch = version_match[:patch].to_i || 0 |
| 81 | + |
| 82 | + case bump_type |
| 83 | + when 'patch' |
| 84 | + patch += 1 |
| 85 | + when 'minor' |
| 86 | + minor += 1 |
| 87 | + patch = 0 |
| 88 | + when 'major' |
| 89 | + major += 1 |
| 90 | + minor = 0 |
| 91 | + patch = 0 |
| 92 | + end |
| 93 | + |
| 94 | + @version_value = "#{major}.#{minor}.#{patch}" |
| 95 | + end |
| 96 | + |
| 97 | + def update_podspec(version = nil) |
| 98 | + new_version = version || @version_value |
| 99 | + updated_podspec_content = @podspec_content.gsub(@version_regex, "#{@version_match[:begin]}#{new_version}#{@version_match[:end]}") |
| 100 | + |
| 101 | + File.open(@path, "w") { |file| file.puts updated_podspec_content } unless Helper.test? |
| 102 | + |
| 103 | + updated_podspec_content |
| 104 | + end |
| 105 | + end |
| 106 | + end |
| 107 | +end |
0 commit comments