mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Allow railties to specify generators paths.
This commit is contained in:
parent
9acf0af544
commit
4ca9765088
5 changed files with 67 additions and 15 deletions
|
@ -8,7 +8,7 @@ module Rails
|
|||
class << self
|
||||
attr_writer :config
|
||||
alias configure class_eval
|
||||
delegate :initialize!, :load_tasks, :root, :to => :instance
|
||||
delegate :initialize!, :load_tasks, :load_generators, :root, :to => :instance
|
||||
|
||||
private :new
|
||||
def instance
|
||||
|
@ -82,6 +82,10 @@ module Rails
|
|||
end
|
||||
end
|
||||
|
||||
def load_generators
|
||||
plugins.each { |p| p.load_generators }
|
||||
end
|
||||
|
||||
def initializers
|
||||
initializers = Bootstrap.new(self).initializers
|
||||
plugins.each { |p| initializers += p.initializers }
|
||||
|
|
|
@ -168,7 +168,7 @@ module Rails
|
|||
|
||||
# Show help message with available generators.
|
||||
def self.help
|
||||
traverse_load_paths!
|
||||
lookup!
|
||||
|
||||
namespaces = subclasses.map{ |k| k.namespace }
|
||||
namespaces.sort!
|
||||
|
@ -226,22 +226,10 @@ module Rails
|
|||
nil
|
||||
end
|
||||
|
||||
# This will try to load any generator in the load path to show in help.
|
||||
def self.traverse_load_paths! #:nodoc:
|
||||
$LOAD_PATH.each do |base|
|
||||
Dir[File.join(base, "{generators,rails_generators}", "**", "*_generator.rb")].each do |path|
|
||||
begin
|
||||
require path
|
||||
rescue Exception => e
|
||||
# No problem
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Receives namespaces in an array and tries to find matching generators
|
||||
# in the load path.
|
||||
def self.lookup(namespaces) #:nodoc:
|
||||
load_generators_from_railties!
|
||||
paths = namespaces_to_paths(namespaces)
|
||||
|
||||
paths.each do |path|
|
||||
|
@ -261,6 +249,28 @@ module Rails
|
|||
end
|
||||
end
|
||||
|
||||
# This will try to load any generator in the load path to show in help.
|
||||
def self.lookup! #:nodoc:
|
||||
load_generators_from_railties!
|
||||
|
||||
$LOAD_PATH.each do |base|
|
||||
Dir[File.join(base, "{generators,rails_generators}", "**", "*_generator.rb")].each do |path|
|
||||
begin
|
||||
require path
|
||||
rescue Exception => e
|
||||
# No problem
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Allow generators to be loaded from custom paths.
|
||||
def self.load_generators_from_railties! #:nodoc:
|
||||
return if defined?(@generators_from_railties) || Rails.application.nil?
|
||||
@generators_from_railties = true
|
||||
Rails.application.load_generators
|
||||
end
|
||||
|
||||
# Convert namespaces to paths by replacing ":" for "/" and adding
|
||||
# an extra lookup. For example, "rails:model" should be searched
|
||||
# in both: "rails/model/model_generator" and "rails/model_generator".
|
||||
|
|
|
@ -35,13 +35,28 @@ module Rails
|
|||
@rake_tasks
|
||||
end
|
||||
|
||||
def self.generators(&blk)
|
||||
@generators ||= []
|
||||
@generators << blk if blk
|
||||
@generators
|
||||
end
|
||||
|
||||
def rake_tasks
|
||||
self.class.rake_tasks
|
||||
end
|
||||
|
||||
def generators
|
||||
self.class.generators
|
||||
end
|
||||
|
||||
def load_tasks
|
||||
return unless rake_tasks
|
||||
rake_tasks.each { |blk| blk.call }
|
||||
end
|
||||
|
||||
def load_generators
|
||||
return unless generators
|
||||
generators.each { |blk| blk.call }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -148,6 +148,13 @@ class GeneratorsTest < Rails::Generators::TestCase
|
|||
Rails::Generators.subclasses.delete(klass)
|
||||
end
|
||||
|
||||
def test_load_generators_from_railties
|
||||
Rails::Generators::ModelGenerator.expects(:start).with(["Account"], {})
|
||||
Rails::Generators.send(:remove_instance_variable, :@generators_from_railties)
|
||||
Rails.application.expects(:load_generators)
|
||||
Rails::Generators.invoke("model", ["Account"])
|
||||
end
|
||||
|
||||
def test_rails_root_templates
|
||||
template = File.join(Rails.root, "lib", "templates", "active_record", "model", "model.rb")
|
||||
|
||||
|
|
|
@ -30,6 +30,22 @@ module PluginsTest
|
|||
AppTemplate::Application.load_tasks
|
||||
assert $ran_block
|
||||
end
|
||||
|
||||
test "generators block is executed when MyApp.load_generators is called" do
|
||||
$ran_block = false
|
||||
|
||||
class MyTie < Rails::Railtie
|
||||
generators do
|
||||
$ran_block = true
|
||||
end
|
||||
end
|
||||
|
||||
require "#{app_path}/config/environment"
|
||||
|
||||
assert !$ran_block
|
||||
AppTemplate::Application.load_generators
|
||||
assert $ran_block
|
||||
end
|
||||
end
|
||||
|
||||
class ActiveRecordExtensionTest < Test::Unit::TestCase
|
||||
|
|
Loading…
Reference in a new issue