synvert 0.10.0 → 0.12.1

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: 430688d602830d6cbccecf277a580969288b875e992dc1447eb108a7ebd0726c
4
- data.tar.gz: 6ea78cce8187cea7470bbc043978b318a4f0bd608bf13124cb9ec5f75917fe37
3
+ metadata.gz: 6850587d42a82286479a86f22018a83b4e6f3b6d23d65a6518120acde4c4a52c
4
+ data.tar.gz: e9a223cae6a39601d7bd9f793c109d28db4483664da77a20a88c267a4432568e
5
5
  SHA512:
6
- metadata.gz: dad3de4c88092bba08138b0b64d70e92fa33ca2d8d4d7acea22d7b5e468e48fb2b08b8531832c985b03f5a4b6c2e0fda5f5aa415aae204c414fc5d14d18646c1
7
- data.tar.gz: e500c57544648cb230f43e52658d58c36bde8a13f052c5e66441fc3471c43e867c8693a0b4fde23a9a9c6c69eae28c0d174ef5ddedec499ff6594a676b0beece
6
+ metadata.gz: eca37ac2eda8ea96d555b32061336b2576f0efbd5471dbfb0ca58594e21a55e17559675d8f2880d5b0f9740564f719311eb06bb34f90491bed713c0b7d6e3267
7
+ data.tar.gz: 796ce440e914222563a41741b54a89584da3ee2418fe830a37bdfa076beabb794c69312d38e3a319f42c2d483dd72ccb402666dd97e0b0aa539ef47726b0c2b9
data/.travis.yml CHANGED
@@ -1,6 +1,8 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.1
4
- - 2.2
5
3
  - 2.3
6
4
  - 2.4
5
+ - 2.5
6
+ - 2.6
7
+ - 2.7
8
+ - 3.0
data/CHANGELOG.md CHANGED
@@ -1,5 +1,24 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.12.1 (2021-03-01)
4
+
5
+ * Display snippet source code for showing a snippet
6
+
7
+ ## 0.12.0 (2021-03-01)
8
+
9
+ * Display `synvert-core` and `parser` version
10
+ * Generate a new snippet
11
+
12
+ ## 0.11.1 (2021-02-20)
13
+
14
+ * Use `Synvert::VERSION` instead of `Synvert::Core::VERSION`
15
+
16
+ ## 0.11.0 (2021-02-15)
17
+
18
+ * Add `--list-all` option
19
+ * Add post install message
20
+ * Fix `Synvert::Snippet.fetch_core_version`
21
+
3
22
  ## 0.10.0 (2021-02-07)
4
23
 
5
24
  * Use new `Core::Confiruation`
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in synvert.gemspec
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler/gem_tasks'
2
4
  require 'rspec/core/rake_task'
3
5
 
data/bin/synvert CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
- # coding: utf-8
2
+ # frozen_string_literal: true
3
3
 
4
- $LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
4
+ $LOAD_PATH.unshift(File.expand_path('../lib', __dir__))
5
5
  require 'synvert'
6
6
 
7
7
  result = Synvert::CLI.run
data/lib/synvert.rb CHANGED
@@ -1,4 +1,5 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require 'synvert/version'
3
4
  require 'bundler'
4
5
  require 'synvert/core'
data/lib/synvert/cli.rb CHANGED
@@ -1,4 +1,5 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require 'optparse'
3
4
 
4
5
  module Synvert
@@ -14,7 +15,7 @@ module Synvert
14
15
 
15
16
  # Initialize a CLI.
16
17
  def initialize
17
- @options = {command: 'run', custom_snippet_paths: [], snippet_names: []}
18
+ @options = { command: 'run', custom_snippet_paths: [], snippet_names: [] }
18
19
  end
19
20
 
20
21
  # Run the CLI.
@@ -27,6 +28,9 @@ module Synvert
27
28
  when 'list'
28
29
  load_rewriters
29
30
  list_available_rewriters
31
+ when 'list-all'
32
+ load_rewriters
33
+ list_all_rewriters_in_json
30
34
  when 'open'
31
35
  open_rewriter
32
36
  when 'query'
@@ -37,7 +41,10 @@ module Synvert
37
41
  show_rewriter
38
42
  when 'sync'
39
43
  sync_snippets
40
- else # run
44
+ when 'generate'
45
+ generate_snippet
46
+ else
47
+ # run
41
48
  load_rewriters
42
49
  @options[:snippet_names].each do |snippet_name|
43
50
  puts "===== #{snippet_name} started ====="
@@ -67,47 +74,63 @@ module Synvert
67
74
 
68
75
  # Run OptionParser to parse arguments.
69
76
  def run_option_parser(args)
70
- optparse = OptionParser.new do |opts|
71
- opts.banner = 'Usage: synvert [project_path]'
72
- opts.on '-d', '--load SNIPPET_PATHS', '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
- opts.on '-l', '--list', 'list all available snippets' do
76
- @options[:command] = 'list'
77
- end
78
- opts.on '-o', '--open SNIPPET_NAME', 'Open a snippet' do |snippet_name|
79
- @options[:command] = 'open'
80
- @options[:snippet_name] = snippet_name
81
- end
82
- opts.on '-q', '--query QUERY', 'query specified snippets' do |query|
83
- @options[:command] = 'query'
84
- @options[:query] = query
85
- end
86
- opts.on '--skip FILE_PATTERNS', 'skip specified files or directories, separated by comma, e.g. app/models/post.rb,vendor/plugins/**/*.rb' do |file_patterns|
87
- @options[:skip_file_patterns] = file_patterns.split(',')
88
- end
89
- opts.on '-s', '--show SNIPPET_NAME', 'show specified snippet description, SNIPPET_NAME is combined by group and name, e.g. ruby/new_hash_syntax' do |snippet_name|
90
- @options[:command] = 'show'
91
- @options[:snippet_name] = snippet_name
92
- end
93
- opts.on '--sync', 'sync snippets' do
94
- @options[:command] = 'sync'
95
- end
96
- opts.on '-r', '--run SNIPPET_NAMES', 'run specified snippets, each SNIPPET_NAME is combined by group and name, e.g. ruby/new_hash_syntax,ruby/new_lambda_syntax' do |snippet_names|
97
- @options[:snippet_names] = snippet_names.split(',').map(&:strip)
98
- end
99
- opts.on '-v', '--version', 'show this version' do
100
- puts Core::VERSION
101
- exit
77
+ optparse =
78
+ OptionParser.new do |opts|
79
+ opts.banner = 'Usage: synvert [project_path]'
80
+ opts.on '-d',
81
+ '--load SNIPPET_PATHS',
82
+ 'load custom snippets, snippet paths can be local file path or remote http url' do |snippet_paths|
83
+ @options[:custom_snippet_paths] = snippet_paths.split(',').map(&:strip)
84
+ end
85
+ opts.on '--list-all', 'list all available snippets name and description in json format' do
86
+ @options[:command] = 'list-all'
87
+ end
88
+ opts.on '-l', '--list', 'list all available snippets' do
89
+ @options[:command] = 'list'
90
+ end
91
+ opts.on '-o', '--open SNIPPET_NAME', 'Open a snippet' do |snippet_name|
92
+ @options[:command] = 'open'
93
+ @options[:snippet_name] = snippet_name
94
+ end
95
+ opts.on '-q', '--query QUERY', 'query specified snippets' do |query|
96
+ @options[:command] = 'query'
97
+ @options[:query] = query
98
+ end
99
+ opts.on '--skip FILE_PATTERNS',
100
+ 'skip specified files or directories, separated by comma, e.g. app/models/post.rb,vendor/plugins/**/*.rb' do |file_patterns|
101
+ @options[:skip_file_patterns] = file_patterns.split(',')
102
+ end
103
+ opts.on '-s',
104
+ '--show SNIPPET_NAME',
105
+ 'show specified snippet description, SNIPPET_NAME is combined by group and name, e.g. ruby/new_hash_syntax' do |snippet_name|
106
+ @options[:command] = 'show'
107
+ @options[:snippet_name] = snippet_name
108
+ end
109
+ opts.on '--sync', 'sync snippets' do
110
+ @options[:command] = 'sync'
111
+ end
112
+ opts.on '-r',
113
+ '--run SNIPPET_NAMES',
114
+ 'run specified snippets, each SNIPPET_NAME is combined by group and name, e.g. ruby/new_hash_syntax,ruby/new_lambda_syntax' do |snippet_names|
115
+ @options[:snippet_names] = snippet_names.split(',').map(&:strip)
116
+ end
117
+ opts.on '-g', '--generate NEW_SNIPPET_NAME', 'generate a new snippet' do |name|
118
+ @options[:command] = 'generate'
119
+ @options[:snippet_name] = name
120
+ end
121
+ opts.on '-v', '--version', 'show this version' do
122
+ puts "#{VERSION} (with synvert-core #{Core::VERSION} and parser #{Parser::VERSION})"
123
+ exit
124
+ end
102
125
  end
103
- end
104
126
  paths = optparse.parse(args)
105
127
  Core::Configuration.path = paths.first || Dir.pwd
106
128
  if @options[:skip_file_patterns] && !@options[:skip_file_patterns].empty?
107
- skip_files = @options[:skip_file_patterns].map do |file_pattern|
108
- full_file_pattern = File.join(Core::Configuration.path, file_pattern)
109
- Dir.glob(full_file_pattern)
110
- end.flatten
129
+ skip_files =
130
+ @options[:skip_file_patterns].map do |file_pattern|
131
+ full_file_pattern = File.join(Core::Configuration.path, file_pattern)
132
+ Dir.glob(full_file_pattern)
133
+ end.flatten
111
134
  Core::Configuration.skip_files = skip_files
112
135
  end
113
136
  end
@@ -117,14 +140,14 @@ module Synvert
117
140
  Dir.glob(File.join(default_snippets_path, 'lib/**/*.rb')).each { |file| require file }
118
141
 
119
142
  @options[:custom_snippet_paths].each do |snippet_path|
120
- if snippet_path =~ /^http/
143
+ if /^http/.match?(snippet_path)
121
144
  uri = URI.parse snippet_path
122
145
  eval(uri.read)
123
146
  else
124
147
  require snippet_path
125
148
  end
126
149
  end
127
- rescue
150
+ rescue StandardError
128
151
  FileUtils.rm_rf default_snippets_path
129
152
  retry
130
153
  end
@@ -136,7 +159,7 @@ module Synvert
136
159
  else
137
160
  Core::Rewriter.availables.each do |group, rewriters|
138
161
  puts group
139
- rewriters.each do |name, rewriter|
162
+ rewriters.each do |name, _rewriter|
140
163
  puts ' ' + name
141
164
  end
142
165
  end
@@ -144,6 +167,28 @@ module Synvert
144
167
  end
145
168
  end
146
169
 
170
+ def list_all_rewriters_in_json
171
+ if Core::Rewriter.availables.empty?
172
+ puts 'There is no snippet under ~/.synvert, please run `synvert --sync` to fetch snippets.'
173
+ return
174
+ end
175
+
176
+ output = []
177
+ Core::Rewriter.availables.each do |group, rewriters|
178
+ rewriters.each do |name, rewriter|
179
+ rewriter.process_with_sandbox
180
+ output << {
181
+ group: group,
182
+ name: name,
183
+ description: rewriter.description,
184
+ sub_snippets: rewriter.sub_snippets.map(&:name)
185
+ }
186
+ end
187
+ end
188
+
189
+ puts JSON.generate(output)
190
+ end
191
+
147
192
  # Open one rewriter.
148
193
  def open_rewriter
149
194
  editor = [ENV['SYNVERT_EDITOR'], ENV['EDITOR']].find { |e| !e.nil? && !e.empty? }
@@ -162,12 +207,12 @@ module Synvert
162
207
  Core::Rewriter.availables.each do |group, rewriters|
163
208
  if group.include? @options[:query]
164
209
  puts group
165
- rewriters.each do |name, rewriter|
210
+ rewriters.each do |name, _rewriter|
166
211
  puts ' ' + name
167
212
  end
168
213
  elsif rewriters.keys.any? { |name| name.include? @options[:query] }
169
214
  puts group
170
- rewriters.each do |name, rewriter|
215
+ rewriters.each do |name, _rewriter|
171
216
  puts ' ' + name if name.include?(@options[:query])
172
217
  end
173
218
  end
@@ -177,18 +222,9 @@ module Synvert
177
222
 
178
223
  # Show and print one rewriter.
179
224
  def show_rewriter
180
- group, name = @options[:snippet_name].split('/')
181
- rewriter = Core::Rewriter.fetch(group, name)
182
- if rewriter
183
- rewriter.process_with_sandbox
184
- puts rewriter.description
185
- rewriter.sub_snippets.each do |sub_rewriter|
186
- puts
187
- puts '=' * 80
188
- puts "snippet: #{sub_rewriter.name}"
189
- puts '=' * 80
190
- puts sub_rewriter.description
191
- end
225
+ path = File.expand_path(File.join(default_snippets_path, "lib/#{@options[:snippet_name]}.rb"))
226
+ if File.exist?(path)
227
+ puts File.read(path)
192
228
  else
193
229
  puts "snippet #{@options[:snippet_name]} not found"
194
230
  end
@@ -204,6 +240,54 @@ module Synvert
204
240
  end
205
241
  end
206
242
 
243
+ # generate a new snippet
244
+ def generate_snippet
245
+ group, name = @options[:snippet_name].split('/')
246
+ FileUtils.mkdir_p("lib/#{group}")
247
+ FileUtils.mkdir_p("spec/#{group}")
248
+ lib_content = <<~EOF
249
+ # frozen_string_literal: true
250
+
251
+ Synvert::Rewriter.new '#{group}', '#{name}' do
252
+ description <<~EOS
253
+ It convert Foo to Bar
254
+
255
+ ```ruby
256
+ Foo
257
+ ```
258
+
259
+ =>
260
+
261
+ ```ruby
262
+ Bar
263
+ ```
264
+ EOS
265
+
266
+ within_files '**/*.rb' do
267
+ with_node type: 'const', to_source: 'Foo' do
268
+ replace_with 'Bar'
269
+ end
270
+ end
271
+ end
272
+ EOF
273
+ spec_content = <<~EOF
274
+ # frozen_string_literal: true
275
+
276
+ require 'spec_helper'
277
+
278
+ RSpec.describe 'Convert Foo to Bar' do
279
+ let(:rewriter_name) { '#{group}/#{name}' }
280
+ let(:fake_file_path) { 'foobar.rb' }
281
+ let(:test_content) { 'Foo' }
282
+ let(:test_rewritten_content) { 'Bar' }
283
+
284
+ include_examples 'convertable'
285
+ end
286
+ EOF
287
+ File.write("lib/#{group}/#{name}.rb", lib_content)
288
+ File.write("spec/#{group}/#{name}_spec.rb", spec_content)
289
+ end
290
+
207
291
  def default_snippets_path
208
292
  File.join(ENV['HOME'], '.synvert')
209
293
  end
@@ -1,10 +1,16 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require 'open-uri'
3
4
  require 'json'
4
5
 
5
6
  module Synvert
6
7
  # Manage synvert snippets.
7
8
  class Snippet
9
+ def self.fetch_core_version
10
+ content = URI.open('https://rubygems.org/api/v1/versions/synvert-core.json').read
11
+ JSON.parse(content).first['number']
12
+ end
13
+
8
14
  def initialize(snippets_path)
9
15
  @snippets_path = snippets_path
10
16
  end
@@ -18,10 +24,5 @@ module Synvert
18
24
  Kernel.system("git clone https://github.com/xinminlabs/synvert-snippets.git #{@snippets_path}")
19
25
  end
20
26
  end
21
-
22
- def fetch_core_version
23
- content = URI.open('https://rubygems.org/api/v1/versions/synvert-core.json').read
24
- JSON.parse(content).first['number']
25
- end
26
27
  end
27
28
  end
@@ -1,5 +1,5 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Synvert
4
- VERSION = '0.10.0'
4
+ VERSION = '0.12.1'
5
5
  end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
4
 
3
5
  require 'synvert'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module Synvert
@@ -6,9 +8,20 @@ module Synvert
6
8
  let(:snippet) { Snippet.new(snippets_path) }
7
9
  after { FileUtils.rmdir(snippets_path) if File.exist?(snippets_path) }
8
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
+
9
20
  describe 'sync' do
10
21
  it 'git clones snippets' do
11
- expect(Kernel).to receive(:system).with("git clone https://github.com/xinminlabs/synvert-snippets.git #{snippets_path}")
22
+ expect(Kernel).to receive(:system).with(
23
+ "git clone https://github.com/xinminlabs/synvert-snippets.git #{snippets_path}"
24
+ )
12
25
  snippet.sync
13
26
  end
14
27
 
@@ -19,13 +32,5 @@ module Synvert
19
32
  FileUtils.cd File.dirname(__FILE__)
20
33
  end
21
34
  end
22
-
23
- describe 'fetch_core_version' do
24
- it 'gets remote version' do
25
- stub_request(:get, 'https://rubygems.org/api/v1/versions/synvert-core.json').
26
- to_return(:body => '[{"number":"0.4.2"}]')
27
- expect(snippet.fetch_core_version).to eq '0.4.2'
28
- end
29
- end
30
35
  end
31
36
  end
data/synvert.gemspec CHANGED
@@ -1,5 +1,6 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require 'synvert/version'
5
6
 
@@ -17,8 +18,9 @@ Gem::Specification.new do |spec|
17
18
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
19
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
20
  spec.require_paths = ['lib']
21
+ spec.post_install_message = 'Please run `synvert --sync` first to sync snippets remotely.'
20
22
 
21
- spec.add_runtime_dependency 'synvert-core', '>= 0.19.0'
23
+ spec.add_runtime_dependency 'synvert-core', '>= 0.20.1'
22
24
 
23
25
  spec.add_development_dependency 'bundler'
24
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.10.0
4
+ version: 0.12.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-02-07 00:00:00.000000000 Z
11
+ date: 2021-03-01 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.0
19
+ version: 0.20.1
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.19.0
26
+ version: 0.20.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -108,7 +108,7 @@ homepage: https://github.com/xinminlabs/synvert
108
108
  licenses:
109
109
  - MIT
110
110
  metadata: {}
111
- post_install_message:
111
+ post_install_message: Please run `synvert --sync` first to sync snippets remotely.
112
112
  rdoc_options: []
113
113
  require_paths:
114
114
  - lib