Froze rails gems
[depot.git] / vendor / rails / railties / lib / rails_generator / options.rb
1 require 'optparse'
2
3 module Rails
4 module Generator
5 module Options
6 def self.included(base)
7 base.extend(ClassMethods)
8 class << base
9 if respond_to?(:inherited)
10 alias_method :inherited_without_options, :inherited
11 end
12 alias_method :inherited, :inherited_with_options
13 end
14 end
15
16 module ClassMethods
17 def inherited_with_options(sub)
18 inherited_without_options(sub) if respond_to?(:inherited_without_options)
19 sub.extend(Rails::Generator::Options::ClassMethods)
20 end
21
22 def mandatory_options(options = nil)
23 if options
24 write_inheritable_attribute(:mandatory_options, options)
25 else
26 read_inheritable_attribute(:mandatory_options) or write_inheritable_attribute(:mandatory_options, {})
27 end
28 end
29
30 def default_options(options = nil)
31 if options
32 write_inheritable_attribute(:default_options, options)
33 else
34 read_inheritable_attribute(:default_options) or write_inheritable_attribute(:default_options, {})
35 end
36 end
37
38 # Merge together our class options. In increasing precedence:
39 # default_options (class default options)
40 # runtime_options (provided as argument)
41 # mandatory_options (class mandatory options)
42 def full_options(runtime_options = {})
43 default_options.merge(runtime_options).merge(mandatory_options)
44 end
45
46 end
47
48 # Each instance has an options hash that's populated by #parse.
49 def options
50 @options ||= {}
51 end
52 attr_writer :options
53
54 protected
55 # Convenient access to class mandatory options.
56 def mandatory_options
57 self.class.mandatory_options
58 end
59
60 # Convenient access to class default options.
61 def default_options
62 self.class.default_options
63 end
64
65 # Merge together our instance options. In increasing precedence:
66 # default_options (class default options)
67 # options (instance options)
68 # runtime_options (provided as argument)
69 # mandatory_options (class mandatory options)
70 def full_options(runtime_options = {})
71 self.class.full_options(options.merge(runtime_options))
72 end
73
74 # Parse arguments into the options hash. Classes may customize
75 # parsing behavior by overriding these methods:
76 # #banner Usage: ./script/generate [options]
77 # #add_options! Options:
78 # some options..
79 # #add_general_options! General Options:
80 # general options..
81 def parse!(args, runtime_options = {})
82 self.options = {}
83
84 @option_parser = OptionParser.new do |opt|
85 opt.banner = banner
86 add_options!(opt)
87 add_general_options!(opt)
88 opt.parse!(args)
89 end
90
91 return args
92 ensure
93 self.options = full_options(runtime_options)
94 end
95
96 # Raise a usage error. Override usage_message to provide a blurb
97 # after the option parser summary.
98 def usage(message = usage_message)
99 raise UsageError, "#{@option_parser}\n#{message}"
100 end
101
102 def usage_message
103 ''
104 end
105
106 # Override with your own usage banner.
107 def banner
108 "Usage: #{$0} [options]"
109 end
110
111 # Override to add your options to the parser:
112 # def add_options!(opt)
113 # opt.on('-v', '--verbose') { |value| options[:verbose] = value }
114 # end
115 def add_options!(opt)
116 end
117
118 # Adds general options like -h and --quiet. Usually don't override.
119 def add_general_options!(opt)
120 opt.separator ''
121 opt.separator 'Rails Info:'
122 opt.on('-v', '--version', 'Show the Rails version number and quit.')
123 opt.on('-h', '--help', 'Show this help message and quit.') { |v| options[:help] = v }
124
125 opt.separator ''
126 opt.separator 'General Options:'
127
128 opt.on('-p', '--pretend', 'Run but do not make any changes.') { |v| options[:pretend] = v }
129 opt.on('-f', '--force', 'Overwrite files that already exist.') { options[:collision] = :force }
130 opt.on('-s', '--skip', 'Skip files that already exist.') { options[:collision] = :skip }
131 opt.on('-q', '--quiet', 'Suppress normal output.') { |v| options[:quiet] = v }
132 opt.on('-t', '--backtrace', 'Debugging: show backtrace on errors.') { |v| options[:backtrace] = v }
133 opt.on('-c', '--svn', 'Modify files with subversion. (Note: svn must be in path)') do
134 options[:svn] = `svn status`.inject({}) do |opt, e|
135 opt[e.chomp[7..-1]] = true
136 opt
137 end
138 end
139 opt.on('-g', '--git', 'Modify files with git. (Note: git must be in path)') do
140 options[:git] = `git status`.inject({:new => {}, :modified => {}}) do |opt, e|
141 opt[:new][e.chomp[14..-1]] = true if e =~ /new file:/
142 opt[:modified][e.chomp[14..-1]] = true if e =~ /modified:/
143 opt
144 end
145 end
146 end
147
148 end
149 end
150 end