forked from Carthage/Carthage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Resolver.swift
531 lines (464 loc) · 18.5 KB
/
Resolver.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
import Foundation
import Result
import ReactiveSwift
import SPMUtility
/// Protocol for resolving acyclic dependency graphs.
public protocol ResolverProtocol {
init(
versionsForDependency: @escaping (Dependency) -> SignalProducer<PinnedVersion, CarthageError>,
dependenciesForDependency: @escaping (Dependency, PinnedVersion) -> SignalProducer<(Dependency, VersionSpecifier), CarthageError>,
resolvedGitReference: @escaping (Dependency, String) -> SignalProducer<PinnedVersion, CarthageError>
)
func resolve(
dependencies: [Dependency: VersionSpecifier],
lastResolved: [Dependency: PinnedVersion]?,
dependenciesToUpdate: [String]?
) -> SignalProducer<[Dependency: PinnedVersion], CarthageError>
}
/// Responsible for resolving acyclic dependency graphs.
public struct Resolver: ResolverProtocol {
private let versionsForDependency: (Dependency) -> SignalProducer<PinnedVersion, CarthageError>
private let resolvedGitReference: (Dependency, String) -> SignalProducer<PinnedVersion, CarthageError>
private let dependenciesForDependency: (Dependency, PinnedVersion) -> SignalProducer<(Dependency, VersionSpecifier), CarthageError>
/// Instantiates a dependency graph resolver with the given behaviors.
///
/// versionsForDependency - Sends a stream of available versions for a
/// dependency.
/// dependenciesForDependency - Loads the dependencies for a specific
/// version of a dependency.
/// resolvedGitReference - Resolves an arbitrary Git reference to the
/// latest object.
public init(
versionsForDependency: @escaping (Dependency) -> SignalProducer<PinnedVersion, CarthageError>,
dependenciesForDependency: @escaping (Dependency, PinnedVersion) -> SignalProducer<(Dependency, VersionSpecifier), CarthageError>,
resolvedGitReference: @escaping (Dependency, String) -> SignalProducer<PinnedVersion, CarthageError>
) {
self.versionsForDependency = versionsForDependency
self.dependenciesForDependency = dependenciesForDependency
self.resolvedGitReference = resolvedGitReference
}
/// Attempts to determine the latest valid version to use for each
/// dependency in `dependencies`, and all nested dependencies thereof.
///
/// Sends a dictionary with each dependency and its resolved version.
public func resolve(
dependencies: [Dependency: VersionSpecifier],
lastResolved: [Dependency: PinnedVersion]? = nil,
dependenciesToUpdate: [String]? = nil
) -> SignalProducer<[Dependency: PinnedVersion], CarthageError> {
return graphs(for: dependencies, dependencyOf: nil, basedOnGraph: DependencyGraph())
.take(first: 1)
.observe(on: QueueScheduler(qos: .default, name: "org.carthage.CarthageKit.Resolver.resolve"))
.flatMap(.merge) { graph -> SignalProducer<(Dependency, PinnedVersion), CarthageError> in
let orderedNodes = graph.orderedNodes.map { node -> DependencyNode in
node.dependencies = graph.edges[node] ?? []
return node
}
let orderedNodesProducer = SignalProducer<DependencyNode, CarthageError>(orderedNodes)
guard
let dependenciesToUpdate = dependenciesToUpdate,
let lastResolved = lastResolved,
!dependenciesToUpdate.isEmpty else {
// All the dependencies are affected.
return orderedNodesProducer.map { node in node.pinnedDependency }
}
// When target dependencies are specified
return orderedNodesProducer.filterMap { node -> (Dependency, PinnedVersion)? in
// A dependency included in the targets should be affected.
if dependenciesToUpdate.contains(node.dependency.name) {
return node.pinnedDependency
}
// Nested dependencies of the targets should also be affected.
if graph.dependencies(dependenciesToUpdate, containsNestedDependencyOfNode: node) {
return node.pinnedDependency
}
// The dependencies which are not related to the targets
// should not be affected, so use the last resolved version.
if let version = lastResolved[node.dependency] {
return (node.dependency, version)
}
// Skip newly added nodes which are not in the targets.
return nil
}
}
.reduce(into: [:]) { (result: inout [Dependency: PinnedVersion], dependency) in
result[dependency.0] = dependency.1
}
}
/// Sends all permutations of valid DependencyNodes, corresponding to the
/// given dependencies, in the order that they should be tried.
///
/// In other words, this will always send arrays equal in length to
/// `dependencies`. Each array represents one possible permutation of those
/// dependencies (chosen from among the versions that actually exist for
/// each).
private func nodePermutations(for dependencies: [Dependency: VersionSpecifier]) -> SignalProducer<[DependencyNode], CarthageError> {
let scheduler = QueueScheduler(qos: .default, name: "org.carthage.CarthageKit.Resolver.nodePermutations")
return SignalProducer(dependencies)
.map { dependency -> SignalProducer<DependencyNode, CarthageError> in
return SignalProducer<(key: Dependency, value: VersionSpecifier), CarthageError>(value: dependency)
.flatMap(.concat) { dependency -> SignalProducer<PinnedVersion, CarthageError> in
if case let .gitReference(refName) = dependency.value {
return self.resolvedGitReference(dependency.key, refName)
}
return self
.versionsForDependency(dependency.key)
.filter { dependency.value.isSatisfied(by: $0) }
}
.start(on: scheduler)
.observe(on: scheduler)
.map { DependencyNode(dependency: dependency.key, proposedVersion: $0, versionSpecifier: dependency.value) }
.collect()
.map { $0.sorted() }
.flatMap(.concat) { nodes -> SignalProducer<DependencyNode, CarthageError> in
if nodes.isEmpty {
return SignalProducer(error: CarthageError.requiredVersionNotFound(dependency.key, dependency.value))
} else {
return SignalProducer(nodes)
}
}
}
.observe(on: scheduler)
.permute()
}
/// Sends all possible permutations of `inputGraph` oriented around the
/// dependencies of `node`.
///
/// In other words, this attempts to create one transformed graph for each
/// possible permutation of the dependencies for the given node (chosen from
/// among the versions that actually exist for each).
private func graphsForDependenciesOfNode(_ node: DependencyNode, basedOnGraph inputGraph: DependencyGraph) -> SignalProducer<DependencyGraph, CarthageError> {
let scheduler = QueueScheduler(qos: .default, name: "org.carthage.CarthageKit.Resolver.graphsForDependenciesOfNode")
return dependenciesForDependency(node.dependency, node.proposedVersion)
.start(on: scheduler)
.reduce(into: [:]) { result, dependency in
result[dependency.0] = dependency.1
}
.concat(value: [:])
.take(first: 1)
.observe(on: scheduler)
.flatMap(.concat) { dependencies in
return self.graphs(for: dependencies, dependencyOf: node, basedOnGraph: inputGraph)
}
}
/// Recursively permutes `dependencies` and all dependencies thereof,
/// attaching each permutation to `inputGraph` as a dependency of the
/// specified node (or as a root otherwise).
///
/// This is a helper method, and not meant to be called from outside.
private func graphs(
for dependencies: [Dependency: VersionSpecifier],
dependencyOf: DependencyNode?,
basedOnGraph inputGraph: DependencyGraph
) -> SignalProducer<DependencyGraph, CarthageError> {
return nodePermutations(for: dependencies)
.flatMap(.concat) { (nodes: [DependencyNode]) -> SignalProducer<Signal<DependencyGraph, CarthageError>.Event, NoError> in
return self
.graphs(for: nodes, dependencyOf: dependencyOf, basedOnGraph: inputGraph)
.materialize()
}
// Pass through resolution errors only if we never got
// a valid graph.
.dematerializeErrorsIfEmpty()
}
/// Recursively permutes each element in `nodes` and all dependencies
/// thereof, attaching each permutation to `inputGraph` as a dependency of
/// the specified node (or as a root otherwise).
///
/// This is a helper method, and not meant to be called from outside.
private func graphs(
for nodes: [DependencyNode],
dependencyOf: DependencyNode?,
basedOnGraph inputGraph: DependencyGraph
) -> SignalProducer<DependencyGraph, CarthageError> {
let scheduler = QueueScheduler(qos: .default, name: "org.carthage.CarthageKit.Resolver.graphs")
return SignalProducer<(DependencyGraph, [DependencyNode]), CarthageError> { () -> Result<(DependencyGraph, [DependencyNode]), CarthageError> in
var graph = inputGraph
return graph
.addNodes(nodes, dependenciesOf: dependencyOf)
.map { newNodes in
return (graph, newNodes)
}
}
.flatMap(.concat) { graph, nodes -> SignalProducer<DependencyGraph, CarthageError> in
return SignalProducer(nodes)
// Each producer represents all evaluations of one subtree.
.map { node in self.graphsForDependenciesOfNode(node, basedOnGraph: graph) }
.observe(on: scheduler)
.permute()
.flatMap(.concat) { graphs -> SignalProducer<Signal<DependencyGraph, CarthageError>.Event, NoError> in
return SignalProducer<DependencyGraph, CarthageError> {
mergeGraphs([ inputGraph ] + graphs)
}
.materialize()
}
// Pass through resolution errors only if we never got
// a valid graph.
.dematerializeErrorsIfEmpty()
}
}
}
/// Represents an acyclic dependency graph in which each project appears at most
/// once.
///
/// Dependency graphs can exist in an incomplete state, but will never be
/// inconsistent (i.e., include versions that are known to be invalid given the
/// current graph).
private struct DependencyGraph {
/// A full list of all nodes included in the graph.
var allNodes: Set<DependencyNode> = []
/// All nodes that have dependencies, associated with those lists of
/// dependencies themselves including the intermediates.
var edges: [DependencyNode: Set<DependencyNode>] = [:]
/// The root nodes of the graph (i.e., those dependencies that are listed
/// by the top-level project).
var roots: Set<DependencyNode> = []
/// Returns all of the graph nodes, in the order that they should be built.
var orderedNodes: [DependencyNode] {
return allNodes.sorted { lhs, rhs in
let lhsDependencies = self.edges[lhs]
let rhsDependencies = self.edges[rhs]
if let rhsDependencies = rhsDependencies {
// If the right node has a dependency on the left node, the
// left node needs to be built first (and is thus ordered
// first).
if rhsDependencies.contains(lhs) {
return true
}
}
if let lhsDependencies = lhsDependencies {
// If the left node has a dependency on the right node, the
// right node needs to be built first.
if lhsDependencies.contains(rhs) {
return false
}
}
// If neither node depends on each other, sort the one with the
// fewer dependencies first.
let lhsCount = lhsDependencies?.count ?? 0
let rhsCount = rhsDependencies?.count ?? 0
if lhsCount < rhsCount {
return true
} else if lhsCount > rhsCount {
return false
} else {
// If all else fails, compare names.
return lhs.dependency.name < rhs.dependency.name
}
}
}
init() {}
/// Attempts to add the given node to the graph, optionally as a dependency
/// of another.
///
/// If the given node refers to a project which already exists in the graph,
/// this method will attempt to unify the version specifiers of both.
///
/// Returns the node as actually inserted into the graph (which may be
/// different from the node passed in), or an error if this addition would
/// make the graph inconsistent.
mutating func addNode(_ node: DependencyNode, dependencyOf: DependencyNode?) -> Result<DependencyNode, CarthageError> {
var node = node
if let index = allNodes.index(of: node) {
let existingNode = allNodes[index]
if let newSpecifier = intersection(existingNode.versionSpecifier, node.versionSpecifier) {
if newSpecifier.isSatisfied(by: existingNode.proposedVersion) {
node = existingNode
node.versionSpecifier = newSpecifier
} else {
return .failure(CarthageError.requiredVersionNotFound(node.dependency, newSpecifier))
}
} else if existingNode.proposedVersion != node.proposedVersion {
// The guard condition above is required for enabling to build a
// dependency graph in the cases such as: one node has a
// `.gitReference` specifier of a branch name, and the other has
// a `.gitReference` of a SHA which is the HEAD of that branch.
// If the specifiers are not the same but the nodes have the same
// proposed version, the graph should be valid.
//
// See https://github.com/Carthage/Carthage/issues/765.
let existingDependencyOf = edges
.filter { _, value in value.contains(existingNode) }
.map { $0.0 }
.first
let first = (existingNode.versionSpecifier, existingDependencyOf?.dependency)
let second = (node.versionSpecifier, dependencyOf?.dependency)
return .failure(CarthageError.incompatibleRequirements(node.dependency, first, second))
}
} else {
allNodes.insert(node)
}
if let dependencyOf = dependencyOf {
var nodeSet = edges[dependencyOf] ?? Set()
nodeSet.insert(node)
// If the given node has its dependencies, add them also to the list.
if let dependenciesOfNode = edges[node] {
nodeSet.formUnion(dependenciesOfNode)
}
edges[dependencyOf] = nodeSet
// Add a nested dependency to the list of its ancestor.
let edgesCopy = edges
for (ancestor, var itsDependencies) in edgesCopy {
if itsDependencies.contains(dependencyOf) {
itsDependencies.insert(node)
edges[ancestor] = itsDependencies
}
}
} else {
roots.insert(node)
}
return .success(node)
}
/// Attempts to add the given nodes to the graph, optionally as a dependency
/// of another.
///
/// If a given node refers to a project which already exists in the graph,
/// this method will attempt to unify the version specifiers of both.
///
/// Returns the nodes as actually inserted into the graph (which may be
/// different from the node passed in), or an error if this addition would
/// make the graph inconsistent.
mutating func addNodes
<C: Collection>
(_ nodes: C, dependenciesOf: DependencyNode?) -> Result<[DependencyNode], CarthageError>
where C.Iterator.Element == DependencyNode {
var newNodes: [DependencyNode] = []
for node in nodes {
switch self.addNode(node, dependencyOf: dependenciesOf) {
case let .success(newNode):
newNodes.append(newNode)
case let .failure(error):
return Result(error: error)
}
}
return Result(value: newNodes)
}
/// Whether the given node is included or not in the nested dependencies of
/// the given dependencies.
func dependencies(_ dependencies: [String], containsNestedDependencyOfNode node: DependencyNode) -> Bool {
return edges
.first { edge, nodeSet in
return dependencies.contains(edge.dependency.name) && nodeSet.contains(node)
}
.map { _ in true } ?? false
}
}
extension DependencyGraph: Equatable {
fileprivate static func == (_ lhs: DependencyGraph, _ rhs: DependencyGraph) -> Bool {
if lhs.edges.count != rhs.edges.count || lhs.roots.count != rhs.roots.count {
return false
}
for (edge, leftDeps) in lhs.edges {
if let rightDeps = rhs.edges[edge] {
if leftDeps.count != rightDeps.count {
return false
}
for dep in leftDeps {
if !rightDeps.contains(dep) {
return false
}
}
} else {
return false
}
}
for root in lhs.roots {
if !rhs.roots.contains(root) {
return false
}
}
return true
}
}
extension DependencyGraph: CustomStringConvertible {
fileprivate var description: String {
var str = "Roots:"
for root in roots {
str += "\n\t\(root)"
}
str += "\n\nEdges:"
for (node, dependencies) in edges {
str += "\n\t\(node.dependency) ->"
for dep in dependencies {
str += "\n\t\t\(dep)"
}
}
return str
}
}
/// Attempts to unify a collection of graphs.
///
/// Returns the new graph, or an error if the graphs specify inconsistent
/// versions for one or more dependencies.
private func mergeGraphs
<C: Collection>
(_ graphs: C) -> Result<DependencyGraph, CarthageError>
where C.Iterator.Element == DependencyGraph {
precondition(!graphs.isEmpty)
var result: Result<DependencyGraph, CarthageError> = .success(graphs.first!)
for next in graphs {
for root in next.roots {
result = result.flatMap { graph in
var graph = graph
return graph.addNode(root, dependencyOf: nil).map { _ in graph }
}
}
for (node, dependencies) in next.edges {
for dependency in dependencies {
result = result.flatMap { graph in
var graph = graph
return graph.addNode(dependency, dependencyOf: node).map { _ in graph }
}
}
}
}
return result
}
/// A node in, or being considered for, an acyclic dependency graph.
private final class DependencyNode {
/// The dependency that this node refers to.
let dependency: Dependency
/// The version of the dependency that this node represents.
///
/// This version is merely "proposed" because it depends on the final
/// resolution of the graph, as well as whether any "better" graphs exist.
let proposedVersion: PinnedVersion
/// The current requirements applied to this dependency.
///
/// This specifier may change as the graph is added to, and the requirements
/// become more stringent.
var versionSpecifier: VersionSpecifier
/// The dependencies of this node.
var dependencies: Set<DependencyNode> = []
/// A Dependency equivalent to this node.
var pinnedDependency: (Dependency, PinnedVersion) {
return (dependency, proposedVersion)
}
init(dependency: Dependency, proposedVersion: PinnedVersion, versionSpecifier: VersionSpecifier) {
precondition(versionSpecifier.isSatisfied(by: proposedVersion))
self.dependency = dependency
self.proposedVersion = proposedVersion
self.versionSpecifier = versionSpecifier
}
}
extension DependencyNode: Comparable {
fileprivate static func < (_ lhs: DependencyNode, _ rhs: DependencyNode) -> Bool {
let leftSemantic = Version.from(lhs.proposedVersion).value ?? Version(0, 0, 0)
let rightSemantic = Version.from(rhs.proposedVersion).value ?? Version(0, 0, 0)
// Try higher versions first.
return leftSemantic > rightSemantic
}
fileprivate static func == (_ lhs: DependencyNode, _ rhs: DependencyNode) -> Bool {
return lhs.dependency == rhs.dependency
}
}
extension DependencyNode: Hashable {
fileprivate func hash(into hasher: inout Hasher) {
hasher.combine(dependency)
}
}
extension DependencyNode: CustomStringConvertible {
fileprivate var description: String {
return "\(dependency) @ \(proposedVersion) (restricted to \(versionSpecifier))"
}
}