pattern_patch 0.5.2 → 0.5.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pattern_patch/core_ext/string.rb +4 -2
- data/lib/pattern_patch/patch.rb +42 -10
- data/lib/pattern_patch/utilities.rb +16 -12
- data/lib/pattern_patch/version.rb +1 -1
- data/lib/pattern_patch.rb +32 -0
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb0842afd6861b0575ba1aec38ae58f7b63634b40b0701e6a203f6657bfe5b30
|
4
|
+
data.tar.gz: 4f75f07d932d25c2072453a08267ca277db019f1960bc1eee415e9e154efb089
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2b46b2c0340cb8d37976d28cd753a0f124bbd37d80816f97a9599fb216bc6df03bdbfa98e76323889246edd77d3342c9be722e473d4a0c1d1e2d34f8b57ff281
|
7
|
+
data.tar.gz: ac9a4e6bc36cb2b03282c988aeb6cf2a4de7ba2e304e99723e1d91575d7b2e2d5d1e13c6c9a81ac8244af01bec00d52e96bd7f5452362e209c9184e95a1f0ea4
|
@@ -3,7 +3,8 @@ class String
|
|
3
3
|
# data from matches. Modifies the receiver. The receiver
|
4
4
|
# need not match the matches.regexp.
|
5
5
|
#
|
6
|
-
#
|
6
|
+
# @param matches [MatchData] A MatchData object returned by Regexp#match
|
7
|
+
# @return nil
|
7
8
|
def apply_matches!(matches)
|
8
9
|
search_position = 0
|
9
10
|
while (m = /\\(\d+)/.match(self, search_position))
|
@@ -19,7 +20,8 @@ class String
|
|
19
20
|
# in self replaced by appropriate data from matches. The receiver
|
20
21
|
# need not match the matches.regexp.
|
21
22
|
#
|
22
|
-
#
|
23
|
+
# @param matches [MatchData] A MatchData object returned by Regexp#match
|
24
|
+
# @return [String] A modified copy of the receiver
|
23
25
|
def apply_matches(matches)
|
24
26
|
string = clone
|
25
27
|
string.apply_matches! matches
|
data/lib/pattern_patch/patch.rb
CHANGED
@@ -6,22 +6,31 @@ module PatternPatch
|
|
6
6
|
# The PatternPatch::Patch class defines a patch as an operation that
|
7
7
|
# may be applied to any file. Often the operation may also be reverted.
|
8
8
|
class Patch
|
9
|
+
# @!attribute regexp
|
9
10
|
# Regexp defining one or more matching regions in a file.
|
11
|
+
# @return [Regexp] The regular expression associated with this patch
|
10
12
|
attr_accessor :regexp
|
11
13
|
|
14
|
+
# @!attribute text
|
12
15
|
# String with text to use in the patch operation. May contain ERB.
|
16
|
+
# @return [String] The text to use with this patch
|
13
17
|
attr_accessor :text
|
14
18
|
|
19
|
+
# @!attribute mode
|
15
20
|
# Symbol specifying the patch mode: :append (default), :prepend or :replace
|
21
|
+
# @return [Symbol] The mode of this patch
|
16
22
|
attr_accessor :mode
|
17
23
|
|
24
|
+
# @!attribute global
|
18
25
|
# Setting this to true will apply the patch to all matches in the file.
|
19
26
|
# By default (when false), the patch is only applied to the first match.
|
27
|
+
# @return [true, false] Whether this patch is global
|
20
28
|
attr_accessor :global
|
21
29
|
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
30
|
+
# @!attribute text_file
|
31
|
+
# Path to a text file used to populate the text attribute. Setting this
|
32
|
+
# after construction modifies the text attribute.
|
33
|
+
# @return [String] Path to a text file used to populate the text attribute
|
25
34
|
attr_reader :text_file
|
26
35
|
|
27
36
|
class << self
|
@@ -33,7 +42,8 @@ module PatternPatch
|
|
33
42
|
# used to specify a Regexp with modifiers in YAML. Raises if the file
|
34
43
|
# cannot be loaded.
|
35
44
|
#
|
36
|
-
#
|
45
|
+
# @param path [String] Path to a YAML file containing a patch definition
|
46
|
+
# @return [Patch] A Patch initialized from the file
|
37
47
|
def from_yaml(path)
|
38
48
|
hash = YAML.load_file(path).symbolize_keys
|
39
49
|
|
@@ -73,8 +83,16 @@ module PatternPatch
|
|
73
83
|
# Construct a new Patch from the options. The following fields are mapped
|
74
84
|
# to the corresponding attributes: :regexp, :text, :text_file, :mode,
|
75
85
|
# :global. Raises ArgumentError if both :text and :text_file are specified.
|
86
|
+
# All values may be modified between construction and calling #apply or
|
87
|
+
# #revert.
|
76
88
|
#
|
77
|
-
#
|
89
|
+
# @param options [Hash)] Parameters used to construct the Patch
|
90
|
+
# @option options [Regexp] :regexp Value for the regexp attribute
|
91
|
+
# @option options [String] :text Value for the text attribute
|
92
|
+
# @option options [String] :text_file Value for the text_file attribute
|
93
|
+
# @option options [Symbol] :mode (:append) Value for the mode attribute
|
94
|
+
# @option options [true, false] :global (false) Value for the global attribute
|
95
|
+
# @raise [ArgumentError] If both :text and :text_file are specified
|
78
96
|
def initialize(options = {})
|
79
97
|
raise ArgumentError, "text and text_file are mutually exclusive" if options[:text] && options[:text_file]
|
80
98
|
|
@@ -101,8 +119,11 @@ module PatternPatch
|
|
101
119
|
# ERB using the :binding option. Pass the :offset option to specify a
|
102
120
|
# starting offset, in characters, from the beginning of the file.
|
103
121
|
#
|
104
|
-
#
|
105
|
-
#
|
122
|
+
# @param files [Array, String] One or more file paths to which to apply the patch.
|
123
|
+
# @param options [Hash] Options for applying the patch.
|
124
|
+
# @option options [Binding] :binding (nil) A Binding object to use when rendering ERB
|
125
|
+
# @option options [Integer] :offset (0) Offset in characters
|
126
|
+
# @raise [ArgumentError] In case of invalid mode (other than :append, :prepend, :replace)
|
106
127
|
def apply(files, options = {})
|
107
128
|
offset = options[:offset] || 0
|
108
129
|
files = [files] if files.kind_of? String
|
@@ -125,8 +146,11 @@ module PatternPatch
|
|
125
146
|
# ERB using the :binding option. Pass the :offset option to specify a
|
126
147
|
# starting offset, in characters, from the beginning of the file.
|
127
148
|
#
|
128
|
-
#
|
129
|
-
#
|
149
|
+
# @param files [Array, String] One or more file paths to which to apply the patch.
|
150
|
+
# @param options [Hash] Options for applying the patch.
|
151
|
+
# @option options [Binding] :binding (nil) A Binding object to use when rendering ERB
|
152
|
+
# @option options [Integer] :offset (0) Offset in characters
|
153
|
+
# @raise [ArgumentError] In case of invalid mode (other than :append or :prepend)
|
130
154
|
def revert(files, options = {})
|
131
155
|
offset = options[:offset] || 0
|
132
156
|
files = [files] if files.kind_of? String
|
@@ -144,8 +168,16 @@ module PatternPatch
|
|
144
168
|
end
|
145
169
|
end
|
146
170
|
|
171
|
+
# Returns a diagnostic string representation
|
172
|
+
# @return [String] Diagnostic string representation of this Patch
|
147
173
|
def inspect
|
148
|
-
"#<PatternPatch::Patch regexp=#{regexp.inspect} text=#{text.inspect} mode=#{mode.inspect} global=#{global.inspect}>"
|
174
|
+
"#<PatternPatch::Patch regexp=#{regexp.inspect} text=#{text.inspect} text_file=#{text_file.inspect} mode=#{mode.inspect} global=#{global.inspect}>"
|
175
|
+
end
|
176
|
+
|
177
|
+
# Returns a string representation
|
178
|
+
# @return [String] String representation of this Patch
|
179
|
+
def to_s
|
180
|
+
inspect
|
149
181
|
end
|
150
182
|
end
|
151
183
|
end
|
@@ -4,12 +4,14 @@ module PatternPatch
|
|
4
4
|
# Add the specified text after the specified pattern.
|
5
5
|
# Returns a modified copy of the string.
|
6
6
|
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
7
|
+
# @param contents [String] A string to modify, e.g. the contents of a file
|
8
|
+
# @param regexp [Regexp] A regular expression specifying a pattern to be matched
|
9
|
+
# @param text [String] Text to be appended to the specified pattern
|
10
|
+
# @param global [true, false] Boolean flag. If true, patch all occurrences of the regex.
|
11
|
+
# @param mode [Symbol] :append, :prepend or :replace to specify how to apply the patch
|
12
|
+
# @param offset [Integer] Starting position for matching
|
13
|
+
# @return [String] A modified copy of the contents argument
|
14
|
+
# @raise [ArgumentError] In case of invalid mode (other than :append, :prepend, :replace)
|
13
15
|
def apply_patch(contents, regexp, text, global, mode, offset)
|
14
16
|
search_position = offset
|
15
17
|
while (matches = regexp.match(contents, search_position))
|
@@ -37,12 +39,14 @@ module PatternPatch
|
|
37
39
|
# cannot be reverted.
|
38
40
|
# Returns a modified copy of the string.
|
39
41
|
#
|
40
|
-
#
|
41
|
-
#
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
45
|
-
#
|
42
|
+
# @param contents [String] A string to modify, e.g. the contents of a file
|
43
|
+
# @param regexp [Regexp] A regular expression specifying a pattern to be matched
|
44
|
+
# @param text [String] Text to be appended to the specified pattern
|
45
|
+
# @param global [true, false] Boolean flag. If true, patch all occurrences of the regex.
|
46
|
+
# @param mode [Symbol] :append or :prepend. :replace patches cannot be reverted automatically.
|
47
|
+
# @param offset [Integer] Starting position for matching
|
48
|
+
# @return [String] A modified copy of the contents argument
|
49
|
+
# @raise [ArgumentError] In case of invalid mode (other than :append or :prepend)
|
46
50
|
def revert_patch(contents, regexp, text, global, mode, offset)
|
47
51
|
search_position = offset
|
48
52
|
regexp_string = regexp.to_s
|
data/lib/pattern_patch.rb
CHANGED
@@ -2,3 +2,35 @@ require "pattern_patch/core_ext"
|
|
2
2
|
require "pattern_patch/patch"
|
3
3
|
require "pattern_patch/utilities"
|
4
4
|
require "pattern_patch/version"
|
5
|
+
|
6
|
+
# Utility gem for modifying arbitrary text files using a method similar to
|
7
|
+
# rendering Rails partials.
|
8
|
+
#
|
9
|
+
# @author Jimmy Dee (https://github.com/jdee)
|
10
|
+
module PatternPatch
|
11
|
+
# Generic exception class for PatternPatch exceptions
|
12
|
+
class Error < RuntimeError; end
|
13
|
+
|
14
|
+
# Exception generated by patch method when patch_dir is not set properly
|
15
|
+
class ConfigurationError < Error; end
|
16
|
+
|
17
|
+
module Methods
|
18
|
+
# @!attribute patch_dir
|
19
|
+
# Set this to conveniently load patches from a common folder with
|
20
|
+
# the patch method.
|
21
|
+
# @return [String] Path to a directory for use with patch
|
22
|
+
attr_accessor :patch_dir
|
23
|
+
|
24
|
+
# Loads a patch from the patch_dir
|
25
|
+
# @param name [#to_s] Name of a patch to load from the patch_dir
|
26
|
+
# @return [Patch] A patch loaded from the patch_dir
|
27
|
+
# @raise [ConfigurationError] If patch_dir is nil or is not a valid directory path
|
28
|
+
def patch(name)
|
29
|
+
raise ConfigurationError, "patch_dir has not been set" if patch_dir.nil?
|
30
|
+
raise ConfigurationError, "patch_dir is not a directory" unless Dir.exist?(patch_dir)
|
31
|
+
Patch.from_yaml File.join(patch_dir, "#{name}.yml")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
extend Methods
|
36
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pattern_patch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jimmy Dee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-11-
|
11
|
+
date: 2017-11-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -136,6 +136,20 @@ dependencies:
|
|
136
136
|
- - ">="
|
137
137
|
- !ruby/object:Gem::Version
|
138
138
|
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: yard
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
139
153
|
description: This is a utility gem that identifies positions in any text using regular
|
140
154
|
expressions and then inserts patch text at the specified location or replaces matching
|
141
155
|
text. Many patches can be reverted.
|
@@ -170,7 +184,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
170
184
|
version: '0'
|
171
185
|
requirements: []
|
172
186
|
rubyforge_project:
|
173
|
-
rubygems_version: 2.7.
|
187
|
+
rubygems_version: 2.7.2
|
174
188
|
signing_key:
|
175
189
|
specification_version: 4
|
176
190
|
summary: Apply and revert pattern-based patches to text files of any kind
|