Class: Synvert::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/synvert/cli.rb

Overview

Synvert command line interface.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Initialize a CLI.



19
20
21
# File 'lib/synvert/cli.rb', line 19

def initialize
  @options = { command: 'run', format: 'plain' }
end

Class Method Details

.run(args = ARGV) ⇒ Boolean

Initialize the cli and run.

Parameters:

  • args (Array) (defaults to: ARGV)

    arguments, default is ARGV.

Returns:

  • (Boolean)

    true if command runs successfully.



14
15
16
# File 'lib/synvert/cli.rb', line 14

def self.run(args = ARGV)
  new.run(args)
end

Instance Method Details

#run(args) ⇒ Boolean

Run the CLI.

Parameters:

  • args (Array)

    arguments.

Returns:

  • (Boolean)

    true if command runs successfully.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/synvert/cli.rb', line 26

def run(args)
  run_option_parser(args)

  case @options[:command]
  when 'list'
    Command.read_rewriters
    Command.list_available_rewriters(@options[:format])
  when 'open'
    Command.open_rewriter(@options[:snippet_name])
  when 'query'
    Command.read_rewriters
    Command.query_available_rewriters(@options[:query], @options[:format])
  when 'show'
    Command.show_rewriter(@options[:snippet_name])
  when 'sync'
    Command.sync_snippets
  when 'generate'
    Command.generate_snippet(@options[:snippet_name])
  when 'execute'
    Command.read_helpers
    rewriter = eval_snippet_name_by_input(STDIN.read)
    if @options[:execute_command] == 'test'
      Command.test_snippet(rewriter)
    else
      Command.run_snippet(rewriter, @options[:format])
    end
  when 'test'
    Command.read_helpers
    rewriter = Synvert::Core::Utils.eval_snippet(@options[:snippet_name])
    Command.test_snippet(rewriter)
  when 'run'
    Command.read_helpers
    rewriter = Synvert::Core::Utils.eval_snippet(@options[:snippet_name])
    Command.run_snippet(rewriter, @options[:format])
  when 'test_bundle_gems'
    Command.read_helpers
    rewriter = Synvert::Core::Utils.eval_snippet(@options[:snippet_name])
    Command.test_snippet_in_bundle_gems(rewriter)
  else
    # nothing to do
  end
  true
rescue SystemExit
  true
rescue Parser::SyntaxError => e
  puts "Syntax error: #{e.message}"
  puts "file #{e.diagnostic.location.source_buffer.name}"
  puts "line #{e.diagnostic.location.line}"
  false
rescue StandardError
  false
end