Skip to content

Commit d5be1af

Browse files
committed
Added a :patch option to allow reading patch data from a yaml file
1 parent 55c29c1 commit d5be1af

4 files changed

Lines changed: 58 additions & 8 deletions

File tree

‎README.md‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,35 @@ the action appends the specified text to the pattern match. It can also prepend
2727
the text or replace the pattern match with the text. Use an optional `:global`
2828
parameter to apply the patch to all instances of the regular expression.
2929

30+
The `regexp`, `text`, `mode` and `global` options may be specified in a YAML file to
31+
define a patch, e.g.:
32+
33+
```yaml
34+
regexp: '^\s*</application>'
35+
mode: "prepend"
36+
text: " <meta-data android:name=\"foo\" android:value=\"bar\" />\n"
37+
global: false
38+
```
39+
40+
```Ruby
41+
apply_patch file: "examples/PatchTestAndroid/app/src/main/AndroidManifest.xml",
42+
patch: "patch.yaml"
43+
```
44+
3045
### Options
3146
3247
|key|description|type|optional|default value|
3348
|---|-----------|----|--------|-------------|
3449
|:file|Absolute or relative path to a file to patch|String|no| |
35-
|:regexp|A regular expression to match|Regexp|no| |
36-
|:text|Text to append to the match|String|no| |
50+
|:regexp|A regular expression to match|Regexp|yes| |
51+
|:text|Text to append to the match|String|yes| |
3752
|:global|If true, patch all occurrences of the pattern|Boolean|yes|false|
3853
|:mode|:append, :prepend or :replace|Symbol|yes|:append|
3954
|:offset|Offset from which to start matching|Integer|yes|0|
55+
|:patch|A YAML file specifying patch data|String|yes| |
56+
57+
The :regexp and :text options must be set either in a patch file specified using the
58+
:patch argument or via arguments in the Fastfile.
4059
4160
## Example
4261

‎fastlane/Fastfile‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
lane :test do
22
apply_patch file: "examples/PatchTestAndroid/app/src/main/AndroidManifest.xml",
3-
regexp: %r{^\s*</application>},
4-
mode: :prepend,
5-
text: " <meta-data android:name=\"foo\" android:value=\"bar\" />\n"
3+
patch: "patch.yaml"
64
end

‎lib/fastlane/plugin/patch/actions/apply_patch_action.rb‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,33 @@
1+
require 'yaml'
2+
13
module Fastlane
24
module Actions
35
class ApplyPatchAction < Action
46
def self.run(params)
7+
if params[:patch]
8+
# raises
9+
patch = YAML.load_file params[:patch]
10+
11+
# If the :patch option is present, load these params from the
12+
# specified file. Action args override.
13+
%w{regexp text mode global}.each do |option|
14+
value = patch[option]
15+
next if value.nil?
16+
17+
case option.to_sym
18+
when :regexp
19+
params[:regexp] = /#{value}/
20+
when :mode
21+
params[:mode] = value.to_sym
22+
else
23+
params[option.to_sym] = value
24+
end
25+
end
26+
end
27+
28+
UI.user_error! "Must specify :regexp and :text either in a patch or via arguments" if
29+
params[:regexp].nil? || params[:text].nil?
30+
531
helper = Fastlane::Helper::PatchHelper
632
modified_contents = File.open(params[:file], "r") do |f|
733
contents = f.read
@@ -43,11 +69,11 @@ def self.available_options
4369
type: String),
4470
FastlaneCore::ConfigItem.new(key: :regexp,
4571
description: "A regular expression to match",
46-
optional: false,
72+
optional: true,
4773
type: Regexp),
4874
FastlaneCore::ConfigItem.new(key: :text,
4975
description: "Text to append to the match",
50-
optional: false,
76+
optional: true,
5177
type: String),
5278
FastlaneCore::ConfigItem.new(key: :global,
5379
description: "If true, patch all occurrences of the pattern",
@@ -63,7 +89,11 @@ def self.available_options
6389
description: ":append, :prepend or :replace",
6490
optional: true,
6591
default_value: :append,
66-
type: Symbol)
92+
type: Symbol),
93+
FastlaneCore::ConfigItem.new(key: :patch,
94+
description: "A YAML file specifying patch data",
95+
optional: true,
96+
type: String)
6797
]
6898
end
6999

‎patch.yaml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
regexp: '^\s*</application>'
2+
mode: "prepend"
3+
text: " <meta-data android:name=\"foo\" android:value=\"bar\" />\n"

0 commit comments

Comments
 (0)