Class: Fastlane::Helper::PodspecHelper

Inherits:
Object
  • Object
show all
Defined in:
fastlane/lib/fastlane/helper/podspec_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, require_variable_prefix = true) ⇒ PodspecHelper

Returns a new instance of PodspecHelper.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'fastlane/lib/fastlane/helper/podspec_helper.rb', line 10

def initialize(path = nil, require_variable_prefix = true)
  version_var_name = 'version'
  variable_prefix = require_variable_prefix ? /\w\./ : //
  @version_regex = /^(?<begin>[^#]*#{variable_prefix}#{version_var_name}\s*=\s*['"])(?<value>(?<major>[0-9]+)(\.(?<minor>[0-9]+))?(\.(?<patch>[0-9]+))?(?<appendix>(\.[0-9]+)*)?(-(?<prerelease>(.+)))?)(?<end>['"])/i

  return unless (path || '').length > 0
  UI.user_error!("Could not find podspec file at path '#{path}'") unless File.exist?(path)

  @path = File.expand_path(path)
  podspec_content = File.read(path)

  parse(podspec_content)
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



4
5
6
# File 'fastlane/lib/fastlane/helper/podspec_helper.rb', line 4

def path
  @path
end

#podspec_contentObject

Returns the value of attribute podspec_content.



5
6
7
# File 'fastlane/lib/fastlane/helper/podspec_helper.rb', line 5

def podspec_content
  @podspec_content
end

#version_matchObject

Returns the value of attribute version_match.



7
8
9
# File 'fastlane/lib/fastlane/helper/podspec_helper.rb', line 7

def version_match
  @version_match
end

#version_regexObject

Returns the value of attribute version_regex.



6
7
8
# File 'fastlane/lib/fastlane/helper/podspec_helper.rb', line 6

def version_regex
  @version_regex
end

#version_valueObject

Returns the value of attribute version_value.



8
9
10
# File 'fastlane/lib/fastlane/helper/podspec_helper.rb', line 8

def version_value
  @version_value
end

Instance Method Details

#bump_version(bump_type) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'fastlane/lib/fastlane/helper/podspec_helper.rb', line 31

def bump_version(bump_type)
  UI.user_error!("Do not support bump of 'appendix', please use `update_version_appendix(appendix)` instead") if bump_type == 'appendix'

  major = version_match[:major].to_i
  minor = version_match[:minor].to_i || 0
  patch = version_match[:patch].to_i || 0

  case bump_type
  when 'patch'
    patch += 1
  when 'minor'
    minor += 1
    patch = 0
  when 'major'
    major += 1
    minor = 0
    patch = 0
  end

  @version_value = "#{major}.#{minor}.#{patch}"
end

#parse(podspec_content) ⇒ Object



24
25
26
27
28
29
# File 'fastlane/lib/fastlane/helper/podspec_helper.rb', line 24

def parse(podspec_content)
  @podspec_content = podspec_content
  @version_match = @version_regex.match(@podspec_content)
  UI.user_error!("Could not find version in podspec content '#{@podspec_content}'") if @version_match.nil?
  @version_value = @version_match[:value]
end

#update_podspec(version = nil) ⇒ Object



65
66
67
68
69
70
71
72
# File 'fastlane/lib/fastlane/helper/podspec_helper.rb', line 65

def update_podspec(version = nil)
  new_version = version || @version_value
  updated_podspec_content = @podspec_content.gsub(@version_regex, "#{@version_match[:begin]}#{new_version}#{@version_match[:end]}")

  File.open(@path, "w") { |file| file.puts(updated_podspec_content) } unless Helper.test?

  updated_podspec_content
end

#update_version_appendix(appendix = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'fastlane/lib/fastlane/helper/podspec_helper.rb', line 53

def update_version_appendix(appendix = nil)
  new_appendix = appendix || @version_value[:appendix]
  return if new_appendix.nil?

  new_appendix = new_appendix.sub(".", "") if new_appendix.start_with?(".")
  major = version_match[:major].to_i
  minor = version_match[:minor].to_i || 0
  patch = version_match[:patch].to_i || 0

  @version_value = "#{major}.#{minor}.#{patch}.#{new_appendix}"
end