From a2748eda586b11db59e2d0f59ab37910156d6c32 Mon Sep 17 00:00:00 2001 From: Benoit Tigeot Date: Mon, 26 Feb 2018 22:10:12 +0100 Subject: Move rake routes task to rails command After a discussion with matthewd. It was mentioned that rake tasks need to be moved to rails command. See: https://github.com/rails/rails/issues/32117 --- .../lib/rails/commands/routes/routes_command.rb | 43 ++++++++++++++++++++++ railties/lib/rails/tasks.rb | 1 - railties/lib/rails/tasks/routes.rake | 31 ---------------- 3 files changed, 43 insertions(+), 32 deletions(-) create mode 100644 railties/lib/rails/commands/routes/routes_command.rb delete mode 100644 railties/lib/rails/tasks/routes.rake (limited to 'railties/lib') diff --git a/railties/lib/rails/commands/routes/routes_command.rb b/railties/lib/rails/commands/routes/routes_command.rb new file mode 100644 index 0000000000..c4fd6c7eb5 --- /dev/null +++ b/railties/lib/rails/commands/routes/routes_command.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +require "rails/command" + +module Rails + module Command + class RoutesCommand < Base # :nodoc: + class_option :controller, aliases: "-c", type: :string, desc: "Specifies the controller." + class_option :grep_pattern, aliases: "-g", type: :string, desc: "Specifies grep pattern." + + no_commands do + def help + say "Usage: Print out all defined routes in match order, with names." + say "" + say "Target specific controller with -c option, or grep routes using -g option" + say "" + end + end + + def perform(*) + require_application_and_environment! + require "action_dispatch/routing/inspector" + + all_routes = Rails.application.routes.routes + inspector = ActionDispatch::Routing::RoutesInspector.new(all_routes) + + say inspector.format(ActionDispatch::Routing::ConsoleFormatter.new, routes_filter) + end + + private + + def routes_filter + if options.has_key?("controller") + { controller: options["controller"] } + elsif options.has_key?("grep_pattern") + options["grep_pattern"] + else + nil + end + end + end + end +end diff --git a/railties/lib/rails/tasks.rb b/railties/lib/rails/tasks.rb index 2f644a20c9..56f2eba312 100644 --- a/railties/lib/rails/tasks.rb +++ b/railties/lib/rails/tasks.rb @@ -12,7 +12,6 @@ require "rake" middleware misc restart - routes tmp yarn ).tap { |arr| diff --git a/railties/lib/rails/tasks/routes.rake b/railties/lib/rails/tasks/routes.rake deleted file mode 100644 index 403286d280..0000000000 --- a/railties/lib/rails/tasks/routes.rake +++ /dev/null @@ -1,31 +0,0 @@ -# frozen_string_literal: true - -require "optparse" - -desc "Print out all defined routes in match order, with names. Target specific controller with -c option, or grep routes using -g option" -task routes: :environment do - all_routes = Rails.application.routes.routes - require "action_dispatch/routing/inspector" - inspector = ActionDispatch::Routing::RoutesInspector.new(all_routes) - - routes_filter = nil - - OptionParser.new do |opts| - opts.banner = "Usage: rails routes [options]" - - Rake.application.standard_rake_options.each { |args| opts.on(*args) } - - opts.on("-c CONTROLLER") do |controller| - routes_filter = { controller: controller } - end - - opts.on("-g PATTERN") do |pattern| - routes_filter = pattern - end - - end.parse!(ARGV.reject { |x| x == "routes" }) - - puts inspector.format(ActionDispatch::Routing::ConsoleFormatter.new, routes_filter) - - exit 0 # ensure extra arguments aren't interpreted as Rake tasks -end -- cgit v1.2.3