Skip to content

Commit

Permalink
Merge pull request #167 from PureSwift/feature/unit-identifier-plugin
Browse files Browse the repository at this point in the history
Add SwiftPM plugin for `UnitIdentifier` generated code
  • Loading branch information
colemancda authored Nov 5, 2024
2 parents e19e806 + 71078ff commit 2e7b503
Show file tree
Hide file tree
Showing 16 changed files with 2,278 additions and 1,164 deletions.
80 changes: 80 additions & 0 deletions Plugins/GenerateBluetoothDefinitions/CompanyIdentifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// CompanyIdentifier.swift
// Bluetooth
//
// Created by Alsey Coleman Miller on 11/5/24.
//

import Foundation
import PackagePlugin

extension GenerateBluetoothDefinitionsPlugin {

func companyIdentifiersBuildCommands(
for context: PluginContext,
target: SwiftSourceModuleTarget,
commands: inout [Command]
) throws {
guard target.name == "Bluetooth" else {
return
}
// Generate Bluetooth Company Identifier Definitions
let inputFileName = "CompanyIdentifier.json"
let inputPath = target
.sourceFiles(withSuffix: "json")
.filter { $0.type == .unknown }
.first { $0.path.lastComponent == inputFileName }
.map { $0.path }
guard let inputPath = inputPath else {
Diagnostics.error("Missing \(inputFileName)")
throw CocoaError(CocoaError.fileNoSuchFile)
}
let outputDirectory = context.pluginWorkDirectory
let outputPaths = [
outputDirectory.appending("CompanyIdentifiers.swift"),
outputDirectory.appending("CompanyIdentifierNames.swift")
]
let command = Command.buildCommand(
displayName: "Generate Bluetooth Company Identifier Definitions",
executable: try context.tool(named: "GenerateBluetooth").path,
arguments: ["companyIdentifier", inputPath] + outputPaths,
inputFiles: [inputPath],
outputFiles: outputPaths
)
commands.append(command)
}

func companyIdentifierTestsBuildCommands(
for context: PluginContext,
target: SwiftSourceModuleTarget,
commands: inout [Command]
) throws {
// Generate Bluetooth Company Identifier Unit Tests
guard target.name == "BluetoothTests" else {
return
}
let inputFileName = "CompanyIdentifier.json"
guard let bluetoothTarget = try context.package.targets(named: ["Bluetooth"]).first as? SwiftSourceModuleTarget else {
fatalError("Missing Bluetooth target")
}
let inputPath = bluetoothTarget
.sourceFiles(withSuffix: "json")
.filter { $0.type == .unknown }
.first { $0.path.lastComponent == inputFileName }
.map { $0.path }
guard let inputPath = inputPath else {
Diagnostics.error("Missing \(inputFileName)")
throw CocoaError(CocoaError.fileNoSuchFile)
}
let outputDirectory = context.pluginWorkDirectory
let outputPath = outputDirectory.appending("CompanyIdentifierTests.swift")
let command = Command.buildCommand(
displayName: "Generate Bluetooth Company Identifier Unit Tests",
executable: try context.tool(named: "GenerateBluetooth").path,
arguments: ["companyIdentifierTests", inputPath, outputPath],
inputFiles: [inputPath],
outputFiles: [outputPath]
)
commands.append(command)
}
}
66 changes: 15 additions & 51 deletions Plugins/GenerateBluetoothDefinitions/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,60 +13,24 @@ struct GenerateBluetoothDefinitionsPlugin: BuildToolPlugin {

/// This entry point is called when operating on a Swift package.
func createBuildCommands(context: PluginContext, target: Target) throws -> [Command] {
let target = target as! SwiftSourceModuleTarget
guard let target = target as? SwiftSourceModuleTarget else {
return []
}
var commands = [Command]()
commands.reserveCapacity(10)

// Generate Bluetooth Company Identifier Definitions
if target.name == "Bluetooth" {
let inputFileName = "CompanyIdentifiers.json"
let inputPath = target
.sourceFiles(withSuffix: "json")
.filter { $0.type == .unknown }
.first { $0.path.lastComponent == inputFileName }
.map { $0.path }
guard let inputPath = inputPath else {
Diagnostics.error("Missing \(inputFileName)")
throw CocoaError(CocoaError.fileNoSuchFile)
}
let outputDirectory = context.pluginWorkDirectory
let outputPaths = [
outputDirectory.appending("CompanyIdentifiers.swift"),
outputDirectory.appending("CompanyIdentifierNames.swift")
]
let command = Command.buildCommand(
displayName: "Generate Bluetooth Company Identifier Definitions",
executable: try context.tool(named: "GenerateBluetooth").path,
arguments: ["companyIdentifier", inputPath] + outputPaths,
inputFiles: [inputPath],
outputFiles: outputPaths
)
commands.append(command)
}
try companyIdentifiersBuildCommands(for: context, target: target, commands: &commands)
// Generate Bluetooth Company Identifier Unit Tests
if target.name == "BluetoothTests" {
let inputFileName = "CompanyIdentifiers.json"
guard let bluetoothTarget = try context.package.targets(named: ["Bluetooth"]).first as? SwiftSourceModuleTarget else {
fatalError("Missing Bluetooth target")
}
let inputPath = bluetoothTarget
.sourceFiles(withSuffix: "json")
.filter { $0.type == .unknown }
.first { $0.path.lastComponent == inputFileName }
.map { $0.path }
guard let inputPath = inputPath else {
Diagnostics.error("Missing \(inputFileName)")
throw CocoaError(CocoaError.fileNoSuchFile)
}
let outputDirectory = context.pluginWorkDirectory
let outputPath = outputDirectory.appending("CompanyIdentifierTests.swift")
let command = Command.buildCommand(
displayName: "Generate Bluetooth Company Identifier Unit Tests",
executable: try context.tool(named: "GenerateBluetooth").path,
arguments: ["companyIdentifierTests", inputPath, outputPath],
inputFiles: [inputPath],
outputFiles: [outputPath]
)
commands.append(command)
}
try companyIdentifierTestsBuildCommands(for: context, target: target, commands: &commands)

// Generate Bluetooth Unit Identifier Definitions
try unitIdentifiersBuildCommands(for: context, target: target, commands: &commands)
// Generate Bluetooth Unit Identifier Unit Tests
try unitIdentifierTestsBuildCommands(for: context, target: target, commands: &commands)



return commands
}
}
80 changes: 80 additions & 0 deletions Plugins/GenerateBluetoothDefinitions/UnitIdentifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// UnitIdentifier.swift
// Bluetooth
//
// Created by Alsey Coleman Miller on 11/5/24.
//

import Foundation
import PackagePlugin

extension GenerateBluetoothDefinitionsPlugin {

func unitIdentifiersBuildCommands(
for context: PluginContext,
target: SwiftSourceModuleTarget,
commands: inout [Command]
) throws {
guard target.name == "Bluetooth" else {
return
}
// Generate Bluetooth Unit Identifier Definitions
let inputFileName = "UnitIdentifier.json"
let inputPath = target
.sourceFiles(withSuffix: "json")
.filter { $0.type == .unknown }
.first { $0.path.lastComponent == inputFileName }
.map { $0.path }
guard let inputPath = inputPath else {
Diagnostics.error("Missing \(inputFileName)")
throw CocoaError(CocoaError.fileNoSuchFile)
}
let outputDirectory = context.pluginWorkDirectory
let outputPaths = [
outputDirectory.appending("UnitIdentifiers.swift"),
outputDirectory.appending("UnitIdentifierMetadata.swift")
]
let command = Command.buildCommand(
displayName: "Generate Bluetooth Unit Identifier Definitions",
executable: try context.tool(named: "GenerateBluetooth").path,
arguments: ["unitIdentifier", inputPath] + outputPaths,
inputFiles: [inputPath],
outputFiles: outputPaths
)
commands.append(command)
}

func unitIdentifierTestsBuildCommands(
for context: PluginContext,
target: SwiftSourceModuleTarget,
commands: inout [Command]
) throws {
// Generate Bluetooth Unit Identifier Unit Tests
guard target.name == "BluetoothTests" else {
return
}
let inputFileName = "UnitIdentifier.json"
guard let bluetoothTarget = try context.package.targets(named: ["Bluetooth"]).first as? SwiftSourceModuleTarget else {
fatalError("Missing Bluetooth target")
}
let inputPath = bluetoothTarget
.sourceFiles(withSuffix: "json")
.filter { $0.type == .unknown }
.first { $0.path.lastComponent == inputFileName }
.map { $0.path }
guard let inputPath = inputPath else {
Diagnostics.error("Missing \(inputFileName)")
throw CocoaError(CocoaError.fileNoSuchFile)
}
let outputDirectory = context.pluginWorkDirectory
let outputPath = outputDirectory.appending("UnitIdentifierTests.swift")
let command = Command.buildCommand(
displayName: "Generate Bluetooth Unit Identifier Unit Tests",
executable: try context.tool(named: "GenerateBluetooth").path,
arguments: ["unitIdentifierTests", inputPath, outputPath],
inputFiles: [inputPath],
outputFiles: [outputPath]
)
commands.append(command)
}
}
Loading

0 comments on commit 2e7b503

Please sign in to comment.