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

Create a swift bundler command line plugin. #36

Merged
merged 4 commits into from
Dec 10, 2023
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
6 changes: 3 additions & 3 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@
{
"identity" : "tomlkit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/LebJe/TOMLKit",
"location" : "https://github.com/furby-tm/TOMLKit",
"state" : {
"branch" : "0.5.5",
"revision" : "404c4dd011743461bff12d00a5118d0ed59d630c"
"revision" : "f2b3e80a19c7fb3d42e3cfda98076cef8f0d1bd7",
"version" : "0.5.6"
}
},
{
Expand Down
22 changes: 20 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ let package = Package(
name: "swift-bundler",
platforms: [.macOS(.v11)],
products: [
.executable(name: "swift-bundler", targets: ["swift-bundler"])
.executable(name: "swift-bundler", targets: ["swift-bundler"]),
.plugin(name: "SwiftBundlerCommandPlugin", targets: ["SwiftBundlerCommandPlugin"]),
],
dependencies: [
.package(url: "https://github.com/stackotter/swift-arg-parser", from: "1.2.3"),
.package(url: "https://github.com/apple/swift-log", from: "1.4.2"),
.package(url: "https://github.com/pointfreeco/swift-parsing", from: "0.7.1"),
.package(url: "https://github.com/LebJe/TOMLKit", branch: "0.5.5"),
.package(url: "https://github.com/furby-tm/TOMLKit", from: "0.5.6"),
.package(url: "https://github.com/onevcat/Rainbow", from: "3.0.0"),
.package(url: "https://github.com/mxcl/Version.git", from: "2.0.0"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
Expand Down Expand Up @@ -59,5 +60,22 @@ let package = Package(
path: "Documentation",
exclude: ["preview_docs.sh"]
),

.plugin(
name: "SwiftBundlerCommandPlugin",
capability: .command(
intent: .custom(
verb: "bundler",
description: "Run a package as an app."
),
permissions: [
.writeToPackageDirectory(
reason: "Creating an app bundle requires writing to the package directory.")
]
),
dependencies: [
.target(name: "swift-bundler")
]
),
]
)
32 changes: 32 additions & 0 deletions Plugins/SwiftBundlerCommandPlugin/SwiftBundlerCommandPlugin.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Foundation
import PackagePlugin

@main
struct SwiftBundlerCommandPlugin: CommandPlugin {
/// This entry point is called when operating on a Swift package.
func performCommand(context: PluginContext, arguments: [String]) throws {
let bundler = try context.tool(named: "swift-bundler")

try run(command: bundler.path, with: arguments)
}
}

extension SwiftBundlerCommandPlugin {
/// Run a command with the given arguments.
func run(command: Path, with arguments: [String]) throws {
let exec = URL(fileURLWithPath: command.string)

let process = try Process.run(exec, arguments: arguments)
process.waitUntilExit()

// Check whether the subprocess invocation was successful.
if process.terminationReason == .exit,
process.terminationStatus == 0
{
print("SwiftBundlerCommandPlugin successfully completed.")
} else {
let problem = "\(process.terminationReason):\(process.terminationStatus)"
Diagnostics.error("SwiftBundlerCommandPlugin failed: \(problem)")
}
}
}