forked from nsoperations/Carthage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DependencySet.swift
471 lines (405 loc) · 20 KB
/
DependencySet.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
import Foundation
import BTree
typealias DependencyEntry = (key: Dependency, value: VersionSpecifier)
// swiftlint:disable type_body_length
/**
Set representing a complete dependency tree with all compatible versions per dependency.
It uses ConcreteVersionSet as implementation for storing the concrete compatible versions.
*/
final class DependencySet {
// MARK: - Properties
/**
The set of yet unresolved dependencies. For a complete dependency set this should be empty.
*/
public private(set) var unresolvedDependencies: SortedSet<Dependency>
/**
The rejectionError describing the reason for rejection if any.
*/
public var rejectionError: CarthageError?
/**
Whether or not the set is rejected. No further processing necessary.
*/
public var isRejected: Bool {
return rejectionError != nil
}
/**
Whether or not the set is complete. No further processing necessary.
*/
public var isComplete: Bool {
// Dependency resolution is complete if there are no unresolved dependencies anymore
return unresolvedDependencies.isEmpty
}
/**
True if and only if the set is not rejected and is complete.
*/
public var isAccepted: Bool {
return !isRejected && isComplete
}
private var contents: [Dependency: ConcreteVersionSet]
private var updatableDependencyNames: Set<String>
private let resolverContext: ResolverContext
public var pinnedVersions: [Dependency: [PinnedVersion]] {
return contents.mapValues({ versionSet -> [PinnedVersion] in
versionSet.pinnedVersions
})
}
// MARK: - Initializers
public convenience init(requiredDependencies: [DependencyEntry],
updatableDependencyNames: Set<String>,
resolverContext: ResolverContext) throws {
self.init(unresolvedDependencies: SortedSet(requiredDependencies.map { $0.key }),
updatableDependencyNames: updatableDependencyNames,
contents: [Dependency: ConcreteVersionSet](),
resolverContext: resolverContext)
try self.expand(parent: nil, with: requiredDependencies)
}
private init(unresolvedDependencies: SortedSet<Dependency>,
updatableDependencyNames: Set<String>,
contents: [Dependency: ConcreteVersionSet],
resolverContext: ResolverContext) {
self.unresolvedDependencies = unresolvedDependencies
self.updatableDependencyNames = updatableDependencyNames
self.contents = contents
self.resolverContext = resolverContext
}
// MARK: - Public methods
/**
Returns a copy of this set.
*/
public var copy: DependencySet {
return DependencySet(
unresolvedDependencies: unresolvedDependencies,
updatableDependencyNames: updatableDependencyNames,
contents: contents.mapValues { $0.copy },
resolverContext: resolverContext)
}
/**
Returns all resolved dependencies as a dictionary.
*/
public var resolvedDependencies: [Dependency: PinnedVersion] {
return contents.filterMapValues { $0.first?.pinnedVersion }
}
/**
Returns the next unresolved dependency for processing. The dependency returned is the dependency with the highest likelyhood of producing a conflict.
*/
public var nextUnresolvedDependency: Dependency? {
return resolverContext.problematicDependencies.first { unresolvedDependencies.contains($0) } ?? unresolvedDependencies.first
}
/**
The currently resolved versions for the specified dependency.
*/
public func versions(for dependency: Dependency) -> ConcreteVersionSet? {
return contents[dependency]
}
/**
Whether or not this set contains the specified dependency.
*/
public func containsDependency(_ dependency: Dependency) -> Bool {
return contents[dependency] != nil
}
/**
Whether or not the specified dependency is updatable.
*/
public func isUpdatableDependency(_ dependency: Dependency) -> Bool {
return updatableDependencyNames.isEmpty || updatableDependencyNames.contains(dependency.name)
}
/**
Pops a subset of this set for further processing, by taking the next most appropriate unresolved dependency.
Basically this singles out one concrete version for this unresolved dependency and removes this exact versioned dependency from the receiver,
while returning a copy with that version for that dependency.
Example: say the next unresolved dependency is A with versions [1.1, 1.2, 1.3, 1.4]. Then after this operation the receiver would contain versions:
[1.1, 1.2, 1.3] and the returned copy would contain version [1.4].
If there was already exactly one version for the dependency, no copy is returned but the receiver itself.
If there was no further subset to process (no unresolved dependencies or the receiver is already rejected), nil is returned.
*/
public func popSubSet() throws -> DependencySet? {
while !isComplete && !isRejected {
if let dependency = self.nextUnresolvedDependency {
// Select the first version, which is also the most appropriate version (highest version corresponding with version specifier)
guard let versionSet = contents[dependency], let version = versionSet.first else {
// Empty version set for this dependency, so there's no more subsets to consider
return nil
}
let concreteVersionedDependency = ConcreteVersionedDependency(dependency: dependency, concreteVersion: version)
let optionalCachedConflict = resolverContext.cachedConflict(for: concreteVersionedDependency)
let newSet: DependencySet
if let cachedConflict = optionalCachedConflict, cachedConflict.conflictingDependencies == nil {
// Conflicts with the root level definitions: immediately exit with error
_ = removeVersion(version, for: dependency)
newSet = rejectedCopy(rejectionError: cachedConflict.error)
return newSet
}
// Remove all versions except the selected version if needed. If the number of versions is already 1, we don't need a copy.
let count = versionSet.count
if count > 1 {
let copy = self.copy
let valid1 = copy.removeAllVersionsExcept(version, for: dependency)
assert(valid1, "Expected set to contain the specified version")
let valid2 = removeVersion(version, for: dependency)
assert(valid2, "Expected set to contain the specified version")
newSet = copy
} else {
newSet = self
}
// Check for cached conflicts
if let cachedConflict = optionalCachedConflict, let conflictingDependencies = cachedConflict.conflictingDependencies {
// Remove all conflicting dependencies from this set
for concreteDependency in conflictingDependencies {
if newSet.removeVersion(concreteDependency.concreteVersion, for: concreteDependency.dependency) == false {
// Rejected, no versions left
newSet.rejectionError = cachedConflict.error
break
}
}
}
if !newSet.isRejected {
if try newSet.expand(parent: ConcreteVersionedDependency(dependency: dependency, concreteVersion: version),
with: try resolverContext.findDependencies(for: dependency, version: version),
forceUpdatable: isUpdatableDependency(dependency)) {
newSet.unresolvedDependencies.remove(dependency)
}
}
return newSet
}
}
return nil
}
/**
Validates this set for cyclic dependencies.
Returns true if valid, false otherwise (which means a cycle has been encountered).
The rejectionError for the set will be set in case a cycle was encountered.
*/
public func validateForCyclicDepencies(rootDependencies: [Dependency]) throws -> Bool {
var graph = [Dependency: Set<Dependency>]()
let foundCycle = try hasCycle(for: rootDependencies, parent: nil, stack: &graph)
if foundCycle {
switch Algorithms.topologicalSort(graph) {
case let .failure(error):
switch error {
case let .cycle(nodes):
rejectionError = CarthageError.dependencyCycle(nodes)
case let .missing(node):
rejectionError = CarthageError.unknownDependencies([node.name])
}
default:
assertionFailure("Expected topological sort to not succeed, because a cycle is present")
return true
}
}
return !foundCycle
}
/**
Eliminates dependencies with duplicate names, keeping the most relevant one.
The carthage model does not allow two dependencies with the same name, this is because forks should be allowed to override their upstreams.
*/
public func eliminateSameNamedDependencies(rootEntries: [DependencyEntry]) throws {
var names = Set<String>()
var duplicatedDependencyNames = Set<String>()
var versionSpecifiers = [Dependency: VersionSpecifier]()
for entry in rootEntries {
versionSpecifiers[entry.key] = entry.value
}
// Check for dependencies with the same name and store them in the duplicatedDependencyNames set
for (dependency, _) in contents {
let result = names.insert(dependency.name)
if !result.inserted {
duplicatedDependencyNames.insert(dependency.name)
}
}
// For the duplicatedDependencyNames: ensure only the dependency with the highest precedence versionSpecifier remains
for name in duplicatedDependencyNames {
let sameNamedDependencies = contents.compactMap { entry -> (dependency: Dependency, versionSpecifier: VersionSpecifier?)? in
let dependency = entry.key
if dependency.name == name {
return (dependency, versionSpecifiers[dependency])
} else {
return nil
}
}.sorted { entry1, entry2 -> Bool in
let precedence1 = (entry1.versionSpecifier?.precedence ?? 0)
let precedence2 = (entry2.versionSpecifier?.precedence ?? 0)
return precedence1 > precedence2
}
if sameNamedDependencies.count > 1 && (sameNamedDependencies[0].versionSpecifier == nil || sameNamedDependencies[1].versionSpecifier != nil) {
// Cannot determine precedence: report an error.
// Requires a specific versionSpecifier for exactly one of these dependencies in the root Cartfile.
let error = CarthageError.incompatibleDependencies(sameNamedDependencies.map { $0.dependency })
throw error
}
for i in 1..<sameNamedDependencies.count {
let dependency = sameNamedDependencies[i].dependency
contents[dependency] = nil
}
}
}
// MARK: - Private methods
/**
Returns a rejected copy of this set, which is basically an empty set with the rejectionError set.
*/
private func rejectedCopy(rejectionError: CarthageError) -> DependencySet {
let dependencySet = DependencySet(unresolvedDependencies: SortedSet<Dependency>(),
updatableDependencyNames: Set<String>(),
contents: [Dependency: ConcreteVersionSet](),
resolverContext: self.resolverContext)
dependencySet.rejectionError = rejectionError
return dependencySet
}
private func removeVersion(_ version: ConcreteVersion, for dependency: Dependency) -> Bool {
if let versionSet = contents[dependency] {
versionSet.remove(version)
return !versionSet.isEmpty
}
return false
}
private func setVersions(_ versions: ConcreteVersionSet, for dependency: Dependency) -> Bool {
contents[dependency] = versions
return !versions.isEmpty
}
private func removeAllVersionsExcept(_ version: ConcreteVersion, for dependency: Dependency) -> Bool {
if let versionSet = versions(for: dependency) {
versionSet.removeAll(except: version)
return !versionSet.isEmpty
}
return false
}
private func constrainVersions(for dependency: Dependency, with versionSpecifier: VersionSpecifier) throws -> Bool {
if let versionSet = versions(for: dependency) {
let effectiveVersionSpecifier = try versionSpecifier.effectiveSpecifier(for: dependency, retriever: self.resolverContext.projectDependencyRetriever)
versionSet.retainVersions(compatibleWith: effectiveVersionSpecifier)
return !versionSet.isEmpty
}
return false
}
private func addUpdatableDependency(_ dependency: Dependency) {
if !updatableDependencyNames.isEmpty {
updatableDependencyNames.insert(dependency.name)
}
}
/**
Expands this set by iterating over the transitive dependencies and processing them.
*/
@discardableResult
private func expand(parent: ConcreteVersionedDependency?, with transitiveDependencies: [DependencyEntry], forceUpdatable: Bool = false) throws -> Bool {
for (transitiveDependency, versionSpecifier) in transitiveDependencies {
let isUpdatable = forceUpdatable || isUpdatableDependency(transitiveDependency)
if forceUpdatable {
addUpdatableDependency(transitiveDependency)
}
guard try process(transitiveDependency: transitiveDependency,
definedBy: ConcreteVersionSetDefinition(definingDependency: parent, versionSpecifier: versionSpecifier),
isUpdatable: isUpdatable) == true else {
// Errors were encountered, fail fast
return false
}
}
return true
}
/**
Rejects this set with the specified error.
*/
private func reject(dependency: Dependency, error: CarthageError,
definingDependency: ConcreteVersionedDependency? = nil,
conflictingWith conflictingDependency: ConcreteVersionedDependency? = nil) {
rejectionError = error
if let nonNilDefiningDependency = definingDependency {
resolverContext.addCachedConflict(for: nonNilDefiningDependency, conflictingWith: conflictingDependency, error: error)
}
resolverContext.addProblematicDependency(dependency)
}
/**
Processes a transitive dependency.
*/
private func process(transitiveDependency: Dependency, definedBy definition: ConcreteVersionSetDefinition, isUpdatable: Bool) throws -> Bool {
let versionSpecifier = definition.versionSpecifier
let definingDependency = definition.definingDependency
let existingVersionSet = versions(for: transitiveDependency)
if existingVersionSet == nil || (existingVersionSet!.isPinned && isUpdatable) {
let validVersions = try resolverContext.findAllVersions(for: transitiveDependency, compatibleWith: versionSpecifier, isUpdatable: isUpdatable)
if let existingVersionSpecifier = existingVersionSet?.effectiveVersionSpecifier {
// We need to take the existing version specifier into account, constrain the version with this specifier
validVersions.retainVersions(compatibleWith: existingVersionSpecifier)
}
if !setVersions(validVersions, for: transitiveDependency) {
let error: CarthageError
if isUpdatable {
error = CarthageError.requiredVersionNotFound(transitiveDependency, versionSpecifier)
} else {
error = CarthageError.unsatisfiableDependencyList(Array(updatableDependencyNames))
}
reject(dependency: transitiveDependency, error: error, definingDependency: definingDependency)
return false
}
unresolvedDependencies.insert(transitiveDependency)
existingVersionSet?.isPinned = false
validVersions.addDefinition(definition)
} else if let versionSet = existingVersionSet {
defer {
versionSet.addDefinition(definition)
}
if try !constrainVersions(for: transitiveDependency, with: versionSpecifier) {
assert(!versionSet.definitions.isEmpty, "Expected definitions to not be empty")
if let incompatibleDefinition = versionSet.conflictingDefinition(for: versionSpecifier) {
let existingRequirement: CarthageError.VersionRequirement = (specifier: incompatibleDefinition.versionSpecifier,
fromDependency: incompatibleDefinition.definingDependency?.dependency)
let newRequirement: CarthageError.VersionRequirement = (specifier: versionSpecifier,
fromDependency: definition.definingDependency?.dependency)
let error = CarthageError.incompatibleRequirements(transitiveDependency, existingRequirement, newRequirement)
reject(dependency: transitiveDependency, error: error, definingDependency: definition.definingDependency,
conflictingWith: incompatibleDefinition.definingDependency)
} else {
reject(dependency: transitiveDependency, error: CarthageError.unsatisfiableDependencyList(Array(updatableDependencyNames)))
}
return false
}
}
return true
}
/**
Check for a completely resolved set, whether there are no cyclic dependencies.
*/
private func hasCycle(for dependencies: [Dependency], parent: Dependency?, stack: inout [Dependency: Set<Dependency>]) throws -> Bool {
if let definedParent = parent {
if stack[definedParent] == nil {
stack[definedParent] = Set(dependencies)
} else {
return true
}
}
for dependency in dependencies {
if let versionSet = contents[dependency] {
// Only check the most appropriate version
if let version = versionSet.first {
let transitiveDependencies = try resolverContext.findDependencies(for: dependency, version: version).map { $0.0 }
if try hasCycle(for: transitiveDependencies, parent: dependency, stack: &stack) {
return true
}
}
}
}
if let definedParent = parent {
stack[definedParent] = nil
}
return false
}
}
extension VersionSpecifier {
/**
Precedence for sorting VersionSpecifiers with decreasing specifics (The more specific the specifier the higher the precedence).
*/
fileprivate var precedence: Int {
switch self {
case .empty:
return 6
case .gitReference:
return 5
case .exactly:
return 4
case .compatibleWith:
return 3
case .atLeast:
return 2
case .any:
return 1
}
}
}