2005-02-07 08:14:05 -05:00
|
|
|
require File.dirname(__FILE__) + '/options'
|
|
|
|
|
|
|
|
module Rails
|
|
|
|
module Generator
|
|
|
|
module Scripts
|
|
|
|
|
|
|
|
# Generator scripts handle command-line invocation. Each script
|
|
|
|
# responds to an invoke! class method which handles option parsing
|
|
|
|
# and generator invocation.
|
|
|
|
class Base
|
|
|
|
include Options
|
|
|
|
default_options :collision => :ask, :quiet => false
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
def run(args = [], runtime_options = {})
|
|
|
|
begin
|
2005-02-14 20:45:35 -05:00
|
|
|
parse!(args.dup, runtime_options)
|
|
|
|
rescue OptionParser::InvalidOption => e
|
|
|
|
# Don't cry, script. Generators want what you think is invalid.
|
|
|
|
end
|
2005-02-14 19:51:02 -05:00
|
|
|
|
2005-02-14 20:45:35 -05:00
|
|
|
# Generator name is the only required option.
|
|
|
|
unless options[:generator]
|
|
|
|
usage if args.empty?
|
|
|
|
options[:generator] ||= args.shift
|
2005-02-14 20:24:55 -05:00
|
|
|
end
|
2005-02-14 20:45:35 -05:00
|
|
|
|
|
|
|
# Look up generator instance and invoke command on it.
|
|
|
|
Rails::Generator::Base.instance(options[:generator], args, options).command(options[:command]).invoke!
|
|
|
|
rescue => e
|
|
|
|
puts e
|
|
|
|
puts " #{e.backtrace.join("\n ")}\n" if options[:backtrace]
|
|
|
|
raise SystemExit
|
2005-02-07 08:14:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
# Override with your own script usage banner.
|
|
|
|
def banner
|
2006-08-04 21:34:01 -04:00
|
|
|
"Usage: #{$0} generator [options] [args]"
|
2005-02-07 08:14:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def usage_message
|
|
|
|
usage = "\nInstalled Generators\n"
|
2007-06-27 05:25:25 -04:00
|
|
|
Rails::Generator::Base.sources.inject({}) do |mem, source|
|
2005-02-07 08:14:05 -05:00
|
|
|
label = source.label.to_s.capitalize
|
2007-06-27 05:25:25 -04:00
|
|
|
mem[label] ||= []
|
|
|
|
mem[label] |= source.names
|
|
|
|
mem
|
|
|
|
end.each_pair do |label, names|
|
2005-02-07 08:14:05 -05:00
|
|
|
usage << " #{label}: #{names.join(', ')}\n" unless names.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
usage << <<end_blurb
|
|
|
|
|
|
|
|
More are available at http://rubyonrails.org/show/Generators
|
|
|
|
1. Download, for example, login_generator.zip
|
|
|
|
2. Unzip to directory #{Dir.user_home}/.rails/generators/login
|
|
|
|
to use the generator with all your Rails apps
|
|
|
|
end_blurb
|
|
|
|
|
|
|
|
if Object.const_defined?(:RAILS_ROOT)
|
|
|
|
usage << <<end_blurb
|
2007-11-20 17:00:32 -05:00
|
|
|
or to #{File.expand_path(RAILS_ROOT)}/lib/generators/login
|
2005-02-07 08:14:05 -05:00
|
|
|
to use with this app only.
|
|
|
|
end_blurb
|
|
|
|
end
|
|
|
|
|
|
|
|
usage << <<end_blurb
|
|
|
|
3. Run generate with no arguments for usage information
|
|
|
|
#{$0} login
|
|
|
|
|
|
|
|
Generator gems are also available:
|
2005-12-11 00:06:06 -05:00
|
|
|
1. gem search -r generator
|
|
|
|
2. gem install login_generator
|
2005-02-07 08:14:05 -05:00
|
|
|
3. #{$0} login
|
|
|
|
|
|
|
|
end_blurb
|
|
|
|
return usage
|
|
|
|
end
|
|
|
|
end # Base
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|