-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclass_method_file_xref.rb
executable file
·86 lines (69 loc) · 1.79 KB
/
class_method_file_xref.rb
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
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env ruby
# encoding: utf-8
# frozen_string_literal: true
# warn_indent: true
##########################################################
###
## File: class_method_file_xref.rb
## Desc: Create a cross reference of class, methods and file paths
## in markdown format to STDOUT
##
## By: Dewayne VanHoozer ([email protected])
#
# gem install ripper-tags
#
if defined?(RubyVM::YJIT)
RubyVM::YJIT.enable if RubyVM::YJIT.respond_to? :enable
end
require 'json'
require 'pathname'
def usage
puts <<~USAGE
#{Pathname.new(__FILE__).basename} [directory]
The default directory is the PWD
Sends a markdown formatted table to STDOUT that
contains the class name, method name and file path
for all methods defined in the *.rb files located
in the specified directory.
USAGE
end
if ARGV.any? {|arg| %w[-h --help].include? arg}
usage
exit
end
base_path = if ARGV.empty?
Pathname.pwd
else
Pathname.new(ARGV.shift)
end
unless base_path.exist? && base_path.directory?
STDERR.print "\nERROR: #{base_path} must be an existing directory\n\n"
exit(1)
end
root_path = base_path.parent
# Create a 'tags' file at Pathname.pwd
system <<~COMMAND
ripper-tags \
-f cmpx_tags \
--tag-relative=yes \
--format json \
--recursive \
--all-files \
#{base_path}
COMMAND
tags = Pathname.new('cmpx_tags')
data = JSON.parse(tags.read)
.select{|e| "method" == e['kind']}
.sort_by { |e| e["class"] + e["name"] }
tags.delete
puts <<~HEADER
| Class Name | Method Name | Path to File |
| ---------- | ----------- | ------------ |
HEADER
data.each do |e|
print "| #{e['class']} "
print "| #{e['name'] } "
path = Pathname.new(e['path']).relative_path_from(root_path)
print "| #{path} "
puts "|"
end