synvert 0.19.3 → 1.0.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: 2b2cb7297283566f1929523ec1bd27886170ce2c8dc8b4ddf6e07a13d9139ba0
4
- data.tar.gz: 64d388db47dd58a7d40e4a97d7ad954c474b795fd52cfcc39c850e9271b1ad01
3
+ metadata.gz: 13a6cfb15282317c912c0ddd99f6e4dfe5040c2ec28c8f7a1b60d2266b7f1517
4
+ data.tar.gz: c34227427bed2b0d7f3e29112fbd225ff46517fa20fc58c5c9d7c4e4d53a6e2e
5
5
  SHA512:
6
- metadata.gz: 8c005363c25b19c5253732c9f262643fab4ea4c928aaf10bcc11aa5672cf55464864cc7518b1f74d81ae7fb1876f075a699beb9d0444b3de05aaee98d5a15725
7
- data.tar.gz: c6d1bb9f75b6f8a06dfd37e7bf94df5bb9750911bc00c4b9059616dddeb4629e30619b7f1fe38daf6de7f7a806b4f8cc3effd90f740addf9a2f9babb39a04e63
6
+ metadata.gz: 9642ee80da8558c06b294ebc4e4ce786405d6f93455a3700ac5f3299abdfecba8ee2d2d6dc9cafa9f98c6984ce965075850d39b30573fc686673a74da4979809
7
+ data.tar.gz: 83b1a5dd39156e8e70725c58c13e1b9ec0968316944583a1153ac9d57fb64f2a9d696d6e515f59be7c2535b1c10a406577fcefa56b1f792d27fcbac779615286
data/.gitignore CHANGED
@@ -3,7 +3,6 @@
3
3
  .bundle
4
4
  .config
5
5
  .yardoc
6
- Gemfile.lock
7
6
  InstalledFiles
8
7
  _yardoc
9
8
  coverage
data/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.0.0 (2022-09-17)
4
+
5
+ * Test a snippet
6
+ * Require single rewriter instead of loading all rewriters
7
+ * Execute command can be either run or test
8
+ * Add `skip_path` and `only_paths` options
9
+
10
+ ## 0.20.0 (2022-08-20)
11
+
12
+ * Rename `load_rewriters` to `read_rewriters`
13
+ * Run a snippet from remote url or local file path
14
+
3
15
  ## 0.19.3 (2022-07-18)
4
16
 
5
17
  * Require json
data/Gemfile.lock ADDED
@@ -0,0 +1,101 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ synvert (1.0.0)
5
+ synvert-core (>= 1.8.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (6.1.7)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 1.6, < 2)
13
+ minitest (>= 5.1)
14
+ tzinfo (~> 2.0)
15
+ zeitwerk (~> 2.3)
16
+ addressable (2.8.0)
17
+ public_suffix (>= 2.0.2, < 5.0)
18
+ ast (2.4.2)
19
+ concurrent-ruby (1.1.10)
20
+ coveralls (0.8.23)
21
+ json (>= 1.8, < 3)
22
+ simplecov (~> 0.16.1)
23
+ term-ansicolor (~> 1.3)
24
+ thor (>= 0.19.4, < 2.0)
25
+ tins (~> 1.6)
26
+ crack (0.4.5)
27
+ rexml
28
+ diff-lcs (1.4.4)
29
+ docile (1.4.0)
30
+ erubis (2.7.0)
31
+ hashdiff (1.0.1)
32
+ i18n (1.12.0)
33
+ concurrent-ruby (~> 1.0)
34
+ json (2.5.1)
35
+ minitest (5.16.3)
36
+ node_mutation (1.3.3)
37
+ activesupport (< 7.0.0)
38
+ erubis
39
+ node_query (1.6.0)
40
+ activesupport (< 7.0.0)
41
+ parser (3.1.2.1)
42
+ ast (~> 2.4.1)
43
+ parser_node_ext (0.4.0)
44
+ parser
45
+ public_suffix (4.0.6)
46
+ rake (13.0.6)
47
+ rexml (3.2.5)
48
+ rspec (3.10.0)
49
+ rspec-core (~> 3.10.0)
50
+ rspec-expectations (~> 3.10.0)
51
+ rspec-mocks (~> 3.10.0)
52
+ rspec-core (3.10.1)
53
+ rspec-support (~> 3.10.0)
54
+ rspec-expectations (3.10.1)
55
+ diff-lcs (>= 1.2.0, < 2.0)
56
+ rspec-support (~> 3.10.0)
57
+ rspec-mocks (3.10.2)
58
+ diff-lcs (>= 1.2.0, < 2.0)
59
+ rspec-support (~> 3.10.0)
60
+ rspec-support (3.10.2)
61
+ simplecov (0.16.1)
62
+ docile (~> 1.1)
63
+ json (>= 1.8, < 3)
64
+ simplecov-html (~> 0.10.0)
65
+ simplecov-html (0.10.2)
66
+ sync (0.5.0)
67
+ synvert-core (1.8.0)
68
+ activesupport (< 7.0.0)
69
+ erubis
70
+ node_mutation
71
+ node_query
72
+ parser
73
+ parser_node_ext
74
+ term-ansicolor (1.7.1)
75
+ tins (~> 1.0)
76
+ thor (1.1.0)
77
+ tins (1.29.1)
78
+ sync
79
+ tzinfo (2.0.5)
80
+ concurrent-ruby (~> 1.0)
81
+ webmock (3.14.0)
82
+ addressable (>= 2.8.0)
83
+ crack (>= 0.3.2)
84
+ hashdiff (>= 0.4.0, < 2.0.0)
85
+ zeitwerk (2.6.0)
86
+
87
+ PLATFORMS
88
+ x86_64-darwin-20
89
+ x86_64-darwin-21
90
+ x86_64-linux
91
+
92
+ DEPENDENCIES
93
+ bundler
94
+ coveralls
95
+ rake
96
+ rspec
97
+ synvert!
98
+ webmock
99
+
100
+ BUNDLED WITH
101
+ 2.3.7
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Synvert
2
2
 
3
- <img src="https://synvert.xinminlabs.com/img/logo_96.png" alt="logo" width="32" height="32" />
3
+ <img src="https://synvert.net/img/logo_96.png" alt="logo" width="32" height="32" />
4
4
 
5
5
  [![AwesomeCode Status for xinminlabs/synvert-ruby](https://awesomecode.io/projects/47cd9805-171c-4c61-b927-baa46cd4020a/status)](https://awesomecode.io/repos/xinminlabs/synvert-ruby)
6
6
  ![Main workflow](https://github.com/xinminlabs/synvert-ruby/actions/workflows/main.yml/badge.svg)
@@ -40,7 +40,6 @@ $ synvert-ruby -r factory_bot/use_short_syntax
40
40
  ```
41
41
  $ synvert-ruby -h
42
42
  Usage: synvert-ruby [project_path]
43
- -d, --load SNIPPET_PATHS load custom snippets, snippet paths can be local file path or remote http url
44
43
  -l, --list list all available snippets
45
44
  -q, --query QUERY query specified snippets
46
45
  -s, --show SNIPPET_NAME show specified snippet description, SNIPPET_NAME is combined by group and name, e.g. ruby/new_hash_syntax
@@ -48,9 +47,11 @@ Usage: synvert-ruby [project_path]
48
47
  -g, --generate NEW_SNIPPET_NAME generate a new snippet
49
48
  --sync sync snippets
50
49
  --execute execute snippet
51
- -r, --run SNIPPET_NAME run specified snippet, e.g. ruby/new_hash_syntax
50
+ -r, --run SNIPPET_NAME run a snippet with snippet name, e.g. ruby/new_hash_syntax, or remote url, or local file path
51
+ -t, --test SNIPPET_NAME test a snippet with snippet name, e.g. ruby/new_hash_syntax, or remote url, or local file path
52
52
  --show-run-process show processing files when running a snippet
53
- --skip FILE_PATTERNS skip specified files or directories, separated by comma, e.g. app/models/post.rb,vendor/plugins/**/*.rb
53
+ --only-paths DIRECTORIES only specified files or directories, separated by comma, e.g. app/models,app/controllers
54
+ --skip-paths FILE_PATTERNS skip specified files or directories, separated by comma, e.g. vendor/,lib/**/*.rb
54
55
  -f, --format FORMAT output format
55
56
  -v, --version show this version
56
57
  ```
@@ -99,10 +100,28 @@ Run a snippet, analyze and then rewrite code.
99
100
  $ synvert-ruby -r factory_bot/use_short_syntax ~/Sites/xinminlabs/synvert-core-ruby
100
101
  ```
101
102
 
102
- Load custom snippet
103
+ Run a snippet from remote url
103
104
 
104
105
  ```
105
- $ synvert-ruby --load ~/.custom-snippets/my-own-snippet.rb -r my-own-snippet ~/Sites/xinminlabs/synvert-core-ruby
106
+ $ synvert-ruby -r https://raw.githubusercontent.com/xinminlabs/synvert-snippets-ruby/master/lib/factory_bot/use_short_syntax.rb ~/sites/xinminlabs/synvert-core-ruby
107
+ ```
108
+
109
+ Run a snippet from local path
110
+
111
+ ```
112
+ $ synvert-ruby -r ~/.synvert-ruby/lib/factory_bot/use_short_syntax.rb ~/sites/xinminlabs/synvert-core-ruby
113
+ ```
114
+
115
+ Skip paths
116
+
117
+ ```
118
+ $ synvert-ruby -r factory_bot/use_short_syntax --skip-paths vendor/ ~/sites/xinminlabs/synvert-core-ruby
119
+ ```
120
+
121
+ Only paths
122
+
123
+ ```
124
+ $ synvert-ruby -r factory_bot/use_short_syntax --only-paths app/models/ ~/sites/xinminlabs/synvert-core-ruby
106
125
  ```
107
126
 
108
127
  Show processing files when running a snippet.
data/lib/synvert/cli.rb CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  require 'optparse'
4
4
  require 'json'
5
+ require 'uri'
6
+ require 'open-uri'
5
7
 
6
8
  module Synvert
7
9
  # Synvert command line interface.
@@ -16,7 +18,7 @@ module Synvert
16
18
 
17
19
  # Initialize a CLI.
18
20
  def initialize
19
- @options = { command: 'run', custom_snippet_paths: [], format: 'plain' }
21
+ @options = { command: 'run', format: 'plain' }
20
22
  end
21
23
 
22
24
  # Run the CLI.
@@ -27,12 +29,12 @@ module Synvert
27
29
 
28
30
  case @options[:command]
29
31
  when 'list'
30
- load_rewriters
32
+ read_rewriters
31
33
  list_available_rewriters
32
34
  when 'open'
33
35
  open_rewriter
34
36
  when 'query'
35
- load_rewriters
37
+ read_rewriters
36
38
  query_available_rewriters
37
39
  when 'show'
38
40
  show_rewriter
@@ -41,11 +43,15 @@ module Synvert
41
43
  when 'generate'
42
44
  generate_snippet
43
45
  when 'execute'
44
- execute_snippet
46
+ execute_snippet(@options[:execute_command])
47
+ when 'test'
48
+ group, name = get_snippet_name(@options[:snippet_name])
49
+ test_snippet(group, name)
50
+ when 'run'
51
+ group, name = get_snippet_name(@options[:snippet_name])
52
+ run_snippet(group, name)
45
53
  else
46
- # run
47
- load_rewriters
48
- run_snippet
54
+ # nothing to do
49
55
  end
50
56
  true
51
57
  rescue SystemExit
@@ -67,11 +73,6 @@ module Synvert
67
73
  optparse =
68
74
  OptionParser.new do |opts|
69
75
  opts.banner = 'Usage: synvert-ruby [project_path]'
70
- opts.on '-d',
71
- '--load SNIPPET_PATHS',
72
- 'load custom snippets, snippet paths can be local file path or remote http url' do |snippet_paths|
73
- @options[:custom_snippet_paths] = snippet_paths.split(',').map(&:strip)
74
- end
75
76
  opts.on '-l', '--list', 'list all available snippets' do
76
77
  @options[:command] = 'list'
77
78
  end
@@ -96,18 +97,28 @@ module Synvert
96
97
  opts.on '--sync', 'sync snippets' do
97
98
  @options[:command] = 'sync'
98
99
  end
99
- opts.on '--execute', 'execute snippet' do
100
+ opts.on '--execute', 'execute snippet' do |execute_command|
100
101
  @options[:command] = 'execute'
102
+ @options[:execute_command] = execute_command
103
+ end
104
+ opts.on '-r', '--run SNIPPET_NAME', 'run a snippet with snippet name, e.g. ruby/new_hash_syntax, or remote url, or local file path' do |snippet_name|
105
+ @options[:command] = 'run'
106
+ @options[:snippet_name] = snippet_name
101
107
  end
102
- opts.on '-r', '--run SNIPPET_NAME', 'run specified snippet, e.g. ruby/new_hash_syntax' do |snippet_name|
108
+ opts.on '-t', '--test SNIPPET_NAME', 'test a snippet with snippet name, e.g. ruby/new_hash_syntax, or remote url, or local file path' do |snippet_name|
109
+ @options[:command] = 'test'
103
110
  @options[:snippet_name] = snippet_name
104
111
  end
105
112
  opts.on '--show-run-process', 'show processing files when running a snippet' do
106
113
  Core::Configuration.show_run_process = true
107
114
  end
108
- opts.on '--skip FILE_PATTERNS',
109
- 'skip specified files or directories, separated by comma, e.g. app/models/post.rb,vendor/plugins/**/*.rb' do |file_patterns|
110
- @options[:skip_file_patterns] = file_patterns.split(',')
115
+ opts.on '--only-paths DIRECTORIES',
116
+ 'only specified files or directories, separated by comma, e.g. app/models,app/controllers' do |directories|
117
+ @options[:only_paths] = directories
118
+ end
119
+ opts.on '--skip-paths FILE_PATTERNS',
120
+ 'skip specified files or directories, separated by comma, e.g. vendor/,lib/**/*.rb' do |file_patterns|
121
+ @options[:skip_paths] = file_patterns
111
122
  end
112
123
  opts.on '-f', '--format FORMAT', 'output format' do |format|
113
124
  @options[:format] = format
@@ -118,32 +129,18 @@ module Synvert
118
129
  end
119
130
  end
120
131
  paths = optparse.parse(args)
121
- Core::Configuration.path = paths.first || Dir.pwd
122
- if @options[:skip_file_patterns] && !@options[:skip_file_patterns].empty?
123
- skip_files =
124
- @options[:skip_file_patterns].map do |file_pattern|
125
- full_file_pattern = File.join(Core::Configuration.path, file_pattern)
126
- Dir.glob(full_file_pattern)
127
- end.flatten
128
- Core::Configuration.skip_files = skip_files
132
+ Core::Configuration.root_path = paths.first || Dir.pwd
133
+ if @options[:only_paths] && !@options[:only_paths].empty?
134
+ Core::Configuration.only_paths = @options[:only_paths].split(",").map { |only_path| only_path.strip }
135
+ end
136
+ if @options[:skip_paths] && !@options[:skip_paths].empty?
137
+ Core::Configuration.skip_paths = @options[:skip_paths].split(",").map { |skip_path| skip_path.strip }
129
138
  end
130
139
  end
131
140
 
132
- # Load all rewriters.
133
- def load_rewriters
141
+ # read all rewriters.
142
+ def read_rewriters
134
143
  Dir.glob(File.join(default_snippets_home, 'lib/**/*.rb')).each { |file| require file }
135
-
136
- @options[:custom_snippet_paths].each do |snippet_path|
137
- if /^http/.match?(snippet_path)
138
- uri = URI.parse snippet_path
139
- eval(uri.read)
140
- else
141
- require snippet_path
142
- end
143
- end
144
- rescue StandardError
145
- FileUtils.rm_rf default_snippets_home
146
- retry
147
144
  end
148
145
 
149
146
  # List and print all available rewriters.
@@ -233,20 +230,40 @@ module Synvert
233
230
  end
234
231
  end
235
232
 
236
- # run snippets
237
- def run_snippet
238
- snippet_name = @options[:snippet_name]
233
+ # get snippet name
234
+ # it can get from explicit snippet name,
235
+ # or from local path or http url.
236
+ def get_snippet_name(snippet_name)
237
+ if /^http/.match?(snippet_name)
238
+ uri = URI.parse(snippet_name)
239
+ eval(uri.open.read)
240
+ get_last_snippet_name
241
+ elsif File.exists?(snippet_name)
242
+ require(snippet_name)
243
+ get_last_snippet_name
244
+ else
245
+ require(File.join(default_snippets_home, 'lib', "#{snippet_name}.rb"))
246
+ snippet_name.split('/')
247
+ end
248
+ end
249
+
250
+ # get snippet name by user inputs
251
+ def get_snippet_name_by_input(input)
252
+ rewriter = eval(input)
253
+ get_last_snippet_name
254
+ end
255
+
256
+ # run a snippet
257
+ def run_snippet(group, name)
239
258
  if plain_output?
240
- puts "===== #{snippet_name} started ====="
241
- group, name = snippet_name.split('/')
259
+ puts "===== #{group}/#{name} started ====="
242
260
  rewriter = Core::Rewriter.call group, name
243
261
  rewriter.warnings.each do |warning|
244
262
  puts '[Warn] ' + warning.message
245
263
  end
246
264
  puts rewriter.todo if rewriter.todo
247
- puts "===== #{snippet_name} done ====="
265
+ puts "===== #{group}/#{name} done ====="
248
266
  elsif json_output?
249
- group, name = snippet_name.split('/')
250
267
  rewriter = Core::Rewriter.call group, name
251
268
  output = {
252
269
  affected_files: rewriter.affected_files.union(rewriter.sub_snippets.sum(Set.new, &:affected_files)).to_a,
@@ -257,25 +274,20 @@ module Synvert
257
274
  end
258
275
  end
259
276
 
277
+ # test a snippet
278
+ def test_snippet(group, name)
279
+ rewriter = Core::Rewriter.fetch(group, name)
280
+ results = rewriter.test
281
+ puts JSON.generate(results)
282
+ end
283
+
260
284
  # execute snippet
261
- def execute_snippet
262
- input = STDIN.read
263
- if plain_output?
264
- puts '===== execute started ====='
265
- rewriter = eval(input)
266
- rewriter.warnings.each do |warning|
267
- puts '[Warn] ' + warning.message
268
- end
269
- puts rewriter.todo if rewriter.todo
270
- puts '===== execute done ====='
271
- elsif json_output?
272
- rewriter = eval(input)
273
- output = {
274
- affected_files: rewriter.affected_files.union(rewriter.sub_snippets.sum(Set.new, &:affected_files)).to_a,
275
- warnings: rewriter.warnings.union(rewriter.sub_snippets.sum([], &:warnings)),
276
- todo: rewriter.todo
277
- }
278
- puts JSON.generate(output)
285
+ def execute_snippet(execute_command)
286
+ group, name = get_snippet_name_by_input(STDIN.read)
287
+ if execute_command == 'test'
288
+ test_snippet(group, name)
289
+ else
290
+ run_snippet(group, name)
279
291
  end
280
292
  end
281
293
 
@@ -338,5 +350,12 @@ module Synvert
338
350
  def json_output?
339
351
  @options[:format] == 'json'
340
352
  end
353
+
354
+ # get the last registered snippet name
355
+ def get_last_snippet_name
356
+ group = Rewriter.availables.keys.last
357
+ name = Rewriter.availables[group].keys.last
358
+ return group, name
359
+ end
341
360
  end
342
361
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Synvert
4
- VERSION = '0.19.3'
4
+ VERSION = '1.0.0'
5
5
  end
data/synvert-ruby.gemspec CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.require_paths = ['lib']
21
21
  spec.post_install_message = 'Please run `synvert-ruby --sync` first to sync snippets remotely.'
22
22
 
23
- spec.add_runtime_dependency 'synvert-core', '>= 1.5.0'
23
+ spec.add_runtime_dependency 'synvert-core', '>= 1.8.0'
24
24
 
25
25
  spec.add_development_dependency 'bundler'
26
26
  spec.add_development_dependency 'rake'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: synvert
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.19.3
4
+ version: 1.0.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-07-18 00:00:00.000000000 Z
11
+ date: 2022-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: synvert-core
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 1.5.0
19
+ version: 1.8.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 1.5.0
26
+ version: 1.8.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -94,6 +94,7 @@ files:
94
94
  - ".rspec"
95
95
  - CHANGELOG.md
96
96
  - Gemfile
97
+ - Gemfile.lock
97
98
  - LICENSE.txt
98
99
  - README.md
99
100
  - Rakefile