class Fastlane::Helper::AndroidVersionManagerHelper

Public Class Methods

build_gradle_exists?(app_project_dir) click to toggle source
# File lib/fastlane/plugin/android_version_manager/helper/android_version_manager_helper.rb, line 12
def self.build_gradle_exists?(app_project_dir)
  return BuildGradleFile.new(app_project_dir).exists?
end
find_build_gradle(app_project_dir) click to toggle source
# File lib/fastlane/plugin/android_version_manager/helper/android_version_manager_helper.rb, line 16
def self.find_build_gradle(app_project_dir)
  return BuildGradleFile.new(app_project_dir).find
end
get_key_from_gradle_file(file_path, key) click to toggle source

class methods that you define here become available in your action as `Helper::AndroidVersionManagerHelper.your_method` Most actions code are here to follow this advice: docs.fastlane.tools/advanced/actions/#calling-other-actions

# File lib/fastlane/plugin/android_version_manager/helper/android_version_manager_helper.rb, line 23
def self.get_key_from_gradle_file(file_path, key)
  UI.message("Hello from the android_version_manager plugin helper - get_key_from_file!")

  regex = Regexp.new(/(?<key>#{key}\s+)(?<equals>\=[\s]*?)?(?<left>[\'\"]?)(?<value>[a-zA-Z0-9\.\_]+)(?<right>[\'\"]?)(?<comment>.*)/)
  value = ""
  line_found = nil
  line_found_index = nil
  found = false
  Dir.glob(file_path) do |path|
    UI.verbose("get_key_from_gradle_file - path: #{path}")
    UI.verbose("get_key_from_gradle_file - absolute_path: #{File.expand_path(path)}")
    begin
      File.foreach(path).with_index do |line, index|
        unless line.match(regex) && !found
          next
        end
        line_found = line
        line_found_index = index
        _key, _equals, _left, value, _right, _comment = line.match(regex).captures
        break
      end
    end
  end
  return value, line_found, line_found_index
end
get_version_code_from_gradle_file(file_path, key) click to toggle source
# File lib/fastlane/plugin/android_version_manager/helper/android_version_manager_helper.rb, line 67
def self.get_version_code_from_gradle_file(file_path, key)
  version_code, _line, _line_index = get_key_from_gradle_file(file_path, key)

  UI.message("Read version code: #{version_code.inspect}")

  # Error out if version_number is not set
  if version_code.nil? || version_code == ""
    UI.user_error!("Unable to find version code with key #{key} on file #{file_path}")
  end

  version_code = Helper::AndroidVersionManagerHelper.string_to_int(version_code)

  # Error out if version_number is invalid
  if version_code.nil?
    UI.user_error!("Version code with key #{key} on file #{file_path} is invalid, it must be an integer")
  end

  return version_code
end
get_version_name_from_gradle_file(file_path, key) click to toggle source
# File lib/fastlane/plugin/android_version_manager/helper/android_version_manager_helper.rb, line 87
def self.get_version_name_from_gradle_file(file_path, key)
  version_name, _line, _line_index = get_key_from_gradle_file(file_path, key)

  UI.message("Read version name: #{version_name.inspect}")

  # Error out if version_name is not set
  if version_name.nil? || version_name == ""
    UI.user_error!("Unable to find version name with key #{key} on file #{file_path}")
  end

  version_name_parts = version_name.split(".")

  # major
  if version_name_parts.length == 1
    version_name = "#{version_name_parts[0]}.0.0"
  # major.minor
  elsif version_name_parts.length == 2
    version_name = "#{version_name_parts[0]}.#{version_name_parts[1]}.0"
  end

  begin
    version_name = Semantic::Version.new(version_name)
  rescue Exception # rubocop:disable RescueException
    raise $!, "Error parsing version name with key #{key} on file #{file_path}: #{$!}", $!.backtrace
  end

  return version_name
end
set_key_value_on_gradle_file(file_path, key, new_value) click to toggle source
# File lib/fastlane/plugin/android_version_manager/helper/android_version_manager_helper.rb, line 49
def self.set_key_value_on_gradle_file(file_path, key, new_value)
  # this will do the search again, only because we need the values again
  value, _line_found, line_found_index = get_key_from_gradle_file(file_path, key)

  # https://stackoverflow.com/a/4174125/710693
  Dir.glob(file_path) do |path|
    Tempfile.open(".#{File.basename(path)}", File.dirname(path), encoding: "UTF-8", binmode: true) do |tempfile|
      UI.verbose("set_key_value_on_gradle_file - path: #{path}")
      UI.verbose("set_key_value_on_gradle_file - absolute_path: #{File.expand_path(path)}")
      File.foreach(path).with_index do |line, index|
        tempfile.puts(index == line_found_index ? line.sub(value, new_value.to_s) : line)
      end
      tempfile.close
      FileUtils.mv(tempfile.path, path)
    end
  end
end
string_to_int(string) click to toggle source
# File lib/fastlane/plugin/android_version_manager/helper/android_version_manager_helper.rb, line 116
def self.string_to_int(string)
  # without exceptions:
  # num = string.to_i
  # num if num.to_s == string
  Integer(string || "", 10)
rescue ArgumentError
  nil
end