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.



16
17
18
19
20
# File 'lib/synvert/cli.rb', line 16

def initialize
  @options = {command: 'run', custom_snippet_paths: [], snippet_names: []}
  Core::Configuration.instance.set :skip_files, []
  Core::Configuration.instance.set :default_snippets_path, File.join(ENV['HOME'], '.synvert')
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.



11
12
13
# File 'lib/synvert/cli.rb', line 11

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.



25
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
# File 'lib/synvert/cli.rb', line 25

def run(args)
  run_option_parser(args)

  case @options[:command]
  when 'list'
    load_rewriters
    list_available_rewriters
  when 'open'
    open_rewriter
  when 'query'
    load_rewriters
    query_available_rewriters
  when 'show'
    load_rewriters
    show_rewriter
  when 'sync'
    sync_snippets
  else
    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
  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 Synvert::Core::RewriterNotFound => e
  puts e.message
  false
end