bundler 4.0.15 → 4.0.16

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: d4ec7b35f89e61eb8c603099869df9c107accc057741b52f03b849d31ae0e734
4
+ data.tar.gz: 13f346287c2ee66bb2baa3ddfaff121fce05c0f428ae73aa199b148559a80525
5
5
  SHA512:
6
- metadata.gz: e36d0b9bc29b9ca34a12ccb44ec995e774f55348d2181ea220ed70d68232d65f89c338ac799f35ea3a97cddbc73db10ef584756cfa1e480cc0c1be8b97c454ce
7
- data.tar.gz: f5dfd925657ea59c8fde6a4c5bf658e7e3b5b9357a1ccd9d03167dc7cfbcd40db826d3b909a65101e24bd15d15287d4e6505884a6117e152feb516e0597ca2ab
6
+ metadata.gz: 6e70a1fb438a201e9864383eba6a47727102581979a7e15902d219cca5939e623e933b728a812308b0cf7e2210237bfcee2be2d99e36eabb24296360be296607
7
+ data.tar.gz: 105f3457b4a7f4e69a252ee7857fcbeb7c4c91406c1ad76a693f09e2a3cf2a741bf407a91053fbe57d29afe8c88ded24553b6de33deb76ce11c9abff20d117ec
data/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # Changelog
2
2
 
3
+ ## 4.0.16 / 2026-07-10
4
+
5
+ ### Enhancements:
6
+
7
+ * Bundler: Fix Bundler::Fetcher for PQC support, adding integration connection tests. Pull request [#9637](https://github.com/ruby/rubygems/pull/9637) by junaruga
8
+ * Reuse RubyGems' vendored tsort in Bundler. Pull request [#9647](https://github.com/ruby/rubygems/pull/9647) by hsbt
9
+
10
+ ### Bug fixes:
11
+
12
+ * Initialize the new gem's git repo without a subshell. Pull request [#9670](https://github.com/ruby/rubygems/pull/9670) by hsbt
13
+ * Preserve CRLF lockfile line endings on Windows. Pull request [#9669](https://github.com/ruby/rubygems/pull/9669) by hsbt
14
+ * Fix the gemspec error snippet on Windows drive-letter paths. Pull request [#9668](https://github.com/ruby/rubygems/pull/9668) by hsbt
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
+
3
20
  ## 4.0.15 / 2026-06-24
4
21
 
5
22
  ### 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 = "fc18079dba".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
@@ -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?)
@@ -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.16".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.16
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
@@ -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