提交 d5bd4b25 编写于 作者: K Kasper Timm Hansen

Run engine commands through command infrastructure.

上级 6813edc7
module Rails module Rails
module Command module Command
module Actions module Actions
private # Change to the application's path if there is no config.ru file in current directory.
# 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
# This allows us to run `rails server` from other directories, but still get # the main config.ru and properly set the tmp directory.
# the main config.ru and properly set the tmp directory. def set_application_directory!
def set_application_directory! Dir.chdir(File.expand_path("../../", APP_PATH)) unless File.exist?(File.expand_path("config.ru"))
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 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! def require_application_and_environment!
require APP_PATH require APP_PATH
Rails.application.require_environment! Rails.application.require_environment!
end end
def load_tasks
Rails.application.load_tasks
end
def load_generators
Rails.application.load_generators
end
end
end end
end end
end end
...@@ -15,6 +15,11 @@ class Error < Thor::Error # :nodoc: ...@@ -15,6 +15,11 @@ class Error < Thor::Error # :nodoc:
include Actions include Actions
class << self 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 # Tries to get the description from a USAGE file one folder above the command
# root. # root.
def desc(usage = nil, description = nil) def desc(usage = nil, description = nil)
......
Usage: bin/rails COMMAND [args] [options] 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: The most common rails commands are:
generate Generate new code (short-cut alias: "g") generate Generate new code (short-cut alias: "g")
console Start the Rails console (short-cut alias: "c") console Start the Rails console (short-cut alias: "c")
...@@ -11,6 +22,6 @@ The most common rails commands are: ...@@ -11,6 +22,6 @@ The most common rails commands are:
new application called MyApp in "./my_app" new application called MyApp in "./my_app"
All commands can be run with -h (or --help) for more information. All commands can be run with -h (or --help) for more information.
<% end %>
In addition to those commands, there are: In addition to those commands, there are:
module Rails module Rails
module Command module Command
class RakeCommand < Base class RakeCommand < Base
extend Rails::Command::Actions
namespace "rake" namespace "rake"
class << self class << self
...@@ -27,12 +29,12 @@ def rake_tasks ...@@ -27,12 +29,12 @@ def rake_tasks
return @rake_tasks if defined?(@rake_tasks) return @rake_tasks if defined?(@rake_tasks)
ActiveSupport::Deprecation.silence do ActiveSupport::Deprecation.silence do
Rails::Command.require_application_and_environment! require_application_and_environment!
end end
Rake::TaskManager.record_task_metadata = true Rake::TaskManager.record_task_metadata = true
Rake.application.instance_variable_set(:@name, "rails") Rake.application.instance_variable_set(:@name, "rails")
Rails.application.load_tasks load_tasks
@rake_tasks = Rake.application.tasks.select(&:comment) @rake_tasks = Rake.application.tasks.select(&:comment)
end end
......
require "rails/engine/commands_tasks" require "rails/command"
ARGV << "--help" if ARGV.empty?
aliases = { aliases = {
"g" => "generate", "g" => "generate",
...@@ -11,4 +9,4 @@ ...@@ -11,4 +9,4 @@
command = ARGV.shift command = ARGV.shift
command = aliases[command] || command command = aliases[command] || command
Rails::Engine::CommandsTasks.new(ARGV).run_command!(command) Rails::Command.invoke command, ARGV
...@@ -26,7 +26,7 @@ def command_whitelist ...@@ -26,7 +26,7 @@ def command_whitelist
def help_message def help_message
<<-EOT.strip_heredoc <<-EOT.strip_heredoc
Usage: rails COMMAND [ARGS] Usage: bin/rails COMMAND [ARGS]
The common Rails commands available for engines are: The common Rails commands available for engines are:
generate Generate new code (short-cut alias: "g") generate Generate new code (short-cut alias: "g")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册