Skip to content

Commit

Permalink
Allow using an external task for running commands in mac-crafter run …
Browse files Browse the repository at this point in the history
…command

Signed-off-by: Claudio Cambra <[email protected]>
  • Loading branch information
claucambra committed Nov 21, 2024
1 parent 258ba6d commit 9008448
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions admin/osx/mac-crafter/Sources/Utils/Shell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,39 @@

import Foundation

var task: Process?
weak var globalTaskRef: Process?

@discardableResult
func run(
_ launchPath: String,
_ args: [String],
env: [String: String]? = nil,
quiet: Bool = false
quiet: Bool = false,
task: Process = Process()
) -> Int32 {
defer { task = nil }
task = Process()

globalTaskRef = task
signal(SIGINT) { _ in
task?.terminate() // Send terminate signal to the task
exit(0) // Exit the script after cleanup
globalTaskRef?.terminate() // Send terminate signal to the task
exit(0) // Exit the script after cleanup
}

task?.launchPath = launchPath
task?.arguments = args
task.launchPath = launchPath
task.arguments = args

if let env,
let combinedEnv = task?.environment?.merging(env, uniquingKeysWith: { (_, new) in new })
let combinedEnv = task.environment?.merging(env, uniquingKeysWith: { (_, new) in new })
{
task?.environment = combinedEnv
task.environment = combinedEnv
}

if quiet {
task?.standardOutput = nil
task?.standardError = nil
task.standardOutput = nil
task.standardError = nil
}

task?.launch()
task?.waitUntilExit()
return task?.terminationStatus ?? 1
task.launch()
task.waitUntilExit()
return task.terminationStatus
}

func run(
Expand Down

0 comments on commit 9008448

Please sign in to comment.