diff --git a/README.md b/README.md index 9750a1b..ead1cd1 100644 --- a/README.md +++ b/README.md @@ -11,25 +11,17 @@ 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*}, - " \n", - false, - :prepend, - 0 -end - -File.open("AndroidManifest.xml", "w") { |f| f.write modified } +PatternPatch::Patch.new( + regexp: %r{^\s*}, + text: " \n", + mode: :prepend +).apply "AndroidManifest.xml" ``` Capture groups may be used within the text argument in any mode. Note that @@ -37,19 +29,12 @@ 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 @@ -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*}, - " \n", - false, - :prepend, - 0 -end - -File.open("AndroidManifest.xml", "w") { |f| f.write modified } +PatternPatch::Patch.new( + regexp: %r{^\s*}, + text: " \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" +```