Skip to content

Commit 165e3fc

Browse files
committed
Make InstalledSwiftPMConfiguration Codable conformance explicit
Make the conformance to Codable explicit so we can swap in the default swift-testing package version when it wasn't present in the config file.
1 parent e4e428a commit 165e3fc

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

Sources/PackageModel/InstalledSwiftPMConfiguration.swift

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
public struct InstalledSwiftPMConfiguration: Codable {
13+
public struct InstalledSwiftPMConfiguration {
1414
public struct Version: Codable, CustomStringConvertible {
1515
let major: Int
1616
let minor: Int
@@ -42,12 +42,55 @@ public struct InstalledSwiftPMConfiguration: Codable {
4242
patch: 0,
4343
prereleaseIdentifier: "latest"
4444
),
45-
swiftTestingVersionForTestTemplate: .init(
46-
major: 0,
47-
minor: 7,
48-
patch: 0,
49-
prereleaseIdentifier: nil
50-
)
45+
swiftTestingVersionForTestTemplate: defaultSwiftTestingVersionForTestTemplate
46+
)
47+
}
48+
49+
private static var defaultSwiftTestingVersionForTestTemplate: Version {
50+
.init(
51+
major: 0,
52+
minor: 7,
53+
patch: 0,
54+
prereleaseIdentifier: nil
55+
)
56+
}
57+
}
58+
59+
extension InstalledSwiftPMConfiguration: Codable {
60+
enum CodingKeys: CodingKey {
61+
case version
62+
case swiftSyntaxVersionForMacroTemplate
63+
case swiftTestingVersionForTestTemplate
64+
}
65+
66+
public init(from decoder: any Decoder) throws {
67+
let container = try decoder.container(keyedBy: CodingKeys.self)
68+
69+
self.version = try container.decode(
70+
Int.self,
71+
forKey: CodingKeys.version
72+
)
73+
self.swiftSyntaxVersionForMacroTemplate = try container.decode(
74+
Version.self,
75+
forKey: CodingKeys.swiftSyntaxVersionForMacroTemplate
5176
)
77+
self.swiftTestingVersionForTestTemplate = try container.decodeIfPresent(
78+
Version.self,
79+
forKey: CodingKeys.swiftTestingVersionForTestTemplate
80+
) ?? InstalledSwiftPMConfiguration.defaultSwiftTestingVersionForTestTemplate
5281
}
82+
83+
public func encode(to encoder: any Encoder) throws {
84+
var container = encoder.container(keyedBy: CodingKeys.self)
85+
86+
try container.encode(self.version, forKey: CodingKeys.version)
87+
try container.encode(
88+
self.swiftSyntaxVersionForMacroTemplate,
89+
forKey: CodingKeys.swiftSyntaxVersionForMacroTemplate
90+
)
91+
try container.encode(
92+
self.swiftTestingVersionForTestTemplate,
93+
forKey: CodingKeys.swiftTestingVersionForTestTemplate
94+
)
95+
}
5396
}

Sources/PackageModelSyntax/AddTarget.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,8 @@ fileprivate extension PackageDependency {
399399
static func swiftTesting(
400400
configuration: InstalledSwiftPMConfiguration
401401
) -> PackageDependency {
402-
let swiftTestingVersionDefault = configuration
403-
.swiftTestingVersionForTestTemplate
402+
let swiftTestingVersionDefault =
403+
configuration.swiftTestingVersionForTestTemplate
404404
let swiftTestingVersion = Version(swiftTestingVersionDefault.description)!
405405

406406
return .sourceControl(

0 commit comments

Comments
 (0)