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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
# frozen_string_literal: true
module Bundler
class Runtime
include SharedHelpers
def initialize(root, definition)
@root = root
@definition = definition
end
def setup(*groups)
@definition.ensure_equivalent_gemfile_and_lockfile
# Has to happen first
clean_load_path
specs = @definition.specs_for(groups)
SharedHelpers.set_bundle_environment
Bundler.rubygems.replace_entrypoints(specs)
# Activate the specs
load_paths = specs.map do |spec|
check_for_activated_spec!(spec)
Bundler.rubygems.mark_loaded(spec)
spec.load_paths.reject {|path| $LOAD_PATH.include?(path) }
end.reverse.flatten
Gem.add_to_load_path(*load_paths)
setup_manpath
lock(preserve_unknown_sections: true)
self
end
def require(*groups)
groups.map!(&:to_sym)
groups = [:default] if groups.empty?
dependencies = @definition.dependencies.select do |dep|
# Select the dependency if it is in any of the requested groups, and
# for the current platform, and matches the gem constraints.
(dep.groups & groups).any? && dep.should_include?
end
Plugin.hook(Plugin::Events::GEM_BEFORE_REQUIRE_ALL, dependencies)
dependencies.each do |dep|
Plugin.hook(Plugin::Events::GEM_BEFORE_REQUIRE, dep)
# Loop through all the specified autorequires for the
# dependency. If there are none, use the dependency's name
# as the autorequire.
Array(dep.autorequire || dep.name).each do |file|
# Allow `require: true` as an alias for `require: <name>`
file = dep.name if file == true
required_file = file
begin
Kernel.require required_file
rescue LoadError => e
if dep.autorequire.nil? && e.path == required_file
if required_file.include?("-")
required_file = required_file.tr("-", "/")
retry
end
else
raise Bundler::GemRequireError.new e,
"There was an error while trying to load the gem '#{file}'."
end
rescue RuntimeError => e
raise Bundler::GemRequireError.new e,
"There was an error while trying to load the gem '#{file}'."
end
end
Plugin.hook(Plugin::Events::GEM_AFTER_REQUIRE, dep)
end
Plugin.hook(Plugin::Events::GEM_AFTER_REQUIRE_ALL, dependencies)
dependencies
end
def self.definition_method(meth)
define_method(meth) do
raise ArgumentError, "no definition when calling Runtime##{meth}" unless @definition
@definition.send(meth)
end
end
private_class_method :definition_method
definition_method :requested_specs
definition_method :specs
definition_method :dependencies
definition_method :current_dependencies
definition_method :requires
def lock(opts = {})
return if @definition.no_resolve_needed?
@definition.lock(opts[:preserve_unknown_sections])
end
alias_method :gems, :specs
def cache(custom_path = nil, local = false)
cache_path = Bundler.app_cache(custom_path)
SharedHelpers.filesystem_access(cache_path) do |p|
FileUtils.mkdir_p(p)
end unless File.exist?(cache_path)
Bundler.ui.info "Updating files in #{Bundler.settings.app_cache_path}"
specs_to_cache = if Bundler.settings[:cache_all_platforms]
@definition.resolve.materialized_for_all_platforms
else
begin
specs
rescue GemNotFound
if local
Bundler.ui.warn "Some gems seem to be missing from your #{Bundler.settings.app_cache_path} directory."
end
raise
end
end
specs_to_cache.each do |spec|
next if spec.name == "bundler"
source = spec.source
next if source.is_a?(Source::Gemspec)
if source.respond_to?(:migrate_cache)
source.migrate_cache(custom_path, local: local)
elsif source.respond_to?(:cache)
source.cache(spec, custom_path)
end
end
Dir[cache_path.join("*/.git")].each do |git_dir|
FileUtils.rm_rf(git_dir)
FileUtils.touch(File.expand_path("../.bundlecache", git_dir))
end
prune_cache(cache_path) unless Bundler.settings[:no_prune]
end
def prune_cache(cache_path)
SharedHelpers.filesystem_access(cache_path) do |p|
FileUtils.mkdir_p(p)
end unless File.exist?(cache_path)
resolve = @definition.resolve
prune_gem_cache(resolve, cache_path)
prune_git_and_path_cache(resolve, cache_path)
end
def clean(dry_run = false)
gem_bins = Dir["#{Gem.dir}/bin/*"]
git_dirs = Dir["#{Gem.dir}/bundler/gems/*"]
git_cache_dirs = Dir["#{Gem.dir}/cache/bundler/git/*"]
gem_dirs = Dir["#{Gem.dir}/gems/*"]
gem_files = Dir["#{Gem.dir}/cache/*.gem"]
gemspec_files = Dir["#{Gem.dir}/specifications/*.gemspec"]
extension_dirs = Dir["#{Gem.dir}/extensions/*/*/*"] + Dir["#{Gem.dir}/bundler/gems/extensions/*/*/*"]
spec_gem_paths = []
# need to keep git sources around
spec_git_paths = @definition.spec_git_paths
spec_git_cache_dirs = []
spec_gem_executables = []
spec_cache_paths = []
spec_gemspec_paths = []
spec_extension_paths = []
Bundler.rubygems.add_default_gems_to(specs).values.each do |spec|
spec_gem_paths << spec.full_gem_path
# need to check here in case gems are nested like for the rails git repo
md = %r{(.+bundler/gems/.+-[a-f0-9]{7,12})}.match(spec.full_gem_path)
spec_git_paths << md[1] if md
spec_gem_executables << spec.executables.collect do |executable|
e = "#{Bundler.rubygems.gem_bindir}/#{executable}"
[e, "#{e}.bat"]
end
spec_cache_paths << spec.cache_file
spec_gemspec_paths << spec.spec_file
spec_extension_paths << spec.extension_dir if spec.respond_to?(:extension_dir)
spec_git_cache_dirs << spec.source.cache_path.to_s if spec.source.is_a?(Bundler::Source::Git)
end
spec_gem_paths.uniq!
spec_gem_executables.flatten!
stale_gem_bins = gem_bins - spec_gem_executables
stale_git_dirs = git_dirs - spec_git_paths - ["#{Gem.dir}/bundler/gems/extensions"]
stale_git_cache_dirs = git_cache_dirs - spec_git_cache_dirs
stale_gem_dirs = gem_dirs - spec_gem_paths
stale_gem_files = gem_files - spec_cache_paths
stale_gemspec_files = gemspec_files - spec_gemspec_paths
stale_extension_dirs = extension_dirs - spec_extension_paths
removed_stale_gem_dirs = stale_gem_dirs.collect {|dir| remove_dir(dir, dry_run) }
removed_stale_git_dirs = stale_git_dirs.collect {|dir| remove_dir(dir, dry_run) }
output = removed_stale_gem_dirs + removed_stale_git_dirs
unless dry_run
stale_files = stale_gem_bins + stale_gem_files + stale_gemspec_files
stale_files.each do |file|
SharedHelpers.filesystem_access(File.dirname(file)) do |_p|
FileUtils.rm(file) if File.exist?(file)
end
end
stale_dirs = stale_git_cache_dirs + stale_extension_dirs
stale_dirs.each do |stale_dir|
SharedHelpers.filesystem_access(stale_dir) do |dir|
FileUtils.rm_rf(dir) if File.exist?(dir)
end
end
end
output
end
private
def prune_gem_cache(resolve, cache_path)
cached = Dir["#{cache_path}/*.gem"]
cached = cached.delete_if do |path|
spec = Bundler.rubygems.spec_from_gem path
resolve.any? do |s|
s.name == spec.name && s.version == spec.version && !s.source.is_a?(Bundler::Source::Git)
end
end
if cached.any?
Bundler.ui.info "Removing outdated .gem files from #{Bundler.settings.app_cache_path}"
cached.each do |path|
Bundler.ui.info " * #{File.basename(path)}"
File.delete(path)
end
end
end
def prune_git_and_path_cache(resolve, cache_path)
cached = Dir["#{cache_path}/*/.bundlecache"]
cached = cached.delete_if do |path|
name = File.basename(File.dirname(path))
resolve.any? do |s|
source = s.source
source.respond_to?(:app_cache_dirname) && source.app_cache_dirname == name
end
end
if cached.any?
Bundler.ui.info "Removing outdated git and path gems from #{Bundler.settings.app_cache_path}"
cached.each do |path|
path = File.dirname(path)
Bundler.ui.info " * #{File.basename(path)}"
FileUtils.rm_rf(path)
end
end
end
def setup_manpath
# Add man/ subdirectories from activated bundles to MANPATH for man(1)
manuals = $LOAD_PATH.filter_map do |path|
man_subdir = path.sub(/lib$/, "man")
man_subdir unless Dir[man_subdir + "/man?/"].empty?
end
return if manuals.empty?
Bundler::SharedHelpers.set_env "MANPATH", manuals.concat(
ENV["MANPATH"] ? ENV["MANPATH"].to_s.split(File::PATH_SEPARATOR) : [""]
).uniq.join(File::PATH_SEPARATOR)
end
def remove_dir(dir, dry_run)
full_name = Pathname.new(dir).basename.to_s
parts = full_name.split("-")
name = parts[0..-2].join("-")
version = parts.last
output = "#{name} (#{version})"
if dry_run
Bundler.ui.info "Would have removed #{output}"
else
Bundler.ui.info "Removing #{output}"
FileUtils.rm_rf(dir)
end
output
end
def check_for_activated_spec!(spec)
return unless activated_spec = Bundler.rubygems.loaded_specs(spec.name)
return if activated_spec.version == spec.version
suggestion = if activated_spec.default_gem?
"Since #{spec.name} is a default gem, you can either remove your dependency on it" \
" or try updating to a newer version of bundler that supports #{spec.name} as a default gem."
else
"Prepending `bundle exec` to your command may solve this."
end
e = Gem::LoadError.new "You have already activated #{activated_spec.name} #{activated_spec.version}, " \
"but your Gemfile requires #{spec.name} #{spec.version}. #{suggestion}"
e.name = spec.name
e.requirement = Gem::Requirement.new(spec.version.to_s)
raise e
end
end
end
|