-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #167 from PureSwift/feature/unit-identifier-plugin
Add SwiftPM plugin for `UnitIdentifier` generated code
- Loading branch information
Showing
16 changed files
with
2,278 additions
and
1,164 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
Plugins/GenerateBluetoothDefinitions/CompanyIdentifier.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.