Class: Rwm::CLI

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

Constant Summary collapse

COMMANDS =
{
  "init"      => "Commands::Init",
  "bootstrap" => "Commands::Bootstrap",
  "new"       => "Commands::New",
  "info"      => "Commands::Info",
  "graph"     => "Commands::Graph",
  "check"     => "Commands::Check",
  "list"      => "Commands::List",
  "run"       => "Commands::Run",
  "affected"  => "Commands::Affected"
}.freeze
TASK_SHORTCUTS =

Shortcuts that expand to ‘run <task>`

%w[test spec build].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



26
27
28
# File 'lib/rwm/cli.rb', line 26

def initialize(argv)
  @argv = argv.dup
end

Class Method Details

.run(argv) ⇒ Object



22
23
24
# File 'lib/rwm/cli.rb', line 22

def self.run(argv)
  new(argv).run
end

Instance Method Details

#runObject



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

def run
  command_name = @argv.shift

  if command_name.nil? || %w[-h --help help].include?(command_name)
    print_help
    return 0
  end

  if %w[-v --version version].include?(command_name)
    puts "rwm #{Rwm::VERSION}"
    return 0
  end

  # Expand task shortcuts: `rwm test` → `rwm run test`
  if TASK_SHORTCUTS.include?(command_name)
    @argv.unshift(command_name)
    command_name = "run"
  end

  const_name = COMMANDS[command_name]
  unless const_name
    $stderr.puts "Unknown command: #{command_name}"
    $stderr.puts "Run `rwm help` for available commands."
    return 1
  end

  # Autoload the command
  require "rwm/commands/#{command_name}"
  command_class = const_name.split("::").reduce(Rwm) { |mod, name| mod.const_get(name) }
  command_class.new(@argv).run
rescue Rwm::Error => e
  $stderr.puts "Error: #{e.message}"
  1
end