From f8d2a14e251b9985db11475a0cf8b1350efbf8f1 Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Sun, 6 Dec 2015 13:24:42 +0100 Subject: 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. --- railties/lib/rails/commands/command.rb | 30 +++++++++++---------------- railties/lib/rails/commands/commands_tasks.rb | 7 +++---- 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) -- cgit v1.2.3