Skip to content

Commit

Permalink
Make InstalledSwiftPMConfiguration Codable conformance explicit
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
DougGregor committed May 8, 2024
1 parent e4e428a commit 165e3fc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
57 changes: 50 additions & 7 deletions Sources/PackageModel/InstalledSwiftPMConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//
//===----------------------------------------------------------------------===//

public struct InstalledSwiftPMConfiguration: Codable {
public struct InstalledSwiftPMConfiguration {
public struct Version: Codable, CustomStringConvertible {
let major: Int
let minor: Int
Expand Down Expand Up @@ -42,12 +42,55 @@ public struct InstalledSwiftPMConfiguration: Codable {
patch: 0,
prereleaseIdentifier: "latest"
),
swiftTestingVersionForTestTemplate: .init(
major: 0,
minor: 7,
patch: 0,
prereleaseIdentifier: nil
)
swiftTestingVersionForTestTemplate: defaultSwiftTestingVersionForTestTemplate
)
}

private static var defaultSwiftTestingVersionForTestTemplate: Version {
.init(
major: 0,
minor: 7,
patch: 0,
prereleaseIdentifier: nil
)
}
}

extension InstalledSwiftPMConfiguration: Codable {
enum CodingKeys: CodingKey {
case version
case swiftSyntaxVersionForMacroTemplate
case swiftTestingVersionForTestTemplate
}

public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

self.version = try container.decode(
Int.self,
forKey: CodingKeys.version
)
self.swiftSyntaxVersionForMacroTemplate = try container.decode(
Version.self,
forKey: CodingKeys.swiftSyntaxVersionForMacroTemplate
)
self.swiftTestingVersionForTestTemplate = try container.decodeIfPresent(
Version.self,
forKey: CodingKeys.swiftTestingVersionForTestTemplate
) ?? InstalledSwiftPMConfiguration.defaultSwiftTestingVersionForTestTemplate
}

public func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(self.version, forKey: CodingKeys.version)
try container.encode(
self.swiftSyntaxVersionForMacroTemplate,
forKey: CodingKeys.swiftSyntaxVersionForMacroTemplate
)
try container.encode(
self.swiftTestingVersionForTestTemplate,
forKey: CodingKeys.swiftTestingVersionForTestTemplate
)
}
}
4 changes: 2 additions & 2 deletions Sources/PackageModelSyntax/AddTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,8 @@ fileprivate extension PackageDependency {
static func swiftTesting(
configuration: InstalledSwiftPMConfiguration
) -> PackageDependency {
let swiftTestingVersionDefault = configuration
.swiftTestingVersionForTestTemplate
let swiftTestingVersionDefault =
configuration.swiftTestingVersionForTestTemplate
let swiftTestingVersion = Version(swiftTestingVersionDefault.description)!

return .sourceControl(
Expand Down

0 comments on commit 165e3fc

Please sign in to comment.