aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/commands/common_commands_tasks.rb
diff options
context:
space:
mode:
authoryuuji.yaginuma <yuuji.yaginuma@gmail.com>2016-08-14 20:42:24 +0900
committeryuuji.yaginuma <yuuji.yaginuma@gmail.com>2016-08-17 11:36:39 +0900
commit6e95682b1424e8e4320b8a5120ec31959101c138 (patch)
tree3e2977a658988dab3c28c53cba5c7e844699645f /railties/lib/rails/commands/common_commands_tasks.rb
parenta1e4c197cb12fef66530a2edfaeda75566088d1f (diff)
downloadrails-6e95682b1424e8e4320b8a5120ec31959101c138.tar.gz
rails-6e95682b1424e8e4320b8a5120ec31959101c138.tar.bz2
rails-6e95682b1424e8e4320b8a5120ec31959101c138.zip
refactor `CommandsTasks`
Extract a common method of `Rails::CommandsTasks` and `Rails::Engine::CommandsTasks` to the module.
Diffstat (limited to 'railties/lib/rails/commands/common_commands_tasks.rb')
-rw-r--r--railties/lib/rails/commands/common_commands_tasks.rb68
1 files changed, 68 insertions, 0 deletions
diff --git a/railties/lib/rails/commands/common_commands_tasks.rb b/railties/lib/rails/commands/common_commands_tasks.rb
new file mode 100644
index 0000000000..c1484d7ae2
--- /dev/null
+++ b/railties/lib/rails/commands/common_commands_tasks.rb
@@ -0,0 +1,68 @@
+module Rails
+ module CommonCommandsTasks # :nodoc:
+ def run_command!(command)
+ command = parse_command(command)
+
+ if command_whitelist.include?(command)
+ send(command)
+ else
+ run_rake_task(command)
+ end
+ end
+
+ def generate
+ generate_or_destroy(:generate)
+ end
+
+ def destroy
+ generate_or_destroy(:destroy)
+ end
+
+ def test
+ require_command!("test")
+ end
+
+ def version
+ argv.unshift "--version"
+ require_command!("application")
+ end
+
+ def help
+ write_help_message
+ write_commands(commands)
+ end
+
+ private
+
+ def generate_or_destroy(command)
+ require "rails/generators"
+ require_application_and_environment!
+ load_generators
+ require_command!(command)
+ end
+
+ def require_command!(command)
+ require "rails/commands/#{command}"
+ end
+
+ def write_help_message
+ puts help_message
+ end
+
+ def write_commands(commands)
+ width = commands.map { |name, _| name.size }.max || 10
+ commands.each { |command| printf(" %-#{width}s %s\n", *command) }
+ end
+
+ def parse_command(command)
+ case command
+ when "--version", "-v"
+ "version"
+ when "--help", "-h"
+ "help"
+ else
+ command
+ end
+ end
+ end
+end