From 6e95682b1424e8e4320b8a5120ec31959101c138 Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Sun, 14 Aug 2016 20:42:24 +0900 Subject: refactor `CommandsTasks` Extract a common method of `Rails::CommandsTasks` and `Rails::Engine::CommandsTasks` to the module. --- railties/lib/rails/engine/commands_tasks.rb | 112 +++++++--------------------- 1 file changed, 28 insertions(+), 84 deletions(-) (limited to 'railties/lib/rails/engine/commands_tasks.rb') diff --git a/railties/lib/rails/engine/commands_tasks.rb b/railties/lib/rails/engine/commands_tasks.rb index e21a7183fc..d6effdb732 100644 --- a/railties/lib/rails/engine/commands_tasks.rb +++ b/railties/lib/rails/engine/commands_tasks.rb @@ -1,118 +1,62 @@ require "rails/commands/rake_proxy" +require "rails/commands/common_commands_tasks" +require "active_support/core_ext/string/strip" module Rails class Engine class CommandsTasks # :nodoc: include Rails::RakeProxy + include Rails::CommonCommandsTasks 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) + private - if COMMAND_WHITELIST.include?(command) - send(command) - else - run_rake_task(command) + def commands + formatted_rake_tasks end - end - def generate - generate_or_destroy(:generate) - end + def command_whitelist + %w(generate destroy version help test) + end - def destroy - generate_or_destroy(:destroy) - end + def help_message + <<-EOT.strip_heredoc + Usage: rails COMMAND [ARGS] - def test - require_command!("test") - end + 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") - def version - argv.unshift "--version" - require_command!("application") - end + All commands can be run with -h for more information. - def help - write_help_message - write_commands(formatted_rake_tasks) - end + 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). - private + In addition to those commands, there are: + EOT + end - def require_command!(command) - require "rails/commands/#{command}" + def require_application_and_environment! + require ENGINE_PATH end - def generate_or_destroy(command) - load_generators - require_command!(command) + def load_tasks + Rake.application.init("rails") + Rake.application.load_rakefile 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 - require_rake - - 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 end end end -- cgit v1.2.3