Skip to content

Commit cf97c13

Browse files
authored
Refactor manifest serialization (#6145)
This is a fairly large change to refactor the manifest serialization to achieve two goals: get rid of the custom deserialization in `ManifestJSONParser` to use `Codable` throughout and decouple the user-facing `PackageDescription` model from its serialization. - Use `Codable` for `ManifestJSONParser` - Remove `Codable` conformances from public facing PD types and create a private serialization model in `PackageDescriptionSerialization` that can be used for reading and writing Both of these have to happen in a single PR because of a few glaring issues with making the PD model fully codable. There is further work here which can happen in subsequent PRs: - Relocate business logic from `ManifestJSONParser` into `PackageModel` as much as possible - Evaluate the existence of `TargetDescription` etc - it feels like we should be able to get rid of one layer of model objects here, potentially - Evaluate other serialization code in SwiftPM, e.g. for `package describe`, the `ManifestSourceGeneration` stuff etc
1 parent 6e5eb79 commit cf97c13

17 files changed

+1014
-1036
lines changed

Sources/Basics/Observability.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ public struct Diagnostic: CustomStringConvertible {
289289
message = "\(diagnosticData)"
290290
} else if let convertible = error as? DiagnosticDataConvertible {
291291
message = "\(convertible.diagnosticData)"
292+
} else if let decodingError = error as? DecodingError { // special case because `LocalizedError` conversion will hide the underlying error
293+
message = "\(error)"
292294
} else if let localizedError = error as? LocalizedError {
293295
message = localizedError.errorDescription ?? localizedError.localizedDescription
294296
} else {

Sources/PackageDescription/BuildSettings.swift

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
/// The build configuration such as debug or release.
14-
public struct BuildConfiguration: Encodable {
15-
private let config: String
14+
public struct BuildConfiguration {
15+
let config: String
1616

1717
private init(_ config: String) {
1818
self.config = config
@@ -53,10 +53,9 @@ public struct BuildConfiguration: Encodable {
5353
/// ]
5454
/// ),
5555
/// ```
56-
public struct BuildSettingCondition: Encodable {
57-
58-
private let platforms: [Platform]?
59-
private let config: BuildConfiguration?
56+
public struct BuildSettingCondition {
57+
let platforms: [Platform]?
58+
let config: BuildConfiguration?
6059

6160
private init(platforms: [Platform]?, config: BuildConfiguration?) {
6261
self.platforms = platforms
@@ -100,7 +99,7 @@ public struct BuildSettingCondition: Encodable {
10099
}
101100

102101
/// The underlying build setting data.
103-
fileprivate struct BuildSettingData: Encodable {
102+
struct BuildSettingData {
104103

105104
/// The name of the build setting.
106105
let name: String
@@ -113,8 +112,8 @@ fileprivate struct BuildSettingData: Encodable {
113112
}
114113

115114
/// A C-language build setting.
116-
public struct CSetting: Encodable {
117-
private let data: BuildSettingData
115+
public struct CSetting {
116+
let data: BuildSettingData
118117

119118
private init(name: String, value: [String], condition: BuildSettingCondition?) {
120119
self.data = BuildSettingData(name: name, value: value, condition: condition)
@@ -179,8 +178,8 @@ public struct CSetting: Encodable {
179178
}
180179

181180
/// A CXX-language build setting.
182-
public struct CXXSetting: Encodable {
183-
private let data: BuildSettingData
181+
public struct CXXSetting {
182+
let data: BuildSettingData
184183

185184
private init(name: String, value: [String], condition: BuildSettingCondition?) {
186185
self.data = BuildSettingData(name: name, value: value, condition: condition)
@@ -244,8 +243,8 @@ public struct CXXSetting: Encodable {
244243
}
245244

246245
/// A Swift language build setting.
247-
public struct SwiftSetting: Encodable {
248-
private let data: BuildSettingData
246+
public struct SwiftSetting {
247+
let data: BuildSettingData
249248

250249
private init(name: String, value: [String], condition: BuildSettingCondition?) {
251250
self.data = BuildSettingData(name: name, value: value, condition: condition)
@@ -348,8 +347,8 @@ public struct SwiftSetting: Encodable {
348347
}
349348

350349
/// A linker build setting.
351-
public struct LinkerSetting: Encodable {
352-
private let data: BuildSettingData
350+
public struct LinkerSetting {
351+
let data: BuildSettingData
353352

354353
private init(name: String, value: [String], condition: BuildSettingCondition?) {
355354
self.data = BuildSettingData(name: name, value: value, condition: condition)

Sources/PackageDescription/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ add_library(PackageDescription
1313
LanguageStandardSettings.swift
1414
PackageDescription.swift
1515
PackageDescriptionSerialization.swift
16+
PackageDescriptionSerializationConversion.swift
1617
PackageDependency.swift
1718
PackageRequirement.swift
1819
Product.swift

Sources/PackageDescription/LanguageStandardSettings.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
/// The supported C language standard you use to compile C sources in the
1414
/// package.
15-
public enum CLanguageStandard: String, Encodable {
15+
public enum CLanguageStandard: String {
1616

1717
/// The identifier for the ISO C 1990 language standard.
1818
case c89
@@ -89,7 +89,7 @@ public enum CLanguageStandard: String, Encodable {
8989
/// Aliases are available for some C++ language standards. For example,
9090
/// use `cxx98` or `cxx03` for the "ISO C++ 1998 with amendments" standard.
9191
/// To learn more, see [C++ Support in Clang](https://clang.llvm.org/cxx_status.html).
92-
public enum CXXLanguageStandard: String, Encodable {
92+
public enum CXXLanguageStandard: String {
9393

9494
/// The identifier for the ISO C++ 1998 language standard with amendments.
9595
case cxx98 = "c++98"

Sources/PackageDescription/PackageDependency.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
@_implementationOnly import Foundation
14-
15-
// MARK: - file system
1613

1714
extension Package {
1815
/// A package dependency of a Swift package.
@@ -27,7 +24,7 @@ extension Package {
2724
/// package. If you add the Swift package as a package dependency to an app
2825
/// for an Apple platform, you can find the `Package.resolved` file inside
2926
/// your `.xcodeproj` or `.xcworkspace`.
30-
public class Dependency: Encodable {
27+
public class Dependency {
3128
/// The kind of dependency.
3229
@available(_PackageDescription, introduced: 5.6)
3330
public enum Kind {

Sources/PackageDescription/PackageDescription.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,14 +438,15 @@ public enum SystemPackageProvider {
438438
// MARK: - Package Dumping
439439

440440
func manifestToJSON(_ package: Package) -> String {
441-
struct Output: Encodable {
442-
let package: Package
441+
struct Output: Codable {
442+
let package: Serialization.Package
443443
let errors: [String]
444+
let version: Int
444445
}
445446

446447
let encoder = JSONEncoder()
447448
encoder.outputFormatting = [.sortedKeys, .withoutEscapingSlashes]
448-
let data = try! encoder.encode(Output(package: package, errors: errors))
449+
let data = try! encoder.encode(Output(package: .init(package), errors: errors, version: 2))
449450
return String(data: data, encoding: .utf8)!
450451
}
451452

0 commit comments

Comments
 (0)