synvert-core 1.27.1 → 1.28.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +1 -1
- data/README.md +1 -0
- data/lib/synvert/core/helper.rb +57 -0
- data/lib/synvert/core/rewriter.rb +17 -1
- data/lib/synvert/core/version.rb +1 -1
- data/lib/synvert/core.rb +2 -0
- data/spec/synvert/core/helper_spec.rb +30 -0
- data/spec/synvert/core/rewriter_spec.rb +41 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d42815151ad1672667f0bc09cc57083b0620722a8416f2d6e30ebb1f6faabe5
|
4
|
+
data.tar.gz: 51ebfcd1b6022a91e1739974d89c6172f7358ebb3ddae69132408a6f07b76564
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2bf88532f763fa4c3e4b3c7f12d914ad383c838c8b99ca2faf897e50390e409f941b7b59a8f0dde7350d3c1d29bacc972c8d36abb5aee814ff2337eb2dfc9409
|
7
|
+
data.tar.gz: d5ebb445329da6a3b93d34174150238753b299e0a67b7600a0286e19971cb81a789f3a1017e78729439b68e6ceea0f8cf402cfd1e0eab2d51b360f8f5d6755d8
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -69,6 +69,7 @@ DSLs are as follows
|
|
69
69
|
* [remove_file](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#remove_file-instance_method) - remove a file
|
70
70
|
* [helper_method](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#helper_method-instance_method) - define a helper method
|
71
71
|
* [add_snippet](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#add_snippet-instance_method) - call another rewriter
|
72
|
+
* [call_helper](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#call_helper-instance_method) - call a shared rewriter
|
72
73
|
* [redo_until_no_change](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#redo_until_no_change-instance_method) - run the snippet until no change
|
73
74
|
|
74
75
|
Scopes:
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Synvert::Core
|
4
|
+
# Helper is used to define shared snippet.
|
5
|
+
class Helper
|
6
|
+
attr_reader :name, :block
|
7
|
+
|
8
|
+
class << self
|
9
|
+
# Register a helper with its name.
|
10
|
+
#
|
11
|
+
# @param name [String] the unique rewriter name.
|
12
|
+
# @param helper [Synvert::Core::Helper] the helper to register.
|
13
|
+
def register(name, helper)
|
14
|
+
name = name.to_s
|
15
|
+
helpers[name] = helper
|
16
|
+
end
|
17
|
+
|
18
|
+
# Fetch a helper by name.
|
19
|
+
#
|
20
|
+
# @param name [String] rewrtier name.
|
21
|
+
# @return [Synvert::Core::Helper] the matching helper.
|
22
|
+
def fetch(name)
|
23
|
+
name = name.to_s
|
24
|
+
helpers[name]
|
25
|
+
end
|
26
|
+
|
27
|
+
# Get all available helpers
|
28
|
+
#
|
29
|
+
# @return [Hash<String, Synvert::Core::Helper>]
|
30
|
+
def availables
|
31
|
+
helpers
|
32
|
+
end
|
33
|
+
|
34
|
+
# Clear all registered helpers.
|
35
|
+
def clear
|
36
|
+
helpers.clear
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def helpers
|
42
|
+
@helpers ||= {}
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Initialize a Helper.
|
47
|
+
# When a helper is initialized, it is already registered.
|
48
|
+
#
|
49
|
+
# @param name [String] name of the helper.
|
50
|
+
# @yield defines the behaviors of the helper, block code won't be called when initialization.
|
51
|
+
def initialize(name, &block)
|
52
|
+
@name = name
|
53
|
+
@block = block
|
54
|
+
self.class.register(name, self)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -57,7 +57,7 @@ module Synvert::Core
|
|
57
57
|
|
58
58
|
# Get all available rewriters
|
59
59
|
#
|
60
|
-
# @return [Hash<String, Hash<String, Rewriter>>]
|
60
|
+
# @return [Hash<String, Hash<String, Synvert::Core::Rewriter>>]
|
61
61
|
def availables
|
62
62
|
rewriters
|
63
63
|
end
|
@@ -357,6 +357,22 @@ module Synvert::Core
|
|
357
357
|
@sub_snippets << rewriter
|
358
358
|
end
|
359
359
|
|
360
|
+
# It calls a shared rewriter.
|
361
|
+
# @example
|
362
|
+
# Synvert::Rewriter.new 'rails', 'upgrade_6_0_to_6_1' do
|
363
|
+
# call_helper 'rails/set_load_defaults', options: { rails_version: '6.1' }
|
364
|
+
# add_snippet '/Users/flyerhzm/.synvert-ruby/lib/rails/set_load_defaults.rb', options: { rails_version: '6.1' }
|
365
|
+
# add_snippet 'https://github.com/xinminlabs/synvert-snippets-ruby/blob/main/lib/rails/set_load_defaults.rb', options: { rails_version: '6.1' }
|
366
|
+
# end
|
367
|
+
# @param name [String] name of helper.
|
368
|
+
# @param options [Hash] options to pass to helper.
|
369
|
+
def call_helper(name, options: {})
|
370
|
+
helper = Synvert::Core::Helper.fetch(name) || Utils.eval_snippet(name)
|
371
|
+
return unless helper && helper.is_a?(Synvert::Core::Helper)
|
372
|
+
|
373
|
+
instance_exec(options, &helper.block)
|
374
|
+
end
|
375
|
+
|
360
376
|
# It defines helper method for {Synvert::Core::Rewriter::Instance}.
|
361
377
|
# @example
|
362
378
|
# Synvert::Rewriter.new 'rails', 'convert_active_record_dirty_5_0_to_5_1' do
|
data/lib/synvert/core/version.rb
CHANGED
data/lib/synvert/core.rb
CHANGED
@@ -11,6 +11,7 @@ module Synvert
|
|
11
11
|
module Core
|
12
12
|
autoload :Configuration, 'synvert/core/configuration'
|
13
13
|
autoload :Rewriter, 'synvert/core/rewriter'
|
14
|
+
autoload :Helper, 'synvert/core/helper'
|
14
15
|
autoload :Engine, 'synvert/core/engine'
|
15
16
|
autoload :Utils, 'synvert/core/utils'
|
16
17
|
autoload :Strategy, 'synvert/core/strategy'
|
@@ -20,6 +21,7 @@ end
|
|
20
21
|
|
21
22
|
module Synvert
|
22
23
|
Rewriter = Core::Rewriter
|
24
|
+
Helper = Core::Helper
|
23
25
|
Strategy = Core::Strategy
|
24
26
|
|
25
27
|
ALL_RUBY_FILES = %w[**/*.rb]
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Synvert::Core
|
6
|
+
RSpec.describe Helper do
|
7
|
+
describe 'class methods' do
|
8
|
+
before :each do
|
9
|
+
described_class.clear
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'registers and fetches' do
|
13
|
+
helper = described_class.new 'helper' do; end
|
14
|
+
expect(described_class.fetch('helper')).to eq helper
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'available' do
|
18
|
+
it 'lists empty helpers' do
|
19
|
+
expect(described_class.availables).to eq({})
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'registers and lists all available helpers' do
|
23
|
+
helper1 = Helper.new 'helper1' do; end
|
24
|
+
helper2 = Helper.new 'helper2' do; end
|
25
|
+
expect(Helper.availables).to eq({ 'helper1' => helper1, 'helper2' => helper2 })
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -309,6 +309,47 @@ module Synvert::Core
|
|
309
309
|
end
|
310
310
|
end
|
311
311
|
|
312
|
+
describe 'parses call_helper' do
|
313
|
+
it 'eval helper by name' do
|
314
|
+
block_receiver = nil
|
315
|
+
block_options = {}
|
316
|
+
helper =
|
317
|
+
Synvert::Helper.new 'helper' do |options|
|
318
|
+
block_receiver = self.class.name
|
319
|
+
block_options = options
|
320
|
+
end
|
321
|
+
rewriter =
|
322
|
+
Rewriter.new 'group', 'rewriter' do
|
323
|
+
call_helper('helper', options: { foo: 'bar' })
|
324
|
+
end
|
325
|
+
rewriter.process
|
326
|
+
expect(block_receiver).to eq 'Synvert::Core::Rewriter'
|
327
|
+
expect(block_options).to eq(foo: 'bar')
|
328
|
+
end
|
329
|
+
|
330
|
+
it 'adds snippet by http url' do
|
331
|
+
expect(Utils).to receive(:remote_snippet_exists?).with(URI.parse('http://synvert.net/foo/bar.rb')).and_return(true)
|
332
|
+
expect_any_instance_of(URI::HTTP).to receive(:open).and_return(StringIO.new("Synvert::Helper.new 'helper' do\nend"))
|
333
|
+
rewriter =
|
334
|
+
Rewriter.new 'group', 'rewriter' do
|
335
|
+
call_helper 'http://synvert.net/foo/bar.rb'
|
336
|
+
end
|
337
|
+
rewriter.process
|
338
|
+
expect(Synvert::Helper.fetch('helper')).not_to be_nil
|
339
|
+
end
|
340
|
+
|
341
|
+
it 'adds snippet by file path' do
|
342
|
+
expect(File).to receive(:exist?).and_return(true)
|
343
|
+
expect(File).to receive(:read).and_return("Synvert::Helper.new 'helper' do\nend")
|
344
|
+
rewriter =
|
345
|
+
Rewriter.new 'group', 'rewriter' do
|
346
|
+
call_helper '/home/richard/foo/bar.rb'
|
347
|
+
end
|
348
|
+
rewriter.process
|
349
|
+
expect(Synvert::Helper.fetch('helper')).not_to be_nil
|
350
|
+
end
|
351
|
+
end
|
352
|
+
|
312
353
|
it 'parses helper_method' do
|
313
354
|
rewriter =
|
314
355
|
Rewriter.new 'group', 'name' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: synvert-core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.28.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Huang
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-05-
|
11
|
+
date: 2023-05-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -148,6 +148,7 @@ files:
|
|
148
148
|
- lib/synvert/core/engine/haml.rb
|
149
149
|
- lib/synvert/core/engine/slim.rb
|
150
150
|
- lib/synvert/core/errors.rb
|
151
|
+
- lib/synvert/core/helper.rb
|
151
152
|
- lib/synvert/core/node_ext.rb
|
152
153
|
- lib/synvert/core/rewriter.rb
|
153
154
|
- lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb
|
@@ -171,6 +172,7 @@ files:
|
|
171
172
|
- spec/synvert/core/engine/erb_spec.rb
|
172
173
|
- spec/synvert/core/engine/haml_spec.rb
|
173
174
|
- spec/synvert/core/engine/slim_spec.rb
|
175
|
+
- spec/synvert/core/helper_spec.rb
|
174
176
|
- spec/synvert/core/node_ext_spec.rb
|
175
177
|
- spec/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action_spec.rb
|
176
178
|
- spec/synvert/core/rewriter/condition/if_exist_condition_spec.rb
|
@@ -217,6 +219,7 @@ test_files:
|
|
217
219
|
- spec/synvert/core/engine/erb_spec.rb
|
218
220
|
- spec/synvert/core/engine/haml_spec.rb
|
219
221
|
- spec/synvert/core/engine/slim_spec.rb
|
222
|
+
- spec/synvert/core/helper_spec.rb
|
220
223
|
- spec/synvert/core/node_ext_spec.rb
|
221
224
|
- spec/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action_spec.rb
|
222
225
|
- spec/synvert/core/rewriter/condition/if_exist_condition_spec.rb
|