bundler 4.0.15 → 4.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b500418ab2bb689238b11172e7713d90513adbeeea8b4a27e3e9e12e1ac61cdb
4
- data.tar.gz: 5c6463f83fcec1c3310af6cf30fdbe50774e8016a6ac8c277a65fcb14ca52c0a
3
+ metadata.gz: a0e51552827f6a9e6f69879c84b28b5fc102157385d7b5acc8f76ccf23fc1d02
4
+ data.tar.gz: 580c4732a5106f1ac54652b7399e187a365a6b891e6b1eb5093093b7c0bcbbbd
5
5
  SHA512:
6
- metadata.gz: e36d0b9bc29b9ca34a12ccb44ec995e774f55348d2181ea220ed70d68232d65f89c338ac799f35ea3a97cddbc73db10ef584756cfa1e480cc0c1be8b97c454ce
7
- data.tar.gz: f5dfd925657ea59c8fde6a4c5bf658e7e3b5b9357a1ccd9d03167dc7cfbcd40db826d3b909a65101e24bd15d15287d4e6505884a6117e152feb516e0597ca2ab
6
+ metadata.gz: 03f0b65a2e6655251ddd488cfe43983e7e94d1e72be6166ab5c4627f6075ee9d4aabf38e7e6c4514eb906a0ee524aaa6a3fbf2a598fdb7ecc8f3301b803cdf90
7
+ data.tar.gz: 36b037826c24cf525249954629c83c3d131a27a3d009d0a9af749e1108dcd043c151fa7f4f5786edc47a75c5526af6917a7da8ed0b222766e97749a4d298bbfd
data/CHANGELOG.md CHANGED
@@ -1,5 +1,39 @@
1
1
  # Changelog
2
2
 
3
+ ## 4.0.17 / 2026-07-22
4
+
5
+ ### Enhancements:
6
+
7
+ * Open compact index cache in binary mode when appending. Pull request [#9679](https://github.com/ruby/rubygems/pull/9679) by hsbt
8
+
9
+ ### Bug fixes:
10
+
11
+ * Unquote Gem.ruby when spawning it as a separate argv element. Pull request [#9695](https://github.com/ruby/rubygems/pull/9695) by hsbt
12
+ * Escape glob metacharacters in install paths when globbing. Pull request [#9687](https://github.com/ruby/rubygems/pull/9687) by hsbt
13
+ * Avoid space-containing absolute path in RUBYOPT. Pull request [#9696](https://github.com/ruby/rubygems/pull/9696) by hsbt
14
+ * Preserve the locked Bundler checksum when the gem isn't cached. Pull request [#9658](https://github.com/ruby/rubygems/pull/9658) by rwstauner
15
+
16
+ ### Documentation:
17
+
18
+ * Point Bundler gemspec metadata at the moved docs. Pull request [#9648](https://github.com/ruby/rubygems/pull/9648) by hsbt
19
+
20
+ ## 4.0.16 / 2026-07-10
21
+
22
+ ### Enhancements:
23
+
24
+ * Bundler: Fix Bundler::Fetcher for PQC support, adding integration connection tests. Pull request [#9637](https://github.com/ruby/rubygems/pull/9637) by junaruga
25
+ * Reuse RubyGems' vendored tsort in Bundler. Pull request [#9647](https://github.com/ruby/rubygems/pull/9647) by hsbt
26
+
27
+ ### Bug fixes:
28
+
29
+ * Initialize the new gem's git repo without a subshell. Pull request [#9670](https://github.com/ruby/rubygems/pull/9670) by hsbt
30
+ * Preserve CRLF lockfile line endings on Windows. Pull request [#9669](https://github.com/ruby/rubygems/pull/9669) by hsbt
31
+ * Fix the gemspec error snippet on Windows drive-letter paths. Pull request [#9668](https://github.com/ruby/rubygems/pull/9668) by hsbt
32
+
33
+ ### Documentation:
34
+
35
+ * Point Bundler gemspec metadata at the moved docs. Pull request [#9648](https://github.com/ruby/rubygems/pull/9648) by hsbt
36
+
3
37
  ## 4.0.15 / 2026-06-24
4
38
 
5
39
  ### Enhancements:
data/bundler.gemspec CHANGED
@@ -24,9 +24,9 @@ Gem::Specification.new do |s|
24
24
 
25
25
  s.metadata = {
26
26
  "bug_tracker_uri" => "https://github.com/ruby/rubygems/issues?q=is%3Aopen+is%3Aissue+label%3ABundler",
27
- "changelog_uri" => "https://github.com/ruby/rubygems/blob/master/bundler/CHANGELOG.md",
27
+ "changelog_uri" => "https://github.com/ruby/rubygems/blob/master/CHANGELOG-bundler.md",
28
28
  "homepage_uri" => "https://bundler.io/",
29
- "source_code_uri" => "https://github.com/ruby/rubygems/tree/master/bundler",
29
+ "source_code_uri" => "https://github.com/ruby/rubygems",
30
30
  }
31
31
 
32
32
  s.required_ruby_version = ">= 3.2.0"
@@ -5,7 +5,7 @@ module Bundler
5
5
  module BuildMetadata
6
6
  # begin ivars
7
7
  @built_at = nil
8
- @git_commit_sha = "12ba1f437b".freeze
8
+ @git_commit_sha = "f80cbc4c44".freeze
9
9
  # end ivars
10
10
 
11
11
  # A hash representation of the build metadata.
@@ -256,8 +256,7 @@ module Bundler
256
256
 
257
257
  if use_git
258
258
  Bundler.ui.info "\nInitializing git repo in #{target}"
259
- require "shellwords"
260
- `git init #{target.to_s.shellescape}`
259
+ IO.popen(["git", "init", target.to_s], &:read)
261
260
 
262
261
  config[:git_default_branch] = File.read("#{target}/.git/HEAD").split("/").last.chomp
263
262
  end
@@ -18,12 +18,22 @@ module Bundler
18
18
  Bundler.ui.info "Unable to open #{name} because it's a default gem, so the directory it would normally be installed to does not exist."
19
19
  else
20
20
  root_path = spec.full_gem_path
21
- require "shellwords"
22
- command = Shellwords.split(editor) << File.join([root_path, path].compact)
21
+ command = editor_command(editor) << File.join([root_path, path].compact)
23
22
  Bundler.with_original_env do
24
23
  system(*command, { chdir: root_path })
25
24
  end || Bundler.ui.info("Could not run '#{command.join(" ")}'")
26
25
  end
27
26
  end
27
+
28
+ def editor_command(editor)
29
+ # On Windows an editor is often configured with a full path such as
30
+ # C:\Program Files\Microsoft VS Code\Code.exe, which shell splitting
31
+ # would corrupt. Take a value that names an existing file as a
32
+ # single word.
33
+ return [editor] if Gem.win_platform? && File.file?(editor)
34
+
35
+ require "shellwords"
36
+ Shellwords.split(editor)
37
+ end
28
38
  end
29
39
  end
@@ -103,7 +103,7 @@ module Bundler
103
103
  # Returns false without appending when no digests since appending is too error prone to do without digests.
104
104
  def append(data)
105
105
  return false unless digests?
106
- open("a") {|f| f.write data }
106
+ open("ab") {|f| f.write data }
107
107
  verify && commit
108
108
  end
109
109
 
@@ -397,10 +397,6 @@ module Bundler
397
397
 
398
398
  contents = to_lock
399
399
 
400
- # Convert to \r\n if the existing lock has them
401
- # i.e., Windows with `git config core.autocrlf=true`
402
- contents.gsub!(/\n/, "\r\n") if @lockfile_contents.match?("\r\n")
403
-
404
400
  if @locked_bundler_version
405
401
  locked_major = @locked_bundler_version.segments.first
406
402
  current_major = bundler_version_to_lock.segments.first
@@ -421,6 +417,14 @@ module Bundler
421
417
  return
422
418
  end
423
419
 
420
+ # Convert to \r\n if the existing lock has them, i.e., Windows with
421
+ # `git config core.autocrlf=true`. Detect from the bytes on disk because
422
+ # reading in text mode strips carriage returns on Windows, which would
423
+ # otherwise defeat this check and rewrite a `\r\n` lockfile with `\n`.
424
+ if File.exist?(file) && SharedHelpers.filesystem_access(file, :read) {|p| File.binread(p).include?("\r\n") }
425
+ contents.gsub!(/\n/, "\r\n")
426
+ end
427
+
424
428
  begin
425
429
  SharedHelpers.filesystem_access(file) do |p|
426
430
  File.open(p, "wb") {|f| f.puts(contents) }
data/lib/bundler/dsl.rb CHANGED
@@ -602,8 +602,11 @@ module Bundler
602
602
 
603
603
  trace_line = backtrace.find {|l| l.include?(dsl_path) } || trace_line
604
604
  return m unless trace_line
605
- line_number = trace_line.split(":")[1].to_i - 1
605
+ # Match the line number right before `:in` or the end of the line so a
606
+ # Windows drive letter like `C:` does not get mistaken for the number.
607
+ line_number = trace_line[/:(\d+)(?::in\b|\z)/, 1]
606
608
  return m unless line_number
609
+ line_number = line_number.to_i - 1
607
610
 
608
611
  lines = contents.lines.to_a
609
612
  indent = " # "
@@ -318,7 +318,7 @@ module Bundler
318
318
  if ssl_client_cert
319
319
  pem = File.read(ssl_client_cert)
320
320
  con.cert = OpenSSL::X509::Certificate.new(pem)
321
- con.key = OpenSSL::PKey::RSA.new(pem)
321
+ con.key = OpenSSL::PKey.read(pem)
322
322
  end
323
323
 
324
324
  con.read_timeout = Fetcher.api_timeout
@@ -125,7 +125,7 @@ module Bundler
125
125
  # every native extension build with `fatal error U1065: invalid option
126
126
  # '-'`. Skip the jobserver when nmake is in use. Other Windows toolchains
127
127
  # such as mingw use GNU make and keep working through the inherited pipe.
128
- return yield if nmake?
128
+ return yield if nmake? || bsd_make?
129
129
 
130
130
  begin
131
131
  r, w = IO.pipe
@@ -155,6 +155,12 @@ module Bundler
155
155
  /\bnmake/i.match?(make.to_s)
156
156
  end
157
157
 
158
+ def bsd_make?
159
+ return false unless Gem.freebsd_platform?
160
+ make = ENV["MAKE"] || ENV["make"] || "make"
161
+ !/\bgmake/i.match?(make)
162
+ end
163
+
158
164
  def install_serially
159
165
  until finished_installing?
160
166
  raise "failed to find a spec to enqueue while installing serially" unless spec_install = @specs.find(&:ready_to_enqueue?)
@@ -103,17 +103,40 @@ module Bundler
103
103
  end
104
104
 
105
105
  def bundler_checksum
106
+ # `.dev` versions and `SKIP_BUNDLER_CHECKSUM` are deliberate opt-outs (used
107
+ # by Bundler/RubyGems' own development and release tasks): never record a
108
+ # checksum for Bundler itself in those cases.
106
109
  return [] if Bundler.gem_version.to_s.end_with?(".dev") || ENV["SKIP_BUNDLER_CHECKSUM"]
107
110
 
108
111
  bundler_spec = definition.sources.metadata_source.specs.search(["bundler", Bundler.gem_version]).last
109
- return [] unless File.exist?(bundler_spec.cache_file)
110
112
 
111
- require "rubygems/package"
113
+ # Record a fresh checksum from the locally cached gem when it's available.
114
+ # When it isn't (e.g. a fresh checkout/CI that never downloaded the bundler
115
+ # gem), fall back to whatever checksum is already locked rather than
116
+ # dropping it, so the entry stays consistent across environments.
117
+ if File.exist?(bundler_spec.cache_file)
118
+ require "rubygems/package"
119
+
120
+ package = Gem::Package.new(bundler_spec.cache_file)
121
+ definition.sources.metadata_source.checksum_store.register(bundler_spec, Checksum.from_gem_package(package))
122
+ elsif bundled_with_changing?
123
+ # We can't compute a fresh checksum (the bundler gem isn't cached) and the
124
+ # BUNDLED WITH version is changing. Keeping the previously locked checksum
125
+ # would leave a `bundler (<old version>) sha256=...` entry that no longer
126
+ # matches the new BUNDLED WITH version, so drop it instead.
127
+ return []
128
+ end
112
129
 
113
- package = Gem::Package.new(bundler_spec.cache_file)
114
- definition.sources.metadata_source.checksum_store.register(bundler_spec, Checksum.from_gem_package(package))
130
+ return [] if definition.sources.metadata_source.checksum_store.missing?(bundler_spec)
115
131
 
116
132
  [definition.sources.metadata_source.checksum_store.to_lock(bundler_spec)]
117
133
  end
134
+
135
+ def bundled_with_changing?
136
+ locked_gems = definition.locked_gems
137
+ return false unless locked_gems
138
+
139
+ locked_gems.bundler_version != definition.bundler_version_to_lock
140
+ end
118
141
  end
119
142
  end
@@ -141,7 +141,7 @@ module Bundler
141
141
  end
142
142
  end
143
143
 
144
- Dir[cache_path.join("*/.git")].each do |git_dir|
144
+ Gem::Util.glob_files_in_dir("*/.git", cache_path.to_s).each do |git_dir|
145
145
  FileUtils.rm_rf(git_dir)
146
146
  FileUtils.touch(File.expand_path("../.bundlecache", git_dir))
147
147
  end
@@ -159,13 +159,13 @@ module Bundler
159
159
  end
160
160
 
161
161
  def clean(dry_run = false)
162
- gem_bins = Dir["#{Gem.dir}/bin/*"]
163
- git_dirs = Dir["#{Gem.dir}/bundler/gems/*"]
164
- git_cache_dirs = Dir["#{Gem.dir}/cache/bundler/git/*"]
165
- gem_dirs = Dir["#{Gem.dir}/gems/*"]
166
- gem_files = Dir["#{Gem.dir}/cache/*.gem"]
167
- gemspec_files = Dir["#{Gem.dir}/specifications/*.gemspec"]
168
- extension_dirs = Dir["#{Gem.dir}/extensions/*/*/*"] + Dir["#{Gem.dir}/bundler/gems/extensions/*/*/*"]
162
+ gem_bins = Gem::Util.glob_files_in_dir("bin/*", Gem.dir)
163
+ git_dirs = Gem::Util.glob_files_in_dir("bundler/gems/*", Gem.dir)
164
+ git_cache_dirs = Gem::Util.glob_files_in_dir("cache/bundler/git/*", Gem.dir)
165
+ gem_dirs = Gem::Util.glob_files_in_dir("gems/*", Gem.dir)
166
+ gem_files = Gem::Util.glob_files_in_dir("cache/*.gem", Gem.dir)
167
+ gemspec_files = Gem::Util.glob_files_in_dir("specifications/*.gemspec", Gem.dir)
168
+ extension_dirs = Gem::Util.glob_files_in_dir("extensions/*/*/*", Gem.dir) + Gem::Util.glob_files_in_dir("bundler/gems/extensions/*/*/*", Gem.dir)
169
169
  spec_gem_paths = []
170
170
  # need to keep git sources around
171
171
  spec_git_paths = @definition.spec_git_paths
@@ -232,7 +232,7 @@ module Bundler
232
232
  private
233
233
 
234
234
  def prune_gem_cache(resolve, cache_path)
235
- cached = Dir["#{cache_path}/*.gem"]
235
+ cached = Gem::Util.glob_files_in_dir("*.gem", cache_path.to_s)
236
236
 
237
237
  cached = cached.delete_if do |path|
238
238
  spec = Bundler.rubygems.spec_from_gem path
@@ -257,7 +257,7 @@ module Bundler
257
257
  end
258
258
 
259
259
  def prune_git_and_path_cache(resolve, cache_path)
260
- cached = Dir["#{cache_path}/*/.bundlecache"]
260
+ cached = Gem::Util.glob_files_in_dir("*/.bundlecache", cache_path.to_s)
261
261
 
262
262
  cached = cached.delete_if do |path|
263
263
  name = File.basename(File.dirname(path))
@@ -283,7 +283,7 @@ module Bundler
283
283
  # Add man/ subdirectories from activated bundles to MANPATH for man(1)
284
284
  manuals = $LOAD_PATH.filter_map do |path|
285
285
  man_subdir = path.sub(/lib$/, "man")
286
- man_subdir unless Dir[man_subdir + "/man?/"].empty?
286
+ man_subdir unless Dir.glob("man?/", base: man_subdir).empty?
287
287
  end
288
288
 
289
289
  return if manuals.empty?
@@ -75,7 +75,12 @@ module Bundler
75
75
 
76
76
  argv0 = File.exist?($PROGRAM_NAME) ? $PROGRAM_NAME : Process.argv0
77
77
  cmd = [argv0, *ARGV]
78
- cmd.unshift(Gem.ruby) unless File.executable?(argv0)
78
+ unless File.executable?(argv0)
79
+ # Gem.ruby is quoted if it contains whitespace, so split it into argv
80
+ # elements to keep the quotes out of the exec'd command.
81
+ require "shellwords"
82
+ cmd.unshift(*Shellwords.split(Gem.ruby))
83
+ end
79
84
 
80
85
  Bundler.with_original_env do
81
86
  Kernel.exec(
@@ -346,7 +346,12 @@ module Bundler
346
346
 
347
347
  def set_rubyopt
348
348
  rubyopt = [ENV["RUBYOPT"]].compact
349
- setup_require = "-r#{File.expand_path("setup", __dir__)}"
349
+ setup_path = File.expand_path("setup", __dir__)
350
+ # RUBYOPT is split on whitespace with no quoting mechanism, so an
351
+ # absolute path containing spaces would be torn apart. Fall back to
352
+ # requiring by feature name; set_rubylib puts our lib directory first
353
+ # on the child's load path.
354
+ setup_require = /\s/.match?(setup_path) ? "-rbundler/setup" : "-r#{setup_path}"
350
355
  return if !rubyopt.empty? && rubyopt.first.include?(setup_require)
351
356
  rubyopt.unshift setup_require
352
357
  Bundler::SharedHelpers.set_env "RUBYOPT", rubyopt.join(" ")
@@ -320,7 +320,7 @@ module Bundler
320
320
 
321
321
  def serialize_gemspecs_in(destination)
322
322
  destination = destination.expand_path(Bundler.root) if destination.relative?
323
- Dir["#{destination}/#{@glob}"].each do |spec_path|
323
+ Gem::Util.glob_files_in_dir(@glob, destination.to_s).each do |spec_path|
324
324
  # Evaluate gemspecs and cache the result. Gemspecs
325
325
  # in git might require git or other dependencies.
326
326
  # The gemspecs we cache should already be evaluated.
@@ -406,7 +406,7 @@ module Bundler
406
406
  @cached_specs ||= begin
407
407
  idx = Index.new
408
408
 
409
- Dir["#{cache_path}/*.gem"].each do |gemfile|
409
+ Gem::Util.glob_files_in_dir("*.gem", cache_path.to_s).each do |gemfile|
410
410
  s ||= Bundler.rubygems.spec_from_gem(gemfile)
411
411
  s.source = self
412
412
  idx << s
@@ -5,7 +5,7 @@ require_relative "vendored_tsort"
5
5
  module Bundler
6
6
  class SpecSet
7
7
  include Enumerable
8
- include TSort
8
+ include Gem::TSort
9
9
 
10
10
  def initialize(specs)
11
11
  @specs = specs
@@ -315,7 +315,7 @@ module Bundler
315
315
 
316
316
  def sorted
317
317
  @sorted ||= ([@specs.find {|s| s.name == "rake" }] + tsort).compact.uniq
318
- rescue TSort::Cyclic => error
318
+ rescue Gem::TSort::Cyclic => error
319
319
  cgems = extract_circular_gems(error)
320
320
  raise CyclicDependencyError, "Your bundle requires gems that depend" \
321
321
  " on each other, creating an infinite loop. Please remove either" \
@@ -1,4 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Bundler; end
4
- require_relative "vendor/tsort/lib/tsort"
3
+ # The defined? guard avoids reopening Gem::TSort when an old RubyGems has
4
+ # already loaded its own copy, e.g. through rubygems/request_set from
5
+ # Gem.activate_bin_path in binstubs.
6
+ #
7
+ unless defined?(Gem::TSort)
8
+ begin
9
+ require "rubygems/vendored_tsort"
10
+ rescue LoadError
11
+ begin
12
+ # RubyGems 3.4 and 3.5 ship the same file under its pre-3.6 name.
13
+ # Requiring the real tsort here instead would activate the tsort
14
+ # default gem, and `bundler/setup` must not activate any gems.
15
+ require "rubygems/tsort"
16
+ rescue LoadError
17
+ require "tsort"
18
+ Gem::TSort = TSort
19
+ end
20
+ end
21
+ end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: false
2
2
 
3
3
  module Bundler
4
- VERSION = "4.0.15".freeze
4
+ VERSION = "4.0.17".freeze
5
5
 
6
6
  def self.bundler_major_version
7
7
  @bundler_major_version ||= gem_version.segments.first
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bundler
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.15
4
+ version: 4.0.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - André Arko
@@ -350,8 +350,6 @@ files:
350
350
  - lib/bundler/vendor/thor/lib/thor/shell/wrapped_printer.rb
351
351
  - lib/bundler/vendor/thor/lib/thor/util.rb
352
352
  - lib/bundler/vendor/thor/lib/thor/version.rb
353
- - lib/bundler/vendor/tsort/LICENSE.txt
354
- - lib/bundler/vendor/tsort/lib/tsort.rb
355
353
  - lib/bundler/vendor/uri/COPYING
356
354
  - lib/bundler/vendor/uri/lib/uri.rb
357
355
  - lib/bundler/vendor/uri/lib/uri/common.rb
@@ -386,9 +384,9 @@ licenses:
386
384
  - MIT
387
385
  metadata:
388
386
  bug_tracker_uri: https://github.com/ruby/rubygems/issues?q=is%3Aopen+is%3Aissue+label%3ABundler
389
- changelog_uri: https://github.com/ruby/rubygems/blob/master/bundler/CHANGELOG.md
387
+ changelog_uri: https://github.com/ruby/rubygems/blob/master/CHANGELOG-bundler.md
390
388
  homepage_uri: https://bundler.io/
391
- source_code_uri: https://github.com/ruby/rubygems/tree/master/bundler
389
+ source_code_uri: https://github.com/ruby/rubygems
392
390
  rdoc_options: []
393
391
  require_paths:
394
392
  - lib
@@ -403,7 +401,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
403
401
  - !ruby/object:Gem::Version
404
402
  version: 3.4.1
405
403
  requirements: []
406
- rubygems_version: 4.0.13
404
+ rubygems_version: 4.0.16
407
405
  specification_version: 4
408
406
  summary: The best way to manage your application's dependencies
409
407
  test_files: []
@@ -1,22 +0,0 @@
1
- Copyright (C) 1993-2013 Yukihiro Matsumoto. All rights reserved.
2
-
3
- Redistribution and use in source and binary forms, with or without
4
- modification, are permitted provided that the following conditions
5
- are met:
6
- 1. Redistributions of source code must retain the above copyright
7
- notice, this list of conditions and the following disclaimer.
8
- 2. Redistributions in binary form must reproduce the above copyright
9
- notice, this list of conditions and the following disclaimer in the
10
- documentation and/or other materials provided with the distribution.
11
-
12
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
13
- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15
- ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
16
- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17
- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
18
- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19
- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20
- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21
- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22
- SUCH DAMAGE.
@@ -1,455 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- #--
4
- # tsort.rb - provides a module for topological sorting and strongly connected components.
5
- #++
6
- #
7
-
8
- #
9
- # Bundler::TSort implements topological sorting using Tarjan's algorithm for
10
- # strongly connected components.
11
- #
12
- # Bundler::TSort is designed to be able to be used with any object which can be
13
- # interpreted as a directed graph.
14
- #
15
- # Bundler::TSort requires two methods to interpret an object as a graph,
16
- # tsort_each_node and tsort_each_child.
17
- #
18
- # * tsort_each_node is used to iterate for all nodes over a graph.
19
- # * tsort_each_child is used to iterate for child nodes of a given node.
20
- #
21
- # The equality of nodes are defined by eql? and hash since
22
- # Bundler::TSort uses Hash internally.
23
- #
24
- # == A Simple Example
25
- #
26
- # The following example demonstrates how to mix the Bundler::TSort module into an
27
- # existing class (in this case, Hash). Here, we're treating each key in
28
- # the hash as a node in the graph, and so we simply alias the required
29
- # #tsort_each_node method to Hash's #each_key method. For each key in the
30
- # hash, the associated value is an array of the node's child nodes. This
31
- # choice in turn leads to our implementation of the required #tsort_each_child
32
- # method, which fetches the array of child nodes and then iterates over that
33
- # array using the user-supplied block.
34
- #
35
- # require 'bundler/vendor/tsort/lib/tsort'
36
- #
37
- # class Hash
38
- # include Bundler::TSort
39
- # alias tsort_each_node each_key
40
- # def tsort_each_child(node, &block)
41
- # fetch(node).each(&block)
42
- # end
43
- # end
44
- #
45
- # {1=>[2, 3], 2=>[3], 3=>[], 4=>[]}.tsort
46
- # #=> [3, 2, 1, 4]
47
- #
48
- # {1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}.strongly_connected_components
49
- # #=> [[4], [2, 3], [1]]
50
- #
51
- # == A More Realistic Example
52
- #
53
- # A very simple `make' like tool can be implemented as follows:
54
- #
55
- # require 'bundler/vendor/tsort/lib/tsort'
56
- #
57
- # class Make
58
- # def initialize
59
- # @dep = {}
60
- # @dep.default = []
61
- # end
62
- #
63
- # def rule(outputs, inputs=[], &block)
64
- # triple = [outputs, inputs, block]
65
- # outputs.each {|f| @dep[f] = [triple]}
66
- # @dep[triple] = inputs
67
- # end
68
- #
69
- # def build(target)
70
- # each_strongly_connected_component_from(target) {|ns|
71
- # if ns.length != 1
72
- # fs = ns.delete_if {|n| Array === n}
73
- # raise Bundler::TSort::Cyclic.new("cyclic dependencies: #{fs.join ', '}")
74
- # end
75
- # n = ns.first
76
- # if Array === n
77
- # outputs, inputs, block = n
78
- # inputs_time = inputs.map {|f| File.mtime f}.max
79
- # begin
80
- # outputs_time = outputs.map {|f| File.mtime f}.min
81
- # rescue Errno::ENOENT
82
- # outputs_time = nil
83
- # end
84
- # if outputs_time == nil ||
85
- # inputs_time != nil && outputs_time <= inputs_time
86
- # sleep 1 if inputs_time != nil && inputs_time.to_i == Time.now.to_i
87
- # block.call
88
- # end
89
- # end
90
- # }
91
- # end
92
- #
93
- # def tsort_each_child(node, &block)
94
- # @dep[node].each(&block)
95
- # end
96
- # include Bundler::TSort
97
- # end
98
- #
99
- # def command(arg)
100
- # print arg, "\n"
101
- # system arg
102
- # end
103
- #
104
- # m = Make.new
105
- # m.rule(%w[t1]) { command 'date > t1' }
106
- # m.rule(%w[t2]) { command 'date > t2' }
107
- # m.rule(%w[t3]) { command 'date > t3' }
108
- # m.rule(%w[t4], %w[t1 t3]) { command 'cat t1 t3 > t4' }
109
- # m.rule(%w[t5], %w[t4 t2]) { command 'cat t4 t2 > t5' }
110
- # m.build('t5')
111
- #
112
- # == Bugs
113
- #
114
- # * 'tsort.rb' is wrong name because this library uses
115
- # Tarjan's algorithm for strongly connected components.
116
- # Although 'strongly_connected_components.rb' is correct but too long.
117
- #
118
- # == References
119
- #
120
- # R. E. Tarjan, "Depth First Search and Linear Graph Algorithms",
121
- # <em>SIAM Journal on Computing</em>, Vol. 1, No. 2, pp. 146-160, June 1972.
122
- #
123
-
124
- module Bundler::TSort
125
-
126
- VERSION = "0.2.0"
127
-
128
- class Cyclic < StandardError
129
- end
130
-
131
- # Returns a topologically sorted array of nodes.
132
- # The array is sorted from children to parents, i.e.
133
- # the first element has no child and the last node has no parent.
134
- #
135
- # If there is a cycle, Bundler::TSort::Cyclic is raised.
136
- #
137
- # class G
138
- # include Bundler::TSort
139
- # def initialize(g)
140
- # @g = g
141
- # end
142
- # def tsort_each_child(n, &b) @g[n].each(&b) end
143
- # def tsort_each_node(&b) @g.each_key(&b) end
144
- # end
145
- #
146
- # graph = G.new({1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]})
147
- # p graph.tsort #=> [4, 2, 3, 1]
148
- #
149
- # graph = G.new({1=>[2], 2=>[3, 4], 3=>[2], 4=>[]})
150
- # p graph.tsort # raises Bundler::TSort::Cyclic
151
- #
152
- def tsort
153
- each_node = method(:tsort_each_node)
154
- each_child = method(:tsort_each_child)
155
- Bundler::TSort.tsort(each_node, each_child)
156
- end
157
-
158
- # Returns a topologically sorted array of nodes.
159
- # The array is sorted from children to parents, i.e.
160
- # the first element has no child and the last node has no parent.
161
- #
162
- # The graph is represented by _each_node_ and _each_child_.
163
- # _each_node_ should have +call+ method which yields for each node in the graph.
164
- # _each_child_ should have +call+ method which takes a node argument and yields for each child node.
165
- #
166
- # If there is a cycle, Bundler::TSort::Cyclic is raised.
167
- #
168
- # g = {1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]}
169
- # each_node = lambda {|&b| g.each_key(&b) }
170
- # each_child = lambda {|n, &b| g[n].each(&b) }
171
- # p Bundler::TSort.tsort(each_node, each_child) #=> [4, 2, 3, 1]
172
- #
173
- # g = {1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}
174
- # each_node = lambda {|&b| g.each_key(&b) }
175
- # each_child = lambda {|n, &b| g[n].each(&b) }
176
- # p Bundler::TSort.tsort(each_node, each_child) # raises Bundler::TSort::Cyclic
177
- #
178
- def self.tsort(each_node, each_child)
179
- tsort_each(each_node, each_child).to_a
180
- end
181
-
182
- # The iterator version of the #tsort method.
183
- # <tt><em>obj</em>.tsort_each</tt> is similar to <tt><em>obj</em>.tsort.each</tt>, but
184
- # modification of _obj_ during the iteration may lead to unexpected results.
185
- #
186
- # #tsort_each returns +nil+.
187
- # If there is a cycle, Bundler::TSort::Cyclic is raised.
188
- #
189
- # class G
190
- # include Bundler::TSort
191
- # def initialize(g)
192
- # @g = g
193
- # end
194
- # def tsort_each_child(n, &b) @g[n].each(&b) end
195
- # def tsort_each_node(&b) @g.each_key(&b) end
196
- # end
197
- #
198
- # graph = G.new({1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]})
199
- # graph.tsort_each {|n| p n }
200
- # #=> 4
201
- # # 2
202
- # # 3
203
- # # 1
204
- #
205
- def tsort_each(&block) # :yields: node
206
- each_node = method(:tsort_each_node)
207
- each_child = method(:tsort_each_child)
208
- Bundler::TSort.tsort_each(each_node, each_child, &block)
209
- end
210
-
211
- # The iterator version of the Bundler::TSort.tsort method.
212
- #
213
- # The graph is represented by _each_node_ and _each_child_.
214
- # _each_node_ should have +call+ method which yields for each node in the graph.
215
- # _each_child_ should have +call+ method which takes a node argument and yields for each child node.
216
- #
217
- # g = {1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]}
218
- # each_node = lambda {|&b| g.each_key(&b) }
219
- # each_child = lambda {|n, &b| g[n].each(&b) }
220
- # Bundler::TSort.tsort_each(each_node, each_child) {|n| p n }
221
- # #=> 4
222
- # # 2
223
- # # 3
224
- # # 1
225
- #
226
- def self.tsort_each(each_node, each_child) # :yields: node
227
- return to_enum(__method__, each_node, each_child) unless block_given?
228
-
229
- each_strongly_connected_component(each_node, each_child) {|component|
230
- if component.size == 1
231
- yield component.first
232
- else
233
- raise Cyclic.new("topological sort failed: #{component.inspect}")
234
- end
235
- }
236
- end
237
-
238
- # Returns strongly connected components as an array of arrays of nodes.
239
- # The array is sorted from children to parents.
240
- # Each elements of the array represents a strongly connected component.
241
- #
242
- # class G
243
- # include Bundler::TSort
244
- # def initialize(g)
245
- # @g = g
246
- # end
247
- # def tsort_each_child(n, &b) @g[n].each(&b) end
248
- # def tsort_each_node(&b) @g.each_key(&b) end
249
- # end
250
- #
251
- # graph = G.new({1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]})
252
- # p graph.strongly_connected_components #=> [[4], [2], [3], [1]]
253
- #
254
- # graph = G.new({1=>[2], 2=>[3, 4], 3=>[2], 4=>[]})
255
- # p graph.strongly_connected_components #=> [[4], [2, 3], [1]]
256
- #
257
- def strongly_connected_components
258
- each_node = method(:tsort_each_node)
259
- each_child = method(:tsort_each_child)
260
- Bundler::TSort.strongly_connected_components(each_node, each_child)
261
- end
262
-
263
- # Returns strongly connected components as an array of arrays of nodes.
264
- # The array is sorted from children to parents.
265
- # Each elements of the array represents a strongly connected component.
266
- #
267
- # The graph is represented by _each_node_ and _each_child_.
268
- # _each_node_ should have +call+ method which yields for each node in the graph.
269
- # _each_child_ should have +call+ method which takes a node argument and yields for each child node.
270
- #
271
- # g = {1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]}
272
- # each_node = lambda {|&b| g.each_key(&b) }
273
- # each_child = lambda {|n, &b| g[n].each(&b) }
274
- # p Bundler::TSort.strongly_connected_components(each_node, each_child)
275
- # #=> [[4], [2], [3], [1]]
276
- #
277
- # g = {1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}
278
- # each_node = lambda {|&b| g.each_key(&b) }
279
- # each_child = lambda {|n, &b| g[n].each(&b) }
280
- # p Bundler::TSort.strongly_connected_components(each_node, each_child)
281
- # #=> [[4], [2, 3], [1]]
282
- #
283
- def self.strongly_connected_components(each_node, each_child)
284
- each_strongly_connected_component(each_node, each_child).to_a
285
- end
286
-
287
- # The iterator version of the #strongly_connected_components method.
288
- # <tt><em>obj</em>.each_strongly_connected_component</tt> is similar to
289
- # <tt><em>obj</em>.strongly_connected_components.each</tt>, but
290
- # modification of _obj_ during the iteration may lead to unexpected results.
291
- #
292
- # #each_strongly_connected_component returns +nil+.
293
- #
294
- # class G
295
- # include Bundler::TSort
296
- # def initialize(g)
297
- # @g = g
298
- # end
299
- # def tsort_each_child(n, &b) @g[n].each(&b) end
300
- # def tsort_each_node(&b) @g.each_key(&b) end
301
- # end
302
- #
303
- # graph = G.new({1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]})
304
- # graph.each_strongly_connected_component {|scc| p scc }
305
- # #=> [4]
306
- # # [2]
307
- # # [3]
308
- # # [1]
309
- #
310
- # graph = G.new({1=>[2], 2=>[3, 4], 3=>[2], 4=>[]})
311
- # graph.each_strongly_connected_component {|scc| p scc }
312
- # #=> [4]
313
- # # [2, 3]
314
- # # [1]
315
- #
316
- def each_strongly_connected_component(&block) # :yields: nodes
317
- each_node = method(:tsort_each_node)
318
- each_child = method(:tsort_each_child)
319
- Bundler::TSort.each_strongly_connected_component(each_node, each_child, &block)
320
- end
321
-
322
- # The iterator version of the Bundler::TSort.strongly_connected_components method.
323
- #
324
- # The graph is represented by _each_node_ and _each_child_.
325
- # _each_node_ should have +call+ method which yields for each node in the graph.
326
- # _each_child_ should have +call+ method which takes a node argument and yields for each child node.
327
- #
328
- # g = {1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]}
329
- # each_node = lambda {|&b| g.each_key(&b) }
330
- # each_child = lambda {|n, &b| g[n].each(&b) }
331
- # Bundler::TSort.each_strongly_connected_component(each_node, each_child) {|scc| p scc }
332
- # #=> [4]
333
- # # [2]
334
- # # [3]
335
- # # [1]
336
- #
337
- # g = {1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}
338
- # each_node = lambda {|&b| g.each_key(&b) }
339
- # each_child = lambda {|n, &b| g[n].each(&b) }
340
- # Bundler::TSort.each_strongly_connected_component(each_node, each_child) {|scc| p scc }
341
- # #=> [4]
342
- # # [2, 3]
343
- # # [1]
344
- #
345
- def self.each_strongly_connected_component(each_node, each_child) # :yields: nodes
346
- return to_enum(__method__, each_node, each_child) unless block_given?
347
-
348
- id_map = {}
349
- stack = []
350
- each_node.call {|node|
351
- unless id_map.include? node
352
- each_strongly_connected_component_from(node, each_child, id_map, stack) {|c|
353
- yield c
354
- }
355
- end
356
- }
357
- nil
358
- end
359
-
360
- # Iterates over strongly connected component in the subgraph reachable from
361
- # _node_.
362
- #
363
- # Return value is unspecified.
364
- #
365
- # #each_strongly_connected_component_from doesn't call #tsort_each_node.
366
- #
367
- # class G
368
- # include Bundler::TSort
369
- # def initialize(g)
370
- # @g = g
371
- # end
372
- # def tsort_each_child(n, &b) @g[n].each(&b) end
373
- # def tsort_each_node(&b) @g.each_key(&b) end
374
- # end
375
- #
376
- # graph = G.new({1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]})
377
- # graph.each_strongly_connected_component_from(2) {|scc| p scc }
378
- # #=> [4]
379
- # # [2]
380
- #
381
- # graph = G.new({1=>[2], 2=>[3, 4], 3=>[2], 4=>[]})
382
- # graph.each_strongly_connected_component_from(2) {|scc| p scc }
383
- # #=> [4]
384
- # # [2, 3]
385
- #
386
- def each_strongly_connected_component_from(node, id_map={}, stack=[], &block) # :yields: nodes
387
- Bundler::TSort.each_strongly_connected_component_from(node, method(:tsort_each_child), id_map, stack, &block)
388
- end
389
-
390
- # Iterates over strongly connected components in a graph.
391
- # The graph is represented by _node_ and _each_child_.
392
- #
393
- # _node_ is the first node.
394
- # _each_child_ should have +call+ method which takes a node argument
395
- # and yields for each child node.
396
- #
397
- # Return value is unspecified.
398
- #
399
- # #Bundler::TSort.each_strongly_connected_component_from is a class method and
400
- # it doesn't need a class to represent a graph which includes Bundler::TSort.
401
- #
402
- # graph = {1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}
403
- # each_child = lambda {|n, &b| graph[n].each(&b) }
404
- # Bundler::TSort.each_strongly_connected_component_from(1, each_child) {|scc|
405
- # p scc
406
- # }
407
- # #=> [4]
408
- # # [2, 3]
409
- # # [1]
410
- #
411
- def self.each_strongly_connected_component_from(node, each_child, id_map={}, stack=[]) # :yields: nodes
412
- return to_enum(__method__, node, each_child, id_map, stack) unless block_given?
413
-
414
- minimum_id = node_id = id_map[node] = id_map.size
415
- stack_length = stack.length
416
- stack << node
417
-
418
- each_child.call(node) {|child|
419
- if id_map.include? child
420
- child_id = id_map[child]
421
- minimum_id = child_id if child_id && child_id < minimum_id
422
- else
423
- sub_minimum_id =
424
- each_strongly_connected_component_from(child, each_child, id_map, stack) {|c|
425
- yield c
426
- }
427
- minimum_id = sub_minimum_id if sub_minimum_id < minimum_id
428
- end
429
- }
430
-
431
- if node_id == minimum_id
432
- component = stack.slice!(stack_length .. -1)
433
- component.each {|n| id_map[n] = nil}
434
- yield component
435
- end
436
-
437
- minimum_id
438
- end
439
-
440
- # Should be implemented by a extended class.
441
- #
442
- # #tsort_each_node is used to iterate for all nodes over a graph.
443
- #
444
- def tsort_each_node # :yields: node
445
- raise NotImplementedError.new
446
- end
447
-
448
- # Should be implemented by a extended class.
449
- #
450
- # #tsort_each_child is used to iterate for child nodes of _node_.
451
- #
452
- def tsort_each_child(node) # :yields: child
453
- raise NotImplementedError.new
454
- end
455
- end