Skip to content

Commit 6a7ea8e

Browse files
committed
Finish PatchHelper specs and update README
1 parent 8f5c581 commit 6a7ea8e

5 files changed

Lines changed: 57 additions & 39 deletions

File tree

‎README.md‎

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,21 @@ fastlane add_plugin patch
1414

1515
Apply and revert pattern-based patches to any text file.
1616

17+
This is a very preliminary plugin to apply and revert patches to text files. One
18+
of the main intended use cases for this plugin is source-code modification, e.g.
19+
when automatically integrating an SDK.
20+
21+
Please provide any feedback via issues in this repo.
22+
1723
### apply_patch action
1824

1925
```Ruby
20-
apply_patch files: "examples/PatchTestAndroid/app/src/main/AndroidManifest.xml",
21-
regexp: %r{^\s*</application>},
22-
mode: :prepend,
23-
text: " <meta-data android:name=\"foo\" android:value=\"bar\" />\n"
26+
apply_patch(
27+
files: "examples/PatchTestAndroid/app/src/main/AndroidManifest.xml",
28+
regexp: %r{^\s*</application>},
29+
mode: :prepend,
30+
text: " <meta-data android:name=\"foo\" android:value=\"bar\" />\n"
31+
)
2432
```
2533

2634
This action matches one or all occurrences of a specified regular expression and
@@ -42,24 +50,30 @@ global: false
4250
4351
**Fastfile**:
4452
```Ruby
45-
apply_patch files: "examples/PatchTestAndroid/app/src/main/AndroidManifest.xml",
46-
patch: "patch.yaml"
53+
apply_patch(
54+
files: "examples/PatchTestAndroid/app/src/main/AndroidManifest.xml",
55+
patch: "patch.yaml"
56+
)
4757
```
4858

4959
### revert_patch action
5060

5161
Revert patches by passing the same arguments to the `revert_patch` action:
5262

5363
```Ruby
54-
revert_patch files: "examples/PatchTestAndroid/app/src/main/AndroidManifest.xml",
55-
regexp: %r{^\s*</application>},
56-
mode: :prepend,
57-
text: " <meta-data android:name=\"foo\" android:value=\"bar\" />\n"
64+
revert_patch(
65+
files: "examples/PatchTestAndroid/app/src/main/AndroidManifest.xml",
66+
regexp: %r{^\s*</application>},
67+
mode: :prepend,
68+
text: " <meta-data android:name=\"foo\" android:value=\"bar\" />\n"
69+
)
5870
```
5971

6072
```Ruby
61-
revert_patch files: "examples/PatchTestAndroid/app/src/main/AndroidManifest.xml",
62-
patch: "patch.yaml"
73+
revert_patch(
74+
files: "examples/PatchTestAndroid/app/src/main/AndroidManifest.xml",
75+
patch: "patch.yaml"
76+
)
6377
```
6478

6579
Patches using the `:replace` mode cannot be reverted.

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ def self.run(params)
3131
helper = Fastlane::Helper::PatchHelper
3232
helper.files_from_params(params).each do |file|
3333
modified_contents = File.open(file, "r") do |f|
34-
contents = f.read
35-
helper.apply_patch contents,
34+
helper.apply_patch f.read,
3635
params[:regexp],
3736
params[:text],
3837
params[:global],
@@ -54,13 +53,12 @@ def self.authors
5453
["Jimmy Dee"]
5554
end
5655

57-
def self.return_value
58-
# If your method provides a return value, you can describe here what it does
59-
end
60-
6156
def self.details
62-
# Optional:
63-
"More to come"
57+
<<-EOF
58+
This is a very preliminary plugin to apply and revert patches to text files. One
59+
of the main intended use cases for this plugin is source-code modification, e.g.
60+
when automatically integrating an SDK.
61+
EOF
6462
end
6563

6664
def self.available_options
@@ -74,7 +72,7 @@ def self.available_options
7472
optional: true,
7573
type: Regexp),
7674
FastlaneCore::ConfigItem.new(key: :text,
77-
description: "Text to append to the match",
75+
description: "Text used to modify to the match",
7876
optional: true,
7977
type: String),
8078
FastlaneCore::ConfigItem.new(key: :global,
@@ -100,10 +98,6 @@ def self.available_options
10098
end
10199

102100
def self.is_supported?(platform)
103-
# Adjust this if your plugin only works for a particular platform (iOS vs. Android, for example)
104-
# See: https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Platforms.md
105-
#
106-
# [:ios, :mac, :android].include?(platform)
107101
true
108102
end
109103
end

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ def self.run(params)
3131
helper = Fastlane::Helper::PatchHelper
3232
helper.files_from_params(params).each do |file|
3333
modified_contents = File.open(file, "r") do |f|
34-
contents = f.read
35-
helper.revert_patch contents,
34+
helper.revert_patch f.read,
3635
params[:regexp],
3736
params[:text],
3837
params[:global],
@@ -54,13 +53,12 @@ def self.authors
5453
["Jimmy Dee"]
5554
end
5655

57-
def self.return_value
58-
# If your method provides a return value, you can describe here what it does
59-
end
60-
6156
def self.details
62-
# Optional:
63-
"More to come"
57+
<<-EOF
58+
This is a very preliminary plugin to apply and revert patches to text files. One
59+
of the main intended use cases for this plugin is source-code modification, e.g.
60+
when automatically integrating an SDK.
61+
EOF
6462
end
6563

6664
def self.available_options
@@ -74,7 +72,7 @@ def self.available_options
7472
optional: true,
7573
type: Regexp),
7674
FastlaneCore::ConfigItem.new(key: :text,
77-
description: "Text to append to the match",
75+
description: "Text used to modify to the match",
7876
optional: true,
7977
type: String),
8078
FastlaneCore::ConfigItem.new(key: :global,
@@ -100,10 +98,6 @@ def self.available_options
10098
end
10199

102100
def self.is_supported?(platform)
103-
# Adjust this if your plugin only works for a particular platform (iOS vs. Android, for example)
104-
# See: https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Platforms.md
105-
#
106-
# [:ios, :mac, :android].include?(platform)
107101
true
108102
end
109103
end

‎lib/fastlane/plugin/patch/helper/patch_helper.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def files_from_params(params)
7575
when String
7676
params[:files].split(",")
7777
else
78-
raise ArgumentError "Invalid type #{params[:files].class} for :files option. Specify an Array or a String."
78+
raise ArgumentError, "Invalid type #{params[:files].class} for :files option. Specify an Array or a String."
7979
end
8080
end
8181
end

‎spec/patch_helper_spec.rb‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,20 @@
108108
end
109109
end
110110
end
111+
112+
describe 'files_from_params' do
113+
it 'returns an array of strings when an array is passed' do
114+
expect(helper.files_from_params(files: [:a, :b, :c])).to eq %w{a b c}
115+
end
116+
117+
it 'splits at commas when a string is passed' do
118+
expect(helper.files_from_params(files: 'a,b,c')).to eq %w{a b c}
119+
end
120+
121+
it 'raises for any other type' do
122+
expect do
123+
helper.files_from_params(files: true)
124+
end.to raise_error ArgumentError
125+
end
126+
end
111127
end

0 commit comments

Comments
 (0)