Generator scripts handle command-line invocation. Each script responds to an invoke! class method which handles option parsing and generator invocation.
Methods
Included Modules
Public Instance methods
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.
[ show source ]
# File railties/lib/rails_generator/scripts.rb, line 17 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
Protected Instance methods
Override with your own script usage banner.
[ show source ]
# File railties/lib/rails_generator/scripts.rb, line 40 40: def banner 41: "Usage: #{$0} generator [options] [args]" 42: end
[ show source ]
# File railties/lib/rails_generator/scripts.rb, line 44 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 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 53: mem 54: end.each do |label, names| 55: usage << " #{label}: #{names.join(', ')}\n" unless names.empty? 56: end 57: 58: usage << "\nMore are available at http://wiki.rubyonrails.org/rails/pages/AvailableGenerators\n 1. Download, for example, login_generator.zip\n 2. Unzip to directory \#{Dir.user_home}/.rails/generators/login\n to use the generator with all your Rails apps\n" 59: 60: if Object.const_defined?(:RAILS_ROOT) 61: usage << " or to \#{File.expand_path(RAILS_ROOT)}/lib/generators/login\n to use with this app only.\n" 62: end 63: 64: usage << " 3. Run generate with no arguments for usage information\n \#{$0} login\n\nGenerator gems are also available:\n 1. gem search -r generator\n 2. gem install login_generator\n 3. \#{$0} login\n\n" 65: return usage 66: end