Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/trunk/railties/lib/rails_generator/scripts.rb

Revision 9017, 2.9 kB (checked in by david, 9 months ago)

Fixed that script/generate would not look for plugin generators in plugin_paths (closes #11000) [glv]

Line 
1 require File.dirname(__FILE__) + '/options'
2
3 module Rails
4   module Generator
5     module Scripts
6
7       # Generator scripts handle command-line invocation.  Each script
8       # responds to an invoke! class method which handles option parsing
9       # and generator invocation.
10       class Base
11         include Options
12         default_options :collision => :ask, :quiet => false
13
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 = {})
18           begin
19             parse!(args.dup, runtime_options)
20           rescue OptionParser::InvalidOption => e
21             # Don't cry, script. Generators want what you think is invalid.
22           end
23
24           # Generator name is the only required option.
25           unless options[:generator]
26             usage if args.empty?
27             options[:generator] ||= args.shift
28           end
29
30           # Look up generator instance and invoke command on it.
31           Rails::Generator::Base.instance(options[:generator], args, options).command(options[:command]).invoke!
32         rescue => e
33           puts e
34           puts "  #{e.backtrace.join("\n  ")}\n" if options[:backtrace]
35           raise SystemExit
36         end
37
38         protected
39           # Override with your own script usage banner.
40           def banner
41             "Usage: #{$0} generator [options] [args]"
42           end
43
44           def usage_message
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 esthetic 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
53               mem
54             end.each do |label, names|
55               usage << "  #{label}: #{names.join(', ')}\n" unless names.empty?
56             end
57
58             usage << <<end_blurb
59
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
64 end_blurb
65
66             if Object.const_defined?(:RAILS_ROOT)
67               usage << <<end_blurb
68      or to #{File.expand_path(RAILS_ROOT)}/lib/generators/login
69      to use with this app only.
70 end_blurb
71             end
72
73             usage << <<end_blurb
74   3. Run generate with no arguments for usage information
75        #{$0} login
76
77 Generator gems are also available:
78   1. gem search -r generator
79   2. gem install login_generator
80   3. #{$0} login
81
82 end_blurb
83             return usage
84           end
85       end # Base
86
87     end
88   end
89 end
Note: See TracBrowser for help on using the browser.