From 6bbbbf34a9db9b4663c2440bb44d5c7e3acd82a7 Mon Sep 17 00:00:00 2001 From: zunda <47569369+zunda-pixel@users.noreply.github.com> Date: Thu, 12 Dec 2024 01:35:31 +0900 Subject: [PATCH] Fix model structure (#19) --- .../WorkSpacePackage.swift | 36 +++++++++++++----- Sources/LicenseProviderExec/main.swift | 38 ++++++++++++------- 2 files changed, 52 insertions(+), 22 deletions(-) diff --git a/Sources/LicenseProviderExec/WorkSpacePackage.swift b/Sources/LicenseProviderExec/WorkSpacePackage.swift index 020b007..6314c9a 100644 --- a/Sources/LicenseProviderExec/WorkSpacePackage.swift +++ b/Sources/LicenseProviderExec/WorkSpacePackage.swift @@ -6,7 +6,6 @@ import Foundation struct WorkSpacePackage: Decodable, Hashable { let name: String - let location: URL let subPath: String let kind: Kind @@ -15,7 +14,14 @@ struct WorkSpacePackage: Decodable, Hashable { case subpath } - enum CodingKeys: CodingKey { + enum RawKind: String, Decodable { + case remoteSourceControl + case localSourceControl + case fileSystem + case registry + } + + enum PackageCodingKeys: CodingKey { case name case location case kind @@ -26,21 +32,33 @@ struct WorkSpacePackage: Decodable, Hashable { self.subPath = try packageContainer.decode(String.self, forKey: .subpath) let container = try packageContainer.nestedContainer( - keyedBy: CodingKeys.self, + keyedBy: PackageCodingKeys.self, forKey: .packageRef ) self.name = try container.decode(String.self, forKey: .name) - self.location = try container.decode(URL.self, forKey: .location) - self.kind = try container.decode(Kind.self, forKey: .kind) + let rawKind = try container.decode(RawKind.self, forKey: .kind) + switch rawKind { + case .fileSystem: + let location = try container.decode(URL.self, forKey: .location) + self.kind = .fileSystem(location: location) + case .localSourceControl: + let location = try container.decode(URL.self, forKey: .location) + self.kind = .localSourceControl(location: location) + case .remoteSourceControl: + let location = try container.decode(URL.self, forKey: .location) + self.kind = .remoteSourceControl(location: location) + case .registry: + self.kind = .registry + } } } extension WorkSpacePackage { - enum Kind: String, Decodable { - case remoteSourceControl - case localSourceControl - case fileSystem + enum Kind: Hashable { + case remoteSourceControl(location: URL) + case localSourceControl(location: URL) + case fileSystem(location: URL) case registry } } diff --git a/Sources/LicenseProviderExec/main.swift b/Sources/LicenseProviderExec/main.swift index ea7ea4c..402813f 100644 --- a/Sources/LicenseProviderExec/main.swift +++ b/Sources/LicenseProviderExec/main.swift @@ -1,12 +1,24 @@ import Foundation +func generateKindInitializer(kind: WorkSpacePackage.Kind) -> String { + switch kind { + case .fileSystem(let location): + return ".fileSystem(location: URL(string: \"\(location.absoluteString)\")!)" + case .localSourceControl(let location): + return ".localSourceControl(location: URL(string: \"\(location.absoluteString)\")!)" + case .remoteSourceControl(let location): + return ".remoteSourceControl(location: URL(string: \"\(location.absoluteString)\")!)" + case .registry: + return ".registry" + } +} + func generateSourceCode(packages: [WorkSpacePackage: String]) -> String { let workspaceInits = packages.sorted(by: \.key.name).map { """ .init( name: "\($0.key.name)", - location: URL(string: "\($0.key.location)")!, - kind: .\($0.key.kind), + kind: \(generateKindInitializer(kind: $0.key.kind)), license: \""" \($0.value) \""" @@ -24,18 +36,17 @@ func generateSourceCode(packages: [WorkSpacePackage: String]) -> String { } struct Package: Sendable, Hashable, Identifiable { - let id = UUID() - let name: String - let location: URL - let kind: Kind - let license: String + var id = UUID() + var name: String + var kind: Kind + var license: String } extension Package { - enum Kind: String, Sendable, Hashable { - case remoteSourceControl - case localSourceControl - case fileSystem + enum Kind: Sendable, Hashable { + case remoteSourceControl(location: URL) + case localSourceControl(location: URL) + case fileSystem(location: URL) case registry } } @@ -52,10 +63,11 @@ let workspace = try JSONDecoder().decode(WorkSpace.self, from: jsonData) var packages: [WorkSpacePackage: String] = [:] for package in workspace.packages { + let subPath: URL? = switch package.kind { - case .localSourceControl, .fileSystem: - package.location + case .localSourceControl(let location), .fileSystem(let location): + location case .remoteSourceControl: sourcePackagesPath .appendingPathComponent("checkouts")