synvert-core 1.9.1 → 1.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0bd7dcc7605d1a18cc68ebfba86d5947960bd1320da0b5f7ae41abc8638fed62
4
- data.tar.gz: 3023d5fb8a740da0ce0191bdd08f7435cc931e690714d2195391cf03586efa70
3
+ metadata.gz: 16de1af456b15373297aebe409711e1725bb329e3da7d4fa8d84023348ceb236
4
+ data.tar.gz: e034ccf44cfce96335f10a38d5aa36793faa08c84e8d75509fc658824cf64676
5
5
  SHA512:
6
- metadata.gz: a3c7f43d890a491a7bb3356ec1695a79278a8c19cf61df4f296a1ea6562cbbcfa27764b36cbb22e06f9f27cd53157e2276392bba42856d58212fddb68e92ffac
7
- data.tar.gz: 9959f0f8290157b96c83011ef770d59b40b845b236e0f8facf24cb6795cd7a837936a8969e380910df2892604dd25f569c66b9ff0c7aa5649e441b5ddf72b32a
6
+ metadata.gz: 3f745e87c308b417c5938c2731af00254abe5beba70472bca1ebfdf534127b7df8f6ab33261b8eb5d31d33deaa471927a780a14720f0ee406eea44f5a7e06495
7
+ data.tar.gz: ac2c35d21a4e6558b14c861966c6326446926bc27cc9e2d278757df31cf68a33142e62d47f9d40e5ec72c1d3fd8efde6574046e00506c9a12d0a800580a00023
@@ -1,17 +1,10 @@
1
- # This workflow uses actions that are not certified by GitHub.
2
- # They are provided by a third-party and are governed by
3
- # separate terms of service, privacy policy, and support
4
- # documentation.
5
- # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
- # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
-
8
- name: CI
1
+ name: Test & deploy documentation
9
2
 
10
3
  on:
11
4
  push:
12
- branches: [ master ]
5
+ branches: [ main ]
13
6
  pull_request:
14
- branches: [ master ]
7
+ branches: [ main ]
15
8
 
16
9
  jobs:
17
10
  test:
@@ -29,3 +22,22 @@ jobs:
29
22
  bundler-cache: true
30
23
  - name: Run tests
31
24
  run: bundle exec rake
25
+
26
+ deploy:
27
+ runs-on: ubuntu-latest
28
+ needs: test
29
+ name: Update gh-pages to docs
30
+ steps:
31
+ - uses: actions/checkout@v2
32
+ - uses: actions/setup-ruby@v1
33
+ with:
34
+ ruby-version: '3.1'
35
+ - name: Install required gem dependencies
36
+ run: gem install yard redcarpet github-markup --no-document
37
+ - name: Build YARD Ruby Documentation
38
+ run: yardoc --output-dir docs
39
+ - name: Deploy
40
+ uses: peaceiris/actions-gh-pages@v3
41
+ with:
42
+ deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }}
43
+ publish_dir: ./docs
data/.yardopts ADDED
@@ -0,0 +1,2 @@
1
+ --markup-provider=redcarpet
2
+ --markup=markdown
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.10.0 (2022-10-09)
4
+
5
+ * Eval snippet by http url, file path or snippet name
6
+ * Remove `execute_command` option
7
+ * Remove `Rewriter.call`
8
+ * Remove `Rewriter.execute`
9
+
10
+ ## 1.9.2 (2022-10-03)
11
+
12
+ * Fix `test` in sub_snippets
13
+
3
14
  ## 1.9.1 (2022-09-23)
4
15
 
5
16
  * Read / write absolute path
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- synvert-core (1.9.1)
4
+ synvert-core (1.10.0)
5
5
  activesupport (< 7.0.0)
6
6
  erubis
7
7
  node_mutation
@@ -49,10 +49,10 @@ GEM
49
49
  method_source (1.0.0)
50
50
  minitest (5.16.3)
51
51
  nenv (0.3.0)
52
- node_mutation (1.4.1)
52
+ node_mutation (1.4.4)
53
53
  activesupport (< 7.0.0)
54
54
  erubis
55
- node_query (1.6.0)
55
+ node_query (1.7.0)
56
56
  activesupport (< 7.0.0)
57
57
  notiffany (0.1.3)
58
58
  nenv (~> 0.1)
@@ -85,7 +85,7 @@ GEM
85
85
  thor (1.2.1)
86
86
  tzinfo (2.0.5)
87
87
  concurrent-ruby (~> 1.0)
88
- zeitwerk (2.6.0)
88
+ zeitwerk (2.6.1)
89
89
 
90
90
  PLATFORMS
91
91
  ruby
@@ -8,6 +8,8 @@ module Synvert::Core
8
8
  # One Rewriter checks if the depndency version matches, and it can contain one or many {Synvert::Core::Rewriter::Instance},
9
9
  # which define the behavior what files and what codes to detect and rewrite to what code.
10
10
  class Rewriter
11
+ DEFAULT_OPTIONS = { run_instance: true, write_to_file: true }.freeze
12
+
11
13
  autoload :ReplaceErbStmtWithExprAction, 'synvert/core/rewriter/action/replace_erb_stmt_with_expr_action'
12
14
 
13
15
  autoload :Warning, 'synvert/core/rewriter/warning'
@@ -32,15 +34,6 @@ module Synvert::Core
32
34
  autoload :AnyValue, 'synvert/core/rewriter/any_value'
33
35
 
34
36
  class << self
35
- # Execute the temporary rewriter without group and name.
36
- #
37
- # @yield defines the behaviors of the rewriter.
38
- def execute(&block)
39
- rewriter = Rewriter.new('', '', &block)
40
- rewriter.process
41
- rewriter
42
- end
43
-
44
37
  # Register a rewriter with its group and name.
45
38
  #
46
39
  # @param group [String] the rewriter group.
@@ -68,24 +61,6 @@ module Synvert::Core
68
61
  rewriter
69
62
  end
70
63
 
71
- # Get a registered rewriter by group and name, then process that rewriter.
72
- #
73
- # @param group [String] the rewriter group.
74
- # @param name [String] the rewriter name.
75
- # @param options [Hash]
76
- # @option options [Boolean] :run_instance (true) process the instance.
77
- # @return [Synvert::Core::Rewriter] the registered rewriter.
78
- # @raise [Synvert::Core::RewriterNotFound] if the registered rewriter is not found.
79
- def call(group, name, options = { run_instance: true })
80
- rewriter = fetch(group, name)
81
- if options[:run_instance]
82
- rewriter.process
83
- else
84
- rewriter.process_with_sandbox
85
- end
86
- rewriter
87
- end
88
-
89
64
  # Get all available rewriters
90
65
  #
91
66
  # @return [Hash<String, Hash<String, Rewriter>>]
@@ -121,7 +96,12 @@ module Synvert::Core
121
96
  # @return [Rewriter::RubyVersion] the ruby version
122
97
  # @!attribute [r] gem_spec
123
98
  # @return [Rewriter::GemSpec] the gem spec
124
- attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec
99
+ # @!attribute [r] test_results
100
+ # @return [Array<Object>] the test results
101
+ # @!attribute [rw] options
102
+ # @return [Hash] the rewriter options
103
+ attr_reader :group, :name, :sub_snippets, :helpers, :warnings, :affected_files, :ruby_version, :gem_spec, :test_results
104
+ attr_accessor :options
125
105
 
126
106
  # Initialize a Rewriter.
127
107
  # When a rewriter is initialized, it is already registered.
@@ -138,7 +118,7 @@ module Synvert::Core
138
118
  @warnings = []
139
119
  @affected_files = Set.new
140
120
  @redo_until_no_change = false
141
- @options = { run_instance: true, write_to_file: true }
121
+ @options = DEFAULT_OPTIONS.dup
142
122
  @test_results = []
143
123
  self.class.register(@group, @name, self)
144
124
  end
@@ -253,7 +233,7 @@ module Synvert::Core
253
233
  instance.process
254
234
  else
255
235
  results = instance.test
256
- @test_results += results.select { |result| result.affected? }
236
+ merge_test_results(results)
257
237
  end
258
238
  end
259
239
 
@@ -302,22 +282,34 @@ module Synvert::Core
302
282
  # Synvert::Rewriter.new 'minitest', 'better_syntax' do
303
283
  # add_snippet 'minitest', 'assert_empty'
304
284
  # add_snippet 'minitest', 'assert_equal_arguments_order'
305
- # add_snippet 'minitest', 'assert_includes'
306
- # add_snippet 'minitest', 'assert_instance_of'
307
- # add_snippet 'minitest', 'assert_kind_of'
308
- # add_snippet 'minitest', 'assert_match'
309
- # add_snippet 'minitest', 'assert_nil'
310
- # add_snippet 'minitest', 'assert_operator'
311
- # add_snippet 'minitest', 'assert_path_exists'
312
- # add_snippet 'minitest', 'assert_predicate'
313
- # add_snippet 'minitest', 'assert_respond_to'
314
- # add_snippet 'minitest', 'assert_silent'
315
- # add_snippet 'minitest', 'assert_truthy'
285
+ # add_snippet 'minitest/assert_instance_of'
286
+ # add_snippet 'minitest/assert_kind_of'
287
+ # add_snippet '/Users/flyerhzm/.synvert-ruby/lib/minitest/assert_match.rb'
288
+ # add_snippet '/Users/flyerhzm/.synvert-ruby/lib/minitest/assert_nil.rb'
289
+ # add_snippet 'https://github.com/xinminlabs/synvert-snippets-ruby/blob/main/lib/minitest/assert_silent.rb'
290
+ # add_snippet 'https://github.com/xinminlabs/synvert-snippets-ruby/blob/main/lib/minitest/assert_truthy.rb'
316
291
  # end
317
- # @param group [String] group of another rewriter.
292
+ # @param group [String] group of another rewriter, if there's no name parameter, the group can be http url, file path or snippet name.
318
293
  # @param name [String] name of another rewriter.
319
- def add_snippet(group, name)
320
- @sub_snippets << self.class.call(group.to_s, name.to_s, @options)
294
+ def add_snippet(group, name = nil)
295
+ rewriter =
296
+ if name
297
+ Rewriter.fetch(group, name) || Utils.eval_snippet([group, name].join('/'))
298
+ else
299
+ Utils.eval_snippet(group)
300
+ end
301
+ return unless rewriter && rewriter.is_a?(Rewriter)
302
+
303
+ rewriter.options = @options
304
+ if !rewriter.options[:write_to_file]
305
+ results = rewriter.test
306
+ merge_test_results(results)
307
+ elsif rewriter.options[:run_instance]
308
+ rewriter.process
309
+ else
310
+ rewriter.process_with_sandbox
311
+ end
312
+ @sub_snippets << rewriter
321
313
  end
322
314
 
323
315
  # Parse +helper_method+ dsl, it defines helper method for {Synvert::Core::Rewriter::Instance}.
@@ -367,5 +359,11 @@ module Synvert::Core
367
359
  def redo_until_no_change
368
360
  @redo_until_no_change = true
369
361
  end
362
+
363
+ private
364
+
365
+ def merge_test_results(results)
366
+ @test_results += results.select { |result| result.affected? }
367
+ end
370
368
  end
371
369
  end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uri'
4
+
5
+ module Synvert::Core
6
+ class Utils
7
+ class << self
8
+ def eval_snippet(snippet_name)
9
+ if is_valid_url?(snippet_name)
10
+ uri = URI.parse(format_url(snippet_name))
11
+ eval(uri.open.read)
12
+ elsif is_valid_file?(snippet_name)
13
+ eval(File.read(snippet_name))
14
+ else
15
+ eval(File.read(File.join(default_snippets_home, 'lib', "#{snippet_name}.rb")))
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def is_valid_url?(url)
22
+ /^http/.match?(url)
23
+ end
24
+
25
+ def is_valid_file?(path)
26
+ File.exist?(path)
27
+ end
28
+
29
+ def default_snippets_home
30
+ ENV['SYNVERT_SNIPPETS_HOME'] || File.join(ENV['HOME'], '.synvert-ruby')
31
+ end
32
+
33
+ def format_url(url)
34
+ convert_to_github_raw_url(url)
35
+ end
36
+
37
+ def convert_to_github_raw_url(url)
38
+ return url unless url.include?('//github.com/')
39
+
40
+ url.sub('//github.com/', '//raw.githubusercontent.com/').sub('/blob/', '/')
41
+ end
42
+ end
43
+ end
44
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '1.9.1'
5
+ VERSION = '1.10.0'
6
6
  end
7
7
  end
data/lib/synvert/core.rb CHANGED
@@ -19,6 +19,7 @@ module Synvert
19
19
  autoload :Engine, 'synvert/core/engine'
20
20
  autoload :RewriterNotFound, 'synvert/core/exceptions'
21
21
  autoload :MethodNotSupported, 'synvert/core/exceptions'
22
+ autoload :Utils, 'synvert/core/utils'
22
23
  end
23
24
  end
24
25
 
@@ -4,12 +4,6 @@ require 'spec_helper'
4
4
 
5
5
  module Synvert::Core
6
6
  describe Rewriter do
7
- it '.execute' do
8
- run = false
9
- Rewriter.execute { run = true }
10
- expect(run).to be_truthy
11
- end
12
-
13
7
  it 'parses description' do
14
8
  rewriter =
15
9
  Rewriter.new 'group', 'name' do
@@ -209,7 +203,7 @@ module Synvert::Core
209
203
  rewriter1 = Rewriter.new 'group', 'rewriter1'
210
204
  rewriter2 =
211
205
  Rewriter.new 'group', 'rewriter2' do
212
- add_snippet :group, :rewriter1
206
+ add_snippet 'group', 'rewriter1'
213
207
  end
214
208
  expect(rewriter1).to receive(:process)
215
209
  rewriter2.process
@@ -219,13 +213,32 @@ module Synvert::Core
219
213
  rewriter1 = Rewriter.new 'group', 'rewriter1'
220
214
  rewriter2 =
221
215
  Rewriter.new 'group', 'rewriter2' do
222
- add_snippet :group, :rewriter1
216
+ add_snippet 'group', 'rewriter1'
223
217
  end
224
218
  expect(rewriter1).to receive(:process)
225
219
  rewriter2.process
226
220
  expect(rewriter2.sub_snippets).to eq [rewriter1]
227
221
  end
228
222
 
223
+ it 'adds snippet by http url' do
224
+ expect_any_instance_of(URI).to receive(:open).and_return(StringIO.new("Rewriter.new 'group', 'sub_rewriter' do\nend"))
225
+ rewriter = Rewriter.new 'group', 'rewriter' do
226
+ add_snippet 'http://synvert.net/foo/bar.rb'
227
+ end
228
+ rewriter.process
229
+ expect(Rewriter.fetch('group', 'sub_rewriter')).not_to be_nil
230
+ end
231
+
232
+ it 'adds snippet by file path' do
233
+ expect(File).to receive(:exist?).and_return(true)
234
+ expect(File).to receive(:read).and_return("Rewriter.new 'group', 'sub_rewriter' do\nend")
235
+ rewriter = Rewriter.new 'group', 'rewriter' do
236
+ add_snippet '/home/richard/foo/bar.rb'
237
+ end
238
+ rewriter.process
239
+ expect(Rewriter.fetch('group', 'sub_rewriter')).not_to be_nil
240
+ end
241
+
229
242
  it 'raises RewriterNotFound' do
230
243
  rewriter =
231
244
  Rewriter.new 'group', 'name' do
@@ -276,23 +289,6 @@ module Synvert::Core
276
289
  expect(Rewriter.fetch('group', 'rewriter')).to eq rewriter
277
290
  end
278
291
 
279
- it 'registers and calls rewriter' do
280
- rewriter = Rewriter.new 'group', 'rewriter'
281
- expect(rewriter).to receive(:process)
282
- Rewriter.call 'group', 'rewriter'
283
- end
284
-
285
- it 'registers and calls rewriter in sandbox mode' do
286
- rewriter = Rewriter.new 'group', 'rewriter'
287
- expect(rewriter).to receive(:process_with_sandbox)
288
- Rewriter.call 'group', 'rewriter', run_instance: false
289
- end
290
-
291
- it 'raises RewriterNotFound if rewriter not found' do
292
- expect { Rewriter.call 'group', 'rewriter' }
293
- .to raise_error(RewriterNotFound)
294
- end
295
-
296
292
  context 'available' do
297
293
  it 'lists empty rewriters' do
298
294
  expect(Rewriter.availables).to eq({})
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ module Synvert::Core
4
+ RSpec.describe Utils do
5
+ describe '.eval_snippet' do
6
+ it 'evals snippet by http url' do
7
+ expect_any_instance_of(URI).to receive(:open).and_return(StringIO.new("Rewriter.new 'group', 'name' do\nend"))
8
+ rewriter = described_class.eval_snippet('http://example.com/rewriter.rb')
9
+ expect(rewriter.group).to eq 'group'
10
+ expect(rewriter.name).to eq 'name'
11
+ end
12
+
13
+ it 'adds snippet by file path' do
14
+ expect(File).to receive(:exist?).and_return(true)
15
+ expect(File).to receive(:read).and_return("Rewriter.new 'group', 'name' do\nend")
16
+ rewriter = described_class.eval_snippet('/home/richard/foo/bar.rb')
17
+ expect(rewriter.group).to eq 'group'
18
+ expect(rewriter.name).to eq 'name'
19
+ end
20
+
21
+ it 'adds snippet by snippet name' do
22
+ expect(File).to receive(:exist?).and_return(false)
23
+ expect(File).to receive(:read).and_return("Rewriter.new 'group', 'name' do\nend")
24
+ rewriter = described_class.eval_snippet('/home/richard/foo/bar.rb')
25
+ expect(rewriter.group).to eq 'group'
26
+ expect(rewriter.name).to eq 'name'
27
+ end
28
+ end
29
+ end
30
+ end
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.9.1
4
+ version: 1.10.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: 2022-09-23 00:00:00.000000000 Z
11
+ date: 2022-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -104,6 +104,7 @@ files:
104
104
  - ".github/workflows/main.yml"
105
105
  - ".gitignore"
106
106
  - ".rspec"
107
+ - ".yardopts"
107
108
  - CHANGELOG.md
108
109
  - Gemfile
109
110
  - Gemfile.lock
@@ -133,6 +134,7 @@ files:
133
134
  - lib/synvert/core/rewriter/scope/query_scope.rb
134
135
  - lib/synvert/core/rewriter/scope/within_scope.rb
135
136
  - lib/synvert/core/rewriter/warning.rb
137
+ - lib/synvert/core/utils.rb
136
138
  - lib/synvert/core/version.rb
137
139
  - spec/spec_helper.rb
138
140
  - spec/support/parser_helper.rb
@@ -153,6 +155,7 @@ files:
153
155
  - spec/synvert/core/rewriter/scope_spec.rb
154
156
  - spec/synvert/core/rewriter/warning_spec.rb
155
157
  - spec/synvert/core/rewriter_spec.rb
158
+ - spec/synvert/core/utils_spec.rb
156
159
  - synvert-core-ruby.gemspec
157
160
  homepage: https://github.com/xinminlabs/synvert-core-ruby
158
161
  licenses:
@@ -173,7 +176,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
173
176
  - !ruby/object:Gem::Version
174
177
  version: '0'
175
178
  requirements: []
176
- rubygems_version: 3.3.7
179
+ rubygems_version: 3.3.22
177
180
  signing_key:
178
181
  specification_version: 4
179
182
  summary: convert ruby code to better syntax.
@@ -197,3 +200,4 @@ test_files:
197
200
  - spec/synvert/core/rewriter/scope_spec.rb
198
201
  - spec/synvert/core/rewriter/warning_spec.rb
199
202
  - spec/synvert/core/rewriter_spec.rb
203
+ - spec/synvert/core/utils_spec.rb