Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Railtie < Rails::Railtie # :nodoc:
task :load_config do
ActiveRecord::Tasks::DatabaseTasks.database_configuration = Rails.application.config.database_configuration

if defined?(ENGINE_PATH) && engine = Rails::Engine.find(ENGINE_PATH)
if defined?(ENGINE_ROOT) && engine = Rails::Engine.find(ENGINE_ROOT)
if engine.paths['db/migrate'].existent
ActiveRecord::Tasks::DatabaseTasks.migrations_paths += engine.paths['db/migrate'].to_a
end
Expand Down
35 changes: 3 additions & 32 deletions railties/lib/rails/engine/commands.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'rails/engine/commands_tasks'

ARGV << '--help' if ARGV.empty?

aliases = {
Expand All @@ -9,35 +11,4 @@
command = ARGV.shift
command = aliases[command] || command

require ENGINE_PATH
engine = ::Rails::Engine.find(ENGINE_ROOT)

case command
when 'generate', 'destroy', 'test'
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 %w(-h --help).include?(command)
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" (short-cut alias: "d")
test Run tests (short-cut alias: "t")

All commands can be run with -h for more information.

If you want to run any commands that need to be run in context
of the application, like `rails server` or `rails console`,
you should do it from application's directory (typically test/dummy).
EOT
exit(1)
end
Rails::Engine::CommandsTasks.new(ARGV).run_command!(command)
116 changes: 116 additions & 0 deletions railties/lib/rails/engine/commands_tasks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
require 'rails/commands/rake_proxy'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to require rake too, no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has require a rake in the rails/commands/rake_proxy, I think here that it is not need.


module Rails
class Engine
class CommandsTasks # :nodoc:
include Rails::RakeProxy

attr_reader :argv

HELP_MESSAGE = <<-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" (short-cut alias: "d")
test Run tests (short-cut alias: "t")

All commands can be run with -h for more information.

If you want to run any commands that need to be run in context
of the application, like `rails server` or `rails console`,
you should do it from application's directory (typically test/dummy).

In addition to those commands, there are:
EOT

COMMAND_WHITELIST = %w(generate destroy version help test)

def initialize(argv)
@argv = argv
end

def run_command!(command)
command = parse_command(command)

if COMMAND_WHITELIST.include?(command)
send(command)
else
run_rake_task(command)
end
end

def generate
generate_or_destroy(:generate)
end

def destroy
generate_or_destroy(:destroy)
end

def test
require_command!("test")
end

def version
argv.unshift '--version'
require_command!("application")
end

def help
write_help_message
write_commands(formatted_rake_tasks)
end

private

def require_command!(command)
require "rails/commands/#{command}"
end

def generate_or_destroy(command)
load_generators
require_command!(command)
end

def load_generators
require 'rails/generators'
require ENGINE_PATH

engine = ::Rails::Engine.find(ENGINE_ROOT)
Rails::Generators.namespace = engine.railtie_namespace
engine.load_generators
end

def write_help_message
puts HELP_MESSAGE
end

def write_commands(commands)
width = commands.map { |name, _| name.size }.max || 10
commands.each { |command| printf(" %-#{width}s %s\n", *command) }
end

def parse_command(command)
case command
when '--version', '-v'
'version'
when '--help', '-h'
'help'
else
command
end
end

def rake_tasks
return @rake_tasks if defined?(@rake_tasks)

load_generators
Rake::TaskManager.record_task_metadata = true
Rake.application.init('rails')
Rake.application.load_rakefile
@rake_tasks = Rake.application.tasks.select(&:comment)
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't rake_tasks included from the Rails::RakeProxy?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rails::RakeProxy#rake_tasks has become a prerequisite of the code that uses the Rails application, did not work for as it is the case of the Rails Engine, it was defined separately.

end
end
end
4 changes: 2 additions & 2 deletions railties/lib/rails/tasks/engine.rake
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ task "load_app" do
end
task :environment => "app:environment"

if !defined?(ENGINE_PATH) || !ENGINE_PATH
ENGINE_PATH = find_engine_path(APP_RAKEFILE)
if !defined?(ENGINE_ROOT) || !ENGINE_ROOT
ENGINE_ROOT = find_engine_path(APP_RAKEFILE)
end
end

Expand Down
2 changes: 1 addition & 1 deletion railties/test/generators/plugin_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def test_ensure_that_migration_tasks_work_with_mountable_option
run_generator [destination_root, "--mountable"]
FileUtils.cd destination_root
quietly { system 'bundle install' }
output = `bundle exec rake db:migrate 2>&1`
output = `bin/rails db:migrate 2>&1`
assert $?.success?, "Command failed: #{output}"
end

Expand Down
8 changes: 4 additions & 4 deletions railties/test/generators/scaffold_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ def test_scaffold_tests_pass_by_default_inside_mountable_engine
Dir.chdir(engine_path) do
quietly do
`bin/rails g scaffold User name:string age:integer;
bundle exec rake db:migrate`
bin/rails db:migrate`
end
assert_match(/8 runs, 13 assertions, 0 failures, 0 errors/, `bin/rails test 2>&1`)
end
Expand All @@ -500,7 +500,7 @@ def test_scaffold_tests_pass_by_default_inside_full_engine
Dir.chdir(engine_path) do
quietly do
`bin/rails g scaffold User name:string age:integer;
bundle exec rake db:migrate`
bin/rails db:migrate`
end
assert_match(/8 runs, 13 assertions, 0 failures, 0 errors/, `bin/rails test 2>&1`)
end
Expand All @@ -514,7 +514,7 @@ def test_scaffold_tests_pass_by_default_inside_api_mountable_engine
Dir.chdir(engine_path) do
quietly do
`bin/rails g scaffold User name:string age:integer;
bundle exec rake db:migrate`
bin/rails db:migrate`
end
assert_match(/6 runs, 8 assertions, 0 failures, 0 errors/, `bin/rails test 2>&1`)
end
Expand All @@ -528,7 +528,7 @@ def test_scaffold_tests_pass_by_default_inside_api_full_engine
Dir.chdir(engine_path) do
quietly do
`bin/rails g scaffold User name:string age:integer;
bundle exec rake db:migrate`
bin/rails db:migrate`
end
assert_match(/6 runs, 8 assertions, 0 failures, 0 errors/, `bin/rails test 2>&1`)
end
Expand Down