提交 f8d2a14e 编写于 作者: K Kasper Timm Hansen

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.
上级 65443ceb
......@@ -10,17 +10,20 @@ def initialize(argv = [])
@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 @@ def self.set_banner(command_name, banner)
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 @@ def self.command_name_for(task_name)
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
......
......@@ -32,15 +32,14 @@ class CommandsTasks # :nodoc:
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)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册