Skip to content

Commit

Permalink
Added plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
samdeane committed Sep 3, 2024
1 parent cc7f477 commit 0e43665
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
21 changes: 19 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ let package = Package(
products: [
.executable(name: "rt", targets: ["ReleaseTools"]),
.library(name: "Resources", targets: ["Resources"]),
.plugin(name: "ReleaseToolsPlugin", targets: ["ReleaseToolsPlugin"]),
],

dependencies: [
Expand All @@ -20,7 +21,6 @@ let package = Package(
.package(url: "https://github.com/elegantchaos/Logger.git", from: "1.6.0"),
.package(url: "https://github.com/elegantchaos/Runner.git", from: "2.0.5"),
.package(url: "https://github.com/elegantchaos/ChaosByteStreams", from: "1.0.0"),
.package(url: "https://github.com/elegantchaos/XCTestExtensions.git", from: "1.3.0"),
.package(url: "https://github.com/elegantchaos/Versionator.git", from: "2.0.2"),
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.0"),
],
Expand Down Expand Up @@ -52,9 +52,26 @@ let package = Package(
]
),

.plugin(
name: "ReleaseToolsPlugin",
capability: .command(
intent: .custom(
verb: "rt",
description: "Manages archiving and uploading releases."
),
permissions: [
.writeToPackageDirectory(reason: "Builds and archives releases.")
]
),

dependencies: [
"ReleaseTools"
]
),

.testTarget(
name: "ReleaseToolsTests",
dependencies: ["ReleaseTools", "XCTestExtensions"]
dependencies: ["ReleaseTools"]
),
]
)
44 changes: 44 additions & 0 deletions Plugins/ReleaseToolPlugin/ReleaseToolPlugin.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Created by Sam Deane on 03/09/24.
// All code (c) 2024 - present day, Elegant Chaos Limited.
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

import Foundation
import PackagePlugin

@main struct ReleaseToolsPlugin: CommandPlugin {
func run(tool: String, arguments: [String], context: PackagePlugin.PluginContext, cwd: URL) async throws -> String {
let tool = try context.tool(named: tool)

Diagnostics.remark("Running \(tool) \(arguments.joined(separator: " ")).")

let outputPipe = Pipe()
return try await withCheckedThrowingContinuation { continuation in
let process = Process()
process.executableURL = tool.url
process.arguments = arguments
process.currentDirectoryURL = cwd

process.standardOutput = outputPipe
process.terminationHandler = { process in
let data = outputPipe.fileHandleForReading.readDataToEndOfFile()
continuation.resume(
returning: String(decoding: data, as: UTF8.self)
)
}
do {
try process.run()
} catch {
continuation.resume(throwing: error)
}
}
}

func performCommand(context: PackagePlugin.PluginContext, arguments: [String]) async throws {
let output = try await run(
tool: "ActionBuilderTool", arguments: arguments, context: context, cwd: context.package.directoryURL)

Diagnostics.remark(output)

}
}

0 comments on commit 0e43665

Please sign in to comment.