Repository navigation
Expand file tree
/
Copy pathboutes
More file actions
executable file
·73 lines (59 loc) · 2.16 KB
/
Copy pathboutes
File metadata and controls
executable file
·73 lines (59 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env ruby
# boutes - bundle exec rails routes cache
require "colorize"
require "optimist"
require_relative "lib/dbrady_cli"
String.disable_colorization unless $stdout.tty?
class Application
include DbradyCli
opt_flag :grape, :line_numbers, :rails
opt_reader :output
DEFAULT_BOUTES_PATH = File.expand_path("tmp/rails-routes.log").freeze
def run
@opts = Optimist.options do
banner <<~BANNER
Usage:
boutes - create boutes cache
boutes [<regex>] - search boutes cache
Options:
BANNER
opt :debug, "Print extra debug info", default: false
opt :grape, "Emit grape API routes", default: true
opt :line_numbers, "Include line numbers in output", short: :l, default: false
opt :output, "Name of file to output", default: DEFAULT_BOUTES_PATH
opt :pretend, "Print commands but do not run them", default: false
opt :rails, "Emit rails routes", default: true
opt :quiet, "Run with minimal output", default: false
opt :verbose, "Run with verbose output (overrides --quiet)", default: false
end
opts[:quiet] = !opts[:verbose] if opts[:verbose_given]
puts opts.inspect if opts[:debug]
cache_routes if ARGV.empty? || !boutes_file_exists?
run_command %Q|ag --nonumbers -i "#{ARGV.first}" "#{boutes_path}"| unless ARGV.empty?
end
def cache_routes
puts "Caching routes."
run_command "bin/rails routes > #{boutes_path}" if rails?
run_command "bin/rails grape:routes >> #{boutes_path}" if grape?
number_boute_file_lines
end
def boutes_file_exists?
File.exist?(boutes_path)
end
def number_boute_file_lines
lines = File.readlines(boutes_path).map(&:rstrip)
digits = 1 + (Math.log(lines.size) / Math.log(100)).ceil
format = "%#{digits}d | %s"
lines_with_numbers = lines.map.with_index(1) do |line, index|
format % [index, line]
end
output_lines = line_numbers? ? lines_with_numbers : lines
File.open(boutes_path, "w") { |fp| fp.puts output_lines }
end
def boutes_path
@boutes_path ||= opts[:output_given] ? opts[:output] : DEFAULT_BOUTES_PATH
end
end
if __FILE__ == $0
Application.new.run
end