From 74d04dd1d60738e80896dbc787846e35dd7a2578 Mon Sep 17 00:00:00 2001 From: Lud Date: Sun, 19 May 2024 21:14:31 +0800 Subject: [PATCH] feat: rake task to list all routes --- lib/cli/commands/new_app/files/rakefile.rb | 3 +++ lib/tasks/routes.rake | 26 ++++++++++++++++++++++ spec/test_app/Rakefile | 3 +++ 3 files changed, 32 insertions(+) create mode 100644 lib/tasks/routes.rake diff --git a/lib/cli/commands/new_app/files/rakefile.rb b/lib/cli/commands/new_app/files/rakefile.rb index d7d397a..e8ffd5b 100644 --- a/lib/cli/commands/new_app/files/rakefile.rb +++ b/lib/cli/commands/new_app/files/rakefile.rb @@ -15,6 +15,9 @@ def self.content # frozen_string_literal: true require "rake" + require_relative "app" + + Dir.glob("#{Kirei::GEM_ROOT}/lib/tasks/**/*.rake").each { import(_1) } Dir.glob("lib/tasks/**/*.rake").each { import(_1) } diff --git a/lib/tasks/routes.rake b/lib/tasks/routes.rake new file mode 100644 index 0000000..d52262d --- /dev/null +++ b/lib/tasks/routes.rake @@ -0,0 +1,26 @@ +# typed: false +# frozen_string_literal: true + +require_relative("../kirei") + +namespace :kirei do + desc "Prints all available routes" + task :routes do + router = Kirei::Routing::Router.instance + + longest_path = router.routes.keys.map(&:length).max + + routes_by_controller = router.routes.values.group_by(&:controller) + + puts "\n" + + routes_by_controller.each do |controller, routes| + puts "#{controller}:" + routes.each do |route| + verb = route.verb.serialize.upcase.ljust(7 + 3) # 7 is the length of the longest verb + puts "#{verb} #{route.path.ljust(longest_path + 1)} => ##{route.action}" + end + puts "\n" + end + end +end diff --git a/spec/test_app/Rakefile b/spec/test_app/Rakefile index 81d157b..8eb4660 100644 --- a/spec/test_app/Rakefile +++ b/spec/test_app/Rakefile @@ -2,5 +2,8 @@ # frozen_string_literal: true require "rake" +require_relative "app" + +Dir.glob("#{Kirei::GEM_ROOT}/lib/tasks/**/*.rake").each { import(_1) } Dir.glob("lib/tasks/**/*.rake").each { import(_1) }