Class: RubiGen::Scripts::Base

Inherits:
Object
  • Object
show all
Includes:
Options
Defined in:
lib/rubigen/scripts.rb

Overview

Generator scripts handle command-line invocation. Each script responds to an invoke! class method which handles option parsing and generator invocation.

Direct Known Subclasses

Destroy, Generate, Update

Instance Attribute Summary collapse

Attributes included from Options

#options

Instance Method Summary collapse

Methods included from Options

included

Instance Attribute Details

#stdoutObject (readonly)

Returns the value of attribute stdout.



12
13
14
# File 'lib/rubigen/scripts.rb', line 12

def stdout
  @stdout
end

Instance Method Details

#run(args = [], runtime_options = {}) ⇒ Object

Run the generator script. Takes an array of unparsed arguments and a hash of parsed arguments, takes the generator as an option or first remaining argument, and invokes the requested command.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rubigen/scripts.rb', line 17

def run(args = [], runtime_options = {})
  @stdout = runtime_options[:stdout] || $stdout
  begin
    parse!(args.dup, runtime_options)
  rescue OptionParser::InvalidOption => e
    # Don't cry, script. Generators want what you think is invalid.
  end

  # Generator name is the only required option.
  unless options[:generator]
    usage if args.empty?
    options[:generator] ||= args.shift
  end

  # Look up generator instance and invoke command on it.
  RubiGen::Base.instance(options[:generator], args, options).command(options[:command]).invoke!
rescue => e
  stdout.puts e
  stdout.puts "  #{e.backtrace.join("\n  ")}\n" if options[:backtrace]
  raise SystemExit unless options[:no_exit]
end