Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
zunda-pixel committed Aug 12, 2024
1 parent be48b2a commit a7df916
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 74 deletions.
7 changes: 4 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ let package = Package(
.tvOS(.v12),
.watchOS(.v4),
.macCatalyst(.v13),
.visionOS(.v1),
],
products: [
.plugin(
name: "LicenseProviderPlugin",
targets: [
"LicenseProviderPlugin",
"LicenseProviderPlugin"
]
),
)
],
targets: [
.executableTarget(
Expand All @@ -29,6 +30,6 @@ let package = Package(
dependencies: [
.target(name: "LicenseProviderExec")
]
)
),
]
)
62 changes: 33 additions & 29 deletions Plugins/LicenseProviderPlugin/LicenseProviderPlugin.swift
Original file line number Diff line number Diff line change
@@ -1,74 +1,78 @@
import PackagePlugin
import Foundation
import PackagePlugin

@main
struct LicenseViewPlugin {
static let commandName = "LicenseProviderExec"

func sourcePackagesPath(workDirectory: URL) -> URL? {
var workDirectory = workDirectory

guard workDirectory.absoluteString.contains("SourcePackages") else {
return nil
}

while workDirectory.lastPathComponent != "SourcePackages" {
workDirectory = workDirectory.deletingLastPathComponent()
}

return workDirectory
}

func buildCommands(executablePath: URL, workDirectory: URL) -> Command? {
let fileName = "LicenseProvider.swift"

let output = workDirectory.appending(path: fileName)
guard let sourcePackages = sourcePackagesPath(workDirectory: workDirectory) else {
return nil
}

return .buildCommand(
displayName: "LicenseProviderPlugin",
executable: executablePath,
arguments: [
output.path(),
sourcePackages.path()
sourcePackages.path(),
],
outputFiles: [ output ]
outputFiles: [output]
)
}
}

extension LicenseViewPlugin: BuildToolPlugin {
func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] {
let executablePath = try context.tool(named: LicenseViewPlugin.commandName).url

guard let command = buildCommands(
executablePath: executablePath,
workDirectory: context.pluginWorkDirectoryURL
) else {

guard
let command = buildCommands(
executablePath: executablePath,
workDirectory: context.pluginWorkDirectoryURL
)
else {
return []
}

return [command]
}
}

#if canImport(XcodeProjectPlugin)
import XcodeProjectPlugin
import XcodeProjectPlugin

extension LicenseViewPlugin: XcodeBuildToolPlugin {
func createBuildCommands(context: XcodePluginContext, target: XcodeTarget) throws -> [Command] {
let executablePath = try context.tool(named: LicenseViewPlugin.commandName).url

guard let command = buildCommands(
executablePath: executablePath,
workDirectory: context.pluginWorkDirectoryURL
) else {
return []
extension LicenseViewPlugin: XcodeBuildToolPlugin {
func createBuildCommands(context: XcodePluginContext, target: XcodeTarget) throws -> [Command] {
let executablePath = try context.tool(named: LicenseViewPlugin.commandName).url

guard
let command = buildCommands(
executablePath: executablePath,
workDirectory: context.pluginWorkDirectoryURL
)
else {
return []
}

return [command]
}

return [command]
}
}
#endif
11 changes: 7 additions & 4 deletions Sources/LicenseProviderExec/WorkSpace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ import Foundation

struct WorkSpace: Decodable {
let packages: [WorkSpacePackage]

private enum ObjectCodingKeys: CodingKey {
case object
}

private enum DependenciesCodingKeys: CodingKey {
case dependencies
}

init(from decoder: Decoder) throws {
let objectContainer = try decoder.container(keyedBy: ObjectCodingKeys.self)
let dependenciesContainer = try objectContainer.nestedContainer(keyedBy: DependenciesCodingKeys.self, forKey: .object)
let dependenciesContainer = try objectContainer.nestedContainer(
keyedBy: DependenciesCodingKeys.self,
forKey: .object
)
self.packages = try dependenciesContainer.decode([WorkSpacePackage].self, forKey: .dependencies)
}
}
15 changes: 9 additions & 6 deletions Sources/LicenseProviderExec/WorkSpacePackage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,26 @@ struct WorkSpacePackage: Decodable, Hashable {
let name: String
let location: URL
let subPath: String

enum PackageRefCodingKeys: CodingKey {
case packageRef
case subpath
}

enum CodingKeys: CodingKey {
case name
case location
}

init(from decoder: Decoder) throws {
let packageContainer = try decoder.container(keyedBy: PackageRefCodingKeys.self)

self.subPath = try packageContainer.decode(String.self, forKey: .subpath)
let container = try packageContainer.nestedContainer(keyedBy: CodingKeys.self, forKey: .packageRef)

let container = try packageContainer.nestedContainer(
keyedBy: CodingKeys.self,
forKey: .packageRef
)

self.name = try container.decode(String.self, forKey: .name)
self.location = try container.decode(URL.self, forKey: .location)
}
Expand Down
68 changes: 36 additions & 32 deletions Sources/LicenseProviderExec/main.swift
Original file line number Diff line number Diff line change
@@ -1,54 +1,59 @@
import Foundation

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)")!,
license: \"""
\($0.value)
\"""
)
"""
let workspaceInits = packages.sorted(by: \.key.name).map {
"""
.init(
name: "\($0.key.name)",
location: URL(string: "\($0.key.location)")!,
license: \"""
\($0.value)
\"""
)
"""
}

let sourceCode = """
import Foundation
enum LicenseProvider {
static let packages: [Package] = [
\(workspaceInits.joined(separator: ",\n"))
]
}
import Foundation
enum LicenseProvider {
static let packages: [Package] = [
\(workspaceInits.joined(separator: ",\n"))
]
}
struct Package: Hashable, Identifiable {
let id = UUID()
let name: String
let location: URL
let license: String
}
"""
struct Package: Hashable, Identifiable {
let id = UUID()
let name: String
let location: URL
let license: String
}
"""
return sourceCode
}

let sourcePackagesPath = URL(fileURLWithPath: CommandLine.arguments[2])

let jsonData = try Data(contentsOf: sourcePackagesPath.appendingPathComponent("workspace-state.json"))
let jsonData = try Data(
contentsOf: sourcePackagesPath.appendingPathComponent("workspace-state.json"))
let workspace = try JSONDecoder().decode(WorkSpace.self, from: jsonData)

var packages: [WorkSpacePackage: String] = [:]

for package in workspace.packages {
let subPath = sourcePackagesPath.appendingPathComponent("checkouts").appendingPathComponent(package.subPath)
let contents = try FileManager.default.contentsOfDirectory(at: subPath, includingPropertiesForKeys: nil).filter { path in
for package in workspace.packages {
let subPath = sourcePackagesPath.appendingPathComponent("checkouts").appendingPathComponent(
package.subPath)
let contents = try FileManager.default.contentsOfDirectory(
at: subPath, includingPropertiesForKeys: nil
).filter { path in
let pathWithoutExtension = path.deletingPathExtension()

return pathWithoutExtension.lastPathComponent.lowercased() == "license"
}

if let content = contents.first {
let fileData = try Data(contentsOf: content)

packages[package] = String(decoding: fileData, as: UTF8.self)
}
}
Expand All @@ -60,7 +65,6 @@ let outputFilePath = URL(fileURLWithPath: CommandLine.arguments[1])

try sourceCodeData.write(to: outputFilePath)


extension Sequence {
func sorted<T: Comparable>(by keyPath: KeyPath<Element, T>, isAscending: Bool = true) -> [Element]
{
Expand Down

0 comments on commit a7df916

Please sign in to comment.