shattered_ruby 0.5.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/console +4 -0
- data/bin/destroy +9 -0
- data/bin/generate +7 -0
- data/bin/runner +9 -0
- data/bin/shatter +22 -0
- data/lib/commands/console.rb +23 -0
- data/lib/game_loader.rb +118 -0
- data/lib/rails_generator/base.rb +203 -0
- data/lib/rails_generator/commands.rb +519 -0
- data/lib/rails_generator/generators/applications/shattered_app/USAGE +10 -0
- data/lib/rails_generator/generators/applications/shattered_app/shattered_app_generator.rb +103 -0
- data/lib/rails_generator/generators/components/actor/actor_generator.rb +22 -0
- data/lib/rails_generator/generators/components/actor/templates/actor.rb +0 -0
- data/lib/rails_generator/generators/components/model/USAGE +17 -0
- data/lib/rails_generator/generators/components/model/model_generator.rb +22 -0
- data/lib/rails_generator/generators/components/model/templates/fixtures.yml +5 -0
- data/lib/rails_generator/generators/components/model/templates/model.rb +2 -0
- data/lib/rails_generator/generators/components/model/templates/unit_test.rb +11 -0
- data/lib/rails_generator/generators/components/state/USAGE +30 -0
- data/lib/rails_generator/generators/components/state/state_generator.rb +19 -0
- data/lib/rails_generator/generators/components/state/templates/state.rb +31 -0
- data/lib/rails_generator/generators/components/view/USAGE +30 -0
- data/lib/rails_generator/generators/components/view/templates/material +4 -0
- data/lib/rails_generator/generators/components/view/templates/view.rb +9 -0
- data/lib/rails_generator/generators/components/view/view_generator.rb +28 -0
- data/lib/rails_generator/lookup.rb +209 -0
- data/lib/rails_generator/manifest.rb +53 -0
- data/lib/rails_generator/options.rb +143 -0
- data/lib/rails_generator/scripts/destroy.rb +7 -0
- data/lib/rails_generator/scripts/generate.rb +7 -0
- data/lib/rails_generator/scripts/update.rb +12 -0
- data/lib/rails_generator/scripts.rb +83 -0
- data/lib/rails_generator/simple_logger.rb +46 -0
- data/lib/rails_generator/spec.rb +44 -0
- data/lib/rails_generator.rb +43 -0
- data/lib/shatter.rb +7 -0
- data/lib/tasks/documentation.rake +46 -0
- data/lib/tasks/framework.rake +80 -0
- data/lib/tasks/log.rake +9 -0
- data/lib/tasks/misc.rake +4 -0
- data/lib/tasks/pre_namespace_aliases.rake +28 -0
- data/lib/tasks/shattered.rb +6 -0
- data/lib/tasks/statistics.rake +17 -0
- data/lib/tasks/testing.rake +102 -0
- data/lib/templates/MIT-LICENSE +20 -0
- data/lib/templates/README +35 -0
- data/lib/templates/Rakefile +11 -0
- data/lib/templates/configs/boot.rb +35 -0
- data/lib/templates/configs/empty.log +0 -0
- data/lib/templates/configs/ogre.cfg +4 -0
- data/lib/templates/configs/ogre_plugins.windows.cfg +14 -0
- data/lib/templates/configs/runner.rb +5 -0
- data/lib/templates/doc/README_FOR_APP +2 -0
- data/lib/templates/environments/environment.rb +10 -0
- data/lib/templates/media/basic.rmaterial +18 -0
- data/lib/templates/media/default.mesh +0 -0
- data/lib/templates/media/default.png +0 -0
- data/lib/templates/media/offset_map.rmaterial +117 -0
- data/lib/templates/test/test_helper.rb +5 -0
- metadata +153 -0
@@ -0,0 +1,22 @@
|
|
1
|
+
class ModelGenerator < Rails::Generator::NamedBase #:nodoc:
|
2
|
+
def manifest
|
3
|
+
record do |m|
|
4
|
+
self.class.check_collisions(m, class_path, class_name)
|
5
|
+
self.class.generate(m, class_path, file_name)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.check_collisions(m, class_path, class_name)
|
10
|
+
# Check for class naming collisions.
|
11
|
+
m.class_collisions class_path, class_name, "#{class_name}Test"
|
12
|
+
end
|
13
|
+
def self.generate(m, class_path, file_name)
|
14
|
+
# Model, test, and fixture directories.
|
15
|
+
m.directory File.join('app/models', class_path)
|
16
|
+
m.directory File.join('test/unit', class_path)
|
17
|
+
|
18
|
+
# Model class, unit test, and fixtures.
|
19
|
+
m.template '../../model/templates/model.rb', File.join('app/models', class_path, "#{file_name}.rb")
|
20
|
+
m.template '../../model/templates/unit_test.rb', File.join('test/unit', class_path, "#{file_name}_test.rb")
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
Description:
|
2
|
+
The controller generator creates stubs for a new controller and its views.
|
3
|
+
|
4
|
+
The generator takes a controller name and a list of views as arguments.
|
5
|
+
The controller name may be given in CamelCase or under_score and should
|
6
|
+
not be suffixed with 'Controller'. To create a controller within a
|
7
|
+
module, specify the controller name as 'module/controller'.
|
8
|
+
|
9
|
+
The generator creates a controller class in app/controllers with view
|
10
|
+
templates in app/views/controller_name, a helper class in app/helpers,
|
11
|
+
and a functional test suite in test/functional.
|
12
|
+
|
13
|
+
Example:
|
14
|
+
./script/generate controller CreditCard open debit credit close
|
15
|
+
|
16
|
+
Credit card controller with URLs like /credit_card/debit.
|
17
|
+
Controller: app/controllers/credit_card_controller.rb
|
18
|
+
Views: app/views/credit_card/debit.rhtml [...]
|
19
|
+
Helper: app/helpers/credit_card_helper.rb
|
20
|
+
Test: test/functional/credit_card_controller_test.rb
|
21
|
+
|
22
|
+
Modules Example:
|
23
|
+
./script/generate controller 'admin/credit_card' suspend late_fee
|
24
|
+
|
25
|
+
Credit card admin controller with URLs /admin/credit_card/suspend.
|
26
|
+
Controller: app/controllers/admin/credit_card_controller.rb
|
27
|
+
Views: app/views/admin/credit_card/debit.rhtml [...]
|
28
|
+
Helper: app/helpers/admin/credit_card_helper.rb
|
29
|
+
Test: test/functional/admin/credit_card_controller_test.rb
|
30
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class StateGenerator < Rails::Generator::NamedBase #:nodoc:
|
2
|
+
def manifest
|
3
|
+
record do |m|
|
4
|
+
# Check for class naming collisions.
|
5
|
+
m.class_collisions class_path, "#{class_name}State"
|
6
|
+
|
7
|
+
# Controller, helper, views, and test directories.
|
8
|
+
m.directory File.join('app/states', class_path)
|
9
|
+
|
10
|
+
# Controller class, functional test, and helper class.
|
11
|
+
m.template 'state.rb',
|
12
|
+
File.join('app/states',
|
13
|
+
class_path,
|
14
|
+
"#{file_name}_state.rb")
|
15
|
+
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class <%= class_name %>State < ShatteredState::Base
|
2
|
+
key :pressed => :escape, :action => :quit
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
# The different types of scene managers provided by Ogre are:
|
6
|
+
# :general, :terrain, :nature, :paging, :indoor
|
7
|
+
@scene_manager = create_scene_manager :general
|
8
|
+
|
9
|
+
setup_camera
|
10
|
+
setup_viewport
|
11
|
+
end
|
12
|
+
|
13
|
+
# Cameras are the renderers of the world.
|
14
|
+
# Scene managers can have many cameras - for example, in a first person racing game you could have
|
15
|
+
# a camera facing forward, and a camera facing backwards, rendering to the rear view window.
|
16
|
+
def setup_camera
|
17
|
+
@camera = scene_manager.create_camera("camera")
|
18
|
+
@camera.set_near_clip_distance 1
|
19
|
+
@camera.set_far_clip_distance 10000
|
20
|
+
@camera.position = v(0,0,-10)
|
21
|
+
@camera.look_at v(0,0,0)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Viewports are your windows into the world.
|
25
|
+
# Cameras can have many viewports, but usually the relationship is 1-1.
|
26
|
+
def setup_viewport
|
27
|
+
@viewport = create_viewport(@camera)
|
28
|
+
@viewport.set_background_colour Ogre::ColourValue.new(0.2, 0.2, 0.2)
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
Description:
|
2
|
+
The controller generator creates stubs for a new controller and its views.
|
3
|
+
|
4
|
+
The generator takes a controller name and a list of views as arguments.
|
5
|
+
The controller name may be given in CamelCase or under_score and should
|
6
|
+
not be suffixed with 'Controller'. To create a controller within a
|
7
|
+
module, specify the controller name as 'module/controller'.
|
8
|
+
|
9
|
+
The generator creates a controller class in app/controllers with view
|
10
|
+
templates in app/views/controller_name, a helper class in app/helpers,
|
11
|
+
and a functional test suite in test/functional.
|
12
|
+
|
13
|
+
Example:
|
14
|
+
./script/generate controller CreditCard open debit credit close
|
15
|
+
|
16
|
+
Credit card controller with URLs like /credit_card/debit.
|
17
|
+
Controller: app/controllers/credit_card_controller.rb
|
18
|
+
Views: app/views/credit_card/debit.rhtml [...]
|
19
|
+
Helper: app/helpers/credit_card_helper.rb
|
20
|
+
Test: test/functional/credit_card_controller_test.rb
|
21
|
+
|
22
|
+
Modules Example:
|
23
|
+
./script/generate controller 'admin/credit_card' suspend late_fee
|
24
|
+
|
25
|
+
Credit card admin controller with URLs /admin/credit_card/suspend.
|
26
|
+
Controller: app/controllers/admin/credit_card_controller.rb
|
27
|
+
Views: app/views/admin/credit_card/debit.rhtml [...]
|
28
|
+
Helper: app/helpers/admin/credit_card_helper.rb
|
29
|
+
Test: test/functional/admin/credit_card_controller_test.rb
|
30
|
+
|
@@ -0,0 +1,9 @@
|
|
1
|
+
class <%= class_name %>View < ShatteredView::Base
|
2
|
+
material "<%= file_name %>_material", :template => "basic", :texture => "<%= file_name %>.png"
|
3
|
+
mesh "<%= file_name %>", :material => :<%= file_name %>_material
|
4
|
+
|
5
|
+
# View works by executing after any model event
|
6
|
+
# This initialize will work after the model's initialize
|
7
|
+
def initialize
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class ViewGenerator < Rails::Generator::NamedBase #:nodoc:
|
2
|
+
def manifest
|
3
|
+
record do |m|
|
4
|
+
self.class.check_collisions(m, class_path, class_name)
|
5
|
+
self.class.generate(m, class_path, file_name)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.check_collisions(m, class_path, class_name)
|
10
|
+
# Check for class naming collisions.
|
11
|
+
m.class_collisions class_path, "#{class_name}View"
|
12
|
+
end
|
13
|
+
def self.generate(m, class_path, file_name)
|
14
|
+
# Controller, helper, views, and test directories.
|
15
|
+
m.directory File.join('app/views', class_path)
|
16
|
+
m.directory File.join('app/media', class_path, file_name)
|
17
|
+
|
18
|
+
# Controller class, functional test, and helper class.
|
19
|
+
m.template '../../view/templates/view.rb',
|
20
|
+
File.join('app/views',
|
21
|
+
class_path,
|
22
|
+
"#{file_name}_view.rb")
|
23
|
+
m.file "../../../../../templates/media/default.mesh",
|
24
|
+
File.join( 'app/media', class_path, file_name, "#{file_name}.mesh")
|
25
|
+
m.file "../../../../../templates/media/default.png",
|
26
|
+
File.join( 'app/media', class_path, file_name, "#{file_name}.png")
|
27
|
+
end
|
28
|
+
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 SHATTERED_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 SHATTERED_ROOT is defined we know we're
|
93
|
+
# generating in the context of a Rails application, so search
|
94
|
+
# SHATTERED_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? ::SHATTERED_ROOT
|
101
|
+
sources << PathSource.new(:lib, "#{::SHATTERED_ROOT}/lib/generators")
|
102
|
+
sources << PathSource.new(:vendor, "#{::SHATTERED_ROOT}/vendor/generators")
|
103
|
+
sources << PathSource.new(:plugins, "#{::SHATTERED_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
|