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

Ditch `Commands` namespace for base command.

Reading `Rails::Commands::Command` feels excessive. Especially if users can subclass command
to write their own commands — which I'd like to aim for.

Switch to `Rails::Command` before we get too far into things.
上级 93ee1b10
require 'rails/commands/commands_tasks'
module Rails
class Command
attr_reader :argv
def initialize(argv = [])
@argv = argv
@option_parser = build_option_parser
@options = {}
end
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)
else
Rails::CommandsTasks.new(argv).run_command!(task_name)
end
end
def run(command_name)
parse_options_for(command_name)
@option_parser.parse! @argv
public_send(command_name)
end
def self.options_for(command_name, &options_to_parse)
@@command_options[command_name] = options_to_parse
end
def self.set_banner(command_name, banner)
options_for(command_name) { |opts, _| opts.banner = banner }
end
private
@@commands = []
@@command_options = {}
def parse_options_for(command_name)
@@command_options.fetch(command_name, proc {}).call(@option_parser, @options)
end
def build_option_parser
OptionParser.new do |opts|
opts.on('-h', '--help', 'Show this help.') do
puts opts
exit
end
end
end
def self.inherited(command)
@@commands << command
end
def self.command_name_for(task_name)
task_name.gsub(':', '_').to_sym
end
def self.command_for(command_name)
@@commands.find do |command|
command.public_instance_methods.include?(command_name)
end
end
end
end
ARGV << '--help' if ARGV.empty?
aliases = {
"g" => "generate",
"d" => "destroy",
......@@ -8,15 +10,10 @@
"t" => "test",
}
if ARGV.empty?
ARGV << '--help'
command = ''
else
command = ARGV.shift
command = aliases[command] || command
end
command = ARGV.shift
command = aliases[command] || command
require 'rails/commands/command'
require 'rails/command'
require 'rails/commands/dev_cache'
Rails::Commands::Command.run(command, ARGV)
Rails::Command.run(command, ARGV)
require 'rails/commands/commands_tasks'
module Rails
module Commands
class Command
attr_reader :argv
def initialize(argv = [])
@argv = argv
@option_parser = build_option_parser
@options = {}
end
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)
else
Rails::CommandsTasks.new(argv).run_command!(task_name)
end
end
def run(command_name)
parse_options_for(command_name)
@option_parser.parse! @argv
public_send(command_name)
end
def self.options_for(command_name, &options_to_parse)
@@command_options[command_name] = options_to_parse
end
def self.set_banner(command_name, banner)
options_for(command_name) { |opts, _| opts.banner = banner }
end
private
@@commands = []
@@command_options = {}
def parse_options_for(command_name)
@@command_options.fetch(command_name, proc {}).call(@option_parser, @options)
end
def build_option_parser
OptionParser.new do |opts|
opts.on('-h', '--help', 'Show this help.') do
puts opts
exit
end
end
end
def self.inherited(command)
@@commands << command
end
def self.command_name_for(task_name)
task_name.gsub(':', '_').to_sym
end
def self.command_for(command_name)
@@commands.find do |command|
command.public_instance_methods.include?(command_name)
end
end
end
end
end
require 'rails/commands/command'
require 'rails/command'
module Rails
module Commands
# This is a wrapper around the Rails dev:cache command
class DevCache < Command
set_banner :dev_cache, 'Toggle development mode caching on/off'
set_banner :dev_cache, 'Toggle development mode caching on/off'
def dev_cache
if File.exist? 'tmp/caching-dev.txt'
File.delete 'tmp/caching-dev.txt'
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册