Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 114 lines (89 sloc) 3.387 kB
ea8e85c @jimweirich added copyright and license information, added datadir methods
jimweirich authored
1 #--
2 # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
3 # All rights reserved.
4 # See LICENSE.txt for permissions.
5 #++
6
35dd71d @drbrain Correctly include documentation for methods added to Kernel
drbrain authored
7 module Kernel
fd92455 @gsinclair * lib/rubygems/custom_require.rb: new file to replace
gsinclair authored
8
e814d62 @drbrain Override the custom_require that ships with ruby 1.9.
drbrain authored
9 if defined?(gem_original_require) then
10 # Ruby ships with a custom_require, override its require
11 remove_method :require
12 else
13 ##
14 # The Kernel#require from before RubyGems was loaded.
15
16 alias gem_original_require require
17 private :gem_original_require
18 end
28aca90 @drbrain Privatize kernel methods, expose Kernel extensions to RDoc.
drbrain authored
19
20 ##
21 # When RubyGems is required, Kernel#require is replaced with our own which
22 # is capable of loading gems on demand.
fd92455 @gsinclair * lib/rubygems/custom_require.rb: new file to replace
gsinclair authored
23 #
24 # When you call <tt>require 'x'</tt>, this is what happens:
2a08287 @jimweirich Some reformatting of comments.
jimweirich authored
25 # * If the file can be loaded from the existing Ruby loadpath, it
26 # is.
27 # * Otherwise, installed gems are searched for a file that matches.
28 # If it's found in gem 'y', that gem is activated (added to the
29 # loadpath).
fd92455 @gsinclair * lib/rubygems/custom_require.rb: new file to replace
gsinclair authored
30 #
2a08287 @jimweirich Some reformatting of comments.
jimweirich authored
31 # The normal <tt>require</tt> functionality of returning false if
32 # that file has already been loaded is preserved.
28aca90 @drbrain Privatize kernel methods, expose Kernel extensions to RDoc.
drbrain authored
33
b06875e @zenspider + #require now checks unresolved dependencies first
zenspider authored
34 def require path
3c3a3bb @evanphx Restructure #require, add tests for exceptional cases
evanphx authored
35 # If there are no unresolved deps, then we can use just try
36 # normal require handle loading a gem from the rescue below.
37
c917125 @evanphx - Remove Gem.loaded_path and uses
evanphx authored
38 if Gem::Specification.unresolved_deps.empty? then
3c3a3bb @evanphx Restructure #require, add tests for exceptional cases
evanphx authored
39 return gem_original_require(path)
40 end
41
42 # If +path+ is for a gem that has already been loaded, don't
43 # bother trying to find it in an unresolved gem, just go straight
44 # to normal require.
bb81071 @drbrain Add TODO for various improvements to RubyGems.
drbrain authored
45 #--
46 # TODO request access to the C implementation of this to speed up RubyGems
3c3a3bb @evanphx Restructure #require, add tests for exceptional cases
evanphx authored
47
48 spec = Gem::Specification.find { |s|
49 s.activated? and s.contains_requirable_file? path
50 }
51
52 return gem_original_require(path) if spec
53
54 # Attempt to find +path+ in any unresolved gems...
55
56 found_specs = Gem::Specification.find_in_unresolved path
57
58 # If there are no directly unresolved gems, then try and find +path+
59 # in any gems that are available via the currently unresolved gems.
60 # For example, given:
61 #
62 # a => b => c => d
63 #
64 # If a and b are currently active with c being unresolved and d.rb is
65 # requested, then find_in_unresolved_tree will find d.rb in d because
66 # it's a dependency of c.
67 #
68 if found_specs.empty? then
69 found_specs = Gem::Specification.find_in_unresolved_tree path
70
71 found_specs.each do |found_spec|
72 found_spec.activate
73 end
74
75 # We found +path+ directly in an unresolved gem. Now we figure out, of
76 # the possible found specs, which one we should activate.
b06875e @zenspider + #require now checks unresolved dependencies first
zenspider authored
77 else
3c3a3bb @evanphx Restructure #require, add tests for exceptional cases
evanphx authored
78
79 # Check that all the found specs are just different
80 # versions of the same gem
81 names = found_specs.map(&:name).uniq
82
83 if names.size > 1 then
84 raise Gem::LoadError, "#{path} found in multiple gems: #{names.join ', '}"
b06875e @zenspider + #require now checks unresolved dependencies first
zenspider authored
85 end
98594cb @zenspider Nailed the last activation/require test
zenspider authored
86
3c3a3bb @evanphx Restructure #require, add tests for exceptional cases
evanphx authored
87 # Ok, now find a gem that has no conflicts, starting
88 # at the highest version.
89 valid = found_specs.select { |s| s.conflicts.empty? }.last
90
91 unless valid then
92 le = Gem::LoadError.new "unable to find a version of '#{names.first}' to activate"
93 le.name = names.first
94 raise le
95 end
96
97 valid.activate
b06875e @zenspider + #require now checks unresolved dependencies first
zenspider authored
98 end
3c3a3bb @evanphx Restructure #require, add tests for exceptional cases
evanphx authored
99
100 gem_original_require path
84aa8c1 @jimweirich Changed require to rethrow the original exception after attempting a …
jimweirich authored
101 rescue LoadError => load_error
c1fd10d @zenspider - Fixed gem loading issue caused by dependencies not resolving.
zenspider authored
102 if load_error.message.start_with?("Could not find") or
103 (load_error.message.end_with?(path) and Gem.try_activate(path)) then
36e6aff @zenspider Merged 1.3.7/ruby-core changes to master
zenspider authored
104 return gem_original_require(path)
fd92455 @gsinclair * lib/rubygems/custom_require.rb: new file to replace
gsinclair authored
105 end
36e6aff @zenspider Merged 1.3.7/ruby-core changes to master
zenspider authored
106
107 raise load_error
fd92455 @gsinclair * lib/rubygems/custom_require.rb: new file to replace
gsinclair authored
108 end
28aca90 @drbrain Privatize kernel methods, expose Kernel extensions to RDoc.
drbrain authored
109
110 private :require
111
112 end
fd92455 @gsinclair * lib/rubygems/custom_require.rb: new file to replace
gsinclair authored
113
Something went wrong with that request. Please try again.