2011-01-18 19:08:49 -05:00
|
|
|
######################################################################
|
|
|
|
# This file is imported from the rubygems project.
|
|
|
|
# DO NOT make modifications in this repo. They _will_ be reverted!
|
|
|
|
# File a patch instead and assign it to Ryan Davis or Eric Hodel.
|
|
|
|
######################################################################
|
|
|
|
|
2008-09-25 06:13:50 -04:00
|
|
|
##
|
2007-11-10 02:48:56 -05:00
|
|
|
# GemPathSearcher has the capability to find loadable files inside
|
|
|
|
# gems. It generates data up front to speed up searches later.
|
2008-09-25 06:13:50 -04:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
class Gem::GemPathSearcher
|
|
|
|
|
2008-09-25 06:13:50 -04:00
|
|
|
##
|
2007-11-10 02:48:56 -05:00
|
|
|
# Initialise the data we need to make searches later.
|
2008-09-25 06:13:50 -04:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def initialize
|
2010-02-21 21:52:35 -05:00
|
|
|
# We want a record of all the installed gemspecs, in the order we wish to
|
|
|
|
# examine them.
|
2011-03-01 04:41:32 -05:00
|
|
|
# TODO: remove this stupid method
|
2007-11-10 02:48:56 -05:00
|
|
|
@gemspecs = init_gemspecs
|
2010-02-21 21:52:35 -05:00
|
|
|
|
|
|
|
# Map gem spec to glob of full require_path directories. Preparing this
|
|
|
|
# information may speed up searches later.
|
2007-11-10 02:48:56 -05:00
|
|
|
@lib_dirs = {}
|
2010-02-21 21:52:35 -05:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
@gemspecs.each do |spec|
|
2010-02-21 21:52:35 -05:00
|
|
|
@lib_dirs[spec.object_id] = lib_dirs_for spec
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-09-25 06:13:50 -04:00
|
|
|
##
|
2011-01-18 19:08:49 -05:00
|
|
|
# Look in all the installed gems until a matching +glob+ is found.
|
2007-11-10 02:48:56 -05:00
|
|
|
# Return the _gemspec_ of the gem where it was found. If no match
|
|
|
|
# is found, return nil.
|
|
|
|
#
|
|
|
|
# The gems are searched in alphabetical order, and in reverse
|
|
|
|
# version order.
|
|
|
|
#
|
|
|
|
# For example:
|
|
|
|
#
|
|
|
|
# find('log4r') # -> (log4r-1.1 spec)
|
|
|
|
# find('log4r.rb') # -> (log4r-1.1 spec)
|
|
|
|
# find('rake/rdoctask') # -> (rake-0.4.12 spec)
|
|
|
|
# find('foobarbaz') # -> nil
|
|
|
|
#
|
|
|
|
# Matching paths can have various suffixes ('.rb', '.so', and
|
|
|
|
# others), which may or may not already be attached to _file_.
|
|
|
|
# This method doesn't care about the full filename that matches;
|
|
|
|
# only that there is a match.
|
2008-09-25 06:13:50 -04:00
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
def find(glob)
|
2011-03-01 04:41:32 -05:00
|
|
|
# HACK violation of encapsulation
|
2011-01-18 19:08:49 -05:00
|
|
|
@gemspecs.find do |spec|
|
2011-03-01 04:41:32 -05:00
|
|
|
# TODO: inverted responsibility
|
2011-01-18 19:08:49 -05:00
|
|
|
matching_file? spec, glob
|
|
|
|
end
|
2008-09-25 06:13:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2011-01-18 19:08:49 -05:00
|
|
|
# Works like #find, but finds all gemspecs matching +glob+.
|
2008-09-25 06:13:50 -04:00
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
def find_all(glob)
|
2011-03-01 04:41:32 -05:00
|
|
|
# HACK violation of encapsulation
|
2008-09-25 06:13:50 -04:00
|
|
|
@gemspecs.select do |spec|
|
2011-03-01 04:41:32 -05:00
|
|
|
# TODO: inverted responsibility
|
2011-01-18 19:08:49 -05:00
|
|
|
matching_file? spec, glob
|
2011-03-01 04:41:32 -05:00
|
|
|
end || []
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_in_unresolved(glob)
|
|
|
|
# HACK violation
|
|
|
|
specs = Gem.unresolved_deps.values.map { |dep|
|
|
|
|
Gem.source_index.search dep, true
|
|
|
|
}.flatten
|
|
|
|
|
|
|
|
specs.select do |spec|
|
|
|
|
# TODO: inverted responsibility
|
|
|
|
matching_file? spec, glob
|
|
|
|
end || []
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_in_unresolved_tree glob
|
|
|
|
# HACK violation
|
|
|
|
# TODO: inverted responsibility
|
|
|
|
specs = Gem.unresolved_deps.values.map { |dep|
|
|
|
|
Gem.source_index.search dep, true
|
|
|
|
}.flatten
|
|
|
|
|
|
|
|
specs.reverse_each do |spec|
|
|
|
|
trails = matching_paths(spec, glob)
|
|
|
|
next if trails.empty?
|
|
|
|
return trails.map(&:reverse).sort.first.reverse
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2011-03-01 04:41:32 -05:00
|
|
|
|
|
|
|
[]
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2008-09-25 06:13:50 -04:00
|
|
|
##
|
|
|
|
# Attempts to find a matching path using the require_paths of the given
|
|
|
|
# +spec+.
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2008-09-25 06:13:50 -04:00
|
|
|
def matching_file?(spec, path)
|
2011-03-01 04:41:32 -05:00
|
|
|
not matching_files(spec, path).empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def matching_paths(spec, path)
|
|
|
|
trails = []
|
|
|
|
|
|
|
|
spec.traverse do |from_spec, dep, to_spec, trail|
|
|
|
|
next unless to_spec.conflicts.empty?
|
|
|
|
trails << trail unless matching_files(to_spec, path).empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
trails
|
2008-09-25 06:13:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Returns files matching +path+ in +spec+.
|
|
|
|
#--
|
|
|
|
# Some of the intermediate results are cached in @lib_dirs for speed.
|
|
|
|
|
|
|
|
def matching_files(spec, path)
|
2010-02-21 21:52:35 -05:00
|
|
|
return [] unless @lib_dirs[spec.object_id] # case no paths
|
2010-08-08 03:42:51 -04:00
|
|
|
glob = File.join @lib_dirs[spec.object_id], "#{path}#{Gem.suffix_pattern}"
|
|
|
|
Dir[glob].select { |f| File.file? f.untaint }
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2008-09-25 06:13:50 -04:00
|
|
|
##
|
|
|
|
# Return a list of all installed gemspecs, sorted by alphabetical order and
|
2009-06-09 17:38:59 -04:00
|
|
|
# in reverse version order. (bar-2, bar-1, foo-2)
|
2008-09-25 06:13:50 -04:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def init_gemspecs
|
2009-06-09 17:38:59 -04:00
|
|
|
specs = Gem.source_index.map { |_, spec| spec }
|
|
|
|
|
|
|
|
specs.sort { |a, b|
|
|
|
|
names = a.name <=> b.name
|
|
|
|
next names if names.nonzero?
|
|
|
|
b.version <=> a.version
|
2007-11-10 02:48:56 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-09-25 06:13:50 -04:00
|
|
|
##
|
2007-11-10 02:48:56 -05:00
|
|
|
# Returns library directories glob for a gemspec. For example,
|
|
|
|
# '/usr/local/lib/ruby/gems/1.8/gems/foobar-1.0/{lib,ext}'
|
2008-09-25 06:13:50 -04:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def lib_dirs_for(spec)
|
2010-02-21 21:52:35 -05:00
|
|
|
"#{spec.full_gem_path}/{#{spec.require_paths.join(',')}}" if
|
|
|
|
spec.require_paths
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|