lib/synvert/cli.rb in synvert-0.13.0 vs lib/synvert/cli.rb in synvert-0.14.0

- old
+ new

@@ -13,11 +13,11 @@ new.run(args) end # Initialize a CLI. def initialize - @options = { command: 'run', custom_snippet_paths: [], snippet_names: [] } + @options = { command: 'run', custom_snippet_paths: [], format: 'plain' } end # Run the CLI. # @param args [Array] arguments. # @return [Boolean] true if command runs successfully. @@ -26,13 +26,10 @@ case @options[:command] when 'list' load_rewriters list_available_rewriters - when 'list-all' - load_rewriters - list_all_rewriters_in_json when 'open' open_rewriter when 'query' load_rewriters query_available_rewriters @@ -44,20 +41,11 @@ when 'generate' generate_snippet else # run load_rewriters - @options[:snippet_names].each do |snippet_name| - puts "===== #{snippet_name} started =====" - group, name = snippet_name.split('/') - rewriter = Core::Rewriter.call group, name - rewriter.warnings.each do |warning| - puts '[Warn] ' + warning.message - end - puts rewriter.todo if rewriter.todo - puts "===== #{snippet_name} done =====" - end + run_snippet end true rescue SystemExit true rescue Parser::SyntaxError => e @@ -80,13 +68,10 @@ opts.on '-d', '--load SNIPPET_PATHS', 'load custom snippets, snippet paths can be local file path or remote http url' do |snippet_paths| @options[:custom_snippet_paths] = snippet_paths.split(',').map(&:strip) end - opts.on '--list-all', 'list all available snippets name and description in json format' do - @options[:command] = 'list-all' - end opts.on '-l', '--list', 'list all available snippets' do @options[:command] = 'list' end opts.on '-o', '--open SNIPPET_NAME', 'Open a snippet' do |snippet_name| @options[:command] = 'open' @@ -108,18 +93,21 @@ end opts.on '--sync', 'sync snippets' do @options[:command] = 'sync' end 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| - @options[:snippet_names] = snippet_names.split(',').map(&:strip) + '--run SNIPPET_NAME', + 'run specified snippet, e.g. ruby/new_hash_syntax' do |snippet_name| + @options[:snippet_name] = snippet_name end opts.on '-g', '--generate NEW_SNIPPET_NAME', 'generate a new snippet' do |name| @options[:command] = 'generate' @options[:snippet_name] = name end + opts.on '-f', '--format FORMAT', 'output format' do |format| + @options[:format] = format + end opts.on '-v', '--version', 'show this version' do puts "#{VERSION} (with synvert-core #{Core::VERSION} and parser #{Parser::VERSION})" exit end end @@ -154,41 +142,37 @@ # List and print all available rewriters. def list_available_rewriters if Core::Rewriter.availables.empty? puts 'There is no snippet under ~/.synvert, please run `synvert --sync` to fetch snippets.' - else + return + end + + if plain_output? Core::Rewriter.availables.each do |group, rewriters| puts group rewriters.each do |name, _rewriter| puts ' ' + name end end puts - end - end - - def list_all_rewriters_in_json - if Core::Rewriter.availables.empty? - puts 'There is no snippet under ~/.synvert, please run `synvert --sync` to fetch snippets.' - return - end - - output = [] - Core::Rewriter.availables.each do |group, rewriters| - rewriters.each do |name, rewriter| - rewriter.process_with_sandbox - output << { - group: group, - name: name, - description: rewriter.description, - sub_snippets: rewriter.sub_snippets.map(&:name) - } + elsif json_output? + output = [] + Core::Rewriter.availables.each do |group, rewriters| + rewriters.each do |name, rewriter| + rewriter.process_with_sandbox + output << { + group: group, + name: name, + description: rewriter.description, + sub_snippets: rewriter.sub_snippets.map(&:name) + } + end end - end - puts JSON.generate(output) + puts JSON.generate(output) + end end # Open one rewriter. def open_rewriter editor = [ENV['SYNVERT_EDITOR'], ENV['EDITOR']].find { |e| !e.nil? && !e.empty? } @@ -238,21 +222,44 @@ if Gem::Version.new(core_version) > Gem::Version.new(Synvert::Core::VERSION) puts "synvert-core is updated, please install synvert-core #{core_version}" end end + # run snippets + def run_snippet + snippet_name = @options[:snippet_name] + if plain_output? + puts "===== #{snippet_name} started =====" + group, name = snippet_name.split('/') + rewriter = Core::Rewriter.call group, name + rewriter.warnings.each do |warning| + puts '[Warn] ' + warning.message + end + puts rewriter.todo if rewriter.todo + puts "===== #{snippet_name} done =====" + elsif json_output? + group, name = snippet_name.split('/') + rewriter = Core::Rewriter.call group, name + puts JSON.generate({ + affected_files: rewriter.affected_files.union(rewriter.sub_snippets.map(&:affected_files).reduce(:+)).to_a, + warnings: rewriter.warnings.union(rewriter.sub_snippets.map(&:warnings).reduce(:+)), + todo: rewriter.todo + }) + 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 convert Foo to Bar + It converts Foo to Bar ```ruby Foo ``` @@ -288,8 +295,16 @@ File.write("spec/#{group}/#{name}_spec.rb", spec_content) end def default_snippets_home ENV['SYNVERT_SNIPPETS_HOME'] || File.join(ENV['HOME'], '.synvert') + end + + def plain_output? + @options[:format] == 'plain' + end + + def json_output? + @options[:format] == 'json' end end end