Skip to content

Commit cde3573

Browse files
authored
rename resolveToResolvedVersion to resolveBasedOnResolvedVersionsFile (#3719)
motivation: code should be easier to reason about changes: * rename resolveToResolvedVersion to resolveBasedOnResolvedVersionsFile * cleanup redunant private function abstraction where possible to reduce complexity
1 parent 713d280 commit cde3573

File tree

3 files changed

+50
-36
lines changed

3 files changed

+50
-36
lines changed

Sources/Commands/SwiftTool.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ public class SwiftTool {
572572
let root = try getWorkspaceRoot()
573573

574574
if options.forceResolvedVersions {
575-
try workspace.resolveToResolvedVersion(root: root, diagnostics: diagnostics)
575+
try workspace.resolveBasedOnResolvedVersionsFile(root: root, diagnostics: diagnostics)
576576
} else {
577577
try workspace.resolve(root: root, diagnostics: diagnostics)
578578
}

Sources/SPMTestSupport/MockWorkspace.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ public final class MockWorkspace {
392392
root: root,
393393
dependencyManifests: dependencyManifests,
394394
pinsStore: pinsStore,
395-
extraConstraints: []
395+
constraints: []
396396
)
397397

398398
check(ResolutionPrecomputationResult(result: result, diagnostics: diagnostics))

Sources/Workspace/Workspace.swift

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -607,10 +607,15 @@ extension Workspace {
607607
let constraint = PackageContainerConstraint(package: dependency.packageRef, requirement: requirement, products: .nothing)
608608

609609
// Run the resolution.
610-
try self._resolve(root: root, forceResolution: false, extraConstraints: [constraint], diagnostics: diagnostics)
610+
try self.resolve(
611+
root: root,
612+
forceResolution: false,
613+
constraints: [constraint],
614+
diagnostics: diagnostics
615+
)
611616
}
612617

613-
/// Cleans the build artefacts from workspace data.
618+
/// Cleans the build artifacts from workspace data.
614619
///
615620
/// - Parameters:
616621
/// - diagnostics: The diagnostics engine that reports errors, warnings
@@ -809,16 +814,17 @@ extension Workspace {
809814
// Perform dependency resolution, if required.
810815
let manifests: DependencyManifests
811816
if forceResolvedVersions {
812-
manifests = try self._resolveToResolvedVersion(
817+
manifests = try self.resolveBasedOnResolvedVersionsFile(
813818
root: root,
814819
explicitProduct: explicitProduct,
815820
diagnostics: diagnostics
816821
)
817822
} else {
818-
manifests = try self._resolve(
823+
manifests = try self.resolve(
819824
root: root,
820825
explicitProduct: explicitProduct,
821826
forceResolution: false,
827+
constraints: [],
822828
diagnostics: diagnostics
823829
)
824830
}
@@ -868,7 +874,12 @@ extension Workspace {
868874
forceResolution: Bool = false,
869875
diagnostics: DiagnosticsEngine
870876
) throws {
871-
try self._resolve(root: root, forceResolution: forceResolution, diagnostics: diagnostics)
877+
try self.resolve(
878+
root: root,
879+
forceResolution: forceResolution,
880+
constraints: [],
881+
diagnostics: diagnostics
882+
)
872883
}
873884

874885
/// Loads and returns manifests at the given paths.
@@ -1966,29 +1977,31 @@ extension Workspace {
19661977

19671978
extension Workspace {
19681979

1980+
@available(*, deprecated, message: "renamed to resolveBasedOnResolvedVersionsFile")
1981+
public func resolveToResolvedVersion(root: PackageGraphRootInput,diagnostics: DiagnosticsEngine) throws {
1982+
try self.resolveBasedOnResolvedVersionsFile(root: root, diagnostics: diagnostics)
1983+
}
1984+
19691985
/// Resolves the dependencies according to the entries present in the Package.resolved file.
19701986
///
19711987
/// This method bypasses the dependency resolution and resolves dependencies
19721988
/// according to the information in the resolved file.
1973-
public func resolveToResolvedVersion(
1974-
root: PackageGraphRootInput,
1975-
diagnostics: DiagnosticsEngine
1976-
) throws {
1977-
try self._resolveToResolvedVersion(root: root, diagnostics: diagnostics)
1989+
public func resolveBasedOnResolvedVersionsFile(root: PackageGraphRootInput, diagnostics: DiagnosticsEngine) throws {
1990+
try self.resolveBasedOnResolvedVersionsFile(root: root, explicitProduct: .none, diagnostics: diagnostics)
19781991
}
19791992

19801993
/// Resolves the dependencies according to the entries present in the Package.resolved file.
19811994
///
19821995
/// This method bypasses the dependency resolution and resolves dependencies
19831996
/// according to the information in the resolved file.
19841997
@discardableResult
1985-
fileprivate func _resolveToResolvedVersion(
1998+
fileprivate func resolveBasedOnResolvedVersionsFile(
19861999
root: PackageGraphRootInput,
1987-
explicitProduct: String? = nil,
2000+
explicitProduct: String?,
19882001
diagnostics: DiagnosticsEngine
19892002
) throws -> DependencyManifests {
19902003
// Ensure the cache path exists.
1991-
createCacheDirectories(with: diagnostics)
2004+
self.createCacheDirectories(with: diagnostics)
19922005

19932006
// FIXME: this should not block
19942007
let rootManifests = try temp_await { self.loadRootManifests(packages: root.packages, diagnostics: diagnostics, completion: $0) }
@@ -2032,10 +2045,11 @@ extension Workspace {
20322045

20332046
let currentManifests = try self.loadDependencyManifests(root: graphRoot, diagnostics: diagnostics, automaticallyAddManagedDependencies: true)
20342047

2035-
let precomputationResult = try precomputeResolution(
2048+
let precomputationResult = try self.precomputeResolution(
20362049
root: graphRoot,
20372050
dependencyManifests: currentManifests,
2038-
pinsStore: pinsStore
2051+
pinsStore: pinsStore,
2052+
constraints: []
20392053
)
20402054

20412055
if case let .required(reason) = precomputationResult {
@@ -2060,18 +2074,18 @@ extension Workspace {
20602074
/// imposed outside of manifest and pins file. E.g., when using a command
20612075
/// like `$ swift package resolve foo --version 1.0.0`.
20622076
@discardableResult
2063-
fileprivate func _resolve(
2077+
fileprivate func resolve(
20642078
root: PackageGraphRootInput,
20652079
explicitProduct: String? = nil,
20662080
forceResolution: Bool,
2067-
extraConstraints: [PackageContainerConstraint] = [],
2081+
constraints: [PackageContainerConstraint],
20682082
diagnostics: DiagnosticsEngine,
20692083
retryOnPackagePathMismatch: Bool = true,
20702084
resetPinsStoreOnFailure: Bool = true
20712085
) throws -> DependencyManifests {
20722086

20732087
// Ensure the cache path exists and validate that edited dependencies.
2074-
createCacheDirectories(with: diagnostics)
2088+
self.createCacheDirectories(with: diagnostics)
20752089

20762090
// FIXME: this should not block
20772091
// Load the root manifests and currently checked out manifests.
@@ -2098,14 +2112,14 @@ extension Workspace {
20982112
// there are extra constraints.
20992113
if !missingPackageURLs.isEmpty {
21002114
delegate?.willResolveDependencies(reason: .newPackages(packages: Array(missingPackageURLs)))
2101-
} else if !extraConstraints.isEmpty || forceResolution {
2115+
} else if !constraints.isEmpty || forceResolution {
21022116
delegate?.willResolveDependencies(reason: .forced)
21032117
} else {
2104-
let result = try precomputeResolution(
2118+
let result = try self.precomputeResolution(
21052119
root: graphRoot,
21062120
dependencyManifests: currentManifests,
21072121
pinsStore: pinsStore,
2108-
extraConstraints: extraConstraints
2122+
constraints: constraints
21092123
)
21102124

21112125
switch result {
@@ -2122,17 +2136,17 @@ extension Workspace {
21222136
}
21232137

21242138
// Create the constraints.
2125-
var constraints = [PackageContainerConstraint]()
2126-
constraints += currentManifests.editedPackagesConstraints()
2127-
constraints += try graphRoot.constraints() + extraConstraints
2139+
var computedConstraints = [PackageContainerConstraint]()
2140+
computedConstraints += currentManifests.editedPackagesConstraints()
2141+
computedConstraints += try graphRoot.constraints() + constraints
21282142

21292143
// Perform dependency resolution.
21302144
let resolver = try createResolver(pinsMap: pinsStore.pinsMap)
21312145
self.activeResolver = resolver
21322146

2133-
let result = resolveDependencies(
2147+
let result = self.resolveDependencies(
21342148
resolver: resolver,
2135-
constraints: constraints,
2149+
constraints: computedConstraints,
21362150
diagnostics: diagnostics)
21372151

21382152
// Reset the active resolver.
@@ -2163,11 +2177,11 @@ extension Workspace {
21632177
if retryOnPackagePathMismatch {
21642178
// Retry resolution which will most likely resolve correctly now since
21652179
// we have the manifest files of all the dependencies.
2166-
return try self._resolve(
2180+
return try self.resolve(
21672181
root: root,
21682182
explicitProduct: explicitProduct,
21692183
forceResolution: forceResolution,
2170-
extraConstraints: extraConstraints,
2184+
constraints: constraints,
21712185
diagnostics: diagnostics,
21722186
retryOnPackagePathMismatch: false,
21732187
resetPinsStoreOnFailure: resetPinsStoreOnFailure
@@ -2180,11 +2194,11 @@ extension Workspace {
21802194
pinsStore.unpinAll()
21812195
try pinsStore.saveState()
21822196
// try again with pins reset
2183-
return try self._resolve(
2197+
return try self.resolve(
21842198
root: root,
21852199
explicitProduct: explicitProduct,
21862200
forceResolution: forceResolution,
2187-
extraConstraints: extraConstraints,
2201+
constraints: constraints,
21882202
diagnostics: diagnostics,
21892203
retryOnPackagePathMismatch: false,
21902204
resetPinsStoreOnFailure: false
@@ -2228,18 +2242,18 @@ extension Workspace {
22282242
root: PackageGraphRoot,
22292243
dependencyManifests: DependencyManifests,
22302244
pinsStore: PinsStore,
2231-
extraConstraints: [PackageContainerConstraint] = []
2245+
constraints: [PackageContainerConstraint]
22322246
) throws -> ResolutionPrecomputationResult {
2233-
let constraints =
2247+
let computedConstraints =
22342248
try root.constraints() +
22352249
// Include constraints from the manifests in the graph root.
22362250
root.manifests.values.flatMap{ try $0.dependencyConstraints(productFilter: .everything) } +
22372251
dependencyManifests.dependencyConstraints() +
2238-
extraConstraints
2252+
constraints
22392253

22402254
let precomputationProvider = ResolverPrecomputationProvider(root: root, dependencyManifests: dependencyManifests)
22412255
let resolver = PubgrubDependencyResolver(provider: precomputationProvider, pinsMap: pinsStore.pinsMap)
2242-
let result = resolver.solve(constraints: constraints)
2256+
let result = resolver.solve(constraints: computedConstraints)
22432257

22442258
switch result {
22452259
case .success:

0 commit comments

Comments
 (0)