synvert 0.19.3 → 0.20.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: 0fb489331d776e367ae6be1c1ab39330b59419b07b9db40e1c6ea215fd612338
4
+ data.tar.gz: fbbb3fc50e1c05bc67aef081b4490e49f1c839dc24eb76941e2487f2cb240cd3
5
5
  SHA512:
6
- metadata.gz: 8c005363c25b19c5253732c9f262643fab4ea4c928aaf10bcc11aa5672cf55464864cc7518b1f74d81ae7fb1876f075a699beb9d0444b3de05aaee98d5a15725
7
- data.tar.gz: c6d1bb9f75b6f8a06dfd37e7bf94df5bb9750911bc00c4b9059616dddeb4629e30619b7f1fe38daf6de7f7a806b4f8cc3effd90f740addf9a2f9babb39a04e63
6
+ metadata.gz: dad0c74122ab91f6a05907af7bca9fb3badbb3a137a6914227508814de3682f6578975bd9a2aa51a577881ccb6c22e837b43b2b36c01769eb703f63b2eb1e9a2
7
+ data.tar.gz: ff5309c3d2ca47e01be1a02aeb5ab9e3f1f6d0ef1c8ea8dc5fe3e8bcf03afeda5035aebf0c4fc26acf62e44097999294fab431936a665e3726ba9a6314522641
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.20.0 (2022-08-20)
4
+
5
+ * Rename `load_rewriters` to `read_rewriters`
6
+ * Run a snippet from remote url or local file path
7
+
3
8
  ## 0.19.3 (2022-07-18)
4
9
 
5
10
  * Require json
data/README.md CHANGED
@@ -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,7 +47,7 @@ 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 specified snippet, e.g. ruby/new_hash_syntax, or remote url, or local file path
52
51
  --show-run-process show processing files when running a snippet
53
52
  --skip FILE_PATTERNS skip specified files or directories, separated by comma, e.g. app/models/post.rb,vendor/plugins/**/*.rb
54
53
  -f, --format FORMAT output format
@@ -99,10 +98,16 @@ Run a snippet, analyze and then rewrite code.
99
98
  $ synvert-ruby -r factory_bot/use_short_syntax ~/Sites/xinminlabs/synvert-core-ruby
100
99
  ```
101
100
 
102
- Load custom snippet
101
+ run a snippet from remote url
103
102
 
104
103
  ```
105
- $ synvert-ruby --load ~/.custom-snippets/my-own-snippet.rb -r my-own-snippet ~/Sites/xinminlabs/synvert-core-ruby
104
+ $ synvert-ruby -r https://raw.githubusercontent.com/xinminlabs/synvert-snippets-ruby/master/lib/factory_bot/use_short_syntax.rb ~/sites/xinminlabs/synvert-core-ruby
105
+ ```
106
+
107
+ run a snippet from local path
108
+
109
+ ```
110
+ $ synvert-ruby -r ~/.synvert-ruby/lib/factory_bot/use_short_syntax.rb ~/sites/xinminlabs/synvert-core-ruby
106
111
  ```
107
112
 
108
113
  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
@@ -43,9 +45,19 @@ module Synvert
43
45
  when 'execute'
44
46
  execute_snippet
45
47
  else
46
- # run
47
- load_rewriters
48
- run_snippet
48
+ if /^http/.match?(@options[:snippet_name])
49
+ uri = URI.parse(@options[:snippet_name])
50
+ eval(uri.open.read)
51
+ snippet_name = get_last_snippet_name
52
+ run_snippet(snippet_name)
53
+ elsif File.exists?(@options[:snippet_name])
54
+ require(@options[:snippet_name])
55
+ snippet_name = get_last_snippet_name
56
+ run_snippet(snippet_name)
57
+ else
58
+ read_rewriters
59
+ run_snippet(@options[:snippet_name])
60
+ end
49
61
  end
50
62
  true
51
63
  rescue SystemExit
@@ -67,11 +79,6 @@ module Synvert
67
79
  optparse =
68
80
  OptionParser.new do |opts|
69
81
  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
82
  opts.on '-l', '--list', 'list all available snippets' do
76
83
  @options[:command] = 'list'
77
84
  end
@@ -99,7 +106,7 @@ module Synvert
99
106
  opts.on '--execute', 'execute snippet' do
100
107
  @options[:command] = 'execute'
101
108
  end
102
- opts.on '-r', '--run SNIPPET_NAME', 'run specified snippet, e.g. ruby/new_hash_syntax' do |snippet_name|
109
+ opts.on '-r', '--run SNIPPET_NAME', 'run specified snippet, e.g. ruby/new_hash_syntax, or remote url, or local file path' do |snippet_name|
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
@@ -129,21 +136,9 @@ module Synvert
129
136
  end
130
137
  end
131
138
 
132
- # Load all rewriters.
133
- def load_rewriters
139
+ # read all rewriters.
140
+ def read_rewriters
134
141
  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
142
  end
148
143
 
149
144
  # List and print all available rewriters.
@@ -234,8 +229,7 @@ module Synvert
234
229
  end
235
230
 
236
231
  # run snippets
237
- def run_snippet
238
- snippet_name = @options[:snippet_name]
232
+ def run_snippet(snippet_name)
239
233
  if plain_output?
240
234
  puts "===== #{snippet_name} started ====="
241
235
  group, name = snippet_name.split('/')
@@ -338,5 +332,12 @@ module Synvert
338
332
  def json_output?
339
333
  @options[:format] == 'json'
340
334
  end
335
+
336
+ # get the last registered snippet name
337
+ def get_last_snippet_name
338
+ group = Rewriter.availables.keys.last
339
+ name = Rewriter.availables[group].keys.last
340
+ return [group, name].join("/")
341
+ end
341
342
  end
342
343
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Synvert
4
- VERSION = '0.19.3'
4
+ VERSION = '0.20.0'
5
5
  end
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: 0.20.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-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: synvert-core