Skip to content

Commit a56c214

Browse files
authored
preserve old formatting of Package.resolved in tools-version < 5.6 (#3931) (#3939)
motivation: We moved to JSONEncoder in 5.6 which formats JSON in a non-traditional way (add space around the field name colon), and different from previous version of SwiftPM. We can make the change tools-version dependent. changes: user older JSON encoder when tools-version < 5.6, preserving previous format rdar://86341850
1 parent 814c841 commit a56c214

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

Sources/PackageGraph/PinsStore.swift

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,9 @@ fileprivate struct PinsStorage {
184184
data = try self.encoder.encode(container)
185185
} else {
186186
let container = try V1(pins: pins, mirrors: mirrors)
187-
data = try self.encoder.encode(container)
187+
let json = container.toLegacyJSON()
188+
let bytes = json.toBytes(prettyPrint: true)
189+
data = Data(bytes.contents)
188190
}
189191
#if !os(Windows)
190192
// rdar://83646952: add newline for POSIXy systems
@@ -231,8 +233,23 @@ fileprivate struct PinsStorage {
231233
)
232234
}
233235

236+
// backwards compatibility of JSON format
237+
func toLegacyJSON() -> JSON {
238+
return .init([
239+
"version": self.version.toJSON(),
240+
"object": self.object.toLegacyJSON()
241+
])
242+
}
243+
234244
struct Container: Codable {
235245
var pins: [Pin]
246+
247+
// backwards compatibility of JSON format
248+
func toLegacyJSON() -> JSON {
249+
return .init([
250+
"pins": self.pins.map { $0.toLegacyJSON() },
251+
])
252+
}
236253
}
237254

238255
struct Pin: Codable {
@@ -256,6 +273,15 @@ fileprivate struct PinsStorage {
256273
self.repositoryURL = mirrors.originalURL(for: location) ?? location
257274
self.state = try .init(pin.state)
258275
}
276+
277+
// backwards compatibility of JSON format
278+
func toLegacyJSON() -> JSON {
279+
return .init([
280+
"package": self.package.toJSON(),
281+
"repositoryURL": self.repositoryURL.toJSON(),
282+
"state": self.state.toLegacyJSON()
283+
])
284+
}
259285
}
260286

261287
struct State: Codable {
@@ -281,6 +307,15 @@ fileprivate struct PinsStorage {
281307
throw StringError("invalid pin state: \(state)")
282308
}
283309
}
310+
311+
// backwards compatibility of JSON format
312+
func toLegacyJSON() -> JSON {
313+
return .init([
314+
"revision": self.revision.toJSON(),
315+
"version": self.version.toJSON(),
316+
"branch": self.branch.toJSON()
317+
])
318+
}
284319
}
285320
}
286321

0 commit comments

Comments
 (0)