Class: Fastlane::Helper::PatchHelper
- Inherits:
-
Object
- Object
- Fastlane::Helper::PatchHelper
- Defined in:
- lib/fastlane/plugin/patch/helper/patch_helper.rb
Class Method Summary collapse
-
.apply_patch(contents, regexp, text, global, mode, offset) ⇒ Object
Add the specified text after the specified pattern.
- .files_from_params(params) ⇒ Object
-
.revert_patch(contents, regexp, text, global, mode, offset) ⇒ Object
Reverts a patch.
Class Method Details
.apply_patch(contents, regexp, text, global, mode, offset) ⇒ Object
Add the specified text after the specified pattern. Returns a modified copy of the string.
:contents: A string to modify, e.g. the contents of a file :regexp: A regular expression specifying a pattern to be matched :text: Text to be appended to the specified pattern :global: Boolean flag. If true, patch all occurrences of the regex. :mode: :append, :prepend or :replace to specify how to apply the patch :offset: Starting position for matching
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/fastlane/plugin/patch/helper/patch_helper.rb', line 43 def apply_patch(contents, regexp, text, global, mode, offset) search_position = offset while (matches = regexp.match(contents, search_position)) patched_pattern = case mode when :append "#{matches[0]}#{text.apply_matches matches}" when :prepend "#{text.apply_matches matches}#{matches[0]}" when :replace matches[0].sub regexp, text else raise ArgumentError, "Invalid mode argument. Specify :append, :prepend or :replace." end contents = "#{matches.pre_match}#{patched_pattern}#{matches.post_match}" break unless global search_position = matches.pre_match.length + patched_pattern.length end contents end |
.files_from_params(params) ⇒ Object
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/fastlane/plugin/patch/helper/patch_helper.rb', line 106 def files_from_params(params) case params[:files] when Array params[:files].map(&:to_s) when String params[:files].split(",") else raise ArgumentError, "Invalid type #{params[:files].class} for :files option. Specify an Array or a String." end end |
.revert_patch(contents, regexp, text, global, mode, offset) ⇒ Object
Reverts a patch. Use the same arguments that were supplied to apply_patch. The mode argument can only be :append or :prepend. Patches using :replace cannot be reverted. Returns a modified copy of the string.
:contents: A string to modify, e.g. the contents of a file :regexp: A regular expression specifying a pattern to be matched :text: Text to be appended to the specified pattern :global: Boolean flag. If true, patch all occurrences of the regex. :mode: :append or :prepend. :replace patches cannot be reverted automatically. :offset: Starting position for matching
76 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 103 104 |
# File 'lib/fastlane/plugin/patch/helper/patch_helper.rb', line 76 def revert_patch(contents, regexp, text, global, mode, offset) search_position = offset regexp_string = regexp.to_s patched_regexp = case mode when :append /#{regexp_string}#{text}/m when :prepend # TODO: Capture groups aren't currently revertible in :prepend mode. # This patched regexp can turn into something like /\1.*(\d+)/. # The capture group reference cannot occur in the regexp before definition # of the group. This would have to be transformed to something like # /(\d+).*\1/. Patch reversion is probably not a major use case right # now, so ignore for the moment. /#{text}#{regexp_string}/m else raise ArgumentError, "Invalid mode argument. Specify :append or :prepend." end while (matches = patched_regexp.match(contents, search_position)) reverted_text = matches[0].sub(text.apply_matches(matches), '') contents = "#{matches.pre_match}#{reverted_text}#{matches.post_match}" break unless global search_position = matches.pre_match.length + reverted_text.length end contents end |