Class: Fastlane::Helper::PodspecHelper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ PodspecHelper

Returns a new instance of PodspecHelper.



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

def initialize(path = nil)
  version_var_name = 'version'
  @version_regex = /^(?<begin>[^#]*#{version_var_name}\s*=\s*['"])(?<value>(?<major>[0-9]+)(\.(?<minor>[0-9]+))?(\.(?<patch>[0-9]+))?)(?<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 '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 '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 '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 '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 'lib/fastlane/helper/podspec_helper.rb', line 8

def version_value
  @version_value
end

Instance Method Details

#bump_version(bump_type) ⇒ Object



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

def bump_version(bump_type)
  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



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

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



50
51
52
53
54
55
56
57
# File 'lib/fastlane/helper/podspec_helper.rb', line 50

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