mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #32121 from benoittgt/move-rails-routes-to-rails-command
Move rake routes task to rails command
This commit is contained in:
commit
6bd33d66dd
5 changed files with 170 additions and 143 deletions
43
railties/lib/rails/commands/routes/routes_command.rb
Normal file
43
railties/lib/rails/commands/routes/routes_command.rb
Normal file
|
@ -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
|
|
@ -12,7 +12,6 @@ require "rake"
|
||||||
middleware
|
middleware
|
||||||
misc
|
misc
|
||||||
restart
|
restart
|
||||||
routes
|
|
||||||
tmp
|
tmp
|
||||||
yarn
|
yarn
|
||||||
).tap { |arr|
|
).tap { |arr|
|
||||||
|
|
|
@ -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
|
|
|
@ -162,117 +162,6 @@ module ApplicationTests
|
||||||
assert_equal expected_output, output
|
assert_equal expected_output, output
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_rails_routes_with_global_search_key
|
|
||||||
app_file "config/routes.rb", <<-RUBY
|
|
||||||
Rails.application.routes.draw do
|
|
||||||
get '/cart', to: 'cart#show'
|
|
||||||
post '/cart', to: 'cart#create'
|
|
||||||
get '/basketballs', to: 'basketball#index'
|
|
||||||
end
|
|
||||||
RUBY
|
|
||||||
|
|
||||||
output = rails("routes", "-g", "show")
|
|
||||||
assert_equal <<~MESSAGE, output
|
|
||||||
Prefix Verb URI Pattern Controller#Action
|
|
||||||
cart GET /cart(.:format) cart#show
|
|
||||||
rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
|
|
||||||
rails_blob_variation GET /rails/active_storage/variants/:signed_blob_id/:variation_key/*filename(.:format) active_storage/variants#show
|
|
||||||
rails_blob_preview GET /rails/active_storage/previews/:signed_blob_id/:variation_key/*filename(.:format) active_storage/previews#show
|
|
||||||
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
|
|
||||||
MESSAGE
|
|
||||||
|
|
||||||
output = rails("routes", "-g", "POST")
|
|
||||||
assert_equal <<~MESSAGE, output
|
|
||||||
Prefix Verb URI Pattern Controller#Action
|
|
||||||
POST /cart(.:format) cart#create
|
|
||||||
rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
|
|
||||||
MESSAGE
|
|
||||||
|
|
||||||
output = rails("routes", "-g", "basketballs")
|
|
||||||
assert_equal " Prefix Verb URI Pattern Controller#Action\n" \
|
|
||||||
"basketballs GET /basketballs(.:format) basketball#index\n", output
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_rails_routes_with_controller_search_key
|
|
||||||
app_file "config/routes.rb", <<-RUBY
|
|
||||||
Rails.application.routes.draw do
|
|
||||||
get '/cart', to: 'cart#show'
|
|
||||||
get '/basketball', to: 'basketball#index'
|
|
||||||
end
|
|
||||||
RUBY
|
|
||||||
|
|
||||||
output = rails("routes", "-c", "cart")
|
|
||||||
assert_equal "Prefix Verb URI Pattern Controller#Action\n cart GET /cart(.:format) cart#show\n", output
|
|
||||||
|
|
||||||
output = rails("routes", "-c", "Cart")
|
|
||||||
assert_equal "Prefix Verb URI Pattern Controller#Action\n cart GET /cart(.:format) cart#show\n", output
|
|
||||||
|
|
||||||
output = rails("routes", "-c", "CartController")
|
|
||||||
assert_equal "Prefix Verb URI Pattern Controller#Action\n cart GET /cart(.:format) cart#show\n", output
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_rails_routes_with_namespaced_controller_search_key
|
|
||||||
app_file "config/routes.rb", <<-RUBY
|
|
||||||
Rails.application.routes.draw do
|
|
||||||
namespace :admin do
|
|
||||||
resource :post
|
|
||||||
end
|
|
||||||
end
|
|
||||||
RUBY
|
|
||||||
expected_output = [" Prefix Verb URI Pattern Controller#Action",
|
|
||||||
" new_admin_post GET /admin/post/new(.:format) admin/posts#new",
|
|
||||||
"edit_admin_post GET /admin/post/edit(.:format) admin/posts#edit",
|
|
||||||
" admin_post GET /admin/post(.:format) admin/posts#show",
|
|
||||||
" PATCH /admin/post(.:format) admin/posts#update",
|
|
||||||
" PUT /admin/post(.:format) admin/posts#update",
|
|
||||||
" DELETE /admin/post(.:format) admin/posts#destroy",
|
|
||||||
" POST /admin/post(.:format) admin/posts#create\n"].join("\n")
|
|
||||||
|
|
||||||
output = rails("routes", "-c", "Admin::PostController")
|
|
||||||
assert_equal expected_output, output
|
|
||||||
|
|
||||||
output = rails("routes", "-c", "PostController")
|
|
||||||
assert_equal expected_output, output
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_rails_routes_displays_message_when_no_routes_are_defined
|
|
||||||
app_file "config/routes.rb", <<-RUBY
|
|
||||||
Rails.application.routes.draw do
|
|
||||||
end
|
|
||||||
RUBY
|
|
||||||
|
|
||||||
assert_equal <<~MESSAGE, rails("routes")
|
|
||||||
Prefix Verb URI Pattern Controller#Action
|
|
||||||
rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
|
|
||||||
rails_blob_variation GET /rails/active_storage/variants/:signed_blob_id/:variation_key/*filename(.:format) active_storage/variants#show
|
|
||||||
rails_blob_preview GET /rails/active_storage/previews/:signed_blob_id/:variation_key/*filename(.:format) active_storage/previews#show
|
|
||||||
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
|
|
||||||
update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
|
|
||||||
rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
|
|
||||||
MESSAGE
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_rake_routes_with_rake_options
|
|
||||||
app_file "config/routes.rb", <<-RUBY
|
|
||||||
Rails.application.routes.draw do
|
|
||||||
get '/cart', to: 'cart#show'
|
|
||||||
end
|
|
||||||
RUBY
|
|
||||||
|
|
||||||
output = Dir.chdir(app_path) { `bin/rake --rakefile Rakefile routes` }
|
|
||||||
|
|
||||||
assert_equal <<~MESSAGE, output
|
|
||||||
Prefix Verb URI Pattern Controller#Action
|
|
||||||
cart GET /cart(.:format) cart#show
|
|
||||||
rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
|
|
||||||
rails_blob_variation GET /rails/active_storage/variants/:signed_blob_id/:variation_key/*filename(.:format) active_storage/variants#show
|
|
||||||
rails_blob_preview GET /rails/active_storage/previews/:signed_blob_id/:variation_key/*filename(.:format) active_storage/previews#show
|
|
||||||
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
|
|
||||||
update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
|
|
||||||
rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
|
|
||||||
MESSAGE
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_logger_is_flushed_when_exiting_production_rake_tasks
|
def test_logger_is_flushed_when_exiting_production_rake_tasks
|
||||||
add_to_config <<-RUBY
|
add_to_config <<-RUBY
|
||||||
rake_tasks do
|
rake_tasks do
|
||||||
|
|
127
railties/test/commands/routes_test.rb
Normal file
127
railties/test/commands/routes_test.rb
Normal file
|
@ -0,0 +1,127 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "isolation/abstract_unit"
|
||||||
|
require "env_helpers"
|
||||||
|
require "rails/command"
|
||||||
|
require "rails/commands/routes/routes_command"
|
||||||
|
|
||||||
|
class Rails::Command::RoutesTest < ActiveSupport::TestCase
|
||||||
|
setup :build_app
|
||||||
|
teardown :teardown_app
|
||||||
|
|
||||||
|
test "test singular resource output in rake routes" do
|
||||||
|
app_file "config/routes.rb", <<-RUBY
|
||||||
|
Rails.application.routes.draw do
|
||||||
|
resource :post
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
expected_output = [" Prefix Verb URI Pattern Controller#Action",
|
||||||
|
" new_post GET /post/new(.:format) posts#new",
|
||||||
|
"edit_post GET /post/edit(.:format) posts#edit",
|
||||||
|
" post GET /post(.:format) posts#show",
|
||||||
|
" PATCH /post(.:format) posts#update",
|
||||||
|
" PUT /post(.:format) posts#update",
|
||||||
|
" DELETE /post(.:format) posts#destroy",
|
||||||
|
" POST /post(.:format) posts#create\n"].join("\n")
|
||||||
|
|
||||||
|
output = run_routes_command(["-c", "PostController"])
|
||||||
|
assert_equal expected_output, output
|
||||||
|
end
|
||||||
|
|
||||||
|
test "test rails routes with global search key" do
|
||||||
|
app_file "config/routes.rb", <<-RUBY
|
||||||
|
Rails.application.routes.draw do
|
||||||
|
get '/cart', to: 'cart#show'
|
||||||
|
post '/cart', to: 'cart#create'
|
||||||
|
get '/basketballs', to: 'basketball#index'
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
output = run_routes_command(["-g", "show"])
|
||||||
|
assert_equal <<~MESSAGE, output
|
||||||
|
Prefix Verb URI Pattern Controller#Action
|
||||||
|
cart GET /cart(.:format) cart#show
|
||||||
|
rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
|
||||||
|
rails_blob_variation GET /rails/active_storage/variants/:signed_blob_id/:variation_key/*filename(.:format) active_storage/variants#show
|
||||||
|
rails_blob_preview GET /rails/active_storage/previews/:signed_blob_id/:variation_key/*filename(.:format) active_storage/previews#show
|
||||||
|
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
|
||||||
|
MESSAGE
|
||||||
|
|
||||||
|
output = run_routes_command(["-g", "POST"])
|
||||||
|
assert_equal <<~MESSAGE, output
|
||||||
|
Prefix Verb URI Pattern Controller#Action
|
||||||
|
POST /cart(.:format) cart#create
|
||||||
|
rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
|
||||||
|
MESSAGE
|
||||||
|
|
||||||
|
output = run_routes_command(["-g", "basketballs"])
|
||||||
|
assert_equal " Prefix Verb URI Pattern Controller#Action\n" \
|
||||||
|
"basketballs GET /basketballs(.:format) basketball#index\n", output
|
||||||
|
end
|
||||||
|
|
||||||
|
test "test rails routes with controller search_key" do
|
||||||
|
app_file "config/routes.rb", <<-RUBY
|
||||||
|
Rails.application.routes.draw do
|
||||||
|
get '/cart', to: 'cart#show'
|
||||||
|
get '/basketball', to: 'basketball#index'
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
output = run_routes_command(["-c", "cart"])
|
||||||
|
assert_equal "Prefix Verb URI Pattern Controller#Action\n cart GET /cart(.:format) cart#show\n", output
|
||||||
|
|
||||||
|
output = run_routes_command(["routes", "-c", "Cart"])
|
||||||
|
assert_equal "Prefix Verb URI Pattern Controller#Action\n cart GET /cart(.:format) cart#show\n", output
|
||||||
|
|
||||||
|
output = run_routes_command(["-c", "CartController"])
|
||||||
|
assert_equal "Prefix Verb URI Pattern Controller#Action\n cart GET /cart(.:format) cart#show\n", output
|
||||||
|
end
|
||||||
|
|
||||||
|
test "test rails routes with namespaced controller search key" do
|
||||||
|
app_file "config/routes.rb", <<-RUBY
|
||||||
|
Rails.application.routes.draw do
|
||||||
|
namespace :admin do
|
||||||
|
resource :post
|
||||||
|
end
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
expected_output = [" Prefix Verb URI Pattern Controller#Action",
|
||||||
|
" new_admin_post GET /admin/post/new(.:format) admin/posts#new",
|
||||||
|
"edit_admin_post GET /admin/post/edit(.:format) admin/posts#edit",
|
||||||
|
" admin_post GET /admin/post(.:format) admin/posts#show",
|
||||||
|
" PATCH /admin/post(.:format) admin/posts#update",
|
||||||
|
" PUT /admin/post(.:format) admin/posts#update",
|
||||||
|
" DELETE /admin/post(.:format) admin/posts#destroy",
|
||||||
|
" POST /admin/post(.:format) admin/posts#create\n"].join("\n")
|
||||||
|
|
||||||
|
output = run_routes_command(["-c", "Admin::PostController"])
|
||||||
|
assert_equal expected_output, output
|
||||||
|
|
||||||
|
output = run_routes_command(["-c", "PostController"])
|
||||||
|
assert_equal expected_output, output
|
||||||
|
end
|
||||||
|
|
||||||
|
test "test rails routes displays message when no routes are defined" do
|
||||||
|
app_file "config/routes.rb", <<-RUBY
|
||||||
|
Rails.application.routes.draw do
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
assert_equal <<~MESSAGE, run_routes_command
|
||||||
|
Prefix Verb URI Pattern Controller#Action
|
||||||
|
rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
|
||||||
|
rails_blob_variation GET /rails/active_storage/variants/:signed_blob_id/:variation_key/*filename(.:format) active_storage/variants#show
|
||||||
|
rails_blob_preview GET /rails/active_storage/previews/:signed_blob_id/:variation_key/*filename(.:format) active_storage/previews#show
|
||||||
|
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
|
||||||
|
update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
|
||||||
|
rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
|
||||||
|
MESSAGE
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def run_routes_command(args = [])
|
||||||
|
rails "routes", args
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue