2023-02-10 14:04:00 +01:00
|
|
|
import ArgumentParser
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
enum Utilities {
|
|
|
|
enum Error: LocalizedError {
|
|
|
|
case scriptFailed(command: String, path: String)
|
|
|
|
|
|
|
|
var errorDescription: String? {
|
|
|
|
switch self {
|
|
|
|
case let .scriptFailed(command, path):
|
|
|
|
return "command \(command) failed in path: \(path)"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-05 19:36:51 +02:00
|
|
|
static var projectDirectoryURL: URL { URL(fileURLWithPath: FileManager.default.currentDirectoryPath) }
|
2023-05-30 09:48:55 +02:00
|
|
|
static var parentDirectoryURL: URL { Utilities.projectDirectoryURL.deletingLastPathComponent() }
|
|
|
|
static var sdkDirectoryURL: URL { parentDirectoryURL.appendingPathComponent("matrix-rust-sdk") }
|
2023-02-10 14:04:00 +01:00
|
|
|
|
|
|
|
/// Runs a command in zsh.
|
|
|
|
@discardableResult
|
|
|
|
static func zsh(_ command: String, workingDirectoryURL: URL = projectDirectoryURL) throws -> String? {
|
|
|
|
let process = Process()
|
2023-04-05 19:36:51 +02:00
|
|
|
process.executableURL = URL(fileURLWithPath: "/bin/zsh")
|
2023-03-30 18:17:32 +01:00
|
|
|
process.arguments = ["-cu", command]
|
2023-02-10 14:04:00 +01:00
|
|
|
process.currentDirectoryURL = workingDirectoryURL
|
|
|
|
|
|
|
|
let outputPipe = Pipe()
|
|
|
|
process.standardOutput = outputPipe
|
|
|
|
|
|
|
|
try process.run()
|
|
|
|
process.waitUntilExit()
|
|
|
|
|
|
|
|
guard process.terminationReason == .exit, process.terminationStatus == 0 else { throw Error.scriptFailed(command: command, path: workingDirectoryURL.absoluteString) }
|
|
|
|
|
|
|
|
guard let outputData = try outputPipe.fileHandleForReading.readToEnd() else { return nil }
|
|
|
|
return String(data: outputData, encoding: .utf8)
|
|
|
|
}
|
|
|
|
}
|