tap 0.7.9 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History +28 -0
- data/MIT-LICENSE +1 -1
- data/README +71 -43
- data/Rakefile +81 -64
- data/Tutorial +235 -0
- data/bin/tap +80 -44
- data/lib/tap.rb +41 -12
- data/lib/tap/app.rb +243 -246
- data/lib/tap/file_task.rb +357 -118
- data/lib/tap/generator.rb +88 -29
- data/lib/tap/generator/generators/config/config_generator.rb +4 -2
- data/lib/tap/generator/generators/config/templates/config.erb +1 -2
- data/lib/tap/generator/generators/file_task/file_task_generator.rb +3 -18
- data/lib/tap/generator/generators/file_task/templates/task.erb +22 -15
- data/lib/tap/generator/generators/file_task/templates/test.erb +13 -2
- data/{test/test/inference_methods/test_assert_files_exist/input/input_1.txt → lib/tap/generator/generators/generator/USAGE} +0 -0
- data/lib/tap/generator/generators/generator/generator_generator.rb +21 -0
- data/lib/tap/generator/generators/generator/templates/generator.erb +23 -0
- data/lib/tap/generator/generators/generator/templates/usage.erb +1 -0
- data/{test/test/inference_methods/test_assert_files_exist/input/input_2.txt → lib/tap/generator/generators/package/USAGE} +0 -0
- data/lib/tap/generator/generators/package/package_generator.rb +38 -0
- data/lib/tap/generator/generators/package/templates/package.erb +186 -0
- data/lib/tap/generator/generators/root/root_generator.rb +14 -9
- data/lib/tap/generator/generators/root/templates/Rakefile +20 -14
- data/{test/test/inference_methods/test_infer_glob/expected/file.yml → lib/tap/generator/generators/root/templates/ReadMe.txt} +0 -0
- data/lib/tap/generator/generators/root/templates/tap.yml +82 -0
- data/lib/tap/generator/generators/root/templates/test/tap_test_helper.rb +0 -1
- data/lib/tap/generator/generators/root/templates/test/tap_test_suite.rb +2 -1
- data/{test/test/inference_methods/test_infer_glob/expected/file_1.txt → lib/tap/generator/generators/script/USAGE} +0 -0
- data/lib/tap/generator/generators/script/script_generator.rb +17 -0
- data/lib/tap/generator/generators/script/templates/script.erb +42 -0
- data/lib/tap/generator/generators/task/task_generator.rb +1 -1
- data/lib/tap/generator/generators/task/templates/task.erb +24 -16
- data/lib/tap/generator/generators/task/templates/test.erb +13 -17
- data/lib/tap/generator/generators/workflow/templates/task.erb +10 -10
- data/lib/tap/generator/generators/workflow/templates/test.erb +1 -1
- data/lib/tap/generator/generators/workflow/workflow_generator.rb +3 -18
- data/lib/tap/root.rb +108 -146
- data/lib/tap/script.rb +362 -0
- data/lib/tap/script/console.rb +28 -0
- data/lib/tap/script/destroy.rb +13 -1
- data/lib/tap/script/generate.rb +13 -1
- data/lib/tap/script/run.rb +100 -57
- data/lib/tap/support/batch_queue.rb +0 -3
- data/lib/tap/support/logger.rb +6 -3
- data/lib/tap/support/rake.rb +54 -0
- data/lib/tap/support/task_configuration.rb +169 -0
- data/lib/tap/support/tdoc.rb +198 -0
- data/lib/tap/support/tdoc/config_attr.rb +338 -0
- data/lib/tap/support/tdoc/tdoc_html_generator.rb +38 -0
- data/lib/tap/support/tdoc/tdoc_html_template.rb +42 -0
- data/lib/tap/support/versions.rb +33 -1
- data/lib/tap/task.rb +339 -227
- data/lib/tap/test.rb +86 -128
- data/lib/tap/test/env_vars.rb +16 -5
- data/lib/tap/test/file_methods.rb +373 -0
- data/lib/tap/test/subset_methods.rb +299 -180
- data/lib/tap/version.rb +2 -1
- data/lib/tap/workflow.rb +2 -0
- data/test/app/lib/app_test_task.rb +1 -0
- data/test/app_test.rb +327 -83
- data/test/check/binding_eval.rb +23 -0
- data/test/check/define_method_check.rb +22 -0
- data/test/check/dependencies_check.rb +175 -0
- data/test/check/inheritance_check.rb +22 -0
- data/test/file_task_test.rb +524 -291
- data/test/{test/inference_methods/test_infer_glob/expected/file_2.txt → root/glob/one.txt} +0 -0
- data/test/root/glob/two.txt +0 -0
- data/test/root_test.rb +330 -262
- data/test/script_test.rb +194 -0
- data/test/support/audit_test.rb +5 -2
- data/test/support/combinator_test.rb +10 -10
- data/test/support/rake_test.rb +35 -0
- data/test/support/task_configuration_test.rb +272 -0
- data/test/support/tdoc_test.rb +363 -0
- data/test/support/templater_test.rb +2 -2
- data/test/support/versions_test.rb +32 -0
- data/test/tap_test_helper.rb +39 -0
- data/test/task_base_test.rb +115 -0
- data/test/task_class_test.rb +56 -4
- data/test/task_execute_test.rb +29 -0
- data/test/task_test.rb +89 -70
- data/test/test/env_vars_test.rb +48 -0
- data/test/test/{inference_methods → file_methods}/test_assert_expected/expected/file.txt +0 -0
- data/test/test/{inference_methods → file_methods}/test_assert_expected/expected/folder/file.txt +0 -0
- data/test/test/{inference_methods → file_methods}/test_assert_expected/input/file.txt +0 -0
- data/test/test/{inference_methods → file_methods}/test_assert_expected/input/folder/file.txt +0 -0
- data/test/test/file_methods/test_assert_files_exist/input/input_1.txt +0 -0
- data/test/test/file_methods/test_assert_files_exist/input/input_2.txt +0 -0
- data/test/test/file_methods/test_assert_output_files_equal/expected/one.txt +1 -0
- data/test/test/file_methods/test_assert_output_files_equal/expected/two.txt +1 -0
- data/test/test/file_methods/test_assert_output_files_equal/input/one.txt +1 -0
- data/test/test/file_methods/test_assert_output_files_equal/input/two.txt +1 -0
- data/test/test/{inference_methods → file_methods}/test_file_compare/expected/output_1.txt +0 -0
- data/test/test/{inference_methods → file_methods}/test_file_compare/expected/output_2.txt +0 -0
- data/test/test/{inference_methods → file_methods}/test_file_compare/input/input_1.txt +0 -0
- data/test/test/{inference_methods → file_methods}/test_file_compare/input/input_2.txt +0 -0
- data/test/test/file_methods/test_infer_glob/expected/file.yml +0 -0
- data/test/test/file_methods/test_infer_glob/expected/file_1.txt +0 -0
- data/test/test/file_methods/test_infer_glob/expected/file_2.txt +0 -0
- data/test/test/file_methods/test_method_glob/expected/file.yml +0 -0
- data/test/test/file_methods/test_method_glob/expected/file_1.txt +0 -0
- data/test/test/file_methods/test_method_glob/expected/file_2.txt +0 -0
- data/test/test/{inference_methods → file_methods}/test_yml_compare/expected/output_1.yml +0 -0
- data/test/test/{inference_methods → file_methods}/test_yml_compare/expected/output_2.yml +0 -0
- data/test/test/{inference_methods → file_methods}/test_yml_compare/input/input_1.yml +0 -0
- data/test/test/{inference_methods → file_methods}/test_yml_compare/input/input_2.yml +0 -0
- data/test/test/file_methods_test.rb +204 -0
- data/test/test/subset_methods_test.rb +93 -33
- data/test/test/test_assert_expected_result_files/expected/task/name/a.txt +1 -0
- data/test/test/test_assert_expected_result_files/expected/task/name/b.txt +1 -0
- data/test/test/test_assert_expected_result_files/input/a.txt +1 -0
- data/test/test/test_assert_expected_result_files/input/b.txt +1 -0
- data/test/test/test_file_task_test/expected/one.txt +1 -0
- data/test/test/test_file_task_test/expected/two.txt +1 -0
- data/test/test/test_file_task_test/input/one.txt +1 -0
- data/test/test/test_file_task_test/input/two.txt +1 -0
- data/test/test_test.rb +143 -3
- data/test/workflow_test.rb +2 -0
- data/vendor/rails_generator.rb +56 -0
- data/vendor/rails_generator/base.rb +263 -0
- data/vendor/rails_generator/commands.rb +581 -0
- data/vendor/rails_generator/generated_attribute.rb +42 -0
- data/vendor/rails_generator/lookup.rb +209 -0
- data/vendor/rails_generator/manifest.rb +53 -0
- data/vendor/rails_generator/options.rb +143 -0
- data/vendor/rails_generator/scripts.rb +83 -0
- data/vendor/rails_generator/scripts/destroy.rb +7 -0
- data/vendor/rails_generator/scripts/generate.rb +7 -0
- data/vendor/rails_generator/scripts/update.rb +12 -0
- data/vendor/rails_generator/simple_logger.rb +46 -0
- data/vendor/rails_generator/spec.rb +44 -0
- metadata +180 -196
- data/lib/tap/generator/generators/root/templates/app.yml +0 -19
- data/lib/tap/generator/generators/root/templates/config/process_tap_request.yml +0 -4
- data/lib/tap/generator/generators/root/templates/lib/process_tap_request.rb +0 -26
- data/lib/tap/generator/generators/root/templates/public/images/nav.jpg +0 -0
- data/lib/tap/generator/generators/root/templates/public/stylesheets/color.css +0 -57
- data/lib/tap/generator/generators/root/templates/public/stylesheets/layout.css +0 -108
- data/lib/tap/generator/generators/root/templates/public/stylesheets/normalize.css +0 -40
- data/lib/tap/generator/generators/root/templates/public/stylesheets/typography.css +0 -21
- data/lib/tap/generator/generators/root/templates/server/config/environment.rb +0 -60
- data/lib/tap/generator/generators/root/templates/server/lib/tasks/clear_database_prerequisites.rake +0 -5
- data/lib/tap/generator/generators/root/templates/server/test/test_helper.rb +0 -53
- data/lib/tap/script/server.rb +0 -12
- data/lib/tap/support/rap.rb +0 -38
- data/lib/tap/test/inference_methods.rb +0 -298
- data/test/task/config/task_with_config.yml +0 -1
- data/test/test/inference_methods_test.rb +0 -311
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
|
3
|
+
module Rails
|
4
|
+
module Generator
|
5
|
+
class GeneratedAttribute
|
6
|
+
attr_accessor :name, :type, :column
|
7
|
+
|
8
|
+
def initialize(name, type)
|
9
|
+
@name, @type = name, type.to_sym
|
10
|
+
@column = ActiveRecord::ConnectionAdapters::Column.new(name, nil, @type)
|
11
|
+
end
|
12
|
+
|
13
|
+
def field_type
|
14
|
+
@field_type ||= case type
|
15
|
+
when :integer, :float, :decimal then :text_field
|
16
|
+
when :datetime, :timestamp, :time then :datetime_select
|
17
|
+
when :date then :date_select
|
18
|
+
when :string then :text_field
|
19
|
+
when :text then :text_area
|
20
|
+
when :boolean then :check_box
|
21
|
+
else
|
22
|
+
:text_field
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def default
|
27
|
+
@default ||= case type
|
28
|
+
when :integer then 1
|
29
|
+
when :float then 1.5
|
30
|
+
when :decimal then "9.99"
|
31
|
+
when :datetime, :timestamp, :time then Time.now.to_s(:db)
|
32
|
+
when :date then Date.today.to_s(:db)
|
33
|
+
when :string then "MyString"
|
34
|
+
when :text then "MyText"
|
35
|
+
when :boolean then false
|
36
|
+
else
|
37
|
+
""
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,209 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec'
|
2
|
+
|
3
|
+
class Object
|
4
|
+
class << self
|
5
|
+
# Lookup missing generators using const_missing. This allows any
|
6
|
+
# generator to reference another without having to know its location:
|
7
|
+
# RubyGems, ~/.rails/generators, and RAILS_ROOT/generators.
|
8
|
+
def lookup_missing_generator(class_id)
|
9
|
+
if md = /(.+)Generator$/.match(class_id.to_s)
|
10
|
+
name = md.captures.first.demodulize.underscore
|
11
|
+
Rails::Generator::Base.lookup(name).klass
|
12
|
+
else
|
13
|
+
const_missing_before_generators(class_id)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
unless respond_to?(:const_missing_before_generators)
|
18
|
+
alias_method :const_missing_before_generators, :const_missing
|
19
|
+
alias_method :const_missing, :lookup_missing_generator
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# User home directory lookup adapted from RubyGems.
|
25
|
+
def Dir.user_home
|
26
|
+
if ENV['HOME']
|
27
|
+
ENV['HOME']
|
28
|
+
elsif ENV['USERPROFILE']
|
29
|
+
ENV['USERPROFILE']
|
30
|
+
elsif ENV['HOMEDRIVE'] and ENV['HOMEPATH']
|
31
|
+
"#{ENV['HOMEDRIVE']}:#{ENV['HOMEPATH']}"
|
32
|
+
else
|
33
|
+
File.expand_path '~'
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
module Rails
|
39
|
+
module Generator
|
40
|
+
|
41
|
+
# Generator lookup is managed by a list of sources which return specs
|
42
|
+
# describing where to find and how to create generators. This module
|
43
|
+
# provides class methods for manipulating the source list and looking up
|
44
|
+
# generator specs, and an #instance wrapper for quickly instantiating
|
45
|
+
# generators by name.
|
46
|
+
#
|
47
|
+
# A spec is not a generator: it's a description of where to find
|
48
|
+
# the generator and how to create it. A source is anything that
|
49
|
+
# yields generators from #each. PathSource and GemSource are provided.
|
50
|
+
module Lookup
|
51
|
+
def self.included(base)
|
52
|
+
base.extend(ClassMethods)
|
53
|
+
base.use_component_sources!
|
54
|
+
end
|
55
|
+
|
56
|
+
# Convenience method to instantiate another generator.
|
57
|
+
def instance(generator_name, args, runtime_options = {})
|
58
|
+
self.class.instance(generator_name, args, runtime_options)
|
59
|
+
end
|
60
|
+
|
61
|
+
module ClassMethods
|
62
|
+
# The list of sources where we look, in order, for generators.
|
63
|
+
def sources
|
64
|
+
read_inheritable_attribute(:sources) or use_component_sources!
|
65
|
+
end
|
66
|
+
|
67
|
+
# Add a source to the end of the list.
|
68
|
+
def append_sources(*args)
|
69
|
+
sources.concat(args.flatten)
|
70
|
+
invalidate_cache!
|
71
|
+
end
|
72
|
+
|
73
|
+
# Add a source to the beginning of the list.
|
74
|
+
def prepend_sources(*args)
|
75
|
+
write_inheritable_array(:sources, args.flatten + sources)
|
76
|
+
invalidate_cache!
|
77
|
+
end
|
78
|
+
|
79
|
+
# Reset the source list.
|
80
|
+
def reset_sources
|
81
|
+
write_inheritable_attribute(:sources, [])
|
82
|
+
invalidate_cache!
|
83
|
+
end
|
84
|
+
|
85
|
+
# Use application generators (app, ?).
|
86
|
+
def use_application_sources!
|
87
|
+
reset_sources
|
88
|
+
sources << PathSource.new(:builtin, "#{File.dirname(__FILE__)}/generators/applications")
|
89
|
+
end
|
90
|
+
|
91
|
+
# Use component generators (model, controller, etc).
|
92
|
+
# 1. Rails application. If RAILS_ROOT is defined we know we're
|
93
|
+
# generating in the context of a Rails application, so search
|
94
|
+
# RAILS_ROOT/generators.
|
95
|
+
# 2. User home directory. Search ~/.rails/generators.
|
96
|
+
# 3. RubyGems. Search for gems named *_generator.
|
97
|
+
# 4. Builtins. Model, controller, mailer, scaffold.
|
98
|
+
def use_component_sources!
|
99
|
+
reset_sources
|
100
|
+
if defined? ::RAILS_ROOT
|
101
|
+
sources << PathSource.new(:lib, "#{::RAILS_ROOT}/lib/generators")
|
102
|
+
sources << PathSource.new(:vendor, "#{::RAILS_ROOT}/vendor/generators")
|
103
|
+
sources << PathSource.new(:plugins, "#{::RAILS_ROOT}/vendor/plugins/**/generators")
|
104
|
+
end
|
105
|
+
sources << PathSource.new(:user, "#{Dir.user_home}/.rails/generators")
|
106
|
+
sources << GemSource.new if Object.const_defined?(:Gem)
|
107
|
+
sources << PathSource.new(:builtin, "#{File.dirname(__FILE__)}/generators/components")
|
108
|
+
end
|
109
|
+
|
110
|
+
# Lookup knows how to find generators' Specs from a list of Sources.
|
111
|
+
# Searches the sources, in order, for the first matching name.
|
112
|
+
def lookup(generator_name)
|
113
|
+
@found ||= {}
|
114
|
+
generator_name = generator_name.to_s.downcase
|
115
|
+
@found[generator_name] ||= cache.find { |spec| spec.name == generator_name }
|
116
|
+
unless @found[generator_name]
|
117
|
+
chars = generator_name.scan(/./).map{|c|"#{c}.*?"}
|
118
|
+
rx = /^#{chars}$/
|
119
|
+
gns = cache.select{|spec| spec.name =~ rx }
|
120
|
+
@found[generator_name] ||= gns.first if gns.length == 1
|
121
|
+
raise GeneratorError, "Pattern '#{generator_name}' matches more than one generator: #{gns.map{|sp|sp.name}.join(', ')}" if gns.length > 1
|
122
|
+
end
|
123
|
+
@found[generator_name] or raise GeneratorError, "Couldn't find '#{generator_name}' generator"
|
124
|
+
end
|
125
|
+
|
126
|
+
# Convenience method to lookup and instantiate a generator.
|
127
|
+
def instance(generator_name, args = [], runtime_options = {})
|
128
|
+
lookup(generator_name).klass.new(args, full_options(runtime_options))
|
129
|
+
end
|
130
|
+
|
131
|
+
private
|
132
|
+
# Lookup and cache every generator from the source list.
|
133
|
+
def cache
|
134
|
+
@cache ||= sources.inject([]) { |cache, source| cache + source.map }
|
135
|
+
end
|
136
|
+
|
137
|
+
# Clear the cache whenever the source list changes.
|
138
|
+
def invalidate_cache!
|
139
|
+
@cache = nil
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
# Sources enumerate (yield from #each) generator specs which describe
|
145
|
+
# where to find and how to create generators. Enumerable is mixed in so,
|
146
|
+
# for example, source.collect will retrieve every generator.
|
147
|
+
# Sources may be assigned a label to distinguish them.
|
148
|
+
class Source
|
149
|
+
include Enumerable
|
150
|
+
|
151
|
+
attr_reader :label
|
152
|
+
def initialize(label)
|
153
|
+
@label = label
|
154
|
+
end
|
155
|
+
|
156
|
+
# The each method must be implemented in subclasses.
|
157
|
+
# The base implementation raises an error.
|
158
|
+
def each
|
159
|
+
raise NotImplementedError
|
160
|
+
end
|
161
|
+
|
162
|
+
# Return a convenient sorted list of all generator names.
|
163
|
+
def names
|
164
|
+
map { |spec| spec.name }.sort
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
|
169
|
+
# PathSource looks for generators in a filesystem directory.
|
170
|
+
class PathSource < Source
|
171
|
+
attr_reader :path
|
172
|
+
|
173
|
+
def initialize(label, path)
|
174
|
+
super label
|
175
|
+
@path = path
|
176
|
+
end
|
177
|
+
|
178
|
+
# Yield each eligible subdirectory.
|
179
|
+
def each
|
180
|
+
Dir["#{path}/[a-z]*"].each do |dir|
|
181
|
+
if File.directory?(dir)
|
182
|
+
yield Spec.new(File.basename(dir), dir, label)
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
|
189
|
+
# GemSource hits the mines to quarry for generators. The latest versions
|
190
|
+
# of gems named *_generator are selected.
|
191
|
+
class GemSource < Source
|
192
|
+
def initialize
|
193
|
+
super :RubyGems
|
194
|
+
end
|
195
|
+
|
196
|
+
# Yield latest versions of generator gems.
|
197
|
+
def each
|
198
|
+
Gem::cache.search(/_generator$/).inject({}) { |latest, gem|
|
199
|
+
hem = latest[gem.name]
|
200
|
+
latest[gem.name] = gem if hem.nil? or gem.version > hem.version
|
201
|
+
latest
|
202
|
+
}.values.each { |gem|
|
203
|
+
yield Spec.new(gem.name.sub(/_generator$/, ''), gem.full_gem_path, label)
|
204
|
+
}
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
end
|
209
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Rails
|
2
|
+
module Generator
|
3
|
+
|
4
|
+
# Manifest captures the actions a generator performs. Instantiate
|
5
|
+
# a manifest with an optional target object, hammer it with actions,
|
6
|
+
# then replay or rewind on the object of your choice.
|
7
|
+
#
|
8
|
+
# Example:
|
9
|
+
# manifest = Manifest.new { |m|
|
10
|
+
# m.make_directory '/foo'
|
11
|
+
# m.create_file '/foo/bar.txt'
|
12
|
+
# }
|
13
|
+
# manifest.replay(creator)
|
14
|
+
# manifest.rewind(destroyer)
|
15
|
+
class Manifest
|
16
|
+
attr_reader :target
|
17
|
+
|
18
|
+
# Take a default action target. Yield self if block given.
|
19
|
+
def initialize(target = nil)
|
20
|
+
@target, @actions = target, []
|
21
|
+
yield self if block_given?
|
22
|
+
end
|
23
|
+
|
24
|
+
# Record an action.
|
25
|
+
def method_missing(action, *args, &block)
|
26
|
+
@actions << [action, args, block]
|
27
|
+
end
|
28
|
+
|
29
|
+
# Replay recorded actions.
|
30
|
+
def replay(target = nil)
|
31
|
+
send_actions(target || @target, @actions)
|
32
|
+
end
|
33
|
+
|
34
|
+
# Rewind recorded actions.
|
35
|
+
def rewind(target = nil)
|
36
|
+
send_actions(target || @target, @actions.reverse)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Erase recorded actions.
|
40
|
+
def erase
|
41
|
+
@actions = []
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
def send_actions(target, actions)
|
46
|
+
actions.each do |method, args, block|
|
47
|
+
target.send(method, *args, &block)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,143 @@
|
|
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
|
+
end
|
140
|
+
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|