lib/synvert/cli.rb in synvert-1.4.7 vs lib/synvert/cli.rb in synvert-1.5.0
- old
+ new
@@ -26,34 +26,39 @@
def run(args)
run_option_parser(args)
case @options[:command]
when 'list'
- read_rewriters
- list_available_rewriters
+ Command.read_rewriters
+ Command.list_available_rewriters(@options[:foramt])
when 'open'
- open_rewriter
+ Command.open_rewriter(@options[:snippet_name])
when 'query'
- read_rewriters
- query_available_rewriters
+ Command.read_rewriters
+ Command.query_available_rewriters
when 'show'
- show_rewriter
+ Command.show_rewriter(@options[:snippet_name])
when 'sync'
- sync_snippets
+ Command.sync_snippets
when 'generate'
- generate_snippet
+ Comamnd.generate_snippet(@options[:snippet_name])
when 'execute'
- read_helpers
- execute_snippet(@options[:execute_command])
+ Command.read_helpers
+ rewriter = eval_snippet_name_by_input(STDIN.read)
+ if @options[:execute_command] == 'test'
+ Comamnd.test_snippet(rewriter)
+ else
+ Comamnd.run_snippet(rewriter, @options[:format])
+ end
when 'test'
- read_helpers
+ Command.read_helpers
rewriter = Synvert::Core::Utils.eval_snippet(@options[:snippet_name])
- test_snippet(rewriter)
+ Command.test_snippet(rewriter)
when 'run'
- read_helpers
+ Command.read_helpers
rewriter = Synvert::Core::Utils.eval_snippet(@options[:snippet_name])
- run_snippet(rewriter)
+ Command.run_snippet(rewriter, @options[:format])
else
# nothing to do
end
true
rescue SystemExit
@@ -151,216 +156,11 @@
if @options[:skip_paths] && !@options[:skip_paths].empty?
Core::Configuration.skip_paths = @options[:skip_paths].split(",").map { |skip_path| skip_path.strip }
end
end
- # read all rewriters.
- def read_rewriters
- Dir.glob(File.join(default_snippets_home, 'lib/**/*.rb')).each { |file| require file }
- end
-
- # read all helpers.
- def read_helpers
- Dir.glob(File.join(default_snippets_home, 'lib/helpers/**/*.rb')).each { |file| require file }
- end
-
- # List and print all available rewriters.
- def list_available_rewriters
- if Core::Rewriter.availables.empty?
- puts "There is no snippet under #{default_snippets_home}, please run `synvert-ruby --sync` to fetch snippets."
- return
- end
-
- if plain_output?
- Core::Rewriter.availables.each do |group, rewriters|
- puts group
- rewriters.each do |name, _rewriter|
- puts ' ' + name
- end
- end
- puts
- elsif json_output?
- output = []
- Core::Rewriter.availables.each do |group, rewriters|
- rewriters.each do |name, rewriter|
- rewriter.process_with_sandbox
- sub_snippets =
- rewriter.sub_snippets.map { |sub_snippet|
- { group: sub_snippet.group, name: sub_snippet.name }
- }
- item = { group: group, name: name, description: rewriter.description, sub_snippets: sub_snippets }
- item[:ruby_version] = rewriter.ruby_version.version if rewriter.ruby_version
- item[:gem_spec] = { name: rewriter.gem_spec.name, version: rewriter.gem_spec.version } if rewriter.gem_spec
- output << item
- end
- end
-
- puts output.to_json
- end
- end
-
- # Open one rewriter.
- def open_rewriter
- editor = [ENV['SYNVERT_EDITOR'], ENV['EDITOR']].find { |e| !e.nil? && !e.empty? }
- return puts 'To open a synvert snippet, set $EDITOR or $SYNVERT_EDITOR' unless editor
-
- path = File.expand_path(File.join(default_snippets_home, "lib/#{@options[:snippet_name]}.rb"))
- if File.exist?(path)
- system editor, path
- else
- puts "Can't run #{editor} #{path}"
- end
- end
-
- # Query and print available rewriters.
- def query_available_rewriters
- Core::Rewriter.availables.each do |group, rewriters|
- if group.include? @options[:query]
- puts group
- rewriters.each do |name, _rewriter|
- puts ' ' + name
- end
- elsif rewriters.keys.any? { |name| name.include? @options[:query] }
- puts group
- rewriters.each do |name, _rewriter|
- puts ' ' + name if name.include?(@options[:query])
- end
- end
- end
- puts
- end
-
- # Show and print one rewriter.
- def show_rewriter
- path = File.expand_path(File.join(default_snippets_home, "lib/#{@options[:snippet_name]}.rb"))
- if File.exist?(path)
- puts File.read(path)
- else
- puts "snippet #{@options[:snippet_name]} not found"
- end
- end
-
- # sync snippets
- def sync_snippets
- Snippet.new(default_snippets_home).sync
- puts 'synvert snippets are synced'
- end
-
# eval snippet name by user input
def eval_snippet_name_by_input(input)
eval(input)
- end
-
- # run a snippet
- def run_snippet(rewriter)
- if plain_output?
- puts "===== #{rewriter.group}/#{rewriter.name} started ====="
- rewriter.process
- rewriter.warnings.each do |warning|
- puts '[Warn] ' + warning.message
- end
- puts "===== #{rewriter.group}/#{rewriter.name} done ====="
- elsif json_output?
- rewriter.process
- output = {
- affected_files: rewriter.affected_files.union(rewriter.sub_snippets.sum(Set.new, &:affected_files)).to_a,
- warnings: rewriter.warnings.union(rewriter.sub_snippets.sum([], &:warnings))
- }
- puts output.to_json
- end
- rescue StandardError => e
- if ENV['DEBUG']
- puts e.backtrace.join("\n")
- end
- if plain_output?
- puts "Error: #{e.message}"
- else
- puts({ error: e.message }.to_json)
- end
- raise
- end
-
- # test a snippet
- def test_snippet(rewriter)
- results = rewriter.test
- puts results.to_json
- rescue StandardError => e
- if ENV['DEBUG']
- puts e.backtrace.join("\n")
- end
- puts({ error: e.message }.to_json)
- raise
- end
-
- # execute snippet
- def execute_snippet(execute_command)
- rewriter = eval_snippet_name_by_input(STDIN.read)
- if execute_command == 'test'
- test_snippet(rewriter)
- else
- run_snippet(rewriter)
- end
- end
-
- # generate a new snippet
- def generate_snippet
- group, name = @options[:snippet_name].split('/')
- FileUtils.mkdir_p("lib/#{group}")
- FileUtils.mkdir_p("spec/#{group}")
- lib_content = <<~EOF
- # frozen_string_literal: true
-
- Synvert::Rewriter.new '#{group}', '#{name}' do
- description <<~EOS
- It converts Foo to Bar
-
- ```ruby
- Foo
- ```
-
- =>
-
- ```ruby
- Bar
- ```
- EOS
-
- within_files '**/*.rb' do
- with_node type: 'const', to_source: 'Foo' do
- replace_with 'Bar'
- end
- end
- end
- EOF
- spec_content = <<~EOF
- # frozen_string_literal: true
-
- require 'spec_helper'
-
- RSpec.describe 'Convert Foo to Bar' do
- let(:rewriter_name) { '#{group}/#{name}' }
- let(:fake_file_path) { 'foobar.rb' }
- let(:test_content) { 'Foo' }
- let(:test_rewritten_content) { 'Bar' }
-
- include_examples 'convertable'
- end
- EOF
- File.write("lib/#{group}/#{name}.rb", lib_content)
- File.write("spec/#{group}/#{name}_spec.rb", spec_content)
- end
-
- def default_snippets_home
- # ENV['HOME'] may use \ as file separator,
- # but File.join always uses / as file separator.
- ENV['SYNVERT_SNIPPETS_HOME'] || File.join(ENV['HOME'].gsub("\\", "/"), '.synvert-ruby')
- end
-
- def plain_output?
- @options[:format] == 'plain'
- end
-
- def json_output?
- @options[:format] == 'json'
end
end
end