Skip to content

Commit

Permalink
Merge pull request #3 from jdee/use-pattern-patch
Browse files Browse the repository at this point in the history
Use pattern_patch
  • Loading branch information
jdee authored Oct 12, 2017
2 parents 69f7eef + 7402640 commit 0d50425
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 301 deletions.
4 changes: 3 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
PATH
remote: .
specs:
fastlane-plugin-patch (0.4.0)
fastlane-plugin-patch (0.4.1)
pattern_patch

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -106,6 +107,7 @@ GEM
parallel (1.12.0)
parser (2.4.0.0)
ast (~> 2.2)
pattern_patch (0.1.0)
plist (3.3.0)
powerpack (0.1.1)
pry (0.10.4)
Expand Down
2 changes: 2 additions & 0 deletions fastlane-plugin-patch.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']

spec.add_dependency 'pattern_patch'

spec.add_development_dependency 'pry'
spec.add_development_dependency 'bundler'
spec.add_development_dependency 'rspec'
Expand Down
12 changes: 6 additions & 6 deletions lib/fastlane/plugin/patch/actions/apply_patch_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ def self.run(params)

files.each do |file|
modified_contents = File.open(file, "r") do |f|
helper.apply_patch f.read,
params[:regexp],
params[:text],
params[:global],
params[:mode],
params[:offset]
PatternPatch::Utilities.apply_patch f.read,
params[:regexp],
params[:text],
params[:global],
params[:mode],
params[:offset]
end

File.open(file, "w") { |f| f.write modified_contents }
Expand Down
25 changes: 13 additions & 12 deletions lib/fastlane/plugin/patch/actions/patch_action.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'pattern_patch'
require 'yaml'

module Fastlane
Expand Down Expand Up @@ -35,19 +36,19 @@ def self.run(params)
files.each do |file|
modified_contents = File.open(file, "r") do |f|
if params[:revert]
helper.revert_patch f.read,
params[:regexp],
params[:text],
params[:global],
params[:mode],
params[:offset]
PatternPatch::Utilities.revert_patch f.read,
params[:regexp],
params[:text],
params[:global],
params[:mode],
params[:offset]
else
helper.apply_patch f.read,
params[:regexp],
params[:text],
params[:global],
params[:mode],
params[:offset]
PatternPatch::Utilities.apply_patch f.read,
params[:regexp],
params[:text],
params[:global],
params[:mode],
params[:offset]
end
end

Expand Down
13 changes: 7 additions & 6 deletions lib/fastlane/plugin/patch/actions/revert_patch_action.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'pattern_patch'
require 'yaml'

module Fastlane
Expand Down Expand Up @@ -34,12 +35,12 @@ def self.run(params)

files.each do |file|
modified_contents = File.open(file, "r") do |f|
helper.revert_patch f.read,
params[:regexp],
params[:text],
params[:global],
params[:mode],
params[:offset]
PatternPatch::Utilities.revert_patch f.read,
params[:regexp],
params[:text],
params[:global],
params[:mode],
params[:offset]
end

File.open(file, "w") { |f| f.write modified_contents }
Expand Down
101 changes: 0 additions & 101 deletions lib/fastlane/plugin/patch/helper/patch_helper.rb
Original file line number Diff line number Diff line change
@@ -1,108 +1,7 @@
class String
# Replace capture group references in self with appropriate
# data from matches. Modifies the receiver. The receiver
# need not match the matches.regexp.
#
# :matches: A MatchData object returned by Regexp#match
def apply_matches!(matches)
search_position = 0
while (m = /\\(\d+)/.match(self, search_position))
capture_group = m[1].to_i
search_position = index m[0]
gsub! m[0], matches[capture_group]
search_position += matches[capture_group].length
end
nil
end

# Return a copy of the receiver with capture group references
# in self replaced by appropriate data from matches. The receiver
# need not match the matches.regexp.
#
# :matches: A MatchData object returned by Regexp#match
def apply_matches(matches)
string = clone
string.apply_matches! matches
string
end
end

module Fastlane
module Helper
class PatchHelper
class << self
# 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
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

# 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
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

def files_from_params(params)
case params[:files]
when Array
Expand Down
2 changes: 1 addition & 1 deletion lib/fastlane/plugin/patch/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Fastlane
module Patch
VERSION = "0.4.0"
VERSION = "0.4.1"
end
end
Loading

0 comments on commit 0d50425

Please sign in to comment.