Class: Synvert::CLI

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(args = ARGV) ⇒ Object



8
9
10
# File 'lib/synvert/cli.rb', line 8

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

Instance Method Details

#run(args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
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
# File 'lib/synvert/cli.rb', line 12

def run(args)
  Configuration.instance.set 'snippet_paths', []
  Configuration.instance.set 'snippet_names', []

  command = :run
  optparse = OptionParser.new do |opts|
    opts.banner = "Usage: synvert [project_path]"
    opts.on '--load-snippets SNIPPET_PATHS', 'load additional snippets, snippet paths can be local file path or remote http url' do |snippet_paths|
      Configuration.instance.set 'snippet_paths', snippet_paths.split(',')
    end
    opts.on '--list-snippets', 'list all available snippets' do
      command = :list
    end
    opts.on '--run-snippets SNIPPET_NAMES', 'run specified snippets' do |snippet_names|
      Configuration.instance.set 'snippet_names', snippet_names.split(',')
    end
  end
  paths = optparse.parse(args)
  Configuration.instance.set :path, paths.first || Dir.pwd

  Dir.glob(File.join(File.dirname(__FILE__), 'snippets/**/*.rb')).each { |file| eval(File.read(file)) }
  Configuration.instance.get('snippet_paths').each do |snippet_path|
    if snippet_path =~ /^http/
      uri = URI.parse snippet_path
      eval(uri.read)
    else
      eval(File.read(snippet_path))
    end
  end
  Configuration.instance.get('snippet_names').each do |snippet_name|
    puts "===== #{snippet_name} started ====="
    rewriter = Rewriter.call snippet_name
    puts rewriter.todo_list if rewriter.todo_list
    puts "===== #{snippet_name} done ====="
  end

  if :list == command
    puts "%-40s %s" % ['name', 'description']
    puts "%-40s %s" % ['----', '-----------']
    Rewriter.availables.each do |rewriter|
      puts "%-40s %s" % [rewriter.name, rewriter.description]
    end
  end
end