aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails_generator/scripts.rb
blob: 3763b75dba19b755cdc65bd60085cb3055e553d2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
            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.
          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
        end

        protected
          # Override with your own script usage banner.
          def banner
            "Usage: #{$0} generator [options] [args]"
          end

          def usage_message
            usage = "\nInstalled Generators\n"
            Rails::Generator::Base.sources.inject([]) do |mem, source|
              # Using an association list instead of a hash to preserve order,
              # for aesthetic reasons more than anything else.
              label = source.label.to_s.capitalize
              pair = mem.assoc(label)
              mem << (pair = [label, []]) if pair.nil?
              pair[1] |= source.names
              mem
            end.each do |label, names|
              usage << "  #{label}: #{names.join(', ')}\n" unless names.empty?
            end

            usage << <<end_blurb

You can also install additional generators for your own use:
  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
     or to #{File.expand_path(RAILS_ROOT)}/lib/generators/login
     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:
  1. gem search -r generator
  2. gem install login_generator
  3. #{$0} login

end_blurb
            return usage
          end
      end # Base

    end
  end
end