Class: Fastlane::Helper::AndroidVersionManageHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/android_version_manage/helper/android_version_manage_helper.rb

Constant Summary collapse

GRADLE_FILE_TEST =
"/tmp/fastlane/tests/android-version-manage/app/build.gradle.kts"

Class Method Summary collapse

Class Method Details

.get_gradle_file(gradle_file) ⇒ Object



9
10
11
# File 'lib/fastlane/plugin/android_version_manage/helper/android_version_manage_helper.rb', line 9

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

.get_gradle_file_path(gradle_file) ⇒ Object



13
14
15
16
# File 'lib/fastlane/plugin/android_version_manage/helper/android_version_manage_helper.rb', line 13

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

.get_new_version_code(gradle_file, new_version_code) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/fastlane/plugin/android_version_manage/helper/android_version_manage_helper.rb', line 18

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) ⇒ Object



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

def self.get_new_version_name(gradle_file, new_version_name, bump_type)
  if new_version_name.nil?
    current_version_name = self.read_key_from_gradle_file(gradle_file, "versionName")
    current_version_parts = current_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
  end

  return new_version_name.to_s
end

.read_key_from_gradle_file(gradle_file, key) ⇒ Object



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

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



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

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) && 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