Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix model structure #19

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
38 changes: 25 additions & 13 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,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")
Expand Down
Loading