1 require File
.dirname(__FILE__
) + '/options'
7 # Generator scripts handle command-line invocation. Each script
8 # responds to an invoke! class method which handles option parsing
9 # and generator invocation.
12 default_options
:collision => :ask, :quiet => false
14 # Run the generator script. Takes an array of unparsed arguments
15 # and a hash of parsed arguments, takes the generator as an option
16 # or first remaining argument, and invokes the requested command.
17 def run(args
= [], runtime_options
= {})
19 parse
!(args
.dup
, runtime_options
)
20 rescue OptionParser
::InvalidOption => e
21 # Don't cry, script. Generators want what you think is invalid.
24 # Generator name is the only required option.
25 unless options
[:generator]
27 options
[:generator] ||= args
.shift
30 # Look up generator instance and invoke command on it.
31 Rails
::Generator::Base.instance(options
[:generator], args
, options
).command(options
[:command]).invoke
!
34 puts
" #{e.backtrace.join("\n ")}\n" if options
[:backtrace]
39 # Override with your own script usage banner.
41 "Usage: #{$0} generator [options] [args]"
45 usage
= "\nInstalled Generators\n"
46 Rails
::Generator::Base.sources
.inject([]) do |mem
, source
|
47 # Using an association list instead of a hash to preserve order,
48 # for aesthetic reasons more than anything else.
49 label
= source
.label
.to_s
.capitalize
50 pair
= mem
.assoc(label
)
51 mem
<< (pair
= [label
, []]) if pair
.nil?
52 pair
[1] |= source
.names
54 end.each
do |label
, names
|
55 usage
<< " #{label}: #{names.join(', ')}\n" unless names
.empty
?
60 More are available at http://wiki.rubyonrails.org/rails/pages/AvailableGenerators
61 1. Download, for example, login_generator.zip
62 2. Unzip to directory #{Dir.user_home}/.rails/generators/login
63 to use the generator with all your Rails apps
66 if Object
.const_defined
?(:RAILS_ROOT)
68 or to #{File.expand_path(RAILS_ROOT)}/lib/generators/login
69 to use with this app only.
74 3. Run generate with no arguments for usage information
77 Generator gems are also available:
78 1. gem search -r generator
79 2. gem install login_generator