Class: CLI

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

Overview

Command line interface functionality

Constant Summary collapse

FILENAME_AND_METHOD_RE =
/^([^\:]+)\:(.+)$/.freeze
BACKTRACE_RE =
/^(#{Modulation::DIR})|(bin\/mdl)/.freeze

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



7
8
9
10
11
# File 'lib/modulation/modules/cli.rb', line 7

def initialize(argv)
  @argv = argv

  process
end

Instance Method Details

#cleanup_backtrace(error) ⇒ Object



49
50
51
52
# File 'lib/modulation/modules/cli.rb', line 49

def cleanup_backtrace(error)
  backtrace = error.backtrace.reject { |l| l =~ BACKTRACE_RE }
  error.set_backtrace(backtrace)
end

#collect_deps(path, array) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/modulation/modules/cli.rb', line 54

def collect_deps(path, array)
  if File.directory?(path)
    Dir["#{path}/**/*.rb"].each { |fn| collect_deps(fn, array) }
  else
    array << File.expand_path(path)
    mod = import(File.expand_path(path))
    if mod.respond_to?(:__traverse_dependencies)
      mod.__traverse_dependencies { |m| array << m.__module_info[:location] }
    end
  end
end

#depsObject



66
67
68
69
70
# File 'lib/modulation/modules/cli.rb', line 66

def deps
  paths = []
  @argv.each { |arg| collect_deps(arg, paths) }
  puts(*paths)
end

#filename_and_method_from_arg(arg) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/modulation/modules/cli.rb', line 38

def filename_and_method_from_arg(arg)
  if arg =~ FILENAME_AND_METHOD_RE
    match = Regexp.last_match
    [match[1], match[2].to_sym]
  else
    [arg, :main]
  end
end

#packObject



72
73
74
# File 'lib/modulation/modules/cli.rb', line 72

def pack
  STDOUT << import('@modulation/packer').pack(@argv, hide_filenames: true)
end

#processObject



13
14
15
16
17
18
19
20
21
# File 'lib/modulation/modules/cli.rb', line 13

def process
  cmd = ARGV.shift
  if respond_to? cmd.to_sym
    send cmd
  else
    ARGV.unshift cmd
    run
  end
end

#runObject



23
24
25
26
27
28
# File 'lib/modulation/modules/cli.rb', line 23

def run
  @argv.each { |arg| run_file arg }
rescue StandardError => e
  cleanup_backtrace(e)
  raise e
end

#run_file(arg) ⇒ Object



30
31
32
33
34
# File 'lib/modulation/modules/cli.rb', line 30

def run_file(arg)
  fn, method = filename_and_method_from_arg(arg)
  mod = import(File.expand_path(fn))
  mod.send(method) if method
end

#versionObject



76
77
78
# File 'lib/modulation/modules/cli.rb', line 76

def version
  puts "Modulation version #{Modulation::VERSION}"
end