Class: Fastlane::Helper::AndroidVersionHelperHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/android_version_helper/helper/android_version_helper_helper.rb

Class Method Summary collapse

Class Method Details

.find_in_file(file_path, regex) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fastlane/plugin/android_version_helper/helper/android_version_helper_helper.rb', line 8

def self.find_in_file(file_path, regex)
  UI.user_error!("Could not find the file '#{file_path}'") unless File.exist?(file_path)

  content = File.readlines(file_path)
  captures = nil
  found = false
  line_index = nil

  content.each_with_index do |line, index|
    unless line.match(regex) && !found
      next
    end

    captures = line.match(regex)&.captures
    found = true
    line_index = index
    break
  end

  if found
    UI.message("#{captures[0]} was found in file '#{file_path}' on line #{line_index}")
  else
    UI.message("No line matched with regex '#{regex.inspect}' in file '#{file_path}'")
  end

  return captures&.at(0), line_index, content
end

.replace_in_file(file_path, regex, replacement) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/fastlane/plugin/android_version_helper/helper/android_version_helper_helper.rb', line 36

def self.replace_in_file(file_path, regex, replacement)
  value, line_index, content = find_in_file(file_path, regex)
  unless line_index.nil?
    content[line_index] = content[line_index].sub(value, replacement.to_s)
    File.write(file_path, content.join)
    UI.message("#{value} was replaced with #{replacement} in file '#{file_path}' on line #{line_index}")
  end

  return replacement.to_s, line_index, content
end