| 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: arg = arg.sub(/#{Regexp.union(*EXT)}$/, '')
37: dirs = $LOAD_PATH
38: spec = searcher.find arg
39:
40: if spec then
41: if options[:search_gems_first] then
42: dirs = gem_paths(spec) + $LOAD_PATH
43: else
44: dirs = $LOAD_PATH + gem_paths(spec)
45: end
46: end
47:
48: paths = find_paths arg, dirs
49:
50: if paths.empty? then
51: alert_error "Can't find ruby library file or shared library #{arg}"
52: else
53: say paths
54: found = true
55: end
56: end
57:
58: terminate_interaction 1 unless found
59: end
# File lib/rubygems/commands/which_command.rb, line 61
61: def find_paths(package_name, dirs)
62: result = []
63:
64: dirs.each do |dir|
65: EXT.each do |ext|
66: full_path = File.join dir, "#{package_name}#{ext}"
67: if File.exist? full_path then
68: result << full_path
69: return result unless options[:show_all]
70: end
71: end
72: end
73:
74: result
75: end