lib/synvert/cli.rb in synvert-0.20.0 vs lib/synvert/cli.rb in synvert-1.0.0

- old
+ new

@@ -41,25 +41,19 @@ when 'sync' sync_snippets when 'generate' generate_snippet when 'execute' - execute_snippet + execute_snippet(@options[:execute_command]) + when 'test' + group, name = get_snippet_name(@options[:snippet_name]) + test_snippet(group, name) + when 'run' + group, name = get_snippet_name(@options[:snippet_name]) + run_snippet(group, name) else - if /^http/.match?(@options[:snippet_name]) - uri = URI.parse(@options[:snippet_name]) - eval(uri.open.read) - snippet_name = get_last_snippet_name - run_snippet(snippet_name) - elsif File.exists?(@options[:snippet_name]) - require(@options[:snippet_name]) - snippet_name = get_last_snippet_name - run_snippet(snippet_name) - else - read_rewriters - run_snippet(@options[:snippet_name]) - end + # nothing to do end true rescue SystemExit true rescue Parser::SyntaxError => e @@ -101,41 +95,49 @@ @options[:snippet_name] = name end opts.on '--sync', 'sync snippets' do @options[:command] = 'sync' end - opts.on '--execute', 'execute snippet' do + opts.on '--execute', 'execute snippet' do |execute_command| @options[:command] = 'execute' + @options[:execute_command] = execute_command end - 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| + opts.on '-r', '--run SNIPPET_NAME', 'run a snippet with snippet name, e.g. ruby/new_hash_syntax, or remote url, or local file path' do |snippet_name| + @options[:command] = 'run' @options[:snippet_name] = snippet_name end + opts.on '-t', '--test SNIPPET_NAME', 'test a snippet with snippet name, e.g. ruby/new_hash_syntax, or remote url, or local file path' do |snippet_name| + @options[:command] = 'test' + @options[:snippet_name] = snippet_name + end opts.on '--show-run-process', 'show processing files when running a snippet' do Core::Configuration.show_run_process = true end - 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| - @options[:skip_file_patterns] = file_patterns.split(',') + opts.on '--only-paths DIRECTORIES', + 'only specified files or directories, separated by comma, e.g. app/models,app/controllers' do |directories| + @options[:only_paths] = directories end + opts.on '--skip-paths FILE_PATTERNS', + 'skip specified files or directories, separated by comma, e.g. vendor/,lib/**/*.rb' do |file_patterns| + @options[:skip_paths] = file_patterns + 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 paths = optparse.parse(args) - Core::Configuration.path = paths.first || Dir.pwd - if @options[:skip_file_patterns] && !@options[:skip_file_patterns].empty? - skip_files = - @options[:skip_file_patterns].map do |file_pattern| - full_file_pattern = File.join(Core::Configuration.path, file_pattern) - Dir.glob(full_file_pattern) - end.flatten - Core::Configuration.skip_files = skip_files + Core::Configuration.root_path = paths.first || Dir.pwd + if @options[:only_paths] && !@options[:only_paths].empty? + Core::Configuration.only_paths = @options[:only_paths].split(",").map { |only_path| only_path.strip } end + 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 } @@ -226,52 +228,68 @@ puts "synvert-core is updated, installing synvert-core #{core_version}" system('gem install synvert-core') end end - # run snippets - def run_snippet(snippet_name) + # get snippet name + # it can get from explicit snippet name, + # or from local path or http url. + def get_snippet_name(snippet_name) + if /^http/.match?(snippet_name) + uri = URI.parse(snippet_name) + eval(uri.open.read) + get_last_snippet_name + elsif File.exists?(snippet_name) + require(snippet_name) + get_last_snippet_name + else + require(File.join(default_snippets_home, 'lib', "#{snippet_name}.rb")) + snippet_name.split('/') + end + end + + # get snippet name by user inputs + def get_snippet_name_by_input(input) + rewriter = eval(input) + get_last_snippet_name + end + + # run a snippet + def run_snippet(group, name) if plain_output? - puts "===== #{snippet_name} started =====" - group, name = snippet_name.split('/') + puts "===== #{group}/#{name} started =====" 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 =====" + puts "===== #{group}/#{name} done =====" elsif json_output? - group, name = snippet_name.split('/') rewriter = Core::Rewriter.call group, name 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)), todo: rewriter.todo } puts JSON.generate(output) end end + # test a snippet + def test_snippet(group, name) + rewriter = Core::Rewriter.fetch(group, name) + results = rewriter.test + puts JSON.generate(results) + end + # execute snippet - def execute_snippet - input = STDIN.read - if plain_output? - puts '===== execute started =====' - rewriter = eval(input) - rewriter.warnings.each do |warning| - puts '[Warn] ' + warning.message - end - puts rewriter.todo if rewriter.todo - puts '===== execute done =====' - elsif json_output? - rewriter = eval(input) - 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)), - todo: rewriter.todo - } - puts JSON.generate(output) + def execute_snippet(execute_command) + group, name = get_snippet_name_by_input(STDIN.read) + if execute_command == 'test' + test_snippet(group, name) + else + run_snippet(group, name) end end # generate a new snippet def generate_snippet @@ -335,9 +353,9 @@ # get the last registered snippet name def get_last_snippet_name group = Rewriter.availables.keys.last name = Rewriter.availables[group].keys.last - return [group, name].join("/") + return group, name end end end