Skip to content

Commit

Permalink
README updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jdee committed Nov 6, 2017
1 parent 7a2c23d commit 1b0f507
Showing 1 changed file with 23 additions and 35 deletions.
58 changes: 23 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,30 @@ This is a very preliminary utility gem to apply and revert patches to strings (t
of the main intended use cases for this plugin is source-code modification, e.g.
when automatically integrating an SDK.

The current API is low-level, having evolved out of a Fastlane plugin helper. A better data model
will probably arise in later releases.

Please provide any feedback via issues in this repo.

```Ruby
require "pattern_patch"

# Add a meta-data key to the end of the application element of an Android manifest
modified = File.open("AndroidManifest.xml") do |file|
PatternPatch::Utilities.apply_patch file.read,
%r{^\s*</application>},
" <meta-data android:name=\"foo\" android:value=\"bar\" />\n",
false,
:prepend,
0
end

File.open("AndroidManifest.xml", "w") { |f| f.write modified }
PatternPatch::Patch.new(
regexp: %r{^\s*</application>},
text: " <meta-data android:name=\"foo\" android:value=\"bar\" />\n",
mode: :prepend
).apply "AndroidManifest.xml"
```

Capture groups may be used within the text argument in any mode. Note that
this works best without interpolation (single quotes or %q). If you use double
quotes, the backslash must be escaped, e.g. `text: "\\1\"MyOtherpod\""`.

```Ruby
require "pattern_patch"

# Change the name of a pod in a podspec
modified = File.open("AndroidManifest.xml") do |file|
PatternPatch::Utilities.apply_patch file.read,
/(s\.name\s*=\s*)"MyPod"/,
'\1"MyOtherPod"',
false,
:replace,
0
end

File.open("AndroidManifest.xml", "w") { |f| f.write modified }
PatternPatch::Patch.new(
regexp: /(s\.name\s*=\s*)"MyPod"/,
text: '\1"MyOtherPod"',
mode: :replace
).apply "MyPod.podspec"
```

Patches in `:append` mode using capture groups in the text argument may be
Expand All @@ -61,16 +46,19 @@ Revert patches by passing the optional `:revert` parameter:

```Ruby
# Revert the patch that added the metadata key to the end of the Android manifest, resulting in the original.
modified = File.open("AndroidManifest.xml") do |file|
PatternPatch::Utilities.revert_patch file.read,
%r{^\s*</application>},
" <meta-data android:name=\"foo\" android:value=\"bar\" />\n",
false,
:prepend,
0
end

File.open("AndroidManifest.xml", "w") { |f| f.write modified }
PatternPatch::Patch.new(
regexp: %r{^\s*</application>},
text: " <meta-data android:name=\"foo\" android:value=\"bar\" />\n",
mode: :prepend
).apply "AndroidManifest.xml"
```

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

#### Define patches in YAML files

Load a patch defined in YAML and apply it.

```Ruby
PatternPatch::Patch.from_yaml("patch.yaml").apply "file.txt"
```

0 comments on commit 1b0f507

Please sign in to comment.