1 # frozen_string_literal: true
7 def initialize(root, definition)
9 @definition = definition
13 @definition.ensure_equivalent_gemfile_and_lockfile if Bundler.frozen_bundle?
18 specs = @definition.specs_for(groups)
20 SharedHelpers.set_bundle_environment
21 Bundler.rubygems.replace_entrypoints(specs)
24 load_paths = specs.map do |spec|
25 check_for_activated_spec!(spec)
27 Bundler.rubygems.mark_loaded(spec)
28 spec.load_paths.reject {|path| $LOAD_PATH.include?(path) }
31 Gem.add_to_load_path(*load_paths)
35 lock(preserve_unknown_sections: true)
42 groups = [:default] if groups.empty?
44 dependencies = @definition.dependencies.select do |dep|
45 # Select the dependency if it is in any of the requested groups, and
46 # for the current platform, and matches the gem constraints.
47 (dep.groups & groups).any? && dep.should_include?
50 Plugin.hook(Plugin::Events::GEM_BEFORE_REQUIRE_ALL, dependencies)
52 dependencies.each do |dep|
54 Plugin.hook(Plugin::Events::GEM_BEFORE_REQUIRE, dep)
57 # Loop through all the specified autorequires for the
58 # dependency. If there are none, use the dependency's name
60 Array(dep.autorequire || dep.name).each do |file|
61 # Allow `require: true` as an alias for `require: <name>`
62 file = dep.name if file == true
66 rescue RuntimeError => e
67 raise e if e.is_a?(LoadError) # we handle this a little later
68 raise Bundler::GemRequireError.new e,
69 "There was an error while trying to load the gem '#{file}'."
73 raise if dep.autorequire || e.path != required_file
75 if dep.autorequire.nil? && dep.name.include?("-")
77 namespaced_file = dep.name.tr("-", "/")
78 Kernel.require namespaced_file
80 raise if e.path != namespaced_file
85 Plugin.hook(Plugin::Events::GEM_AFTER_REQUIRE, dep)
88 Plugin.hook(Plugin::Events::GEM_AFTER_REQUIRE_ALL, dependencies)
93 def self.definition_method(meth)
94 define_method(meth) do
95 raise ArgumentError, "no definition when calling Runtime##{meth}" unless @definition
96 @definition.send(meth)
99 private_class_method :definition_method
101 definition_method :requested_specs
102 definition_method :specs
103 definition_method :dependencies
104 definition_method :current_dependencies
105 definition_method :requires
108 return if @definition.no_resolve_needed?
109 @definition.lock(opts[:preserve_unknown_sections])
112 alias_method :gems, :specs
114 def cache(custom_path = nil, local = false)
115 cache_path = Bundler.app_cache(custom_path)
116 SharedHelpers.filesystem_access(cache_path) do |p|
118 end unless File.exist?(cache_path)
120 Bundler.ui.info "Updating files in #{Bundler.settings.app_cache_path}"
122 specs_to_cache = if Bundler.settings[:cache_all_platforms]
123 @definition.resolve.materialized_for_all_platforms
129 Bundler.ui.warn "Some gems seem to be missing from your #{Bundler.settings.app_cache_path} directory."
136 specs_to_cache.each do |spec|
137 next if spec.name == "bundler"
138 next if spec.source.is_a?(Source::Gemspec)
139 spec.source.cache(spec, custom_path) if spec.source.respond_to?(:cache)
142 Dir[cache_path.join("*/.git")].each do |git_dir|
143 FileUtils.rm_rf(git_dir)
144 FileUtils.touch(File.expand_path("../.bundlecache", git_dir))
147 prune_cache(cache_path) unless Bundler.settings[:no_prune]
150 def prune_cache(cache_path)
151 SharedHelpers.filesystem_access(cache_path) do |p|
153 end unless File.exist?(cache_path)
154 resolve = @definition.resolve
155 prune_gem_cache(resolve, cache_path)
156 prune_git_and_path_cache(resolve, cache_path)
159 def clean(dry_run = false)
160 gem_bins = Dir["#{Gem.dir}/bin/*"]
161 git_dirs = Dir["#{Gem.dir}/bundler/gems/*"]
162 git_cache_dirs = Dir["#{Gem.dir}/cache/bundler/git/*"]
163 gem_dirs = Dir["#{Gem.dir}/gems/*"]
164 gem_files = Dir["#{Gem.dir}/cache/*.gem"]
165 gemspec_files = Dir["#{Gem.dir}/specifications/*.gemspec"]
166 extension_dirs = Dir["#{Gem.dir}/extensions/*/*/*"] + Dir["#{Gem.dir}/bundler/gems/extensions/*/*/*"]
168 # need to keep git sources around
169 spec_git_paths = @definition.spec_git_paths
170 spec_git_cache_dirs = []
171 spec_gem_executables = []
172 spec_cache_paths = []
173 spec_gemspec_paths = []
174 spec_extension_paths = []
175 Bundler.rubygems.add_default_gems_to(specs).values.each do |spec|
176 spec_gem_paths << spec.full_gem_path
177 # need to check here in case gems are nested like for the rails git repo
178 md = %r{(.+bundler/gems/.+-[a-f0-9]{7,12})}.match(spec.full_gem_path)
179 spec_git_paths << md[1] if md
180 spec_gem_executables << spec.executables.collect do |executable|
181 e = "#{Bundler.rubygems.gem_bindir}/#{executable}"
184 spec_cache_paths << spec.cache_file
185 spec_gemspec_paths << spec.spec_file
186 spec_extension_paths << spec.extension_dir if spec.respond_to?(:extension_dir)
187 spec_git_cache_dirs << spec.source.cache_path.to_s if spec.source.is_a?(Bundler::Source::Git)
190 spec_gem_executables.flatten!
192 stale_gem_bins = gem_bins - spec_gem_executables
193 stale_git_dirs = git_dirs - spec_git_paths - ["#{Gem.dir}/bundler/gems/extensions"]
194 stale_git_cache_dirs = git_cache_dirs - spec_git_cache_dirs
195 stale_gem_dirs = gem_dirs - spec_gem_paths
196 stale_gem_files = gem_files - spec_cache_paths
197 stale_gemspec_files = gemspec_files - spec_gemspec_paths
198 stale_extension_dirs = extension_dirs - spec_extension_paths
200 removed_stale_gem_dirs = stale_gem_dirs.collect {|dir| remove_dir(dir, dry_run) }
201 removed_stale_git_dirs = stale_git_dirs.collect {|dir| remove_dir(dir, dry_run) }
202 output = removed_stale_gem_dirs + removed_stale_git_dirs
205 stale_files = stale_gem_bins + stale_gem_files + stale_gemspec_files
206 stale_files.each do |file|
207 SharedHelpers.filesystem_access(File.dirname(file)) do |_p|
208 FileUtils.rm(file) if File.exist?(file)
212 stale_dirs = stale_git_cache_dirs + stale_extension_dirs
213 stale_dirs.each do |stale_dir|
214 SharedHelpers.filesystem_access(stale_dir) do |dir|
215 FileUtils.rm_rf(dir) if File.exist?(dir)
225 def prune_gem_cache(resolve, cache_path)
226 cached = Dir["#{cache_path}/*.gem"]
228 cached = cached.delete_if do |path|
229 spec = Bundler.rubygems.spec_from_gem path
232 s.name == spec.name && s.version == spec.version && !s.source.is_a?(Bundler::Source::Git)
237 Bundler.ui.info "Removing outdated .gem files from #{Bundler.settings.app_cache_path}"
239 cached.each do |path|
240 Bundler.ui.info " * #{File.basename(path)}"
246 def prune_git_and_path_cache(resolve, cache_path)
247 cached = Dir["#{cache_path}/*/.bundlecache"]
249 cached = cached.delete_if do |path|
250 name = File.basename(File.dirname(path))
254 source.respond_to?(:app_cache_dirname) && source.app_cache_dirname == name
259 Bundler.ui.info "Removing outdated git and path gems from #{Bundler.settings.app_cache_path}"
261 cached.each do |path|
262 path = File.dirname(path)
263 Bundler.ui.info " * #{File.basename(path)}"
264 FileUtils.rm_rf(path)
270 # Add man/ subdirectories from activated bundles to MANPATH for man(1)
271 manuals = $LOAD_PATH.map do |path|
272 man_subdir = path.sub(/lib$/, "man")
273 man_subdir unless Dir[man_subdir + "/man?/"].empty?
276 return if manuals.empty?
277 Bundler::SharedHelpers.set_env "MANPATH", manuals.concat(
278 ENV["MANPATH"] ? ENV["MANPATH"].to_s.split(File::PATH_SEPARATOR) : [""]
279 ).uniq.join(File::PATH_SEPARATOR)
282 def remove_dir(dir, dry_run)
283 full_name = Pathname.new(dir).basename.to_s
285 parts = full_name.split("-")
286 name = parts[0..-2].join("-")
288 output = "#{name} (#{version})"
291 Bundler.ui.info "Would have removed #{output}"
293 Bundler.ui.info "Removing #{output}"
300 def check_for_activated_spec!(spec)
301 return unless activated_spec = Bundler.rubygems.loaded_specs(spec.name)
302 return if activated_spec.version == spec.version
304 suggestion = if activated_spec.default_gem?
305 "Since #{spec.name} is a default gem, you can either remove your dependency on it" \
306 " or try updating to a newer version of bundler that supports #{spec.name} as a default gem."
308 "Prepending `bundle exec` to your command may solve this."
311 e = Gem::LoadError.new "You have already activated #{activated_spec.name} #{activated_spec.version}, " \
312 "but your Gemfile requires #{spec.name} #{spec.version}. #{suggestion}"
314 e.requirement = Gem::Requirement.new(spec.version.to_s)