| Class | Gem::Commands::WhichCommand |
| In: |
lib/rubygems/commands/which_command.rb
|
| Parent: | Gem::Command |
| EXT | = | %w[.rb .rbw .so .dll .bundle] |
# File lib/rubygems/commands/which_command.rb, line 8
8: def initialize
9: super 'which', 'Find the location of a library file you can require',
10: :search_gems_first => false, :show_all => false
11:
12: add_option '-a', '--[no-]all', 'show all matching files' do |show_all, options|
13: options[:show_all] = show_all
14: end
15:
16: add_option '-g', '--[no-]gems-first',
17: 'search gems before non-gems' do |gems_first, options|
18: options[:search_gems_first] = gems_first
19: end
20: end
# File lib/rubygems/commands/which_command.rb, line 30
30: def execute
31: searcher = Gem::GemPathSearcher.new
32:
33: found = false
34:
35: options[:args].each do |arg|
36: dirs = $LOAD_PATH
37: spec = searcher.find arg
38:
39: if spec then
40: if options[:search_gems_first] then
41: dirs = gem_paths(spec) + $LOAD_PATH
42: else
43: dirs = $LOAD_PATH + gem_paths(spec)
44: end
45: end
46:
47: paths = find_paths arg, dirs
48:
49: if paths.empty? then
50: alert_error "Can't find ruby library file or shared library #{arg}"
51: else
52: say paths
53: found = true
54: end
55: end
56:
57: terminate_interaction 1 unless found
58: end
# File lib/rubygems/commands/which_command.rb, line 60
60: def find_paths(package_name, dirs)
61: result = []
62:
63: dirs.each do |dir|
64: EXT.each do |ext|
65: full_path = File.join dir, "#{package_name}#{ext}"
66: if File.exist? full_path then
67: result << full_path
68: return result unless options[:show_all]
69: end
70: end
71: end
72:
73: result
74: end