[rubygems/rubygems] Use a constant empty tar header to avoid extra allocations
[ruby.git] / lib / bundler / runtime.rb
blob54aa30ce0bd0ad2b6db32e816d3f2f47fd7af8fb
1 # frozen_string_literal: true
3 module Bundler
4   class Runtime
5     include SharedHelpers
7     def initialize(root, definition)
8       @root = root
9       @definition = definition
10     end
12     def setup(*groups)
13       @definition.ensure_equivalent_gemfile_and_lockfile if Bundler.frozen_bundle?
15       # Has to happen first
16       clean_load_path
18       specs = @definition.specs_for(groups)
20       SharedHelpers.set_bundle_environment
21       Bundler.rubygems.replace_entrypoints(specs)
23       # Activate the 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) }
29       end.reverse.flatten
31       Gem.add_to_load_path(*load_paths)
33       setup_manpath
35       lock(preserve_unknown_sections: true)
37       self
38     end
40     def require(*groups)
41       groups.map!(&:to_sym)
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?
48       end
50       Plugin.hook(Plugin::Events::GEM_BEFORE_REQUIRE_ALL, dependencies)
52       dependencies.each do |dep|
53         required_file = nil
54         Plugin.hook(Plugin::Events::GEM_BEFORE_REQUIRE, dep)
56         begin
57           # Loop through all the specified autorequires for the
58           # dependency. If there are none, use the dependency's name
59           # as the autorequire.
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
63             required_file = file
64             begin
65               Kernel.require file
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}'."
70             end
71           end
72         rescue LoadError => e
73           raise if dep.autorequire || e.path != required_file
75           if dep.autorequire.nil? && dep.name.include?("-")
76             begin
77               namespaced_file = dep.name.tr("-", "/")
78               Kernel.require namespaced_file
79             rescue LoadError => e
80               raise if e.path != namespaced_file
81             end
82           end
83         end
85         Plugin.hook(Plugin::Events::GEM_AFTER_REQUIRE, dep)
86       end
88       Plugin.hook(Plugin::Events::GEM_AFTER_REQUIRE_ALL, dependencies)
90       dependencies
91     end
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)
97       end
98     end
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
107     def lock(opts = {})
108       return if @definition.no_resolve_needed?
109       @definition.lock(opts[:preserve_unknown_sections])
110     end
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|
117         FileUtils.mkdir_p(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
124       else
125         begin
126           specs
127         rescue GemNotFound
128           if local
129             Bundler.ui.warn "Some gems seem to be missing from your #{Bundler.settings.app_cache_path} directory."
130           end
132           raise
133         end
134       end
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)
140       end
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))
145       end
147       prune_cache(cache_path) unless Bundler.settings[:no_prune]
148     end
150     def prune_cache(cache_path)
151       SharedHelpers.filesystem_access(cache_path) do |p|
152         FileUtils.mkdir_p(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)
157     end
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/*/*/*"]
167       spec_gem_paths       = []
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}"
182           [e, "#{e}.bat"]
183         end
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)
188       end
189       spec_gem_paths.uniq!
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
204       unless dry_run
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)
209           end
210         end
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)
216           end
217         end
218       end
220       output
221     end
223     private
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
231         resolve.any? do |s|
232           s.name == spec.name && s.version == spec.version && !s.source.is_a?(Bundler::Source::Git)
233         end
234       end
236       if cached.any?
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)}"
241           File.delete(path)
242         end
243       end
244     end
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))
252         resolve.any? do |s|
253           source = s.source
254           source.respond_to?(:app_cache_dirname) && source.app_cache_dirname == name
255         end
256       end
258       if cached.any?
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)
265         end
266       end
267     end
269     def setup_manpath
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?
274       end.compact
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)
280     end
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("-")
287       version  = parts.last
288       output   = "#{name} (#{version})"
290       if dry_run
291         Bundler.ui.info "Would have removed #{output}"
292       else
293         Bundler.ui.info "Removing #{output}"
294         FileUtils.rm_rf(dir)
295       end
297       output
298     end
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."
307       else
308         "Prepending `bundle exec` to your command may solve this."
309       end
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}"
313       e.name = spec.name
314       e.requirement = Gem::Requirement.new(spec.version.to_s)
315       raise e
316     end
317   end