1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
require 'rails/vendor_gem_source_index'
module Gem
def self.source_index=(index)
@@source_index = index
end
end
module Rails
class GemDependency
attr_accessor :lib, :source
def self.unpacked_path
@unpacked_path ||= File.join(RAILS_ROOT, 'vendor', 'gems')
end
@@framework_gems = {}
def self.add_frozen_gem_path
@@paths_loaded ||= begin
Gem.source_index = Rails::VendorGemSourceIndex.new(Gem.source_index)
# loaded before us - we can't change them, so mark them
Gem.loaded_specs.each do |name, spec|
@@framework_gems[name] = spec
end
end
end
def framework_gem?
@@framework_gems.has_key?(name)
end
def vendor_rails?
Gem.loaded_specs.has_key?(name) && Gem.loaded_specs[name].loaded_from.empty?
end
def vendor_gem?
Gem.loaded_specs.has_key?(name) && Gem.loaded_specs[name].loaded_from.include?(self.class.unpacked_path)
end
def initialize(name, options = {})
require 'rubygems' unless Object.const_defined?(:Gem)
if options[:requirement]
req = options[:requirement]
elsif options[:version]
req = Gem::Requirement.create(options[:version])
else
req = Gem::Requirement.default
end
@dep = Gem::Dependency.new(name, req)
@lib = options[:lib]
@source = options[:source]
@loaded = @frozen = @load_paths_added = false
end
def add_load_paths
self.class.add_frozen_gem_path
return if @loaded || @load_paths_added
if framework_gem?
@load_paths_added = @loaded = @frozen = true
return
end
gem @dep
@spec = Gem.loaded_specs[name]
@frozen = @spec.loaded_from.include?(self.class.unpacked_path) if @spec
@load_paths_added = true
rescue Gem::LoadError
end
def dependencies
return [] if framework_gem?
all_dependencies = specification.dependencies.map do |dependency|
GemDependency.new(dependency.name, :requirement => dependency.version_requirements)
end
all_dependencies += all_dependencies.map(&:dependencies).flatten
all_dependencies.uniq
end
def gem_dir(base_directory)
File.join(base_directory, specification.full_name)
end
def load
return if @loaded || @load_paths_added == false
require(@lib || name) unless @lib == false
@loaded = true
rescue LoadError
puts $!.to_s
$!.backtrace.each { |b| puts b }
end
def name
@dep.name.to_s
end
def requirement
r = @dep.version_requirements
(r == Gem::Requirement.default) ? nil : r
end
def frozen?
@frozen ||= vendor_rails? || vendor_gem?
end
def loaded?
@loaded ||= begin
if vendor_rails?
true
else
# check if the gem is loaded by inspecting $"
# specification.files lists all the files contained in the gem
gem_files = specification.files
# select only the files contained in require_paths - typically in bin and lib
require_paths_regexp = Regexp.new("^(#{specification.require_paths*'|'})/")
gem_lib_files = gem_files.select { |f| require_paths_regexp.match(f) }
# chop the leading directory off - a typical file might be in
# lib/gem_name/file_name.rb, but it will be 'require'd as gem_name/file_name.rb
gem_lib_files.map! { |f| f.split('/', 2)[1] }
# if any of the files from the above list appear in $", the gem is assumed to
# have been loaded
!(gem_lib_files & $").empty?
end
end
end
def load_paths_added?
# always try to add load paths - even if a gem is loaded, it may not
# be a compatible version (ie random_gem 0.4 is loaded and a later spec
# needs >= 0.5 - gem 'random_gem' will catch this and error out)
@load_paths_added
end
def install
cmd = "#{gem_command} #{install_command.join(' ')}"
puts cmd
puts %x(#{cmd})
end
def unpack_to(directory)
FileUtils.mkdir_p directory
Dir.chdir directory do
Gem::GemRunner.new.run(unpack_command)
end
# copy the gem's specification into GEMDIR/.specification so that
# we can access information about the gem on deployment systems
# without having the gem installed
spec_filename = File.join(gem_dir(directory), '.specification')
# Gem.activate changes the spec - get the original
spec = Gem::Specification.load(specification.loaded_from)
File.open(spec_filename, 'w') do |file|
file.puts spec.to_yaml
end
end
def ==(other)
self.name == other.name && self.requirement == other.requirement
end
alias_method :"eql?", :"=="
def hash
@dep.hash
end
def specification
# code repeated from Gem.activate. Find a matching spec, or the currently loaded version.
# error out if loaded version and requested version are incompatible.
@spec ||= begin
matches = Gem.source_index.search(@dep)
matches << @@framework_gems[name] if framework_gem?
if Gem.loaded_specs[name] then
# This gem is already loaded. If the currently loaded gem is not in the
# list of candidate gems, then we have a version conflict.
existing_spec = Gem.loaded_specs[name]
unless matches.any? { |spec| spec.version == existing_spec.version } then
raise Gem::Exception,
"can't activate #{@dep}, already activated #{existing_spec.full_name}"
end
# we're stuck with it, so change to match
@dep.version_requirements = Gem::Requirement.create("=#{existing_spec.version}")
existing_spec
else
# new load
matches.last
end
end
end
private
def gem_command
RUBY_PLATFORM =~ /win32/ ? 'gem.bat' : 'gem'
end
def install_command
cmd = %w(install) << name
cmd << "--version" << %("#{requirement.to_s}") if requirement
cmd << "--source" << @source if @source
cmd
end
def unpack_command
cmd = %w(unpack) << name
cmd << "--version" << "= "+specification.version.to_s if requirement
cmd
end
end
end
|