Thibaut Schmitt 279f13dbd5
Some checks failed
gitea-openium/resgen.swift/pipeline/head There was a failure building this commit
Add SwiftLint HARD rules
2025-04-30 17:05:02 +02:00

49 lines
1.2 KiB
Swift

//
// Shell.swift
//
//
// Created by Thibaut Schmitt on 22/12/2021.
//
import Foundation
public enum Shell {
public static var environment: [String: String] {
ProcessInfo.processInfo.environment
}
@discardableResult
public static func shell(
launchPath: String = "/usr/bin/env",
_ args: [String]
) -> (terminationStatus: Int32, output: String?) {
#if os(macOS)
let task = Process()
task.launchPath = launchPath
task.arguments = args
var currentEnv = ProcessInfo.processInfo.environment
for (key, value) in environment {
currentEnv[key] = value
}
task.environment = currentEnv
let pipe = Pipe()
task.standardOutput = pipe
task.launch()
task.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
guard let output = String(data: data, encoding: .utf8) else {
return (terminationStatus: task.terminationStatus, output: nil)
}
return (terminationStatus: task.terminationStatus, output: output)
#else
fatalError("Shell is only available on Mac")
#endif
}
}