aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authorKasper Timm Hansen <kaspth@gmail.com>2015-12-06 13:24:42 +0100
committerKasper Timm Hansen <kaspth@gmail.com>2015-12-06 13:24:42 +0100
commitf8d2a14e251b9985db11475a0cf8b1350efbf8f1 (patch)
treeb14dc725560e7af27ffeee227f1de84fc59b1f68 /railties
parent65443ceb0d55e1445b28e31e772629424e6755c3 (diff)
downloadrails-f8d2a14e251b9985db11475a0cf8b1350efbf8f1.tar.gz
rails-f8d2a14e251b9985db11475a0cf8b1350efbf8f1.tar.bz2
rails-f8d2a14e251b9985db11475a0cf8b1350efbf8f1.zip
Add class level `run` to Rails command.
The class level version is responsible for changing a task name to command name, then finding a command and run it if there is one. The instance level `run` then makes sure arguments have been parsed into `@options` and runs the command by sending it. `Rails::Commands::Command.run` returns true to make it work within `Rails::CommandsTask`, but won't in the future when it handles all option parsing.
Diffstat (limited to 'railties')
-rw-r--r--railties/lib/rails/commands/command.rb30
-rw-r--r--railties/lib/rails/commands/commands_tasks.rb7
2 files changed, 15 insertions, 22 deletions
diff --git a/railties/lib/rails/commands/command.rb b/railties/lib/rails/commands/command.rb
index c8707be3ab..fe0b77cb96 100644
--- a/railties/lib/rails/commands/command.rb
+++ b/railties/lib/rails/commands/command.rb
@@ -10,17 +10,20 @@ module Rails
@options = {}
end
- def run(task_name)
- command_name = self.class.command_name_for(task_name)
+ def self.run(task_name, argv)
+ command_name = command_name_for(task_name)
+ if command = command_for(command_name)
+ command.new(argv).run(command_name)
+ true # Indicate command was found and run.
+ end
+ end
+
+ def run(command_name)
parse_options_for(command_name)
@option_parser.parse! @argv
- if command = command_for(command_name)
- command.public_send(command_name)
- else
- puts @option_parser
- end
+ public_send(command_name)
end
def self.options_for(command_name, &options_to_parse)
@@ -31,11 +34,6 @@ module Rails
options_for(command_name) { |opts, _| opts.banner = banner }
end
- def exists?(task_name) # :nodoc:
- command_name = self.class.command_name_for(task_name)
- !command_for(command_name).nil?
- end
-
private
@@commands = []
@@command_options = {}
@@ -61,14 +59,10 @@ module Rails
task_name.gsub(':', '_').to_sym
end
- def command_for(command_name)
- klass = @@commands.find do |command|
+ def self.command_for(command_name)
+ @@commands.find do |command|
command.public_instance_methods.include?(command_name)
end
-
- if klass
- klass.new(@argv)
- end
end
end
end
diff --git a/railties/lib/rails/commands/commands_tasks.rb b/railties/lib/rails/commands/commands_tasks.rb
index f79b40b2ab..6f3f4f7042 100644
--- a/railties/lib/rails/commands/commands_tasks.rb
+++ b/railties/lib/rails/commands/commands_tasks.rb
@@ -32,15 +32,14 @@ EOT
def initialize(argv)
@argv = argv
- @rails_command = Rails::Commands::Command.new(argv)
end
def run_command!(command)
command = parse_command(command)
- if @rails_command.exists?(command)
- @rails_command.run(command)
- elsif COMMAND_WHITELIST.include?(command)
+ run_with_command = Rails::Commands::Command.run(command, argv)
+
+ if !run_with_command && COMMAND_WHITELIST.include?(command)
send(command)
else
write_error_message(command)