diff options
Diffstat (limited to 'railties')
-rw-r--r-- | railties/lib/rails/command.rb | 70 | ||||
-rw-r--r-- | railties/lib/rails/commands.rb | 15 | ||||
-rw-r--r-- | railties/lib/rails/commands/command.rb | 72 | ||||
-rw-r--r-- | railties/lib/rails/commands/dev_cache.rb | 4 |
4 files changed, 78 insertions, 83 deletions
diff --git a/railties/lib/rails/command.rb b/railties/lib/rails/command.rb new file mode 100644 index 0000000000..6587984b53 --- /dev/null +++ b/railties/lib/rails/command.rb @@ -0,0 +1,70 @@ +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 diff --git a/railties/lib/rails/commands.rb b/railties/lib/rails/commands.rb index df2ed94654..b9c4e02ca0 100644 --- a/railties/lib/rails/commands.rb +++ b/railties/lib/rails/commands.rb @@ -1,3 +1,5 @@ +ARGV << '--help' if ARGV.empty? + aliases = { "g" => "generate", "d" => "destroy", @@ -8,15 +10,10 @@ aliases = { "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) diff --git a/railties/lib/rails/commands/command.rb b/railties/lib/rails/commands/command.rb deleted file mode 100644 index d2e1cd18b4..0000000000 --- a/railties/lib/rails/commands/command.rb +++ /dev/null @@ -1,72 +0,0 @@ -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 diff --git a/railties/lib/rails/commands/dev_cache.rb b/railties/lib/rails/commands/dev_cache.rb index 43675c0e69..ec96e8f630 100644 --- a/railties/lib/rails/commands/dev_cache.rb +++ b/railties/lib/rails/commands/dev_cache.rb @@ -1,10 +1,10 @@ -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' |