diff options
author | Kasper Timm Hansen <kaspth@gmail.com> | 2015-12-13 21:25:40 +0100 |
---|---|---|
committer | Kasper Timm Hansen <kaspth@gmail.com> | 2015-12-13 21:44:03 +0100 |
commit | 3e65c3d3886336e9145438cdeacaf4ebec6a48b8 (patch) | |
tree | 0ce086f68620530149c11946470201c8bdf6603f | |
parent | 02eef9459a19f263b30d8d032017c05bfc97d00b (diff) | |
parent | 52f2f9810eaf3d385ca3eef8ed6fc62e4fd1f7d3 (diff) | |
download | rails-3e65c3d3886336e9145438cdeacaf4ebec6a48b8.tar.gz rails-3e65c3d3886336e9145438cdeacaf4ebec6a48b8.tar.bz2 rails-3e65c3d3886336e9145438cdeacaf4ebec6a48b8.zip |
Merge branch 'rails-rake-proxy'
-rw-r--r-- | guides/source/initialization.md | 12 | ||||
-rw-r--r-- | railties/lib/rails/commands.rb | 2 | ||||
-rw-r--r-- | railties/lib/rails/commands/commands_tasks.rb | 26 | ||||
-rw-r--r-- | railties/lib/rails/commands/rake_proxy.rb | 34 |
4 files changed, 63 insertions, 11 deletions
diff --git a/guides/source/initialization.md b/guides/source/initialization.md index ebe1cb206a..7bf7eebb62 100644 --- a/guides/source/initialization.md +++ b/guides/source/initialization.md @@ -139,7 +139,8 @@ aliases = { "c" => "console", "s" => "server", "db" => "dbconsole", - "r" => "runner" + "r" => "runner", + "t" => "test" } command = ARGV.shift @@ -158,19 +159,20 @@ defined here to find the matching command. ### `rails/commands/command_tasks.rb` -When one types an incorrect rails command, the `run_command` is responsible for -throwing an error message. If the command is valid, a method of the same name -is called. +When one types a valid Rails command, `run_command!` a method of the same name +is called. If Rails doesn't recognize the command, it tries to run a Rake task +of the same name. ```ruby COMMAND_WHITELIST = %w(plugin generate destroy console server dbconsole application runner new version help) def run_command!(command) command = parse_command(command) + if COMMAND_WHITELIST.include?(command) send(command) else - write_error_message(command) + run_rake_task(command) end end ``` diff --git a/railties/lib/rails/commands.rb b/railties/lib/rails/commands.rb index b9c4e02ca0..7627fcf5a0 100644 --- a/railties/lib/rails/commands.rb +++ b/railties/lib/rails/commands.rb @@ -7,7 +7,7 @@ aliases = { "s" => "server", "db" => "dbconsole", "r" => "runner", - "t" => "test", + "t" => "test" } command = ARGV.shift diff --git a/railties/lib/rails/commands/commands_tasks.rb b/railties/lib/rails/commands/commands_tasks.rb index 7e6b49e2a3..da3b9452d5 100644 --- a/railties/lib/rails/commands/commands_tasks.rb +++ b/railties/lib/rails/commands/commands_tasks.rb @@ -1,3 +1,5 @@ +require 'rails/commands/rake_proxy' + module Rails # This is a class which takes in a rails command and initiates the appropriate # initiation sequence. @@ -5,6 +7,8 @@ module Rails # Warning: This class mutates ARGV because some commands require manipulating # it before they are run. class CommandsTasks # :nodoc: + include Rails::RakeProxy + attr_reader :argv HELP_MESSAGE = <<-EOT @@ -20,14 +24,18 @@ The most common rails commands are: new Create a new Rails application. "rails new my_app" creates a new application called MyApp in "./my_app" -In addition to those, there are: - destroy Undo code generated with "generate" (short-cut alias: "d") - plugin new Generates skeleton for developing a Rails plugin - runner Run a piece of code in the application environment (short-cut alias: "r") - All commands can be run with -h (or --help) for more information. + +In addition to those commands, there are: EOT + ADDITIONAL_COMMANDS = [ + [ 'destroy', 'Undo code generated with "generate" (short-cut alias: "d")' ], + [ 'plugin new', 'Generates skeleton for developing a Rails plugin' ], + [ 'runner', + 'Run a piece of code in the application environment (short-cut alias: "r")' ] + ] + COMMAND_WHITELIST = %w(plugin generate destroy console server dbconsole runner new version help test) def initialize(argv) @@ -39,6 +47,8 @@ EOT if COMMAND_WHITELIST.include?(command) send(command) + else + run_rake_task(command) end end @@ -109,6 +119,7 @@ EOT def help write_help_message + write_commands ADDITIONAL_COMMANDS + formatted_rake_tasks end private @@ -150,6 +161,11 @@ EOT 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' diff --git a/railties/lib/rails/commands/rake_proxy.rb b/railties/lib/rails/commands/rake_proxy.rb new file mode 100644 index 0000000000..f7d5df6b2f --- /dev/null +++ b/railties/lib/rails/commands/rake_proxy.rb @@ -0,0 +1,34 @@ +require 'rake' +require 'active_support' + +module Rails + module RakeProxy #:nodoc: + private + def run_rake_task(command) + ARGV.unshift(command) # Prepend the command, so Rake knows how to run it. + + Rake.application.standard_exception_handling do + Rake.application.init('rails') + Rake.application.load_rakefile + Rake.application.top_level + end + end + + def rake_tasks + return @rake_tasks if defined?(@rake_tasks) + + ActiveSupport::Deprecation.silence do + require_application_and_environment! + end + + Rake::TaskManager.record_task_metadata = true + Rake.application.instance_variable_set(:@name, 'rails') + Rails.application.load_tasks + @rake_tasks = Rake.application.tasks.select(&:comment) + end + + def formatted_rake_tasks + rake_tasks.map { |t| [ t.name_with_args, t.comment ] } + end + end +end |