Class: Fastlane::Helper::VersioningAndroidHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/versioning_android/helper/versioning_android_helper.rb

Constant Summary collapse

GRADLE_FILE_TEST =
"/tmp/fastlane/tests/versioning/app/build.gradle"

Class Method Summary collapse

Class Method Details

.get_gradle_file(gradle_file) ⇒ Object



10
11
12
# File 'lib/fastlane/plugin/versioning_android/helper/versioning_android_helper.rb', line 10

def self.get_gradle_file(gradle_file)
  return Helper.test? ? GRADLE_FILE_TEST : gradle_file
end

.get_gradle_file_path(gradle_file) ⇒ Object



14
15
16
17
# File 'lib/fastlane/plugin/versioning_android/helper/versioning_android_helper.rb', line 14

def self.get_gradle_file_path(gradle_file)
  gradle_file = self.get_gradle_file(gradle_file)
  return File.expand_path(gradle_file)
end

.get_new_version_code(gradle_file, new_version_code) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/fastlane/plugin/versioning_android/helper/versioning_android_helper.rb', line 19

def self.get_new_version_code(gradle_file, new_version_code)
  if new_version_code.nil?
    current_version_code = self.read_key_from_gradle_file(gradle_file, "versionCode")
    new_version_code = current_version_code.to_i + 1
  end

  return new_version_code.to_i
end

.get_new_version_name(gradle_file, new_version_name, bump_type = nil) ⇒ Object



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

def self.get_new_version_name(gradle_file, new_version_name, bump_type = nil)
  if new_version_name.nil?
    new_version_name = self.read_key_from_gradle_file(gradle_file, "versionName")
  end

  current_version_parts = new_version_name.split(/[.]/)
  major = current_version_parts[0].to_i
  minor = current_version_parts[1].to_i
  patch = current_version_parts[2].to_i

  if bump_type == "major"
    new_version_name = "#{major + 1}.0.0"
  elsif bump_type == "minor"
    new_version_name = "#{major}.#{minor + 1}.0"
  elsif bump_type == "patch"
    new_version_name = "#{major}.#{minor}.#{patch + 1}"
  end

  return new_version_name.to_s
end

.read_key_from_gradle_file(gradle_file, key) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fastlane/plugin/versioning_android/helper/versioning_android_helper.rb', line 49

def self.read_key_from_gradle_file(gradle_file, key)
  value = false
  begin
    file = File.new(gradle_file, "r")
    while (line = file.gets)
      next unless line.include? key
      components = line.strip.split(' ')
      value = components[components.length - 1].tr("\"", "")
      break
    end
    file.close
  rescue => err
    UI.error("An exception occured while reading gradle file: #{err}")
    err
  end
  return value
end

.save_key_to_gradle_file(gradle_file, key, value) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fastlane/plugin/versioning_android/helper/versioning_android_helper.rb', line 67

def self.save_key_to_gradle_file(gradle_file, key, value)
  current_value = self.read_key_from_gradle_file(gradle_file, key)

  begin
    found = false
    temp_file = Tempfile.new("flSave_#{key}_ToGradleFile")
    File.open(gradle_file, "r") do |file|
      file.each_line do |line|
        if line.include? key and found == false
          found = true
          line.replace line.sub(current_value.to_s, value.to_s)
        end
        temp_file.puts line
      end
      file.close
    end
    temp_file.rewind
    temp_file.close
    FileUtils.mv(temp_file.path, gradle_file)
    temp_file.unlink
  end

  return found == true ? value : -1
end