1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #1356 from flippingbits/fix_engine_generator

Fix engine's generator
This commit is contained in:
José Valim 2011-06-09 00:01:00 -07:00
commit eb8c0a7b1a
18 changed files with 209 additions and 50 deletions

View file

@ -105,14 +105,6 @@ module Rails
self
end
def load_generators(app=self)
initialize_generators
railties.all { |r| r.load_generators(app) }
Rails::Generators.configure!(app.config.generators)
super
self
end
def load_console(app=self)
initialize_console
railties.all { |r| r.load_console(app) }
@ -199,10 +191,6 @@ module Rails
end
end
def initialize_generators
require "rails/generators"
end
def initialize_console
require "pp"
require "rails/console/app"

View file

@ -23,11 +23,7 @@ when 'generate', 'destroy', 'plugin'
require APP_PATH
Rails.application.require_environment!
if defined?(ENGINE_PATH) && engine = Rails::Engine.find(ENGINE_PATH)
Rails.application.load_generators(engine)
else
Rails.application.load_generators
end
Rails.application.load_generators
require "rails/commands/#{command}"
end

View file

@ -7,4 +7,6 @@ if ARGV.first.in?([nil, "-h", "--help"])
end
name = ARGV.shift
Rails::Generators.invoke name, ARGV, :behavior => :invoke, :destination_root => Rails.root
root = defined?(ENGINE_ROOT) ? ENGINE_ROOT : Rails.root
Rails::Generators.invoke name, ARGV, :behavior => :invoke, :destination_root => root

View file

@ -330,6 +330,14 @@ module Rails
autoload :Configuration, "rails/engine/configuration"
autoload :Railties, "rails/engine/railties"
def load_generators(app=self)
initialize_generators
railties.all { |r| r.load_generators(app) }
Rails::Generators.configure!(app.config.generators)
super
self
end
class << self
attr_accessor :called_from, :isolated
alias :isolated? :isolated
@ -567,6 +575,10 @@ module Rails
protected
def initialize_generators
require "rails/generators"
end
def routes?
defined?(@routes)
end

View file

@ -0,0 +1,38 @@
require 'active_support/core_ext/object/inclusion'
ARGV << '--help' if ARGV.empty?
aliases = {
"g" => "generate"
}
command = ARGV.shift
command = aliases[command] || command
require ENGINE_PATH
engine = ::Rails::Engine.find(ENGINE_ROOT)
case command
when 'generate', 'destroy'
require 'rails/generators'
Rails::Generators.namespace = engine.railtie_namespace
engine.load_generators
require "rails/commands/#{command}"
when '--version', '-v'
ARGV.unshift '--version'
require 'rails/commands/application'
else
puts "Error: Command not recognized" unless command.in?(['-h', '--help'])
puts <<-EOT
Usage: rails COMMAND [ARGS]
The common rails commands available for engines are:
generate Generate new code (short-cut alias: "g")
destroy Undo code generated with "generate"
All commands can be run with -h for more information.
EOT
exit(1)
end

View file

@ -20,6 +20,8 @@ module Rails
autoload :ResourceHelpers, 'rails/generators/resource_helpers'
autoload :TestCase, 'rails/generators/test_case'
mattr_accessor :namespace
DEFAULT_ALIASES = {
:rails => {
:actions => '-a',

View file

@ -63,9 +63,7 @@ module Rails
end
def namespace
@namespace ||= if defined?(Rails) && Rails.application
Rails.application.class.parents.detect { |n| n.respond_to?(:_railtie) }
end
Rails::Generators.namespace
end
def namespaced?

View file

@ -126,6 +126,8 @@ task :default => :test
end
def script(force = false)
return unless full?
directory "script", :force => force do |content|
"#{shebang}\n" + content
end

View file

@ -1,4 +1,7 @@
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
ENGINE_PATH = File.expand_path('../..', __FILE__)
load File.expand_path('../../<%= dummy_path %>/script/rails', __FILE__)
ENGINE_ROOT = File.expand_path('../..', __FILE__)
ENGINE_PATH = File.expand_path('../../lib/<%= name -%>/engine', __FILE__)
require 'rails/all'
require 'rails/engine/commands'

View file

@ -1,6 +1,7 @@
require 'rails/initializable'
require 'rails/configuration'
require 'active_support/inflector'
require 'active_support/core_ext/module/introspection'
module Rails
# Railtie is the core of the Rails framework and provides several hooks to extend
@ -192,5 +193,9 @@ module Rails
def load_generators(app)
self.class.generators.each { |block| block.call(app) }
end
def railtie_namespace
@railtie_namespace ||= self.class.parents.detect { |n| n.respond_to?(:_railtie) }
end
end
end

View file

@ -10,7 +10,6 @@ require 'active_support/core_ext/logger'
require 'action_controller'
require 'rails/all'
# TODO: Remove these hacks
module TestApp
class Application < Rails::Application
config.root = File.dirname(__FILE__)

View file

@ -7,15 +7,7 @@ require 'rails/generators/rails/scaffold/scaffold_generator'
class NamespacedGeneratorTestCase < Rails::Generators::TestCase
def setup
TestApp::Application.isolate_namespace(TestApp)
end
def teardown
if TestApp.respond_to?(:_railtie)
TestApp.singleton_class.send(:undef_method, :_railtie)
TestApp.singleton_class.send(:undef_method, :table_name_prefix)
TestApp::Application.isolated = false
end
Rails::Generators.namespace = TestApp
end
end

View file

@ -12,7 +12,6 @@ DEFAULT_PLUGIN_FILES = %w(
lib
lib/bukkits.rb
lib/tasks/bukkits_tasks.rake
script/rails
test/bukkits_test.rb
test/test_helper.rb
test/dummy
@ -172,6 +171,7 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase
assert_file "config/routes.rb", /Rails.application.routes.draw do/
assert_file "lib/bukkits/engine.rb", /module Bukkits\n class Engine < ::Rails::Engine\n end\nend/
assert_file "lib/bukkits.rb", /require "bukkits\/engine"/
assert_file "script/rails"
end
def test_being_quiet_while_creating_dummy_application
@ -199,8 +199,16 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase
assert_file "bukkits.gemspec", /s.version = "0.0.1"/
end
def test_usage_of_engine_commands
run_generator [destination_root, "--full"]
assert_file "script/rails", /ENGINE_PATH = File.expand_path\('..\/..\/lib\/bukkits\/engine', __FILE__\)/
assert_file "script/rails", /ENGINE_ROOT = File.expand_path\('..\/..', __FILE__\)/
assert_file "script/rails", /require 'rails\/all'/
assert_file "script/rails", /require 'rails\/engine\/commands'/
end
def test_shebang
run_generator
run_generator [destination_root, "--full"]
assert_file "script/rails", /#!\/usr\/bin\/env ruby/
end
@ -254,7 +262,6 @@ class CustomPluginGeneratorTest < Rails::Generators::TestCase
assert_file 'spec/dummy'
assert_file 'Rakefile', /task :default => :spec/
assert_file 'Rakefile', /# spec tasks in rakefile/
assert_file 'script/rails', %r{spec/dummy}
end
protected

View file

@ -67,12 +67,12 @@ module SharedGeneratorTests
end
def test_shebang_is_added_to_rails_file
run_generator [destination_root, "--ruby", "foo/bar/baz"]
run_generator [destination_root, "--ruby", "foo/bar/baz", "--full"]
assert_file "script/rails", /#!foo\/bar\/baz/
end
def test_shebang_when_is_the_same_as_default_use_env
run_generator [destination_root, "--ruby", Thor::Util.ruby_command]
run_generator [destination_root, "--ruby", Thor::Util.ruby_command, "--full"]
assert_file "script/rails", /#!\/usr\/bin\/env/
end

View file

@ -287,4 +287,4 @@ Module.new do
end
f.puts "require 'rails/all'"
end
end
end unless defined?(RAILS_ISOLATED_ENGINE)

View file

@ -15,7 +15,7 @@ module RailtiesTest
@plugin = engine "bukkits" do |plugin|
plugin.write "lib/bukkits.rb", <<-RUBY
class Bukkits
module Bukkits
class Engine < ::Rails::Engine
railtie_name "bukkits"
end
@ -36,7 +36,7 @@ module RailtiesTest
test "initializers are executed after application configuration initializers" do
@plugin.write "lib/bukkits.rb", <<-RUBY
class Bukkits
module Bukkits
class Engine < ::Rails::Engine
initializer "dummy_initializer" do
end
@ -77,7 +77,7 @@ module RailtiesTest
add_to_config("config.action_dispatch.show_exceptions = false")
@plugin.write "lib/bukkits.rb", <<-RUBY
class Bukkits
module Bukkits
class Engine < ::Rails::Engine
endpoint lambda { |env| [200, {'Content-Type' => 'text/html'}, ['Hello World']] }
config.middleware.use ::RailtiesTest::EngineTest::Upcaser
@ -127,7 +127,7 @@ module RailtiesTest
test "it provides routes as default endpoint" do
@plugin.write "lib/bukkits.rb", <<-RUBY
class Bukkits
module Bukkits
class Engine < ::Rails::Engine
end
end
@ -153,7 +153,7 @@ module RailtiesTest
test "engine can load its own plugins" do
@plugin.write "lib/bukkits.rb", <<-RUBY
class Bukkits
module Bukkits
class Engine < ::Rails::Engine
end
end
@ -170,7 +170,7 @@ module RailtiesTest
test "engine does not load plugins that already exists in application" do
@plugin.write "lib/bukkits.rb", <<-RUBY
class Bukkits
module Bukkits
class Engine < ::Rails::Engine
end
end
@ -193,7 +193,7 @@ module RailtiesTest
test "it loads its environment file" do
@plugin.write "lib/bukkits.rb", <<-RUBY
class Bukkits
module Bukkits
class Engine < ::Rails::Engine
end
end
@ -212,7 +212,7 @@ module RailtiesTest
test "it passes router in env" do
@plugin.write "lib/bukkits.rb", <<-RUBY
class Bukkits
module Bukkits
class Engine < ::Rails::Engine
endpoint lambda { |env| [200, {'Content-Type' => 'text/html'}, 'hello'] }
end

View file

@ -0,0 +1,115 @@
RAILS_ISOLATED_ENGINE = true
require "isolation/abstract_unit"
require "#{RAILS_FRAMEWORK_ROOT}/railties/lib/rails/generators/test_case"
module RailtiesTests
class GeneratorTest < Rails::Generators::TestCase
include ActiveSupport::Testing::Isolation
TMP_PATH = File.expand_path(File.join(File.dirname(__FILE__), *%w[.. .. tmp]))
self.destination_root = File.join(TMP_PATH, "foo_bar")
def tmp_path(*args)
File.join(TMP_PATH, *args)
end
def engine_path
tmp_path('foo_bar')
end
def bundled_rails(cmd)
`bundle exec rails #{cmd}`
end
def rails(cmd)
environment = File.expand_path('../../../../load_paths', __FILE__)
if File.exist?("#{environment}.rb")
require_environment = "-r #{environment}"
end
`#{Gem.ruby} #{require_environment} #{RAILS_FRAMEWORK_ROOT}/bin/rails #{cmd}`
end
def build_engine(is_mountable=false)
FileUtils.mkdir_p(engine_path)
FileUtils.rm_r(engine_path)
mountable = is_mountable ? "--mountable" : ""
rails("plugin new #{engine_path} --full #{mountable}")
Dir.chdir(engine_path) do
File.open("Gemfile", "w") do |f|
f.write <<-GEMFILE.gsub(/^ {12}/, '')
source "http://rubygems.org"
gem 'rails', :path => '#{RAILS_FRAMEWORK_ROOT}'
gem 'sqlite3'
if RUBY_VERSION < '1.9'
gem "ruby-debug", ">= 0.10.3"
end
GEMFILE
end
end
end
def build_mountable_engine
build_engine(true)
end
def test_controllers_are_correctly_namespaced_when_engine_is_mountable
build_mountable_engine
Dir.chdir(engine_path) do
bundled_rails("g controller topics")
assert_file "app/controllers/foo_bar/topics_controller.rb", /module FooBar\n class TopicsController/
assert_no_file "app/controllers/topics_controller.rb"
end
end
def test_models_are_correctly_namespaced_when_engine_is_mountable
build_mountable_engine
Dir.chdir(engine_path) do
bundled_rails("g model topic")
assert_file "app/models/foo_bar/topic.rb", /module FooBar\n class Topic/
assert_no_file "app/models/topic.rb"
end
end
def test_helpers_are_correctly_namespaced_when_engine_is_mountable
build_mountable_engine
Dir.chdir(engine_path) do
bundled_rails("g helper topics")
assert_file "app/helpers/foo_bar/topics_helper.rb", /module FooBar\n module TopicsHelper/
assert_no_file "app/helpers/topics_helper.rb"
end
end
def test_controllers_are_not_namespaced_when_engine_is_not_mountable
build_engine
Dir.chdir(engine_path) do
bundled_rails("g controller topics")
assert_file "app/controllers/topics_controller.rb", /class TopicsController/
assert_no_file "app/controllers/foo_bar/topics_controller.rb"
end
end
def test_models_are_not_namespaced_when_engine_is_not_mountable
build_engine
Dir.chdir(engine_path) do
bundled_rails("g model topic")
assert_file "app/models/topic.rb", /class Topic/
assert_no_file "app/models/foo_bar/topic.rb"
end
end
def test_helpers_are_not_namespaced_when_engine_is_not_mountable
build_engine
Dir.chdir(engine_path) do
bundled_rails("g helper topics")
assert_file "app/helpers/topics_helper.rb", /module TopicsHelper/
assert_no_file "app/helpers/foo_bar/topics_helper.rb"
end
end
end
end

View file

@ -54,7 +54,7 @@ module RailtiesTest
add_to_config "ActiveRecord::Base.timestamped_migrations = false"
Dir.chdir(app_path) do
output = `rake bukkits:install:migrations`
output = `bundle exec rake bukkits:install:migrations`
assert File.exists?("#{app_path}/db/migrate/2_create_users.rb")
assert File.exists?("#{app_path}/db/migrate/3_add_last_name_to_users.rb")
@ -63,7 +63,7 @@ module RailtiesTest
assert_match /NOTE: Migration 3_create_sessions.rb from bukkits has been skipped/, output
assert_equal 3, Dir["#{app_path}/db/migrate/*.rb"].length
output = `rake railties:install:migrations`
output = `bundle exec rake railties:install:migrations`
assert File.exists?("#{app_path}/db/migrate/4_create_yaffles.rb")
assert_match /NOTE: Migration 3_create_sessions.rb from bukkits has been skipped/, output
@ -71,7 +71,7 @@ module RailtiesTest
assert_no_match /2_create_users/, output
migrations_count = Dir["#{app_path}/db/migrate/*.rb"].length
output = `rake railties:install:migrations`
output = `bundle exec rake railties:install:migrations`
assert_equal migrations_count, Dir["#{app_path}/db/migrate/*.rb"].length
end