Class: Fastlane::Actions::ApplyPatchAction

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

Class Method Summary collapse

Class Method Details

.authorsObject



55
56
57
# File 'lib/fastlane/plugin/patch/actions/apply_patch_action.rb', line 55

def self.authors
  ["Jimmy Dee"]
end

.available_optionsObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/fastlane/plugin/patch/actions/apply_patch_action.rb', line 86

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :files,
                         description: "Absolute or relative path(s) to one or more files to patch",
                            optional: false,
                           is_string: false),
    FastlaneCore::ConfigItem.new(key: :regexp,
                         description: "A regular expression to match",
                            optional: true,
                                type: Regexp),
    FastlaneCore::ConfigItem.new(key: :text,
                         description: "Text used to modify to the match",
                            optional: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :global,
                         description: "If true, patch all occurrences of the pattern",
                            optional: true,
                       default_value: false,
                           is_string: false),
    FastlaneCore::ConfigItem.new(key: :offset,
                         description: "Offset from which to start matching",
                            optional: true,
                       default_value: 0,
                                type: Integer),
    FastlaneCore::ConfigItem.new(key: :mode,
                         description: ":append, :prepend or :replace",
                            optional: true,
                       default_value: :append,
                                type: Symbol),
    FastlaneCore::ConfigItem.new(key: :patch,
                         description: "A YAML file specifying patch data",
                            optional: true,
                                type: String)
  ]
end

.categoryObject



132
133
134
# File 'lib/fastlane/plugin/patch/actions/apply_patch_action.rb', line 132

def self.category
  :deprecated
end

.deprecated_notesObject



126
127
128
129
130
# File 'lib/fastlane/plugin/patch/actions/apply_patch_action.rb', line 126

def self.deprecated_notes
  <<-EOF
Please use the patch action instead.
  EOF
end

.descriptionObject



51
52
53
# File 'lib/fastlane/plugin/patch/actions/apply_patch_action.rb', line 51

def self.description
  "Apply pattern-based patches to any text file."
end

.detailsObject



59
60
61
62
63
64
65
# File 'lib/fastlane/plugin/patch/actions/apply_patch_action.rb', line 59

def self.details
  <<-EOF
Append or prepend text to a specified pattern in a list of files or
replace it, once or globally. Patches are specified by arguments or
YAML files. Revert the same patches with the revert_patch action.
  EOF
end

.example_codeObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fastlane/plugin/patch/actions/apply_patch_action.rb', line 67

def self.example_code
  [
    <<-EOF
      apply_patch(
        files: "examples/PatchTestAndroid/app/src/main/AndroidManifest.xml",
        regexp: %r{^\s*</application>},
        mode: :prepend,
        text: "        <meta-data android:name=\"foo\" android:value=\"bar\" />\n"
      )
    EOF,
    <<-EOF
      apply_patch(
        files: "examples/PatchTestAndroid/app/src/main/AndroidManifest.xml",
        patch: "patch.yaml"
      )
    EOF
  ]
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/fastlane/plugin/patch/actions/apply_patch_action.rb', line 122

def self.is_supported?(platform)
  true
end

.run(params) ⇒ Object



6
7
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fastlane/plugin/patch/actions/apply_patch_action.rb', line 6

def self.run(params)
  if params[:patch]
    # raises
    patch = YAML.load_file params[:patch]

    # If the :patch option is present, load these params from the
    # specified file. Action args override.
    %w{regexp text mode global}.each do |option|
      value = patch[option]
      next if value.nil?

      case option.to_sym
      when :regexp
        params[:regexp] = /#{value}/
      when :mode
        params[:mode] = value.to_sym
      else
        params[option.to_sym] = value
      end
    end
  end

  UI.user_error! "Must specify :regexp and :text either in a patch or via arguments" if
    params[:regexp].nil? || params[:text].nil?

  helper = Helper::PatchHelper
  files = helper.files_from_params params
  UI.user_error! "Must specify at least one file to patch using the :files option" if files.empty?

  files.each do |file|
    modified_contents = File.open(file, "r") do |f|
      PatternPatch::Utilities.apply_patch f.read,
                                          params[:regexp],
                                          params[:text],
                                          params[:global],
                                          params[:mode],
                                          params[:offset]
    end

    File.open(file, "w") { |f| f.write modified_contents }
  end
rescue => e
  UI.user_error! "Error in ApplyPatchAction: #{e.message}\n#{e.backtrace}"
end