Class: Fastlane::Actions::RevertPatchAction

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

Class Method Summary collapse

Class Method Details

.authorsObject



56
57
58
# File 'lib/fastlane/plugin/patch/actions/revert_patch_action.rb', line 56

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/revert_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 or :prepend",
                            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/revert_patch_action.rb', line 132

def self.category
  :deprecated
end

.deprecated_notesObject



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

def self.deprecated_notes
  <<-EOF
Please use the patch action with the :revert option instead.
  EOF
end

.descriptionObject



52
53
54
# File 'lib/fastlane/plugin/patch/actions/revert_patch_action.rb', line 52

def self.description
  "Revert the action of apply_patch"
end

.detailsObject



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

def self.details
  <<-EOF
Revert a patch by specifying the arguments provided to apply_patch
using arguments or the same YAML patch files.
  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/revert_patch_action.rb', line 67

def self.example_code
  [
    <<-EOF
      revert_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
      revert_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/revert_patch_action.rb', line 122

def self.is_supported?(platform)
  true
end

.run(params) ⇒ Object



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
50
# File 'lib/fastlane/plugin/patch/actions/revert_patch_action.rb', line 7

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 revert using the :files option" if files.empty?

  files.each do |file|
    modified_contents = File.open(file, "r") do |f|
      PatternPatch::Utilities.revert_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 RevertPatchAction: #{e.message}\n#{e.backtrace}"
end