From 4495755cd1365690e36b0d1dee1acf91fb6044f6 Mon Sep 17 00:00:00 2001 From: Brent Mifsud <18543934+BrentMifsud@users.noreply.github.com> Date: Sat, 10 Feb 2024 21:53:01 -0500 Subject: [PATCH] Add BuildToolPlugin and XcodeBuildToolPlugin --- Package.swift | 25 +++++++++- Plugins/FormatBuildPlugin/plugin.swift | 63 ++++++++++++++++++++++++++ Plugins/FormatPlugin/plugin.swift | 3 ++ Plugins/LintBuildPlugin/plugin.swift | 63 ++++++++++++++++++++++++++ 4 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 Plugins/FormatBuildPlugin/plugin.swift create mode 100644 Plugins/LintBuildPlugin/plugin.swift diff --git a/Package.swift b/Package.swift index 9a199503..077f0d01 100644 --- a/Package.swift +++ b/Package.swift @@ -33,10 +33,18 @@ let package = Package( name: "FormatPlugin", targets: ["Format Source Code"] ), + .plugin( + name: "FormatBuildPlugin", + targets: ["Format Build Plugin"] + ), .plugin( name: "LintPlugin", targets: ["Lint Source Code"] ), + .plugin( + name: "LintBuildPlugin", + targets: ["Lint Build Plugin"] + ) ], dependencies: dependencies, targets: [ @@ -75,6 +83,14 @@ let package = Package( ], path: "Plugins/FormatPlugin" ), + .plugin( + name: "Format Build Plugin", + capability: .buildTool(), + dependencies: [ + .target(name: "swift-format") + ], + path: "Plugins/FormatBuildPlugin" + ), .plugin( name: "Lint Source Code", capability: .command( @@ -88,6 +104,14 @@ let package = Package( ], path: "Plugins/LintPlugin" ), + .plugin( + name: "Lint Build Plugin", + capability: .buildTool(), + dependencies: [ + .target(name: "swift-format") + ], + path: "Plugins/LintBuildPlugin" + ), .executableTarget( name: "generate-swift-format", dependencies: [ @@ -107,7 +131,6 @@ let package = Package( ], linkerSettings: swiftformatLinkSettings ), - .testTarget( name: "SwiftFormatPerformanceTests", dependencies: [ diff --git a/Plugins/FormatBuildPlugin/plugin.swift b/Plugins/FormatBuildPlugin/plugin.swift new file mode 100644 index 00000000..21b764bd --- /dev/null +++ b/Plugins/FormatBuildPlugin/plugin.swift @@ -0,0 +1,63 @@ +import PackagePlugin +import Foundation + +@main +struct FormatBuildPlugin { + static private let toolName = "swift-format" + + func createBuildCommands( + pluginWorkDirectory: Path, + tool: PluginContext.Tool, + targetDirectory: Path + ) -> [Command] { + let arguments = [ + "format", + targetDirectory.string, + "--recursive", + "--parallel", + "--in-place", + "--configuration", + targetDirectory.appending(subpath: ".swift-format").string + ] + + return [ + .prebuildCommand( + displayName: "Format Source Code", + executable: tool.path, + arguments: arguments, + environment: [:], + outputFilesDirectory: pluginWorkDirectory.appending(Self.toolName) + ) + ] + } +} + +extension FormatBuildPlugin: BuildToolPlugin { + func createBuildCommands( + context: PackagePlugin.PluginContext, + target: PackagePlugin.Target + ) async throws -> [PackagePlugin.Command] { + createBuildCommands( + pluginWorkDirectory: context.pluginWorkDirectory, + tool: try context.tool(named: Self.toolName), + targetDirectory: target.directory + ) + } +} + +#if canImport(XcodeProjectPlugin) +import XcodeProjectPlugin + +extension FormatBuildPlugin: XcodeBuildToolPlugin { + func createBuildCommands( + context: XcodeProjectPlugin.XcodePluginContext, + target: XcodeProjectPlugin.XcodeTarget + ) throws -> [PackagePlugin.Command] { + createBuildCommands( + pluginWorkDirectory: context.pluginWorkDirectory, + tool: try context.tool(named: Self.toolName), + targetDirectory: context.xcodeProject.directory + ) + } +} +#endif diff --git a/Plugins/FormatPlugin/plugin.swift b/Plugins/FormatPlugin/plugin.swift index e544a646..d699d37b 100644 --- a/Plugins/FormatPlugin/plugin.swift +++ b/Plugins/FormatPlugin/plugin.swift @@ -3,6 +3,9 @@ import Foundation @main struct FormatPlugin { + static private let toolName = "swift-format" + static private let configName = ".swift-format" + func format(tool: PluginContext.Tool, targetDirectories: [String], configurationFilePath: String?) throws { let swiftFormatExec = URL(fileURLWithPath: tool.path.string) diff --git a/Plugins/LintBuildPlugin/plugin.swift b/Plugins/LintBuildPlugin/plugin.swift new file mode 100644 index 00000000..cd4e93ea --- /dev/null +++ b/Plugins/LintBuildPlugin/plugin.swift @@ -0,0 +1,63 @@ +import PackagePlugin +import Foundation + +@main +struct LintBuildPlugin { + static private let toolName = "swift-format" + + private func createBuildCommands( + pluginWorkDirectory: Path, + tool: PluginContext.Tool, + targetDirectory: Path + ) -> [Command] { + let arguments = [ + "lint", + targetDirectory.string, + "--recursive", + "--parallel", + "--strict", + "--configuration", + targetDirectory.appending(subpath: ".swift-format").string + ] + + return [ + .prebuildCommand( + displayName: "Lint Source Code", + executable: tool.path, + arguments: arguments, + environment: [:], + outputFilesDirectory: pluginWorkDirectory.appending(Self.toolName) + ) + ] + } +} + +extension LintBuildPlugin: BuildToolPlugin { + func createBuildCommands( + context: PackagePlugin.PluginContext, + target: PackagePlugin.Target + ) async throws -> [PackagePlugin.Command] { + createBuildCommands( + pluginWorkDirectory: context.pluginWorkDirectory, + tool: try context.tool(named: Self.toolName), + targetDirectory: target.directory + ) + } +} + +#if canImport(XcodeProjectPlugin) +import XcodeProjectPlugin + +extension LintBuildPlugin: XcodeBuildToolPlugin { + func createBuildCommands( + context: XcodeProjectPlugin.XcodePluginContext, + target: XcodeProjectPlugin.XcodeTarget + ) throws -> [PackagePlugin.Command] { + createBuildCommands( + pluginWorkDirectory: context.pluginWorkDirectory, + tool: try context.tool(named: Self.toolName), + targetDirectory: context.xcodeProject.directory + ) + } +} +#endif