synvert 0.11.1 → 0.14.1
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/CHANGELOG.md +19 -0
- data/lib/synvert/cli.rb +121 -62
- data/lib/synvert/version.rb +1 -1
- data/spec/synvert/snippet_spec.rb +9 -9
- data/synvert.gemspec +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e99142ef7ed4127f66b23dba5d3ab0ec04d2b8edb315ac48945c15fb6056c294
|
4
|
+
data.tar.gz: dbcbfffa5f570522d6385660663a8438d55ae7216f49cd8964787b31e916aa88
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5346a5cc7fba0bc0aba045206c716d98fc8ac46b5beb109dffa62885e6caa5b09f0e31e9a84b3df324e8dae96f30164782f8211deb66b29a0890ce6b71efbedc
|
7
|
+
data.tar.gz: f2b2b6a0ea082757d333a6c68aabcb03d1ae8ec99a5b95cbe5013bb52af5a4adbcd95dd278103f7f125dea2dc04cdf8ad72ddf70077a85c25af8bdf36f9955b0
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,24 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 0.14.1 (2021-03-13)
|
4
|
+
|
5
|
+
* Fix reduce on empty array
|
6
|
+
|
7
|
+
## 0.14.0 (2021-03-13)
|
8
|
+
|
9
|
+
* Add CLI option `--format`
|
10
|
+
* Run one snippet once
|
11
|
+
|
12
|
+
## 0.13.0 (2021-03-02)
|
13
|
+
|
14
|
+
* Use `ENV['SYNVERT_SNIPPETS_HOME']` to change default snippets home
|
15
|
+
* Display snippet source code for showing a snippet
|
16
|
+
|
17
|
+
## 0.12.0 (2021-03-01)
|
18
|
+
|
19
|
+
* Display `synvert-core` and `parser` version
|
20
|
+
* Generate a new snippet
|
21
|
+
|
3
22
|
## 0.11.1 (2021-02-20)
|
4
23
|
|
5
24
|
* Use `Synvert::VERSION` instead of `Synvert::Core::VERSION`
|
data/lib/synvert/cli.rb
CHANGED
@@ -15,7 +15,7 @@ module Synvert
|
|
15
15
|
|
16
16
|
# Initialize a CLI.
|
17
17
|
def initialize
|
18
|
-
@options = { command: 'run', custom_snippet_paths: [],
|
18
|
+
@options = { command: 'run', custom_snippet_paths: [], format: 'plain' }
|
19
19
|
end
|
20
20
|
|
21
21
|
# Run the CLI.
|
@@ -28,9 +28,6 @@ module Synvert
|
|
28
28
|
when 'list'
|
29
29
|
load_rewriters
|
30
30
|
list_available_rewriters
|
31
|
-
when 'list-all'
|
32
|
-
load_rewriters
|
33
|
-
list_all_rewriters_in_json
|
34
31
|
when 'open'
|
35
32
|
open_rewriter
|
36
33
|
when 'query'
|
@@ -41,19 +38,12 @@ module Synvert
|
|
41
38
|
show_rewriter
|
42
39
|
when 'sync'
|
43
40
|
sync_snippets
|
41
|
+
when 'generate'
|
42
|
+
generate_snippet
|
44
43
|
else
|
45
44
|
# run
|
46
45
|
load_rewriters
|
47
|
-
|
48
|
-
puts "===== #{snippet_name} started ====="
|
49
|
-
group, name = snippet_name.split('/')
|
50
|
-
rewriter = Core::Rewriter.call group, name
|
51
|
-
rewriter.warnings.each do |warning|
|
52
|
-
puts '[Warn] ' + warning.message
|
53
|
-
end
|
54
|
-
puts rewriter.todo if rewriter.todo
|
55
|
-
puts "===== #{snippet_name} done ====="
|
56
|
-
end
|
46
|
+
run_snippet
|
57
47
|
end
|
58
48
|
true
|
59
49
|
rescue SystemExit
|
@@ -80,9 +70,6 @@ module Synvert
|
|
80
70
|
'load custom snippets, snippet paths can be local file path or remote http url' do |snippet_paths|
|
81
71
|
@options[:custom_snippet_paths] = snippet_paths.split(',').map(&:strip)
|
82
72
|
end
|
83
|
-
opts.on '--list-all', 'list all available snippets name and description in json format' do
|
84
|
-
@options[:command] = 'list-all'
|
85
|
-
end
|
86
73
|
opts.on '-l', '--list', 'list all available snippets' do
|
87
74
|
@options[:command] = 'list'
|
88
75
|
end
|
@@ -107,13 +94,18 @@ module Synvert
|
|
107
94
|
opts.on '--sync', 'sync snippets' do
|
108
95
|
@options[:command] = 'sync'
|
109
96
|
end
|
110
|
-
opts.on '-r',
|
111
|
-
|
112
|
-
|
113
|
-
|
97
|
+
opts.on '-r', '--run SNIPPET_NAME', 'run specified snippet, e.g. ruby/new_hash_syntax' do |snippet_name|
|
98
|
+
@options[:snippet_name] = snippet_name
|
99
|
+
end
|
100
|
+
opts.on '-g', '--generate NEW_SNIPPET_NAME', 'generate a new snippet' do |name|
|
101
|
+
@options[:command] = 'generate'
|
102
|
+
@options[:snippet_name] = name
|
103
|
+
end
|
104
|
+
opts.on '-f', '--format FORMAT', 'output format' do |format|
|
105
|
+
@options[:format] = format
|
114
106
|
end
|
115
107
|
opts.on '-v', '--version', 'show this version' do
|
116
|
-
puts VERSION
|
108
|
+
puts "#{VERSION} (with synvert-core #{Core::VERSION} and parser #{Parser::VERSION})"
|
117
109
|
exit
|
118
110
|
end
|
119
111
|
end
|
@@ -131,7 +123,7 @@ module Synvert
|
|
131
123
|
|
132
124
|
# Load all rewriters.
|
133
125
|
def load_rewriters
|
134
|
-
Dir.glob(File.join(
|
126
|
+
Dir.glob(File.join(default_snippets_home, 'lib/**/*.rb')).each { |file| require file }
|
135
127
|
|
136
128
|
@options[:custom_snippet_paths].each do |snippet_path|
|
137
129
|
if /^http/.match?(snippet_path)
|
@@ -142,7 +134,7 @@ module Synvert
|
|
142
134
|
end
|
143
135
|
end
|
144
136
|
rescue StandardError
|
145
|
-
FileUtils.rm_rf
|
137
|
+
FileUtils.rm_rf default_snippets_home
|
146
138
|
retry
|
147
139
|
end
|
148
140
|
|
@@ -150,7 +142,10 @@ module Synvert
|
|
150
142
|
def list_available_rewriters
|
151
143
|
if Core::Rewriter.availables.empty?
|
152
144
|
puts 'There is no snippet under ~/.synvert, please run `synvert --sync` to fetch snippets.'
|
153
|
-
|
145
|
+
return
|
146
|
+
end
|
147
|
+
|
148
|
+
if plain_output?
|
154
149
|
Core::Rewriter.availables.each do |group, rewriters|
|
155
150
|
puts group
|
156
151
|
rewriters.each do |name, _rewriter|
|
@@ -158,29 +153,22 @@ module Synvert
|
|
158
153
|
end
|
159
154
|
end
|
160
155
|
puts
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
rewriter.process_with_sandbox
|
174
|
-
output << {
|
175
|
-
group: group,
|
176
|
-
name: name,
|
177
|
-
description: rewriter.description,
|
178
|
-
sub_snippets: rewriter.sub_snippets.map(&:name)
|
179
|
-
}
|
156
|
+
elsif json_output?
|
157
|
+
output = []
|
158
|
+
Core::Rewriter.availables.each do |group, rewriters|
|
159
|
+
rewriters.each do |name, rewriter|
|
160
|
+
rewriter.process_with_sandbox
|
161
|
+
output << {
|
162
|
+
group: group,
|
163
|
+
name: name,
|
164
|
+
description: rewriter.description,
|
165
|
+
sub_snippets: rewriter.sub_snippets.map(&:name)
|
166
|
+
}
|
167
|
+
end
|
180
168
|
end
|
181
|
-
end
|
182
169
|
|
183
|
-
|
170
|
+
puts JSON.generate(output)
|
171
|
+
end
|
184
172
|
end
|
185
173
|
|
186
174
|
# Open one rewriter.
|
@@ -188,7 +176,7 @@ module Synvert
|
|
188
176
|
editor = [ENV['SYNVERT_EDITOR'], ENV['EDITOR']].find { |e| !e.nil? && !e.empty? }
|
189
177
|
return puts 'To open a synvert snippet, set $EDITOR or $SYNVERT_EDITOR' unless editor
|
190
178
|
|
191
|
-
path = File.expand_path(File.join(
|
179
|
+
path = File.expand_path(File.join(default_snippets_home, "lib/#{@options[:snippet_name]}.rb"))
|
192
180
|
if File.exist? path
|
193
181
|
system editor, path
|
194
182
|
else
|
@@ -216,18 +204,9 @@ module Synvert
|
|
216
204
|
|
217
205
|
# Show and print one rewriter.
|
218
206
|
def show_rewriter
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
rewriter.process_with_sandbox
|
223
|
-
puts rewriter.description
|
224
|
-
rewriter.sub_snippets.each do |sub_rewriter|
|
225
|
-
puts
|
226
|
-
puts '=' * 80
|
227
|
-
puts "snippet: #{sub_rewriter.name}"
|
228
|
-
puts '=' * 80
|
229
|
-
puts sub_rewriter.description
|
230
|
-
end
|
207
|
+
path = File.expand_path(File.join(default_snippets_home, "lib/#{@options[:snippet_name]}.rb"))
|
208
|
+
if File.exist?(path)
|
209
|
+
puts File.read(path)
|
231
210
|
else
|
232
211
|
puts "snippet #{@options[:snippet_name]} not found"
|
233
212
|
end
|
@@ -235,7 +214,7 @@ module Synvert
|
|
235
214
|
|
236
215
|
# sync snippets
|
237
216
|
def sync_snippets
|
238
|
-
Snippet.new(
|
217
|
+
Snippet.new(default_snippets_home).sync
|
239
218
|
puts 'synvert snippets are synced'
|
240
219
|
core_version = Snippet.fetch_core_version
|
241
220
|
if Gem::Version.new(core_version) > Gem::Version.new(Synvert::Core::VERSION)
|
@@ -243,8 +222,88 @@ module Synvert
|
|
243
222
|
end
|
244
223
|
end
|
245
224
|
|
246
|
-
|
247
|
-
|
225
|
+
# run snippets
|
226
|
+
def run_snippet
|
227
|
+
snippet_name = @options[:snippet_name]
|
228
|
+
if plain_output?
|
229
|
+
puts "===== #{snippet_name} started ====="
|
230
|
+
group, name = snippet_name.split('/')
|
231
|
+
rewriter = Core::Rewriter.call group, name
|
232
|
+
rewriter.warnings.each do |warning|
|
233
|
+
puts '[Warn] ' + warning.message
|
234
|
+
end
|
235
|
+
puts rewriter.todo if rewriter.todo
|
236
|
+
puts "===== #{snippet_name} done ====="
|
237
|
+
elsif json_output?
|
238
|
+
group, name = snippet_name.split('/')
|
239
|
+
rewriter = Core::Rewriter.call group, name
|
240
|
+
output = {
|
241
|
+
affected_files: rewriter.affected_files.union(rewriter.sub_snippets.map(&:affected_files).reduce([], :+)).to_a,
|
242
|
+
warnings: rewriter.warnings.union(rewriter.sub_snippets.map(&:warnings).reduce([], :+)),
|
243
|
+
todo: rewriter.todo
|
244
|
+
}
|
245
|
+
puts JSON.generate(output)
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
# generate a new snippet
|
250
|
+
def generate_snippet
|
251
|
+
group, name = @options[:snippet_name].split('/')
|
252
|
+
FileUtils.mkdir_p("lib/#{group}")
|
253
|
+
FileUtils.mkdir_p("spec/#{group}")
|
254
|
+
lib_content = <<~EOF
|
255
|
+
# frozen_string_literal: true
|
256
|
+
|
257
|
+
Synvert::Rewriter.new '#{group}', '#{name}' do
|
258
|
+
description <<~EOS
|
259
|
+
It converts Foo to Bar
|
260
|
+
|
261
|
+
```ruby
|
262
|
+
Foo
|
263
|
+
```
|
264
|
+
|
265
|
+
=>
|
266
|
+
|
267
|
+
```ruby
|
268
|
+
Bar
|
269
|
+
```
|
270
|
+
EOS
|
271
|
+
|
272
|
+
within_files '**/*.rb' do
|
273
|
+
with_node type: 'const', to_source: 'Foo' do
|
274
|
+
replace_with 'Bar'
|
275
|
+
end
|
276
|
+
end
|
277
|
+
end
|
278
|
+
EOF
|
279
|
+
spec_content = <<~EOF
|
280
|
+
# frozen_string_literal: true
|
281
|
+
|
282
|
+
require 'spec_helper'
|
283
|
+
|
284
|
+
RSpec.describe 'Convert Foo to Bar' do
|
285
|
+
let(:rewriter_name) { '#{group}/#{name}' }
|
286
|
+
let(:fake_file_path) { 'foobar.rb' }
|
287
|
+
let(:test_content) { 'Foo' }
|
288
|
+
let(:test_rewritten_content) { 'Bar' }
|
289
|
+
|
290
|
+
include_examples 'convertable'
|
291
|
+
end
|
292
|
+
EOF
|
293
|
+
File.write("lib/#{group}/#{name}.rb", lib_content)
|
294
|
+
File.write("spec/#{group}/#{name}_spec.rb", spec_content)
|
295
|
+
end
|
296
|
+
|
297
|
+
def default_snippets_home
|
298
|
+
ENV['SYNVERT_SNIPPETS_HOME'] || File.join(ENV['HOME'], '.synvert')
|
299
|
+
end
|
300
|
+
|
301
|
+
def plain_output?
|
302
|
+
@options[:format] == 'plain'
|
303
|
+
end
|
304
|
+
|
305
|
+
def json_output?
|
306
|
+
@options[:format] == 'json'
|
248
307
|
end
|
249
308
|
end
|
250
309
|
end
|
data/lib/synvert/version.rb
CHANGED
@@ -8,6 +8,15 @@ module Synvert
|
|
8
8
|
let(:snippet) { Snippet.new(snippets_path) }
|
9
9
|
after { FileUtils.rmdir(snippets_path) if File.exist?(snippets_path) }
|
10
10
|
|
11
|
+
describe '.fetch_core_version' do
|
12
|
+
it 'gets remote version' do
|
13
|
+
stub_request(:get, 'https://rubygems.org/api/v1/versions/synvert-core.json').to_return(
|
14
|
+
body: '[{"number":"0.4.2"}]'
|
15
|
+
)
|
16
|
+
expect(Snippet.fetch_core_version).to eq '0.4.2'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
11
20
|
describe 'sync' do
|
12
21
|
it 'git clones snippets' do
|
13
22
|
expect(Kernel).to receive(:system).with(
|
@@ -23,14 +32,5 @@ module Synvert
|
|
23
32
|
FileUtils.cd File.dirname(__FILE__)
|
24
33
|
end
|
25
34
|
end
|
26
|
-
|
27
|
-
describe 'fetch_core_version' do
|
28
|
-
it 'gets remote version' do
|
29
|
-
stub_request(:get, 'https://rubygems.org/api/v1/versions/synvert-core.json').to_return(
|
30
|
-
body: '[{"number":"0.4.2"}]'
|
31
|
-
)
|
32
|
-
expect(snippet.fetch_core_version).to eq '0.4.2'
|
33
|
-
end
|
34
|
-
end
|
35
35
|
end
|
36
36
|
end
|
data/synvert.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 --sync` first to sync snippets remotely.'
|
22
22
|
|
23
|
-
spec.add_runtime_dependency 'synvert-core', '>= 0.
|
23
|
+
spec.add_runtime_dependency 'synvert-core', '>= 0.22.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.
|
4
|
+
version: 0.14.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Huang
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-03-13 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: 0.
|
19
|
+
version: 0.22.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: 0.
|
26
|
+
version: 0.22.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|