aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKasper Timm Hansen <kaspth@gmail.com>2015-12-06 14:23:28 +0100
committerKasper Timm Hansen <kaspth@gmail.com>2015-12-06 14:23:28 +0100
commit5da05c3e6be52331899742db0dbd6d38e4fdeb1e (patch)
tree23e3d265994cdf1a6a3240a3182963a263895f25
parent93ee1b1021c974b9dd5ceee7166698af902cf34c (diff)
downloadrails-5da05c3e6be52331899742db0dbd6d38e4fdeb1e.tar.gz
rails-5da05c3e6be52331899742db0dbd6d38e4fdeb1e.tar.bz2
rails-5da05c3e6be52331899742db0dbd6d38e4fdeb1e.zip
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.
-rw-r--r--railties/lib/rails/command.rb70
-rw-r--r--railties/lib/rails/commands.rb15
-rw-r--r--railties/lib/rails/commands/command.rb72
-rw-r--r--railties/lib/rails/commands/dev_cache.rb4
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'