Class: Fastlane::Actions::PatchAction

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

Class Method Summary collapse

Class Method Details

.authorsObject



65
66
67
# File 'lib/fastlane/plugin/patch/actions/patch_action.rb', line 65

def self.authors
  ["Jimmy Dee"]
end

.available_optionsObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/fastlane/plugin/patch/actions/patch_action.rb', line 104

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),
    FastlaneCore::ConfigItem.new(key: :revert,
                         description: "Set to true to revert the specified patch rather than apply it",
                            optional: true,
                       default_value: false,
                           is_string: false)
  ]
end

.categoryObject



149
150
151
# File 'lib/fastlane/plugin/patch/actions/patch_action.rb', line 149

def self.category
  :project
end

.descriptionObject



61
62
63
# File 'lib/fastlane/plugin/patch/actions/patch_action.rb', line 61

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

.detailsObject



69
70
71
72
73
74
75
# File 'lib/fastlane/plugin/patch/actions/patch_action.rb', line 69

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 option.
  EOF
end

.example_codeObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/fastlane/plugin/patch/actions/patch_action.rb', line 77

def self.example_code
  [
    <<-EOF
      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
      patch(
        files: "examples/PatchTestAndroid/app/src/main/AndroidManifest.xml",
        patch: "patch.yaml"
      )
    EOF,
    <<-EOF
      patch(
        files: "examples/PatchTestAndroid/app/src/main/AndroidManifest.xml",
        patch: "patch.yaml",
        revert: true
      )
    EOF

  ]
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/fastlane/plugin/patch/actions/patch_action.rb', line 145

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
51
52
53
54
55
56
57
58
59
# File 'lib/fastlane/plugin/patch/actions/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 patch using the :files option" if files.empty?

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

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