-
-
Save roberthopman/322857f5662178deac9cf54adac8d083 to your computer and use it in GitHub Desktop.
copy paste rails routes to csv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add as Rake task or Rails console | |
class CSVFormatter | |
def initialize | |
@buffer = [] | |
end | |
def result | |
@buffer.join("\n") | |
end | |
def section_title(title) | |
@buffer << "\n#{title}:" | |
end | |
def section(routes) | |
routes.map do |r| | |
@buffer << "#{r[:name]},#{r[:verb]},#{r[:path]},#{r[:reqs]}" | |
end | |
end | |
def header(routes) | |
@buffer << 'Prefix,Verb,URI Pattern,Controller#Action' | |
end | |
def no_routes | |
@buffer << <<-MESSAGE.strip_heredoc | |
You don't have any routes defined! | |
Please add some routes in config/routes.rb. | |
For more information about routes, see the Rails guide: http://guides.rubyonrails.org/routing.html. | |
MESSAGE | |
end | |
end | |
# Method to copy text to clipboard | |
def copy_to_clipboard(text) | |
case RbConfig::CONFIG['host_os'] | |
when /darwin/ | |
# macOS | |
IO.popen('pbcopy', 'w') { |f| f << text } | |
when /mswin|mingw|cygwin/ | |
# Windows | |
require 'win32/clipboard' | |
Win32::Clipboard.set_data(text) | |
when /linux|bsd/ | |
# Linux/BSD | |
if system('which xclip > /dev/null 2>&1') | |
IO.popen('xclip -selection clipboard', 'w') { |f| f << text } | |
elsif system('which xsel > /dev/null 2>&1') | |
IO.popen('xsel --clipboard --input', 'w') { |f| f << text } | |
else | |
puts "No clipboard utility found. Please install xclip or xsel." | |
return false | |
end | |
else | |
puts "Unsupported operating system for clipboard operations" | |
return false | |
end | |
true | |
end | |
all_routes = Rails.application.routes.routes | |
require 'action_dispatch/routing/inspector' | |
inspector = ActionDispatch::Routing::RoutesInspector.new(all_routes) | |
# puts inspector.format(ActionDispatch::Routing::ConsoleFormatter.new, ENV['CONTROLLER']) | |
formatter = CSVFormatter.new | |
inspector.format(formatter) | |
routes_text = formatter.result | |
# Copy to clipboard | |
if copy_to_clipboard(routes_text) | |
puts "Routes have been copied to clipboard successfully!" | |
else | |
puts "Failed to copy routes to clipboard." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment