/
gemwhich
executable file
·89 lines (80 loc) · 1.77 KB
/
gemwhich
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
87
88
89
#!/usr/bin/env jruby
#--
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
# All rights reserved.
# See LICENSE.txt for permissions.
#++
require 'rubygems'
require 'optparse'
EXT = %w( .rb .rbw .so .dll )
$search_gems_first = false
$show_all = false
options = OptionParser.new
options.banner = "gemwhich -- Find the location of a library module."
options.separator("")
options.separator("Usage: gemwhich [options] libname...")
options.on('-a', '--all',
"Show all matching files"
) do |value|
$show_all = true
end
options.on('-g', '--gems-first',
"Search GEM libraries before non-GEMs"
) do |value|
$search_gems_first = true
end
options.on_tail('-v', '--verbose',
"Enable verbose output"
) do |value|
$verbose = value
end
options.on_tail('-h', '--help',
"Display this help message"
) do |value|
puts options
exit
end
ARGV << '-h' if ARGV.empty?
begin
options.parse!(ARGV)
rescue OptionParser::InvalidOption => ex
puts "Error: #{ex.message}"
exit
end
def find_paths(package_name, dirs)
result = []
dirs.each do |dir|
EXT.each do |ext|
full_path = File.join(dir, "#{package_name}#{ext}")
if File.exist?(full_path)
result << full_path
return result unless $show_all
end
end
end
result
end
def gem_paths(spec)
spec.require_paths.collect { |d|
File.join(spec.full_gem_path,d)
}
end
searcher = Gem::GemPathSearcher.new
ARGV.each do |arg|
dirs = $LOAD_PATH
spec = searcher.find(arg)
if spec
if $search_gems_first
dirs = gem_paths(spec) + $LOAD_PATH
else
dirs = $LOAD_PATH + gem_paths(spec)
end
puts "(checking gem #{spec.full_name} for #{arg})" if $verbose
end
paths = find_paths(arg, dirs)
if paths.empty?
puts "Can't find #{arg}"
else
puts paths
end
end