aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/commands
diff options
context:
space:
mode:
authorDharam Gollapudi <dharam.gollapudi@gmail.com>2015-11-13 16:58:51 -0800
committerKasper Timm Hansen <kaspth@gmail.com>2015-12-13 20:59:41 +0100
commit52f2f9810eaf3d385ca3eef8ed6fc62e4fd1f7d3 (patch)
treeabce7a1ac3bf7799bf52b19ec43c8d6adf1a15ac /railties/lib/rails/commands
parent02eef9459a19f263b30d8d032017c05bfc97d00b (diff)
downloadrails-52f2f9810eaf3d385ca3eef8ed6fc62e4fd1f7d3.tar.gz
rails-52f2f9810eaf3d385ca3eef8ed6fc62e4fd1f7d3.tar.bz2
rails-52f2f9810eaf3d385ca3eef8ed6fc62e4fd1f7d3.zip
Implement Rake proxy for Rails' command line interface.
Allows any Rake task to be run through `bin/rails` such as `bin/rails db:migrate`, `bin/rails notes` etc. The Rake tasks are appended to Rails' help output, and blend in as standard commands.
Diffstat (limited to 'railties/lib/rails/commands')
-rw-r--r--railties/lib/rails/commands/commands_tasks.rb14
-rw-r--r--railties/lib/rails/commands/rake_proxy.rb42
2 files changed, 56 insertions, 0 deletions
diff --git a/railties/lib/rails/commands/commands_tasks.rb b/railties/lib/rails/commands/commands_tasks.rb
index 7e6b49e2a3..e1713d5798 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
@@ -26,6 +30,7 @@ In addition to those, there are:
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.
+
EOT
COMMAND_WHITELIST = %w(plugin generate destroy console server dbconsole runner new version help test)
@@ -39,6 +44,9 @@ EOT
if COMMAND_WHITELIST.include?(command)
send(command)
+ else
+ ARGV.unshift(command)
+ send(:rake)
end
end
@@ -102,6 +110,10 @@ EOT
end
end
+ def rake
+ invoke_rake
+ end
+
def version
argv.unshift '--version'
require_command!("application")
@@ -109,6 +121,8 @@ EOT
def help
write_help_message
+ write_rake_tasks_help_message
+ write_rake_tasks
end
private
diff --git a/railties/lib/rails/commands/rake_proxy.rb b/railties/lib/rails/commands/rake_proxy.rb
new file mode 100644
index 0000000000..13a9aef694
--- /dev/null
+++ b/railties/lib/rails/commands/rake_proxy.rb
@@ -0,0 +1,42 @@
+require 'rake'
+
+module Rails
+ module RakeProxy #:nodoc:
+
+ RAKE_TASKS_HELP_MESSAGE = <<-EOT
+In addition to those, you can run the rake tasks as rails commands:
+
+ EOT
+
+ private
+
+ def write_rake_tasks_help_message
+ puts RAKE_TASKS_HELP_MESSAGE
+ end
+
+ def write_rake_tasks
+ width = rake_tasks.map { |t| t.name_with_args.length }.max || 10
+ rake_tasks.each do |t|
+ printf("#{Rake.application.name} %-#{width}s # %s\n", t.name_with_args, t.comment)
+ end
+ end
+
+ def rake_tasks
+ return @rake_tasks if defined?(@rake_tasks)
+
+ require_application_and_environment!
+ 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 invoke_rake
+ Rake.application.standard_exception_handling do
+ Rake.application.init('rails')
+ Rake.application.load_rakefile
+ Rake.application.top_level
+ end
+ end
+ end
+end