Class: Fastlane::Actions::FindReplaceStringAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/find_replace_string/actions/find_replace_string_action.rb

Class Method Summary collapse

Class Method Details

.authorsObject



26
27
28
# File 'lib/fastlane/plugin/find_replace_string/actions/find_replace_string_action.rb', line 26

def self.authors
    ["Jonathan Ritchie"]
end

.available_optionsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fastlane/plugin/find_replace_string/actions/find_replace_string_action.rb', line 34

def self.available_options
    [
        FastlaneCore::ConfigItem.new(key: :old_string,
            env_name: "OLD_STRING",
            description: "The string that will be replaced in the file",
            optional: false,
        type: String),
        FastlaneCore::ConfigItem.new(key: :new_string,
            env_name: "NEW_STRING",
            description: "The string that will be injected into the file",
            optional: false,
        type: String),
        FastlaneCore::ConfigItem.new(key: :path_to_file,
            env_name: "PATH_TO_FILE",
            description: "Path to the file to be modified",
            optional: false,
        type: String)
    ]
end

.descriptionObject



22
23
24
# File 'lib/fastlane/plugin/find_replace_string/actions/find_replace_string_action.rb', line 22

def self.description
    "Find and replace a string in a project file"
end

.detailsObject



30
31
32
# File 'lib/fastlane/plugin/find_replace_string/actions/find_replace_string_action.rb', line 30

def self.details
    "This plugin simply allows you to find a replace a string in a project file. All instances of the matched string in the file will be replaced. The string search is case sensitive"
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
# File 'lib/fastlane/plugin/find_replace_string/actions/find_replace_string_action.rb', line 54

def self.is_supported?(platform)
    [:ios, :mac, :android].include?(platform)
    true
end

.run(params) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/fastlane/plugin/find_replace_string/actions/find_replace_string_action.rb', line 4

def self.run(params)
    path_to_file = File.expand_path(params[:path_to_file])
    old_string = params[:old_string]
    new_string = params[:new_string]

    UI.user_error!("ERROR - File not found at location: '#{path_to_file}'") unless File.exist?(path_to_file)

    UI.message("Reading file...")
    file = File.open(path_to_file, "r")
    fileText = file.read
    file.close

    UI.message("Replacing all occurences of '#{old_string}' with '#{new_string}' in file...")
    fileText.gsub!(old_string, new_string)
    File.open(path_to_file, "w") { |file| file << fileText }
    UI.success("Find and replace finished!")
end