Skip to content

Commit 7dce4bf

Browse files
committed
[PinsStore] Add support for loading PinsStore schema 1
- <rdar://problem/34186002> [SR-5843]: SwiftPM needs to be resilient to schema changes
1 parent 9c1532e commit 7dce4bf

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

Sources/Workspace/PinsStore.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public final class PinsStore {
7676
self.persistence = SimplePersistence(
7777
fileSystem: fileSystem,
7878
schemaVersion: PinsStore.schemaVersion,
79+
supportedSchemaVersions: [1],
7980
statePath: pinsFile,
8081
prettyPrint: true)
8182
pinsMap = [:]
@@ -153,6 +154,28 @@ extension PinsStore: SimplePersistanceProtocol {
153154
self.pinsMap = try Dictionary(items: json.get("pins").map({ ($0.packageRef.identity, $0) }))
154155
}
155156

157+
public func restore(from json: JSON, supportedSchemaVersion: Int) throws {
158+
switch supportedSchemaVersion {
159+
case 1:
160+
// We did not have concept of package identity in previous schema but we can
161+
// compute it using the URL.
162+
//
163+
// FIXME: This logic will need to be updated when we require identity and package
164+
// name to be same. <rdar://problem/33693433>
165+
let pinsArray: [JSON] = try json.get("pins")
166+
let pins: [Pin] = try pinsArray.map({ pinData in
167+
let url: String = try pinData.get("repositoryURL")
168+
let ref = PackageReference(
169+
identity: PackageReference.computeIdentity(packageURL: url), path: url)
170+
return try Pin(packageRef: ref, state: pinData.get("state"))
171+
})
172+
self.pinsMap = Dictionary(items: pins.map({ ($0.packageRef.identity, $0) }))
173+
174+
default:
175+
fatalError()
176+
}
177+
}
178+
156179
/// Saves the current state of pins.
157180
public func toJSON() -> JSON {
158181
return JSON([

Tests/WorkspaceTests/PinsStoreTests.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,46 @@ final class PinsStoreTests: XCTestCase {
9696
}
9797
}
9898

99+
func testLoadingSchema1() throws {
100+
var fs = InMemoryFileSystem()
101+
let pinsFile = AbsolutePath("/pinsfile.txt")
102+
103+
try fs.writeFileContents(pinsFile) {
104+
$0 <<< """
105+
{
106+
"object": {
107+
"pins": [
108+
{
109+
"package": "Clang_C",
110+
"repositoryURL": "https://github.com/something/Clang_C.git",
111+
"state": {
112+
"branch": null,
113+
"revision": "90a9574276f0fd17f02f58979423c3fd4d73b59e",
114+
"version": "1.0.2"
115+
}
116+
},
117+
{
118+
"package": "Commandant",
119+
"repositoryURL": "https://github.com/something/Commandant.git",
120+
"state": {
121+
"branch": null,
122+
"revision": "c281992c31c3f41c48b5036c5a38185eaec32626",
123+
"version": "0.12.0"
124+
}
125+
}
126+
]
127+
},
128+
"version": 1
129+
}
130+
"""
131+
}
132+
133+
let store = try PinsStore(pinsFile: pinsFile, fileSystem: fs)
134+
XCTAssertEqual(store.pinsMap.keys.map{$0}.sorted(), ["clang_c", "commandant"])
135+
}
136+
99137
static var allTests = [
100138
("testBasics", testBasics),
139+
("testLoadingSchema1", testLoadingSchema1),
101140
]
102141
}

0 commit comments

Comments
 (0)