pattern_patch 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/pattern_patch/core_ext/hash.rb +16 -0
- data/lib/pattern_patch/core_ext.rb +2 -1
- data/lib/pattern_patch/patch.rb +28 -7
- data/lib/pattern_patch/renderer.rb +62 -0
- data/lib/pattern_patch/version.rb +1 -1
- metadata +48 -59
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9e20771a9100042d880f37afca70c6bf547b167777a8c58a342a637e62fd36ed
|
|
4
|
+
data.tar.gz: f357bd192396f04a8a6757e6ab2736244d12e32679420bb80691836b77235961
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5183c3381e5fcc3e5b4da29832ed62bd66471c23eee239839441f89592c5fe07535e066e9575cee6ec9fa565dce9efe8e608664ed33700bc70daec051ae9a49a
|
|
7
|
+
data.tar.gz: 77cf6f4416a96a82c205b5fc8747323b2ad1ad12f39babc0dcc0b5eef01e887362dd7b509627c38d47c11f03606172613b2e7c51b7d2799497e46765d8c90881
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
class Hash
|
|
2
|
+
# #transform_keys not available in 2.3
|
|
3
|
+
def symbolize_keys
|
|
4
|
+
each_with_object({}) do |ary, hash|
|
|
5
|
+
key, value = *ary
|
|
6
|
+
hash[key.to_sym] = value
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def symbolize_keys!
|
|
11
|
+
hash = symbolize_keys
|
|
12
|
+
clear
|
|
13
|
+
hash.each { |k, v| self[k] = v }
|
|
14
|
+
nil
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
require
|
|
1
|
+
require 'pattern_patch/core_ext/hash'
|
|
2
|
+
require 'pattern_patch/core_ext/string'
|
data/lib/pattern_patch/patch.rb
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
require
|
|
2
|
-
require
|
|
3
|
-
|
|
1
|
+
require 'erb'
|
|
2
|
+
require 'yaml'
|
|
3
|
+
require_relative 'core_ext/hash'
|
|
4
|
+
require_relative 'renderer'
|
|
4
5
|
|
|
5
6
|
module PatternPatch
|
|
6
7
|
# The PatternPatch::Patch class defines a patch as an operation that
|
|
@@ -116,7 +117,7 @@ module PatternPatch
|
|
|
116
117
|
|
|
117
118
|
# Applies the patch to one or more files. ERB is processed in the text
|
|
118
119
|
# field, whether it comes from a text_file or not. Pass a Binding to
|
|
119
|
-
# ERB using the :binding option. Pass the :offset option to specify a
|
|
120
|
+
# ERB using the :binding option or a Hash of locals. Pass the :offset option to specify a
|
|
120
121
|
# starting offset, in characters, from the beginning of the file.
|
|
121
122
|
#
|
|
122
123
|
# @param files [Array, String] One or more file paths to which to apply the patch.
|
|
@@ -125,6 +126,7 @@ module PatternPatch
|
|
|
125
126
|
# @option options [Integer] :offset (0) Offset in characters
|
|
126
127
|
# @option options [Object, nil] :safe_level (PatternPatch.safe_level) A valid value for $SAFE for use with ERb
|
|
127
128
|
# @option options [String] :trim_mode (PatternPatch.trim_mode) A valid ERb trim mode
|
|
129
|
+
# @option options [Hash] :locals A Hash of local variables for rendering the template
|
|
128
130
|
# @raise [ArgumentError] In case of invalid mode (other than :append, :prepend, :replace)
|
|
129
131
|
def apply(files, options = {})
|
|
130
132
|
offset = options[:offset] || 0
|
|
@@ -133,7 +135,16 @@ module PatternPatch
|
|
|
133
135
|
safe_level = options[:safe_level] || PatternPatch.safe_level
|
|
134
136
|
trim_mode = options[:trim_mode] || PatternPatch.trim_mode
|
|
135
137
|
|
|
136
|
-
|
|
138
|
+
locals = options[:locals]
|
|
139
|
+
|
|
140
|
+
raise ArgumentError, ':binding is incompatible with :locals' if options[:binding] && locals
|
|
141
|
+
|
|
142
|
+
renderer = Renderer.new text, safe_level, trim_mode
|
|
143
|
+
if locals.nil?
|
|
144
|
+
patch_text = renderer.render options[:binding]
|
|
145
|
+
else
|
|
146
|
+
patch_text = renderer.render locals
|
|
147
|
+
end
|
|
137
148
|
|
|
138
149
|
files.each do |path|
|
|
139
150
|
modified = Utilities.apply_patch File.read(path),
|
|
@@ -148,7 +159,7 @@ module PatternPatch
|
|
|
148
159
|
|
|
149
160
|
# Reverse the effect of a patch on one or more files. ERB is processed in the text
|
|
150
161
|
# field, whether it comes from a text_file or not. Pass a Binding to
|
|
151
|
-
# ERB using the :binding option. Pass the :offset option to specify a
|
|
162
|
+
# ERB using the :binding option or a Hash of locals. Pass the :offset option to specify a
|
|
152
163
|
# starting offset, in characters, from the beginning of the file.
|
|
153
164
|
#
|
|
154
165
|
# @param files [Array, String] One or more file paths to which to apply the patch.
|
|
@@ -157,6 +168,7 @@ module PatternPatch
|
|
|
157
168
|
# @option options [Integer] :offset (0) Offset in characters
|
|
158
169
|
# @option options [Object, nil] :safe_level (PatternPatch.safe_level) A valid value for $SAFE for use with ERb
|
|
159
170
|
# @option options [String] :trim_mode (PatternPatch.trim_mode) A valid ERb trim mode
|
|
171
|
+
# @option options [Hash] :locals A Hash of local variables for rendering the template
|
|
160
172
|
# @raise [ArgumentError] In case of invalid mode (other than :append or :prepend)
|
|
161
173
|
def revert(files, options = {})
|
|
162
174
|
offset = options[:offset] || 0
|
|
@@ -165,7 +177,16 @@ module PatternPatch
|
|
|
165
177
|
safe_level = options[:safe_level] || PatternPatch.safe_level
|
|
166
178
|
trim_mode = options[:trim_mode] || PatternPatch.trim_mode
|
|
167
179
|
|
|
168
|
-
|
|
180
|
+
locals = options[:locals]
|
|
181
|
+
|
|
182
|
+
raise ArgumentError, ':binding is incompatible with :locals' if options[:binding] && locals
|
|
183
|
+
|
|
184
|
+
renderer = Renderer.new text, safe_level, trim_mode
|
|
185
|
+
if locals.nil?
|
|
186
|
+
patch_text = renderer.render options[:binding]
|
|
187
|
+
else
|
|
188
|
+
patch_text = renderer.render locals
|
|
189
|
+
end
|
|
169
190
|
|
|
170
191
|
files.each do |path|
|
|
171
192
|
modified = Utilities.revert_patch File.read(path),
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
require_relative 'core_ext/hash'
|
|
2
|
+
require 'rubygems'
|
|
3
|
+
|
|
4
|
+
module PatternPatch
|
|
5
|
+
# Provides a fairly clean binding for resolving locals in Ruby < 2.5.
|
|
6
|
+
# All locals become method calls in the binding passed to ERB. A separate
|
|
7
|
+
# class (instead of a Module, e.g.) means minimal clutter in the set of
|
|
8
|
+
# methods available to the binding.
|
|
9
|
+
class Renderer
|
|
10
|
+
class << self
|
|
11
|
+
def is_ruby3?
|
|
12
|
+
version = Gem::Version.new RUBY_VERSION
|
|
13
|
+
requirement = Gem::Requirement.new '>= 3'
|
|
14
|
+
requirement =~ version
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def initialize(text, safe_level = nil, trim_mode = nil)
|
|
19
|
+
if self.class.is_ruby3?
|
|
20
|
+
@template = ERB.new text, trim_mode: trim_mode
|
|
21
|
+
else
|
|
22
|
+
@template = ERB.new text, safe_level, trim_mode
|
|
23
|
+
end
|
|
24
|
+
@locals = {}
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Render an ERB template with a binding or locals.
|
|
28
|
+
# renderer = Renderer.new template_text
|
|
29
|
+
# result = renderer.render binding
|
|
30
|
+
# result = renderer.render a: 'foo', b: 1
|
|
31
|
+
# @param locals_or_binding [Hash, Binding] a Hash of locals or a Binding
|
|
32
|
+
# @return the result of rendering the template
|
|
33
|
+
# @raise ArgumentError for invalid locals
|
|
34
|
+
def render(locals_or_binding = {})
|
|
35
|
+
# Pass a Binding this way.
|
|
36
|
+
return @template.result(locals_or_binding) unless locals_or_binding.kind_of?(Hash)
|
|
37
|
+
|
|
38
|
+
@locals = locals_or_binding.symbolize_keys
|
|
39
|
+
# Any local that corresponds to a method name in this class is invalid
|
|
40
|
+
# because it cannot trigger method_missing. The same goes for
|
|
41
|
+
# locals_or_binding, the only local variable.
|
|
42
|
+
# Avoid new methods and local variables, which will be visible in the binding.
|
|
43
|
+
# Could validate only for Ruby < 2.5, but better to be consistent.
|
|
44
|
+
if @locals.any? { |l| respond_to?(l) || l == :locals_or_binding }
|
|
45
|
+
raise ArgumentError, "Invalid locals: #{@locals.select { |l| respond_to?(l) || l == :locals_or_binding }.map(&:to_str).join ', '}"
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
if @template.respond_to? :result_with_hash
|
|
49
|
+
# ERB#result_with_hash requires Ruby 2.5.
|
|
50
|
+
@template.result_with_hash locals_or_binding
|
|
51
|
+
else
|
|
52
|
+
@template.result binding
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def method_missing(method_sym, *args, &block)
|
|
57
|
+
return super unless @locals.key?(method_sym)
|
|
58
|
+
|
|
59
|
+
@locals[method_sym]
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
metadata
CHANGED
|
@@ -1,161 +1,147 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: pattern_patch
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jimmy Dee
|
|
8
|
-
autorequire:
|
|
8
|
+
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2022-12-06 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
|
-
name:
|
|
14
|
+
name: bundler
|
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
|
16
16
|
requirements:
|
|
17
17
|
- - ">="
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version:
|
|
19
|
+
version: '1.16'
|
|
20
20
|
- - "<"
|
|
21
21
|
- !ruby/object:Gem::Version
|
|
22
|
-
version: '
|
|
23
|
-
type: :
|
|
22
|
+
version: '3'
|
|
23
|
+
type: :development
|
|
24
24
|
prerelease: false
|
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
|
26
26
|
requirements:
|
|
27
27
|
- - ">="
|
|
28
28
|
- !ruby/object:Gem::Version
|
|
29
|
-
version:
|
|
29
|
+
version: '1.16'
|
|
30
30
|
- - "<"
|
|
31
31
|
- !ruby/object:Gem::Version
|
|
32
|
-
version: '
|
|
33
|
-
- !ruby/object:Gem::Dependency
|
|
34
|
-
name: bundler
|
|
35
|
-
requirement: !ruby/object:Gem::Requirement
|
|
36
|
-
requirements:
|
|
37
|
-
- - ">="
|
|
38
|
-
- !ruby/object:Gem::Version
|
|
39
|
-
version: '0'
|
|
40
|
-
type: :development
|
|
41
|
-
prerelease: false
|
|
42
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
43
|
-
requirements:
|
|
44
|
-
- - ">="
|
|
45
|
-
- !ruby/object:Gem::Version
|
|
46
|
-
version: '0'
|
|
32
|
+
version: '3'
|
|
47
33
|
- !ruby/object:Gem::Dependency
|
|
48
34
|
name: pry
|
|
49
35
|
requirement: !ruby/object:Gem::Requirement
|
|
50
36
|
requirements:
|
|
51
|
-
- - "
|
|
37
|
+
- - "~>"
|
|
52
38
|
- !ruby/object:Gem::Version
|
|
53
|
-
version: '0'
|
|
39
|
+
version: '0.12'
|
|
54
40
|
type: :development
|
|
55
41
|
prerelease: false
|
|
56
42
|
version_requirements: !ruby/object:Gem::Requirement
|
|
57
43
|
requirements:
|
|
58
|
-
- - "
|
|
44
|
+
- - "~>"
|
|
59
45
|
- !ruby/object:Gem::Version
|
|
60
|
-
version: '0'
|
|
46
|
+
version: '0.12'
|
|
61
47
|
- !ruby/object:Gem::Dependency
|
|
62
48
|
name: rake
|
|
63
49
|
requirement: !ruby/object:Gem::Requirement
|
|
64
50
|
requirements:
|
|
65
|
-
- - "
|
|
51
|
+
- - "~>"
|
|
66
52
|
- !ruby/object:Gem::Version
|
|
67
|
-
version: '
|
|
53
|
+
version: '12.3'
|
|
68
54
|
type: :development
|
|
69
55
|
prerelease: false
|
|
70
56
|
version_requirements: !ruby/object:Gem::Requirement
|
|
71
57
|
requirements:
|
|
72
|
-
- - "
|
|
58
|
+
- - "~>"
|
|
73
59
|
- !ruby/object:Gem::Version
|
|
74
|
-
version: '
|
|
60
|
+
version: '12.3'
|
|
75
61
|
- !ruby/object:Gem::Dependency
|
|
76
62
|
name: rspec
|
|
77
63
|
requirement: !ruby/object:Gem::Requirement
|
|
78
64
|
requirements:
|
|
79
|
-
- - "
|
|
65
|
+
- - "~>"
|
|
80
66
|
- !ruby/object:Gem::Version
|
|
81
|
-
version: '
|
|
67
|
+
version: '3.8'
|
|
82
68
|
type: :development
|
|
83
69
|
prerelease: false
|
|
84
70
|
version_requirements: !ruby/object:Gem::Requirement
|
|
85
71
|
requirements:
|
|
86
|
-
- - "
|
|
72
|
+
- - "~>"
|
|
87
73
|
- !ruby/object:Gem::Version
|
|
88
|
-
version: '
|
|
74
|
+
version: '3.8'
|
|
89
75
|
- !ruby/object:Gem::Dependency
|
|
90
|
-
name:
|
|
76
|
+
name: rspec_junit_formatter
|
|
91
77
|
requirement: !ruby/object:Gem::Requirement
|
|
92
78
|
requirements:
|
|
93
|
-
- - "
|
|
79
|
+
- - "~>"
|
|
94
80
|
- !ruby/object:Gem::Version
|
|
95
|
-
version: '0'
|
|
81
|
+
version: '0.4'
|
|
96
82
|
type: :development
|
|
97
83
|
prerelease: false
|
|
98
84
|
version_requirements: !ruby/object:Gem::Requirement
|
|
99
85
|
requirements:
|
|
100
|
-
- - "
|
|
86
|
+
- - "~>"
|
|
101
87
|
- !ruby/object:Gem::Version
|
|
102
|
-
version: '0'
|
|
88
|
+
version: '0.4'
|
|
103
89
|
- !ruby/object:Gem::Dependency
|
|
104
|
-
name:
|
|
90
|
+
name: rspec-simplecov
|
|
105
91
|
requirement: !ruby/object:Gem::Requirement
|
|
106
92
|
requirements:
|
|
107
|
-
- - "
|
|
93
|
+
- - "~>"
|
|
108
94
|
- !ruby/object:Gem::Version
|
|
109
|
-
version: '0'
|
|
95
|
+
version: '0.2'
|
|
110
96
|
type: :development
|
|
111
97
|
prerelease: false
|
|
112
98
|
version_requirements: !ruby/object:Gem::Requirement
|
|
113
99
|
requirements:
|
|
114
|
-
- - "
|
|
100
|
+
- - "~>"
|
|
115
101
|
- !ruby/object:Gem::Version
|
|
116
|
-
version: '0'
|
|
102
|
+
version: '0.2'
|
|
117
103
|
- !ruby/object:Gem::Dependency
|
|
118
104
|
name: rubocop
|
|
119
105
|
requirement: !ruby/object:Gem::Requirement
|
|
120
106
|
requirements:
|
|
121
107
|
- - '='
|
|
122
108
|
- !ruby/object:Gem::Version
|
|
123
|
-
version:
|
|
109
|
+
version: 1.39.0
|
|
124
110
|
type: :development
|
|
125
111
|
prerelease: false
|
|
126
112
|
version_requirements: !ruby/object:Gem::Requirement
|
|
127
113
|
requirements:
|
|
128
114
|
- - '='
|
|
129
115
|
- !ruby/object:Gem::Version
|
|
130
|
-
version:
|
|
116
|
+
version: 1.39.0
|
|
131
117
|
- !ruby/object:Gem::Dependency
|
|
132
118
|
name: simplecov
|
|
133
119
|
requirement: !ruby/object:Gem::Requirement
|
|
134
120
|
requirements:
|
|
135
|
-
- - "
|
|
121
|
+
- - "~>"
|
|
136
122
|
- !ruby/object:Gem::Version
|
|
137
|
-
version: '0'
|
|
123
|
+
version: '0.16'
|
|
138
124
|
type: :development
|
|
139
125
|
prerelease: false
|
|
140
126
|
version_requirements: !ruby/object:Gem::Requirement
|
|
141
127
|
requirements:
|
|
142
|
-
- - "
|
|
128
|
+
- - "~>"
|
|
143
129
|
- !ruby/object:Gem::Version
|
|
144
|
-
version: '0'
|
|
130
|
+
version: '0.16'
|
|
145
131
|
- !ruby/object:Gem::Dependency
|
|
146
132
|
name: yard
|
|
147
133
|
requirement: !ruby/object:Gem::Requirement
|
|
148
134
|
requirements:
|
|
149
|
-
- - "
|
|
135
|
+
- - "~>"
|
|
150
136
|
- !ruby/object:Gem::Version
|
|
151
|
-
version: '0'
|
|
137
|
+
version: '0.9'
|
|
152
138
|
type: :development
|
|
153
139
|
prerelease: false
|
|
154
140
|
version_requirements: !ruby/object:Gem::Requirement
|
|
155
141
|
requirements:
|
|
156
|
-
- - "
|
|
142
|
+
- - "~>"
|
|
157
143
|
- !ruby/object:Gem::Version
|
|
158
|
-
version: '0'
|
|
144
|
+
version: '0.9'
|
|
159
145
|
description: This is a utility gem that identifies positions in any text using regular
|
|
160
146
|
expressions and then inserts patch text at the specified location or replaces matching
|
|
161
147
|
text. Many patches can be reverted.
|
|
@@ -166,15 +152,18 @@ extra_rdoc_files: []
|
|
|
166
152
|
files:
|
|
167
153
|
- lib/pattern_patch.rb
|
|
168
154
|
- lib/pattern_patch/core_ext.rb
|
|
155
|
+
- lib/pattern_patch/core_ext/hash.rb
|
|
169
156
|
- lib/pattern_patch/core_ext/string.rb
|
|
170
157
|
- lib/pattern_patch/patch.rb
|
|
158
|
+
- lib/pattern_patch/renderer.rb
|
|
171
159
|
- lib/pattern_patch/utilities.rb
|
|
172
160
|
- lib/pattern_patch/version.rb
|
|
173
161
|
homepage: http://github.com/jdee/pattern_patch
|
|
174
162
|
licenses:
|
|
175
163
|
- MIT
|
|
176
|
-
metadata:
|
|
177
|
-
|
|
164
|
+
metadata:
|
|
165
|
+
rubygems_mfa_required: 'true'
|
|
166
|
+
post_install_message:
|
|
178
167
|
rdoc_options: []
|
|
179
168
|
require_paths:
|
|
180
169
|
- lib
|
|
@@ -189,8 +178,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
189
178
|
- !ruby/object:Gem::Version
|
|
190
179
|
version: '0'
|
|
191
180
|
requirements: []
|
|
192
|
-
rubygems_version: 3.
|
|
193
|
-
signing_key:
|
|
181
|
+
rubygems_version: 3.2.33
|
|
182
|
+
signing_key:
|
|
194
183
|
specification_version: 4
|
|
195
184
|
summary: Apply and revert pattern-based patches to text files of any kind
|
|
196
185
|
test_files: []
|