diff --git a/railties/lib/rails/command/actions.rb b/railties/lib/rails/command/actions.rb index 52d3348db37f99ae41b477dd1e1be2d64322b986..31b656ec31d068d64f8b78e5e3b0aec56d4f2c4d 100644 --- a/railties/lib/rails/command/actions.rb +++ b/railties/lib/rails/command/actions.rb @@ -1,18 +1,42 @@ module Rails module Command module Actions - private - # Change to the application's path if there is no config.ru file in current directory. - # This allows us to run `rails server` from other directories, but still get - # the main config.ru and properly set the tmp directory. - def set_application_directory! - Dir.chdir(File.expand_path("../../", APP_PATH)) unless File.exist?(File.expand_path("config.ru")) + # Change to the application's path if there is no config.ru file in current directory. + # This allows us to run `rails server` from other directories, but still get + # the main config.ru and properly set the tmp directory. + def set_application_directory! + Dir.chdir(File.expand_path("../../", APP_PATH)) unless File.exist?(File.expand_path("config.ru")) + end + + if defined?(ENGINE_PATH) + def require_application_and_environment! + require ENGINE_PATH end + def load_tasks + Rake.application.init("rails") + Rake.application.load_rakefile + end + + def load_generators + engine = ::Rails::Engine.find(ENGINE_ROOT) + Rails::Generators.namespace = engine.railtie_namespace + engine.load_generators + end + else def require_application_and_environment! require APP_PATH Rails.application.require_environment! end + + def load_tasks + Rails.application.load_tasks + end + + def load_generators + Rails.application.load_generators + end + end end end end diff --git a/railties/lib/rails/command/base.rb b/railties/lib/rails/command/base.rb index 2ef8393863efc8d3a4c5fd43e385283c0f775bc9..73ebaca66f6cd6c7dac2a67602ea27c8b1d7d0b5 100644 --- a/railties/lib/rails/command/base.rb +++ b/railties/lib/rails/command/base.rb @@ -15,6 +15,11 @@ class Error < Thor::Error # :nodoc: include Actions class << self + # Returns true when the app is a Rails engine. + def engine? + defined?(ENGINE_ROOT) + end + # Tries to get the description from a USAGE file one folder above the command # root. def desc(usage = nil, description = nil) diff --git a/railties/lib/rails/commands/help/USAGE b/railties/lib/rails/commands/help/USAGE index 0f6ff756ceb42d96491ff265a8e5928c1ae85e71..348f41861f8342bed42ae04810fdc91a4702a1de 100644 --- a/railties/lib/rails/commands/help/USAGE +++ b/railties/lib/rails/commands/help/USAGE @@ -1,5 +1,16 @@ Usage: bin/rails COMMAND [args] [options] +<% if engine? %> +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 `bin/rails server` or `bin/rails console`, +you should do it from the application's directory (typically test/dummy). +<% else %> The most common rails commands are: generate Generate new code (short-cut alias: "g") console Start the Rails console (short-cut alias: "c") @@ -11,6 +22,6 @@ The most common rails commands are: new application called MyApp in "./my_app" All commands can be run with -h (or --help) for more information. - +<% end %> In addition to those commands, there are: diff --git a/railties/lib/rails/commands/rake/rake_command.rb b/railties/lib/rails/commands/rake/rake_command.rb index a4a2464445c4004d97c9d88c7b009a0b62ba0776..a43c884170d5cf18790d249cbb25afcb95bb4017 100644 --- a/railties/lib/rails/commands/rake/rake_command.rb +++ b/railties/lib/rails/commands/rake/rake_command.rb @@ -1,6 +1,8 @@ module Rails module Command class RakeCommand < Base + extend Rails::Command::Actions + namespace "rake" class << self @@ -27,12 +29,12 @@ def rake_tasks return @rake_tasks if defined?(@rake_tasks) ActiveSupport::Deprecation.silence do - Rails::Command.require_application_and_environment! + require_application_and_environment! end Rake::TaskManager.record_task_metadata = true Rake.application.instance_variable_set(:@name, "rails") - Rails.application.load_tasks + load_tasks @rake_tasks = Rake.application.tasks.select(&:comment) end diff --git a/railties/lib/rails/engine/commands.rb b/railties/lib/rails/engine/commands.rb index dfbeea36b85178e3669872c469f7ca25ca78a15a..a23ae44b0bf1c1fcd3d8cdc4dece6ed3a69d03cc 100644 --- a/railties/lib/rails/engine/commands.rb +++ b/railties/lib/rails/engine/commands.rb @@ -1,6 +1,4 @@ -require "rails/engine/commands_tasks" - -ARGV << "--help" if ARGV.empty? +require "rails/command" aliases = { "g" => "generate", @@ -11,4 +9,4 @@ command = ARGV.shift command = aliases[command] || command -Rails::Engine::CommandsTasks.new(ARGV).run_command!(command) +Rails::Command.invoke command, ARGV diff --git a/railties/lib/rails/engine/commands_tasks.rb b/railties/lib/rails/engine/commands_tasks.rb index d6effdb732c96d7a0d22b6ff547bef84f93ce093..65dd27479371ad0f5352c50e9dc217929dffe26f 100644 --- a/railties/lib/rails/engine/commands_tasks.rb +++ b/railties/lib/rails/engine/commands_tasks.rb @@ -26,7 +26,7 @@ def command_whitelist def help_message <<-EOT.strip_heredoc - Usage: rails COMMAND [ARGS] + Usage: bin/rails COMMAND [ARGS] The common Rails commands available for engines are: generate Generate new code (short-cut alias: "g")