Skip to content

Implement proposed adjustments to SE-0303 to use @main for plugins #3712

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

Merged
merged 6 commits into from
Sep 17, 2021
Merged
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@main
@main
struct MyProgram {
static func main() {
print("Hello, Swift.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@main
@main
struct MyProgram {
static func main() {
print("Hello, Swift.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import PackagePlugin

for inputFile in targetBuildContext.inputFiles.filter({ $0.path.extension == "dat" }) {
let inputPath = inputFile.path
let outputName = "Ambiguous_" + inputPath.stem + ".swift"
let outputPath = targetBuildContext.pluginWorkDirectory.appending(outputName)
commandConstructor.addBuildCommand(
displayName:
"This is a constant name",
executable:
try targetBuildContext.tool(named: "MySourceGenBuildTool").path,
arguments: [
"\(inputPath)",
"\(outputPath)"
],
environment: [
"VARIABLE_NAME_PREFIX": "SECOND_PREFIX_"
],
inputFiles: [
inputPath,
],
outputFiles: [
outputPath
]
)
@main
struct MyPlugin: BuildToolPlugin {

func createBuildCommands(context: TargetBuildContext) throws -> [Command] {
var commands: [Command] = []
for inputFile in context.inputFiles.filter({ $0.path.extension == "dat" }) {
let inputPath = inputFile.path
let outputName = "Ambiguous_" + inputPath.stem + ".swift"
let outputPath = context.pluginWorkDirectory.appending(outputName)
commands.append(.buildCommand(
displayName:
"This is a constant name",
executable:
try context.tool(named: "MySourceGenBuildTool").path,
arguments: [
"\(inputPath)",
"\(outputPath)"
],
environment: [
"VARIABLE_NAME_PREFIX": "SECOND_PREFIX_"
],
inputFiles: [
inputPath,
],
outputFiles: [
outputPath
]
))
}
return commands
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
import PackagePlugin

print("Hello from the Build Tool Plugin!")

for inputFile in targetBuildContext.inputFiles.filter({ $0.path.extension == "dat" }) {
let inputPath = inputFile.path
let outputName = inputPath.stem + ".swift"
let outputPath = targetBuildContext.pluginWorkDirectory.appending(outputName)
commandConstructor.addBuildCommand(
displayName:
"Generating \(outputName) from \(inputPath.lastComponent)",
executable:
try targetBuildContext.tool(named: "MySourceGenBuildTool").path,
arguments: [
"\(inputPath)",
"\(outputPath)"
],
environment: [
"VARIABLE_NAME_PREFIX": "PREFIX_"
],
inputFiles: [
inputPath,
],
outputFiles: [
outputPath
]
)
@main
struct MyPlugin: BuildToolPlugin {

func createBuildCommands(context: TargetBuildContext) throws -> [Command] {
let inputFiles = context.inputFiles.filter({ $0.path.extension == "dat" })
return try inputFiles.map {
let inputFile = $0
let inputPath = inputFile.path
let outputName = inputPath.stem + ".swift"
let outputPath = context.pluginWorkDirectory.appending(outputName)
return .buildCommand(
displayName:
"Generating \(outputName) from \(inputPath.lastComponent)",
executable:
try context.tool(named: "MySourceGenBuildTool").path,
arguments: [
"\(inputPath)",
"\(outputPath)"
],
environment: [
"VARIABLE_NAME_PREFIX": "PREFIX_"
],
inputFiles: [
inputPath,
],
outputFiles: [
outputPath
]
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import PackagePlugin

print("Hello from the Build Tool Plugin!")

for inputFile in targetBuildContext.inputFiles.filter({ $0.path.extension == "dat" }) {
let inputPath = inputFile.path
let outputName = inputPath.stem + ".swift"
let outputPath = targetBuildContext.pluginWorkDirectory.appending(outputName)
commandConstructor.addBuildCommand(
displayName:
"Generating \(outputName) from \(inputPath.lastComponent)",
executable:
try targetBuildContext.tool(named: "mytool").path,
arguments: [
"--verbose",
"\(inputPath)",
"\(outputPath)"
],
inputFiles: [
inputPath,
],
outputFiles: [
outputPath
]
)
@main
struct MyPlugin: BuildToolPlugin {

func createBuildCommands(context: TargetBuildContext) throws -> [Command] {
print("Hello from the Build Tool Plugin!")

let inputFiles = context.inputFiles.filter({ $0.path.extension == "dat" })
return try inputFiles.map {
let inputFile = $0
let inputPath = inputFile.path
let outputName = inputPath.stem + ".swift"
let outputPath = context.pluginWorkDirectory.appending(outputName)
return .buildCommand(
displayName:
"Generating \(outputName) from \(inputPath.lastComponent)",
executable:
try context.tool(named: "mytool").path,
arguments: [
"--verbose",
"\(inputPath)",
"\(outputPath)"
],
inputFiles: [
inputPath,
],
outputFiles: [
outputPath
]
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import PackagePlugin

print("Hello from the Build Tool Plugin!")

for inputPath in targetBuildContext.inputFiles.map{ $0.path } {
guard inputPath.extension == "dat" else { continue }
let outputName = inputPath.stem + ".swift"
let outputPath = targetBuildContext.pluginWorkDirectory.appending(outputName)
commandConstructor.addBuildCommand(
displayName:
"Generating \(outputName) from \(inputPath.lastComponent)",
executable:
try targetBuildContext.tool(named: "MySourceGenBuildTool").path,
arguments: [
"\(inputPath)",
"\(outputPath)"
],
inputFiles: [
inputPath,
],
outputFiles: [
outputPath
]
)
@main
struct MyPlugin: BuildToolPlugin {

func createBuildCommands(context: TargetBuildContext) throws -> [Command] {
print("Hello from the Build Tool Plugin!")

return try context.inputFiles.map{ $0.path }.compactMap {
guard $0.extension == "dat" else { return .none }
let outputName = $0.stem + ".swift"
let outputPath = context.pluginWorkDirectory.appending(outputName)
return .buildCommand(
displayName:
"Generating \(outputName) from \($0.lastComponent)",
executable:
try context.tool(named: "MySourceGenBuildTool").path,
arguments: [
"\($0)",
"\(outputPath)"
],
inputFiles: [
$0,
],
outputFiles: [
outputPath
]
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import PackagePlugin

print("Hello from the Prebuild Plugin!")

let outputPaths: [Path] = targetBuildContext.inputFiles.filter{ $0.path.extension == "dat" }.map { file in
targetBuildContext.pluginWorkDirectory.appending(file.path.stem + ".swift")
}
@main
struct MyPlugin: BuildToolPlugin {

func createBuildCommands(context: TargetBuildContext) throws -> [Command] {
print("Hello from the Prebuild Plugin!")

if !outputPaths.isEmpty {
commandConstructor.addPrebuildCommand(
displayName:
"Running prebuild command for target \(targetBuildContext.targetName)",
executable:
Path("/usr/bin/touch"),
arguments:
outputPaths.map{ $0.string },
outputFilesDirectory:
targetBuildContext.pluginWorkDirectory
)
let outputPaths: [Path] = context.inputFiles.filter{ $0.path.extension == "dat" }.map { file in
context.pluginWorkDirectory.appending(file.path.stem + ".swift")
}
var commands: [Command] = []
if !outputPaths.isEmpty {
commands.append(.prebuildCommand(
displayName:
"Running prebuild command for target \(context.targetName)",
executable:
Path("/usr/bin/touch"),
arguments:
outputPaths.map{ $0.string },
outputFilesDirectory:
context.pluginWorkDirectory
))
}
return commands
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
import PackagePlugin
import Foundation

// Check that we can write to the output directory.
let allowedOutputPath = targetBuildContext.pluginWorkDirectory.appending("Foo")
if mkdir(allowedOutputPath.string, 0o777) != 0 {
throw StringError("unexpectedly could not write to '\(allowedOutputPath)': \(String(utf8String: strerror(errno)))")
}

// Check that we cannot write to the source directory.
let disallowedOutputPath = targetBuildContext.targetDirectory.appending("Bar")
if mkdir(disallowedOutputPath.string, 0o777) == 0 {
throw StringError("unexpectedly could write to '\(disallowedOutputPath)'")
}
@main
struct MyPlugin: BuildToolPlugin {

func createBuildCommands(context: TargetBuildContext) throws -> [Command] {

// Check that we can write to the output directory.
let allowedOutputPath = context.pluginWorkDirectory.appending("Foo")
if mkdir(allowedOutputPath.string, 0o777) != 0 {
throw StringError("unexpectedly could not write to '\(allowedOutputPath)': \(String(utf8String: strerror(errno)))")
}

// Check that we cannot write to the source directory.
let disallowedOutputPath = context.targetDirectory.appending("Bar")
if mkdir(disallowedOutputPath.string, 0o777) == 0 {
throw StringError("unexpectedly could write to '\(disallowedOutputPath)'")
}

return []
}

struct StringError: Error, CustomStringConvertible {
var error: String
init(_ error: String) {
self.error = error
struct StringError: Error, CustomStringConvertible {
var error: String
init(_ error: String) {
self.error = error
}
var description: String { error }
}
var description: String { error }
}
10 changes: 5 additions & 5 deletions Sources/PackagePlugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors

add_library(PackagePlugin
PublicAPI/CommandConstructor.swift
PublicAPI/DiagnosticsEmitter.swift
PublicAPI/Command.swift
PublicAPI/Context.swift
PublicAPI/Diagnostics.swift
PublicAPI/FileList.swift
PublicAPI/Globals.swift
PublicAPI/Path.swift
PublicAPI/TargetBuildContext.swift
ImplementationDetails.swift)
PublicAPI/Protocols.swift
Implementation.swift)

target_compile_options(PackagePlugin PUBLIC
$<$<COMPILE_LANGUAGE:Swift>:-package-description-version$<SEMICOLON>999.0>)
Expand Down
Loading