Skip to content

Commit

Permalink
fix model structure
Browse files Browse the repository at this point in the history
  • Loading branch information
zunda-pixel committed Dec 11, 2024
1 parent 1cd6220 commit 7fc0c55
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 31 deletions.
36 changes: 27 additions & 9 deletions Sources/LicenseProviderExec/WorkSpacePackage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Foundation

struct WorkSpacePackage: Decodable, Hashable {
let name: String
let location: URL
let subPath: String
let kind: Kind

Expand All @@ -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
Expand All @@ -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
}
}
55 changes: 33 additions & 22 deletions Sources/LicenseProviderExec/main.swift
Original file line number Diff line number Diff line change
@@ -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)
\"""
Expand All @@ -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
}
}
Expand All @@ -52,17 +63,17 @@ 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 .remoteSourceControl:
sourcePackagesPath
.appendingPathComponent("checkouts")
.appendingPathComponent(package.subPath)
case .registry:
nil
}

let subPath: URL? = switch package.kind {
case .localSourceControl(let location), .fileSystem(let location):
location
case .remoteSourceControl:
sourcePackagesPath
.appendingPathComponent("checkouts")
.appendingPathComponent(package.subPath)
case .registry:
nil
}

guard let subPath else { continue }

Expand Down

0 comments on commit 7fc0c55

Please sign in to comment.