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 1 commit
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
20 changes: 19 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ 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"),
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")
]
),
]
)
45 changes: 45 additions & 0 deletions Plugins/SwiftBundlerCommandPlugin/SwiftBundlerCommandPlugin.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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)
}
}

#if canImport(XcodeProjectPlugin)
import XcodeProjectPlugin

extension SwiftBundlerCommandPlugin: XcodeCommandPlugin {
/// This entry point is called when operating on an Xcode project.
func performCommand(context: XcodePluginContext, arguments: [String]) throws {
let bundler = try context.tool(named: "swift-bundler")

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

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 succesfully completed.")
} else {
let problem = "\(process.terminationReason):\(process.terminationStatus)"
Diagnostics.error("SwiftBundlerCommandPlugin failed: \(problem)")
}
}
}