Skip to content

Commit 017149f

Browse files
authored
Merge pull request #1016 from aciidb0mb3r/reloadable-pinsstore
[Workspace] Use reloadable result on pinsstore
2 parents 203ba32 + d54baa9 commit 017149f

File tree

3 files changed

+43
-31
lines changed

3 files changed

+43
-31
lines changed

Sources/Commands/SwiftPackageTool.swift

+4-3
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ public class SwiftPackageTool: SwiftTool<PackageToolOptions> {
6969

7070
case .update:
7171
let workspace = try getActiveWorkspace()
72+
let pinsStore = try workspace.pinsStore.dematerialize()
7273
// We repin either on explicit repin option or if autopin is enabled.
73-
let repin = options.repin || workspace.pinsStore.autoPin
74+
let repin = options.repin || pinsStore.autoPin
7475
try workspace.updateDependencies(repin: repin)
7576

7677
case .fetch:
@@ -181,7 +182,7 @@ public class SwiftPackageTool: SwiftTool<PackageToolOptions> {
181182
// Toggle enable auto pinning if requested.
182183
if let enableAutoPin = options.pinOptions.enableAutoPin {
183184
let workspace = try getActiveWorkspace()
184-
return try workspace.pinsStore.setAutoPin(on: enableAutoPin)
185+
return try workspace.pinsStore.dematerialize().setAutoPin(on: enableAutoPin)
185186
}
186187

187188
// Get the pin options.
@@ -226,7 +227,7 @@ public class SwiftPackageTool: SwiftTool<PackageToolOptions> {
226227
fatalError("Expected package name from parser")
227228
}
228229
let workspace = try getActiveWorkspace()
229-
try workspace.pinsStore.unpin(package: packageName)
230+
try workspace.pinsStore.dematerialize().unpin(package: packageName)
230231
}
231232
}
232233

Sources/Workspace/Workspace.swift

+18-7
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public class Workspace {
211211
private var fileSystem: FileSystem
212212

213213
/// The Pins store. The pins file will be created when first pin is added to pins store.
214-
public let pinsStore: PinsStore
214+
public let pinsStore: ReloadableResult<PinsStore, AnyError>
215215

216216
/// The manifest loader to use.
217217
let manifestLoader: ManifestLoaderProtocol
@@ -277,7 +277,9 @@ public class Workspace {
277277
repositoryManager: repositoryManager, manifestLoader: manifestLoader, toolsVersionLoader: toolsVersionLoader)
278278
self.fileSystem = fileSystem
279279

280-
self.pinsStore = try PinsStore(pinsFile: pinsFile, fileSystem: self.fileSystem)
280+
self.pinsStore = ReloadableResult {
281+
try PinsStore(pinsFile: pinsFile, fileSystem: fileSystem)
282+
}
281283
self.managedDependencies = ReloadableResult{
282284
try ManagedDependencies(dataPath: dataPath, fileSystem: fileSystem)
283285
}
@@ -531,6 +533,8 @@ public class Workspace {
531533
requirement = currentState.requirement()
532534
}
533535

536+
let pinsStore = try self.pinsStore.dematerialize()
537+
534538
// Compute constraints with the new pin and try to resolve
535539
// dependencies. We only commit the pin if the dependencies can be
536540
// resolved with new constraints.
@@ -573,6 +577,7 @@ public class Workspace {
573577
/// - reset: Remove all current pins before pinning dependencies.
574578
public func pinAll(reason: String? = nil, reset: Bool = false) throws {
575579
if reset {
580+
let pinsStore = try self.pinsStore.dematerialize()
576581
try pinsStore.unpinAll()
577582
}
578583
// Load the dependencies.
@@ -602,6 +607,7 @@ public class Workspace {
602607
return delegate.warning(message: "not pinning \(package). It is being edited but is no longer needed.")
603608
}
604609
}
610+
let pinsStore = try self.pinsStore.dematerialize()
605611
// Commit the pin.
606612
try pinsStore.pin(
607613
package: package,
@@ -759,6 +765,7 @@ public class Workspace {
759765
/// This methods pins all packages if auto pinning is on.
760766
/// Otherwise, only currently pinned packages are repinned.
761767
private func repinPackages() throws {
768+
let pinsStore = try self.pinsStore.dematerialize()
762769
// If autopin is on, pin everything and return.
763770
if pinsStore.autoPin {
764771
return try pinAll(reset: true)
@@ -817,6 +824,7 @@ public class Workspace {
817824
resolvedDependencies: [(RepositorySpecifier, BoundVersion)],
818825
updateBranches: Bool
819826
) throws -> [RepositorySpecifier: PackageStateChange] {
827+
let pinsStore = try self.pinsStore.dematerialize()
820828
var packageStateChanges = [RepositorySpecifier: PackageStateChange]()
821829
let managedDependencies = try self.managedDependencies.dematerialize()
822830
// Set the states from resolved dependencies results.
@@ -887,9 +895,10 @@ public class Workspace {
887895
/// - includePins: If the constraints from pins should be included.
888896
/// - Returns: Array of constraints.
889897
private func computeRootPackagesConstraints(_ rootManifests: [Manifest], includePins: Bool) -> [RepositoryPackageConstraint] {
898+
// FIXME: Need to get rid of bang here.
890899
return rootManifests.flatMap{
891900
$0.package.dependencyConstraints()
892-
} + (includePins ? pinsStore.createConstraints() : [])
901+
} + (includePins ? try! pinsStore.dematerialize().createConstraints() : [])
893902
}
894903

895904
/// Runs the dependency resolver based on constraints provided and returns the results.
@@ -1032,7 +1041,9 @@ public class Workspace {
10321041
// delegate of what is happening.
10331042
delegate.fetchingMissingRepositories(missingURLs)
10341043

1044+
let pinsStore = try! self.pinsStore.dematerialize()
10351045
// Add constraints from the root packages and the current manifests.
1046+
// FIXME: Fix bang here.
10361047
let constraints = computeRootPackagesConstraints(currentManifests.roots, includePins: true)
10371048
+ currentManifests.createConstraints(pinsStore: pinsStore)
10381049

@@ -1171,25 +1182,25 @@ extension Collection {
11711182
///
11721183
/// It is useful for objects that holds a state on disk and needs to be
11731184
/// reloaded frequently.
1174-
final class ReloadableResult<Value, ErrorType: Swift.Error> {
1185+
public final class ReloadableResult<Value, ErrorType: Swift.Error> {
11751186

11761187
/// The constructor closure for the value.
11771188
private let construct: () throws -> Value
11781189

11791190
/// Create a reloadable result.
1180-
init(_ construct: @escaping () throws -> Value) {
1191+
public init(_ construct: @escaping () throws -> Value) {
11811192
self.construct = construct
11821193
}
11831194

11841195
/// Load and return the result.
1185-
func result() -> Result<Value, ErrorType> {
1196+
public func result() -> Result<Value, ErrorType> {
11861197
return try! Result {
11871198
try self.construct()
11881199
}
11891200
}
11901201

11911202
/// Load and return the value.
1192-
func dematerialize() throws -> Value {
1203+
public func dematerialize() throws -> Value {
11931204
return try result().dematerialize()
11941205
}
11951206
}

Tests/WorkspaceTests/WorkspaceTests.swift

+21-21
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ final class WorkspaceTests: XCTestCase {
363363
let workspace = try createWorkspace()
364364

365365
// Turn off auto pinning.
366-
try workspace.pinsStore.setAutoPin(on: false)
366+
try workspace.pinsStore.dematerialize().setAutoPin(on: false)
367367
// Ensure delegates haven't been called yet.
368368
XCTAssert(delegate.fetched.isEmpty)
369369
XCTAssert(delegate.cloned.isEmpty)
@@ -714,17 +714,17 @@ final class WorkspaceTests: XCTestCase {
714714
XCTAssert(graph.lookup("A").version == "1.0.1")
715715
XCTAssert(graph.lookup("AA").version == v1)
716716
// We should have pin for AA automatically.
717-
XCTAssertNotNil(workspace.pinsStore.pinsMap["A"])
718-
XCTAssertNotNil(workspace.pinsStore.pinsMap["AA"])
717+
XCTAssertNotNil(try workspace.pinsStore.dematerialize().pinsMap["A"])
718+
XCTAssertNotNil(try workspace.pinsStore.dematerialize().pinsMap["AA"])
719719
}
720720

721721
// Unpin all of the dependencies.
722722
do {
723723
let workspace = newWorkspace()
724-
try workspace.pinsStore.unpinAll()
724+
try workspace.pinsStore.dematerialize().unpinAll()
725725
// Reset so we have a clean workspace.
726726
try workspace.reset()
727-
try workspace.pinsStore.setAutoPin(on: false)
727+
try workspace.pinsStore.dematerialize().setAutoPin(on: false)
728728
}
729729

730730
// Pin at A at v1.
@@ -749,9 +749,9 @@ final class WorkspaceTests: XCTestCase {
749749
XCTAssertTrue(graph.errors.isEmpty)
750750
XCTAssert(graph.lookup("A").version == "1.0.1")
751751
XCTAssert(graph.lookup("AA").version == v1)
752-
XCTAssertNotNil(workspace.pinsStore.pinsMap["A"])
752+
XCTAssertNotNil(try workspace.pinsStore.dematerialize().pinsMap["A"])
753753
// We should not have pinned AA.
754-
XCTAssertNil(workspace.pinsStore.pinsMap["AA"])
754+
XCTAssertNil(try workspace.pinsStore.dematerialize().pinsMap["AA"])
755755
}
756756
}
757757

@@ -791,15 +791,15 @@ final class WorkspaceTests: XCTestCase {
791791
}
792792
// Try unpinning something which is not pinned.
793793
XCTAssertThrows(PinOperationError.notPinned) {
794-
try workspace.pinsStore.unpin(package: "A")
794+
try workspace.pinsStore.dematerialize().unpin(package: "A")
795795
}
796796
try workspace.pin(dependency: dep, packageName: "A", version: v1)
797797
}
798798

799799
// Turn off autopin.
800800
do {
801801
let workspace = newWorkspace()
802-
try workspace.pinsStore.setAutoPin(on: false)
802+
try workspace.pinsStore.dematerialize().setAutoPin(on: false)
803803
}
804804

805805
// Package graph should load 1.0.1.
@@ -824,7 +824,7 @@ final class WorkspaceTests: XCTestCase {
824824
// Unpin package.
825825
do {
826826
let workspace = newWorkspace()
827-
try workspace.pinsStore.unpin(package: "A")
827+
try workspace.pinsStore.dematerialize().unpin(package: "A")
828828
try workspace.reset()
829829
}
830830

@@ -928,7 +928,7 @@ final class WorkspaceTests: XCTestCase {
928928
// Unpin all of the dependencies.
929929
do {
930930
let workspace = newWorkspace()
931-
try workspace.pinsStore.unpinAll()
931+
try workspace.pinsStore.dematerialize().unpinAll()
932932
// Reset so we have a clean workspace.
933933
try workspace.reset()
934934
}
@@ -1062,7 +1062,7 @@ final class WorkspaceTests: XCTestCase {
10621062
// But we should still be able to repin at v1.
10631063
try pin(at: v1)
10641064
// And also after unpinning.
1065-
try workspace.pinsStore.unpinAll()
1065+
try workspace.pinsStore.dematerialize().unpinAll()
10661066
try pin(at: v1)
10671067

10681068
}
@@ -1134,7 +1134,7 @@ final class WorkspaceTests: XCTestCase {
11341134

11351135
do {
11361136
let workspace = newWorkspace()
1137-
try workspace.pinsStore.setAutoPin(on: false)
1137+
try workspace.pinsStore.dematerialize().setAutoPin(on: false)
11381138
_ = workspace.loadPackageGraph()
11391139
let manifests = try workspace.loadDependencyManifests()
11401140
guard let (_, dep) = manifests.lookup(package: "B") else {
@@ -1266,10 +1266,10 @@ final class WorkspaceTests: XCTestCase {
12661266
// Check pins.
12671267
do {
12681268
let workspace = getWorkspace()
1269-
let dep1Pin = workspace.pinsStore.pinsMap["dep"]!
1269+
let dep1Pin = try workspace.pinsStore.dematerialize().pinsMap["dep"]!
12701270
XCTAssertEqual(dep1Pin.state, CheckoutState(revision: dep1Revision, branch: "develop"))
12711271

1272-
let dep2Pin = workspace.pinsStore.pinsMap["dep2"]!
1272+
let dep2Pin = try workspace.pinsStore.dematerialize().pinsMap["dep2"]!
12731273
XCTAssertEqual(dep2Pin.state, CheckoutState(revision: dep2Revision))
12741274
}
12751275

@@ -1384,7 +1384,7 @@ final class WorkspaceTests: XCTestCase {
13841384
// Set auto pinning off.
13851385
do {
13861386
let workspace = try createWorkspace()
1387-
try workspace.pinsStore.setAutoPin(on: false)
1387+
try workspace.pinsStore.dematerialize().setAutoPin(on: false)
13881388
}
13891389

13901390
// Throw if we have not registered any packages but want to load things.
@@ -1488,7 +1488,7 @@ final class WorkspaceTests: XCTestCase {
14881488

14891489
// We should retain the original pin for a package which is in edit mode.
14901490
try workspace.pinAll(reset: true)
1491-
XCTAssertEqual(workspace.pinsStore.pinsMap["A"]?.state.version, v1)
1491+
XCTAssertEqual(try workspace.pinsStore.dematerialize().pinsMap["A"]?.state.version, v1)
14921492

14931493
// Remove edited checkout.
14941494
try removeFileTree(workspace.editablesPath)
@@ -1527,8 +1527,8 @@ final class WorkspaceTests: XCTestCase {
15271527
try workspace.edit(dependency: bDependency, packageName: "B", revision: bDependency.checkoutState!.revision)
15281528

15291529
XCTAssertEqual(manifests.lookup(package: "A")!.dependency.checkoutState?.version, v1)
1530-
XCTAssertEqual(workspace.pinsStore.pinsMap["A"]?.state.version, v1)
1531-
XCTAssertEqual(workspace.pinsStore.pinsMap["B"]?.state.version, v1)
1530+
XCTAssertEqual(try workspace.pinsStore.dematerialize().pinsMap["A"]?.state.version, v1)
1531+
XCTAssertEqual(try workspace.pinsStore.dematerialize().pinsMap["B"]?.state.version, v1)
15321532

15331533
// Create update.
15341534
let repoPath = AbsolutePath(manifestGraph.repo("A").url)
@@ -1546,9 +1546,9 @@ final class WorkspaceTests: XCTestCase {
15461546
let manifests = try workspace.loadDependencyManifests()
15471547

15481548
XCTAssertEqual(manifests.lookup(package: "A")!.dependency.checkoutState?.version, "1.0.1")
1549-
XCTAssertEqual(workspace.pinsStore.pinsMap["A"]?.state.version, "1.0.1")
1549+
XCTAssertEqual(try workspace.pinsStore.dematerialize().pinsMap["A"]?.state.version, "1.0.1")
15501550
XCTAssertTrue(manifests.lookup(package: "B")!.dependency.state == .edited)
1551-
XCTAssertEqual(workspace.pinsStore.pinsMap["B"]?.state.version, v1)
1551+
XCTAssertEqual(try workspace.pinsStore.dematerialize().pinsMap["B"]?.state.version, v1)
15521552
}
15531553
}
15541554
}

0 commit comments

Comments
 (0)