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

Add BuildToolPlugin and XcodeBuildToolPlugin #690

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
25 changes: 24 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -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(
Expand All @@ -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: [
Expand All @@ -107,7 +131,6 @@ let package = Package(
],
linkerSettings: swiftformatLinkSettings
),

.testTarget(
name: "SwiftFormatPerformanceTests",
dependencies: [
Expand Down
63 changes: 63 additions & 0 deletions Plugins/FormatBuildPlugin/plugin.swift
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions Plugins/FormatPlugin/plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
63 changes: 63 additions & 0 deletions Plugins/LintBuildPlugin/plugin.swift
Original file line number Diff line number Diff line change
@@ -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