Class: CLI

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

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.



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

def initialize(argv)
  @argv = argv

  process
end

Instance Method Details

#cleanup_backtrace(error) ⇒ Object



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

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

#collect_deps(path, array) ⇒ Object



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

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



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

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

#filename_and_method_from_arg(arg) ⇒ Object



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

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



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

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

#processObject



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

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

#runObject



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

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

#run_file(arg) ⇒ Object



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

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



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

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