6 def self.included(base
)
7 base
.extend(ClassMethods
)
9 if respond_to
?(:inherited)
10 alias_method
:inherited_without_options, :inherited
12 alias_method
:inherited, :inherited_with_options
17 def inherited_with_options(sub
)
18 inherited_without_options(sub
) if respond_to
?(:inherited_without_options)
19 sub
.extend(Rails
::Generator::Options::ClassMethods)
22 def mandatory_options(options
= nil)
24 write_inheritable_attribute(:mandatory_options, options
)
26 read_inheritable_attribute(:mandatory_options) or write_inheritable_attribute(:mandatory_options, {})
30 def default_options(options
= nil)
32 write_inheritable_attribute(:default_options, options
)
34 read_inheritable_attribute(:default_options) or write_inheritable_attribute(:default_options, {})
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
)
48 # Each instance has an options hash that's populated by #parse.
55 # Convenient access to class mandatory options.
57 self.class.mandatory_options
60 # Convenient access to class default options.
62 self.class.default_options
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
))
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:
79 # #add_general_options! General Options:
81 def parse
!(args
, runtime_options
= {})
84 @option_parser = OptionParser
.new
do |opt
|
87 add_general_options
!(opt
)
93 self.options
= full_options(runtime_options
)
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}"
106 # Override with your own usage banner.
108 "Usage: #{$0} [options]"
111 # Override to add your options to the parser:
112 # def add_options!(opt)
113 # opt.on('-v', '--verbose') { |value| options[:verbose] = value }
115 def add_options
!(opt
)
118 # Adds general options like -h and --quiet. Usually don't override.
119 def add_general_options
!(opt
)
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
}
126 opt
.separator
'General Options:'
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
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:/