2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2019-04-22 07:56:16 -04:00
|
|
|
require_relative "../command"
|
2007-11-10 02:48:56 -05:00
|
|
|
|
|
|
|
class Gem::Commands::WhichCommand < Gem::Command
|
|
|
|
def initialize
|
2008-09-25 06:13:50 -04:00
|
|
|
super "which", "Find the location of a library file you can require",
|
2007-11-10 02:48:56 -05:00
|
|
|
:search_gems_first => false, :show_all => false
|
|
|
|
|
|
|
|
add_option "-a", "--[no-]all", "show all matching files" do |show_all, options|
|
|
|
|
options[:show_all] = show_all
|
|
|
|
end
|
|
|
|
|
|
|
|
add_option "-g", "--[no-]gems-first",
|
|
|
|
"search gems before non-gems" do |gems_first, options|
|
|
|
|
options[:search_gems_first] = gems_first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def arguments # :nodoc:
|
|
|
|
"FILE name of file to find"
|
|
|
|
end
|
|
|
|
|
|
|
|
def defaults_str # :nodoc:
|
|
|
|
"--no-gems-first --no-all"
|
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
def description # :nodoc:
|
|
|
|
<<-EOF
|
|
|
|
The which command is like the shell which command and shows you where
|
|
|
|
the file you wish to require lives.
|
|
|
|
|
|
|
|
You can use the which command to help determine why you are requiring a
|
|
|
|
version you did not expect or to look at the content of a file you are
|
|
|
|
requiring to see why it does not behave as you expect.
|
|
|
|
EOF
|
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def execute
|
2013-11-18 19:34:13 -05:00
|
|
|
found = true
|
2010-02-21 21:52:35 -05:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
options[:args].each do |arg|
|
2011-05-31 23:45:05 -04:00
|
|
|
arg = arg.sub(/#{Regexp.union(*Gem.suffixes)}$/, "")
|
2007-11-10 02:48:56 -05:00
|
|
|
dirs = $LOAD_PATH
|
2011-05-31 23:45:05 -04:00
|
|
|
|
|
|
|
spec = Gem::Specification.find_by_path arg
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
if spec
|
|
|
|
if options[:search_gems_first]
|
2013-09-18 17:29:41 -04:00
|
|
|
dirs = spec.full_require_paths + $LOAD_PATH
|
2007-11-10 02:48:56 -05:00
|
|
|
else
|
2013-09-18 17:29:41 -04:00
|
|
|
dirs = $LOAD_PATH + spec.full_require_paths
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
paths = find_paths arg, dirs
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
if paths.empty?
|
2017-10-07 21:32:18 -04:00
|
|
|
alert_error "Can't find Ruby library file or shared library #{arg}"
|
2019-06-22 18:41:07 -04:00
|
|
|
found = false
|
2007-11-10 02:48:56 -05:00
|
|
|
else
|
|
|
|
say paths
|
|
|
|
end
|
|
|
|
end
|
2010-02-21 21:52:35 -05:00
|
|
|
|
|
|
|
terminate_interaction 1 unless found
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_paths(package_name, dirs)
|
|
|
|
result = []
|
|
|
|
|
|
|
|
dirs.each do |dir|
|
2011-05-31 23:45:05 -04:00
|
|
|
Gem.suffixes.each do |ext|
|
2007-11-10 02:48:56 -05:00
|
|
|
full_path = File.join dir, "#{package_name}#{ext}"
|
2022-08-08 22:16:07 -04:00
|
|
|
if File.exist?(full_path) && !File.directory?(full_path)
|
2007-11-10 02:48:56 -05:00
|
|
|
result << full_path
|
|
|
|
return result unless options[:show_all]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def usage # :nodoc:
|
2010-02-21 21:52:35 -05:00
|
|
|
"#{program_name} FILE [FILE ...]"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
end
|