| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
require "rubygems/user_interaction" |
| 4 |
|
| 5 |
module Bundler |
| 6 |
class Source |
| 7 |
class Rubygems < Source |
| 8 |
autoload :Remote, File.expand_path("rubygems/remote", __dir__) |
| 9 |
|
| 10 |
# Ask for X gems per API request |
| 11 |
API_REQUEST_SIZE = 50 |
| 12 |
|
| 13 |
attr_accessor :remotes |
| 14 |
|
| 15 |
def initialize(options = {}) |
| 16 |
@options = options |
| 17 |
@remotes = [] |
| 18 |
@dependency_names = [] |
| 19 |
@allow_remote = false |
| 20 |
@allow_cached = false |
| 21 |
@allow_local = options["allow_local"] || false |
| 22 |
@checksum_store = Checksum::Store.new |
| 23 |
|
| 24 |
Array(options["remotes"]).reverse_each {|r| add_remote(r) } |
| 25 |
|
| 26 |
@lockfile_remotes = @remotes if options["from_lockfile"] |
| 27 |
end |
| 28 |
|
| 29 |
def caches |
| 30 |
@caches ||= [cache_path, *Bundler.rubygems.gem_cache] |
| 31 |
end |
| 32 |
|
| 33 |
def local_only! |
| 34 |
@specs = nil |
| 35 |
@allow_local = true |
| 36 |
@allow_cached = false |
| 37 |
@allow_remote = false |
| 38 |
end |
| 39 |
|
| 40 |
def local! |
| 41 |
return if @allow_local |
| 42 |
|
| 43 |
@specs = nil |
| 44 |
@allow_local = true |
| 45 |
end |
| 46 |
|
| 47 |
def remote! |
| 48 |
return if @allow_remote |
| 49 |
|
| 50 |
@specs = nil |
| 51 |
@allow_remote = true |
| 52 |
end |
| 53 |
|
| 54 |
def cached! |
| 55 |
return unless File.exist?(cache_path) |
| 56 |
|
| 57 |
return if @allow_cached |
| 58 |
|
| 59 |
@specs = nil |
| 60 |
@allow_cached = true |
| 61 |
end |
| 62 |
|
| 63 |
def hash |
| 64 |
@remotes.hash |
| 65 |
end |
| 66 |
|
| 67 |
def eql?(other) |
| 68 |
other.is_a?(Rubygems) && other.credless_remotes == credless_remotes |
| 69 |
end |
| 70 |
|
| 71 |
alias_method :==, :eql? |
| 72 |
|
| 73 |
def include?(o) |
| 74 |
o.is_a?(Rubygems) && (o.credless_remotes - credless_remotes).empty? |
| 75 |
end |
| 76 |
|
| 77 |
def multiple_remotes? |
| 78 |
@remotes.size > 1 |
| 79 |
end |
| 80 |
|
| 81 |
def no_remotes? |
| 82 |
@remotes.size == 0 |
| 83 |
end |
| 84 |
|
| 85 |
def can_lock?(spec) |
| 86 |
return super unless multiple_remotes? |
| 87 |
include?(spec.source) |
| 88 |
end |
| 89 |
|
| 90 |
def options |
| 91 |
{ "remotes" => @remotes.map(&:to_s) } |
| 92 |
end |
| 93 |
|
| 94 |
def self.from_lock(options) |
| 95 |
options["remotes"] = Array(options.delete("remote")).reverse |
| 96 |
new(options.merge("from_lockfile" => true)) |
| 97 |
end |
| 98 |
|
| 99 |
def to_lock |
| 100 |
out = String.new("GEM\n") |
| 101 |
lockfile_remotes.reverse_each do |remote| |
| 102 |
out << " remote: #{remote}\n" |
| 103 |
end |
| 104 |
out << " specs:\n" |
| 105 |
end |
| 106 |
|
| 107 |
def to_s |
| 108 |
if remotes.empty? |
| 109 |
"locally installed gems" |
| 110 |
elsif @allow_remote && @allow_cached && @allow_local |
| 111 |
"rubygems repository #{remote_names}, cached gems or installed locally" |
| 112 |
elsif @allow_remote && @allow_local |
| 113 |
"rubygems repository #{remote_names} or installed locally" |
| 114 |
elsif @allow_remote |
| 115 |
"rubygems repository #{remote_names}" |
| 116 |
elsif @allow_cached && @allow_local |
| 117 |
"cached gems or installed locally" |
| 118 |
else |
| 119 |
"locally installed gems" |
| 120 |
end |
| 121 |
end |
| 122 |
|
| 123 |
def identifier |
| 124 |
if remotes.empty? |
| 125 |
"locally installed gems" |
| 126 |
else |
| 127 |
"rubygems repository #{remote_names}" |
| 128 |
end |
| 129 |
end |
| 130 |
alias_method :name, :identifier |
| 131 |
alias_method :to_gemfile, :identifier |
| 132 |
|
| 133 |
def specs |
| 134 |
@specs ||= begin |
| 135 |
# remote_specs usually generates a way larger Index than the other |
| 136 |
# sources, and large_idx.merge! small_idx is way faster than |
| 137 |
# small_idx.merge! large_idx. |
| 138 |
index = @allow_remote ? remote_specs.dup : Index.new |
| 139 |
index.merge!(cached_specs) if @allow_cached |
| 140 |
index.merge!(installed_specs) if @allow_local |
| 141 |
|
| 142 |
# complete with default specs, only if not already available in the |
| 143 |
# index through remote, cached, or installed specs |
| 144 |
index.use(default_specs) if @allow_local |
| 145 |
|
| 146 |
index |
| 147 |
end |
| 148 |
end |
| 149 |
|
| 150 |
def install(spec, options = {}) |
| 151 |
if (spec.default_gem? && !cached_built_in_gem(spec, local: options[:local])) || (installed?(spec) && !options[:force]) |
| 152 |
print_using_message "Using #{version_message(spec, options[:previous_spec])}" |
| 153 |
return nil # no post-install message |
| 154 |
end |
| 155 |
|
| 156 |
if spec.remote |
| 157 |
# Check for this spec from other sources |
| 158 |
uris = [spec.remote, *remotes_for_spec(spec)].map(&:anonymized_uri).uniq |
| 159 |
Installer.ambiguous_gems << [spec.name, *uris] if uris.length > 1 |
| 160 |
end |
| 161 |
|
| 162 |
path = fetch_gem_if_possible(spec, options[:previous_spec]) |
| 163 |
raise GemNotFound, "Could not find #{spec.file_name} for installation" unless path |
| 164 |
|
| 165 |
return if Bundler.settings[:no_install] |
| 166 |
|
| 167 |
install_path = rubygems_dir |
| 168 |
bin_path = Bundler.system_bindir |
| 169 |
|
| 170 |
require_relative "../rubygems_gem_installer" |
| 171 |
|
| 172 |
installer = Bundler::RubyGemsGemInstaller.at( |
| 173 |
path, |
| 174 |
security_policy: Bundler.rubygems.security_policies[Bundler.settings["trust-policy"]], |
| 175 |
install_dir: install_path.to_s, |
| 176 |
bin_dir: bin_path.to_s, |
| 177 |
ignore_dependencies: true, |
| 178 |
wrappers: true, |
| 179 |
env_shebang: true, |
| 180 |
build_args: options[:build_args], |
| 181 |
bundler_extension_cache_path: extension_cache_path(spec) |
| 182 |
) |
| 183 |
|
| 184 |
if spec.remote |
| 185 |
s = begin |
| 186 |
installer.spec |
| 187 |
rescue Gem::Package::FormatError |
| 188 |
Bundler.rm_rf(path) |
| 189 |
raise |
| 190 |
rescue Gem::Security::Exception => e |
| 191 |
raise SecurityError, |
| 192 |
"The gem #{File.basename(path, ".gem")} can't be installed because " \ |
| 193 |
"the security policy didn't allow it, with the message: #{e.message}" |
| 194 |
end |
| 195 |
|
| 196 |
spec.__swap__(s) |
| 197 |
end |
| 198 |
|
| 199 |
spec.source.checksum_store.register(spec, installer.gem_checksum) |
| 200 |
|
| 201 |
message = "Installing #{version_message(spec, options[:previous_spec])}" |
| 202 |
message += " with native extensions" if spec.extensions.any? |
| 203 |
Bundler.ui.confirm message |
| 204 |
|
| 205 |
installed_spec = installer.install |
| 206 |
|
| 207 |
spec.full_gem_path = installed_spec.full_gem_path |
| 208 |
spec.loaded_from = installed_spec.loaded_from |
| 209 |
spec.base_dir = installed_spec.base_dir |
| 210 |
|
| 211 |
spec.post_install_message |
| 212 |
end |
| 213 |
|
| 214 |
def cache(spec, custom_path = nil) |
| 215 |
cached_path = Bundler.settings[:cache_all_platforms] ? fetch_gem_if_possible(spec) : cached_gem(spec) |
| 216 |
raise GemNotFound, "Missing gem file '#{spec.file_name}'." unless cached_path |
| 217 |
return if File.dirname(cached_path) == Bundler.app_cache.to_s |
| 218 |
Bundler.ui.info " * #{File.basename(cached_path)}" |
| 219 |
FileUtils.cp(cached_path, Bundler.app_cache(custom_path)) |
| 220 |
rescue Errno::EACCES => e |
| 221 |
Bundler.ui.debug(e) |
| 222 |
raise InstallError, e.message |
| 223 |
end |
| 224 |
|
| 225 |
def cached_built_in_gem(spec, local: false) |
| 226 |
cached_path = cached_gem(spec) |
| 227 |
if cached_path.nil? && !local |
| 228 |
remote_spec = remote_specs.search(spec).first |
| 229 |
if remote_spec |
| 230 |
cached_path = fetch_gem(remote_spec) |
| 231 |
spec.remote = remote_spec.remote |
| 232 |
else |
| 233 |
Bundler.ui.warn "#{spec.full_name} is built in to Ruby, and can't be cached because your Gemfile doesn't have any sources that contain it." |
| 234 |
end |
| 235 |
end |
| 236 |
cached_path |
| 237 |
end |
| 238 |
|
| 239 |
def add_remote(source) |
| 240 |
uri = normalize_uri(source) |
| 241 |
@remotes.unshift(uri) unless @remotes.include?(uri) |
| 242 |
end |
| 243 |
|
| 244 |
def spec_names |
| 245 |
if dependency_api_available? |
| 246 |
remote_specs.spec_names |
| 247 |
else |
| 248 |
[] |
| 249 |
end |
| 250 |
end |
| 251 |
|
| 252 |
def unmet_deps |
| 253 |
if dependency_api_available? |
| 254 |
remote_specs.unmet_dependency_names |
| 255 |
else |
| 256 |
[] |
| 257 |
end |
| 258 |
end |
| 259 |
|
| 260 |
def remote_fetchers |
| 261 |
@remote_fetchers ||= remotes.to_h do |uri| |
| 262 |
remote = Source::Rubygems::Remote.new(uri) |
| 263 |
[remote, Bundler::Fetcher.new(remote)] |
| 264 |
end.freeze |
| 265 |
end |
| 266 |
|
| 267 |
def fetchers |
| 268 |
@fetchers ||= remote_fetchers.values.freeze |
| 269 |
end |
| 270 |
|
| 271 |
def double_check_for(unmet_dependency_names) |
| 272 |
return unless dependency_api_available? |
| 273 |
|
| 274 |
unmet_dependency_names = unmet_dependency_names.call |
| 275 |
unless unmet_dependency_names.nil? |
| 276 |
if api_fetchers.size <= 1 |
| 277 |
# can't do this when there are multiple fetchers because then we might not fetch from _all_ |
| 278 |
# of them |
| 279 |
unmet_dependency_names -= remote_specs.spec_names # avoid re-fetching things we've already gotten |
| 280 |
end |
| 281 |
return if unmet_dependency_names.empty? |
| 282 |
end |
| 283 |
|
| 284 |
Bundler.ui.debug "Double checking for #{unmet_dependency_names || "all specs (due to the size of the request)"} in #{self}" |
| 285 |
|
| 286 |
fetch_names(api_fetchers, unmet_dependency_names, remote_specs) |
| 287 |
|
| 288 |
specs.use remote_specs |
| 289 |
end |
| 290 |
|
| 291 |
def dependency_names_to_double_check |
| 292 |
names = [] |
| 293 |
remote_specs.each do |spec| |
| 294 |
case spec |
| 295 |
when EndpointSpecification, Gem::Specification, StubSpecification, LazySpecification |
| 296 |
names.concat(spec.runtime_dependencies.map(&:name)) |
| 297 |
when RemoteSpecification # from the full index |
| 298 |
return nil |
| 299 |
else |
| 300 |
raise "unhandled spec type (#{spec.inspect})" |
| 301 |
end |
| 302 |
end |
| 303 |
names |
| 304 |
end |
| 305 |
|
| 306 |
def dependency_api_available? |
| 307 |
@allow_remote && api_fetchers.any? |
| 308 |
end |
| 309 |
|
| 310 |
protected |
| 311 |
|
| 312 |
def remote_names |
| 313 |
remotes.map(&:to_s).join(", ") |
| 314 |
end |
| 315 |
|
| 316 |
def credless_remotes |
| 317 |
remotes.map(&method(:remove_auth)) |
| 318 |
end |
| 319 |
|
| 320 |
def remotes_for_spec(spec) |
| 321 |
specs.search_all(spec.name).inject([]) do |uris, s| |
| 322 |
uris << s.remote if s.remote |
| 323 |
uris |
| 324 |
end |
| 325 |
end |
| 326 |
|
| 327 |
def cached_gem(spec) |
| 328 |
global_cache_path = download_cache_path(spec) |
| 329 |
caches << global_cache_path if global_cache_path |
| 330 |
|
| 331 |
possibilities = caches.map {|p| package_path(p, spec) } |
| 332 |
possibilities.find {|p| File.exist?(p) } |
| 333 |
end |
| 334 |
|
| 335 |
def package_path(cache_path, spec) |
| 336 |
"#{cache_path}/#{spec.file_name}" |
| 337 |
end |
| 338 |
|
| 339 |
def normalize_uri(uri) |
| 340 |
uri = URINormalizer.normalize_suffix(uri.to_s) |
| 341 |
require_relative "../vendored_uri" |
| 342 |
uri = Gem::URI(uri) |
| 343 |
raise ArgumentError, "The source must be an absolute URI. For example:\n" \ |
| 344 |
"source 'https://rubygems.org'" if !uri.absolute? || (uri.is_a?(Gem::URI::HTTP) && uri.host.nil?) |
| 345 |
uri |
| 346 |
end |
| 347 |
|
| 348 |
def remove_auth(remote) |
| 349 |
if remote.user || remote.password |
| 350 |
remote.dup.tap {|uri| uri.user = uri.password = nil }.to_s |
| 351 |
else |
| 352 |
remote.to_s |
| 353 |
end |
| 354 |
end |
| 355 |
|
| 356 |
def installed_specs |
| 357 |
@installed_specs ||= Index.build do |idx| |
| 358 |
Bundler.rubygems.installed_specs.reverse_each do |spec| |
| 359 |
spec.source = self |
| 360 |
next if spec.ignored? |
| 361 |
idx << spec |
| 362 |
end |
| 363 |
end |
| 364 |
end |
| 365 |
|
| 366 |
def default_specs |
| 367 |
@default_specs ||= Index.build do |idx| |
| 368 |
Bundler.rubygems.default_specs.each do |spec| |
| 369 |
spec.source = self |
| 370 |
idx << spec |
| 371 |
end |
| 372 |
end |
| 373 |
end |
| 374 |
|
| 375 |
def cached_specs |
| 376 |
@cached_specs ||= begin |
| 377 |
idx = Index.new |
| 378 |
|
| 379 |
Dir["#{cache_path}/*.gem"].each do |gemfile| |
| 380 |
s ||= Bundler.rubygems.spec_from_gem(gemfile) |
| 381 |
s.source = self |
| 382 |
idx << s |
| 383 |
end |
| 384 |
|
| 385 |
idx |
| 386 |
end |
| 387 |
end |
| 388 |
|
| 389 |
def api_fetchers |
| 390 |
fetchers.select(&:api_fetcher?) |
| 391 |
end |
| 392 |
|
| 393 |
def remote_specs |
| 394 |
@remote_specs ||= Index.build do |idx| |
| 395 |
index_fetchers = fetchers - api_fetchers |
| 396 |
|
| 397 |
if index_fetchers.empty? |
| 398 |
fetch_names(api_fetchers, dependency_names, idx) |
| 399 |
else |
| 400 |
fetch_names(fetchers, nil, idx) |
| 401 |
end |
| 402 |
end |
| 403 |
end |
| 404 |
|
| 405 |
def fetch_names(fetchers, dependency_names, index) |
| 406 |
fetchers.each do |f| |
| 407 |
if dependency_names |
| 408 |
Bundler.ui.info "Fetching gem metadata from #{URICredentialsFilter.credential_filtered_uri(f.uri)}", Bundler.ui.debug? |
| 409 |
index.use f.specs_with_retry(dependency_names, self) |
| 410 |
Bundler.ui.info "" unless Bundler.ui.debug? # new line now that the dots are over |
| 411 |
else |
| 412 |
Bundler.ui.info "Fetching source index from #{URICredentialsFilter.credential_filtered_uri(f.uri)}" |
| 413 |
index.use f.specs_with_retry(nil, self) |
| 414 |
end |
| 415 |
end |
| 416 |
end |
| 417 |
|
| 418 |
def fetch_gem_if_possible(spec, previous_spec = nil) |
| 419 |
if spec.remote |
| 420 |
fetch_gem(spec, previous_spec) |
| 421 |
else |
| 422 |
cached_gem(spec) |
| 423 |
end |
| 424 |
end |
| 425 |
|
| 426 |
def fetch_gem(spec, previous_spec = nil) |
| 427 |
spec.fetch_platform |
| 428 |
|
| 429 |
cache_path = download_cache_path(spec) || default_cache_path_for(rubygems_dir) |
| 430 |
gem_path = package_path(cache_path, spec) |
| 431 |
return gem_path if File.exist?(gem_path) |
| 432 |
|
| 433 |
SharedHelpers.filesystem_access(cache_path) do |p| |
| 434 |
FileUtils.mkdir_p(p) |
| 435 |
end |
| 436 |
download_gem(spec, cache_path, previous_spec) |
| 437 |
|
| 438 |
gem_path |
| 439 |
end |
| 440 |
|
| 441 |
def installed?(spec) |
| 442 |
installed_specs[spec].any? && !spec.deleted_gem? |
| 443 |
end |
| 444 |
|
| 445 |
def rubygems_dir |
| 446 |
Bundler.bundle_path |
| 447 |
end |
| 448 |
|
| 449 |
def default_cache_path_for(dir) |
| 450 |
"#{dir}/cache" |
| 451 |
end |
| 452 |
|
| 453 |
def cache_path |
| 454 |
Bundler.app_cache |
| 455 |
end |
| 456 |
|
| 457 |
private |
| 458 |
|
| 459 |
def lockfile_remotes |
| 460 |
@lockfile_remotes || credless_remotes |
| 461 |
end |
| 462 |
|
| 463 |
# Checks if the requested spec exists in the global cache. If it does, |
| 464 |
# we copy it to the download path, and if it does not, we download it. |
| 465 |
# |
| 466 |
# @param [Specification] spec |
| 467 |
# the spec we want to download or retrieve from the cache. |
| 468 |
# |
| 469 |
# @param [String] download_cache_path |
| 470 |
# the local directory the .gem will end up in. |
| 471 |
# |
| 472 |
# @param [Specification] previous_spec |
| 473 |
# the spec previously locked |
| 474 |
# |
| 475 |
def download_gem(spec, download_cache_path, previous_spec = nil) |
| 476 |
uri = spec.remote.uri |
| 477 |
Bundler.ui.confirm("Fetching #{version_message(spec, previous_spec)}") |
| 478 |
gem_remote_fetcher = remote_fetchers.fetch(spec.remote).gem_remote_fetcher |
| 479 |
Bundler.rubygems.download_gem(spec, uri, download_cache_path, gem_remote_fetcher) |
| 480 |
end |
| 481 |
|
| 482 |
# Returns the global cache path of the calling Rubygems::Source object. |
| 483 |
# |
| 484 |
# Note that the Source determines the path's subdirectory. We use this |
| 485 |
# subdirectory in the global cache path so that gems with the same name |
| 486 |
# -- and possibly different versions -- from different sources are saved |
| 487 |
# to their respective subdirectories and do not override one another. |
| 488 |
# |
| 489 |
# @param [Gem::Specification] specification |
| 490 |
# |
| 491 |
# @return [Pathname] The global cache path. |
| 492 |
# |
| 493 |
def download_cache_path(spec) |
| 494 |
return unless Bundler.feature_flag.global_gem_cache? |
| 495 |
return unless remote = spec.remote |
| 496 |
return unless cache_slug = remote.cache_slug |
| 497 |
|
| 498 |
Bundler.user_cache.join("gems", cache_slug) |
| 499 |
end |
| 500 |
|
| 501 |
def extension_cache_slug(spec) |
| 502 |
return unless remote = spec.remote |
| 503 |
remote.cache_slug |
| 504 |
end |
| 505 |
end |
| 506 |
end |
| 507 |
end |