|
| 1 | +import CryptoKit |
| 2 | +import Foundation |
| 3 | + |
| 4 | +enum WorktrunkInstallerError: LocalizedError { |
| 5 | + case unsupportedArchitecture |
| 6 | + case downloadFailed |
| 7 | + case integrityCheckFailed |
| 8 | + case extractFailed(stderr: String) |
| 9 | + case missingBinary(name: String) |
| 10 | + case installFailed(message: String) |
| 11 | + |
| 12 | + var errorDescription: String? { |
| 13 | + switch self { |
| 14 | + case .unsupportedArchitecture: |
| 15 | + return "Unsupported Mac architecture for Worktrunk installation." |
| 16 | + case .downloadFailed: |
| 17 | + return "Failed to download Worktrunk." |
| 18 | + case .integrityCheckFailed: |
| 19 | + return "Downloaded Worktrunk failed the integrity check." |
| 20 | + case .extractFailed(let stderr): |
| 21 | + if stderr.isEmpty { return "Failed to extract Worktrunk." } |
| 22 | + return "Failed to extract Worktrunk: \(stderr)" |
| 23 | + case .missingBinary(let name): |
| 24 | + return "Downloaded Worktrunk archive did not contain \(name)." |
| 25 | + case .installFailed(let message): |
| 26 | + return message |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +enum WorktrunkInstaller { |
| 32 | + private struct Release { |
| 33 | + static let version = "0.22.0" |
| 34 | + static let assetName = "worktrunk-aarch64-apple-darwin.tar.xz" |
| 35 | + static let sha256 = "1fd193d8ed95453dbeadd900035312a6df61ff3fad43dc85eb1a9f7b48895b3c" |
| 36 | + |
| 37 | + static var url: URL { |
| 38 | + URL(string: "https://github.com/max-sixty/worktrunk/releases/download/v\(version)/\(assetName)")! |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + static func installPinnedWorktrunkIfNeeded() async throws { |
| 43 | + guard isSupportedArchitecture() else { |
| 44 | + throw WorktrunkInstallerError.unsupportedArchitecture |
| 45 | + } |
| 46 | + |
| 47 | + let binDir = AgentStatusPaths.binDir |
| 48 | + let wtDest = binDir.appendingPathComponent("wt", isDirectory: false) |
| 49 | + let gitWtDest = binDir.appendingPathComponent("git-wt", isDirectory: false) |
| 50 | + if FileManager.default.isExecutableFile(atPath: wtDest.path), |
| 51 | + FileManager.default.isExecutableFile(atPath: gitWtDest.path) { |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + try FileManager.default.createDirectory(at: binDir, withIntermediateDirectories: true) |
| 56 | + |
| 57 | + let tmpDir = FileManager.default.temporaryDirectory |
| 58 | + .appendingPathComponent("dev.sidequery.Ghostree", isDirectory: true) |
| 59 | + .appendingPathComponent("worktrunk-install-\(UUID().uuidString)", isDirectory: true) |
| 60 | + try FileManager.default.createDirectory(at: tmpDir, withIntermediateDirectories: true) |
| 61 | + defer { try? FileManager.default.removeItem(at: tmpDir) } |
| 62 | + |
| 63 | + let archiveURL = tmpDir.appendingPathComponent(Release.assetName, isDirectory: false) |
| 64 | + |
| 65 | + do { |
| 66 | + let (downloaded, _) = try await URLSession.shared.download(from: Release.url) |
| 67 | + try FileManager.default.moveItem(at: downloaded, to: archiveURL) |
| 68 | + } catch { |
| 69 | + throw WorktrunkInstallerError.downloadFailed |
| 70 | + } |
| 71 | + |
| 72 | + let actual = try sha256Hex(url: archiveURL) |
| 73 | + guard actual == Release.sha256 else { |
| 74 | + throw WorktrunkInstallerError.integrityCheckFailed |
| 75 | + } |
| 76 | + |
| 77 | + let extractDir = tmpDir.appendingPathComponent("extract", isDirectory: true) |
| 78 | + try FileManager.default.createDirectory(at: extractDir, withIntermediateDirectories: true) |
| 79 | + try extractTarXz(archiveURL: archiveURL, to: extractDir) |
| 80 | + |
| 81 | + let root = extractDir.appendingPathComponent("worktrunk-aarch64-apple-darwin", isDirectory: true) |
| 82 | + let wtSource = root.appendingPathComponent("wt", isDirectory: false) |
| 83 | + let gitWtSource = root.appendingPathComponent("git-wt", isDirectory: false) |
| 84 | + |
| 85 | + guard FileManager.default.fileExists(atPath: wtSource.path) else { |
| 86 | + throw WorktrunkInstallerError.missingBinary(name: "wt") |
| 87 | + } |
| 88 | + guard FileManager.default.fileExists(atPath: gitWtSource.path) else { |
| 89 | + throw WorktrunkInstallerError.missingBinary(name: "git-wt") |
| 90 | + } |
| 91 | + |
| 92 | + try replaceFile(source: wtSource, dest: wtDest) |
| 93 | + try replaceFile(source: gitWtSource, dest: gitWtDest) |
| 94 | + |
| 95 | + try makeExecutable(url: wtDest) |
| 96 | + try makeExecutable(url: gitWtDest) |
| 97 | + _ = try? removeQuarantine(url: wtDest) |
| 98 | + _ = try? removeQuarantine(url: gitWtDest) |
| 99 | + } |
| 100 | + |
| 101 | + private static func isSupportedArchitecture() -> Bool { |
| 102 | + #if arch(arm64) |
| 103 | + return true |
| 104 | + #else |
| 105 | + return false |
| 106 | + #endif |
| 107 | + } |
| 108 | + |
| 109 | + private static func sha256Hex(url: URL) throws -> String { |
| 110 | + let data = try Data(contentsOf: url) |
| 111 | + let digest = SHA256.hash(data: data) |
| 112 | + return digest.map { String(format: "%02x", $0) }.joined() |
| 113 | + } |
| 114 | + |
| 115 | + private static func extractTarXz(archiveURL: URL, to dir: URL) throws { |
| 116 | + let (exitCode, stderr) = try runProcess( |
| 117 | + executable: URL(fileURLWithPath: "/usr/bin/tar"), |
| 118 | + args: ["-xJf", archiveURL.path, "-C", dir.path] |
| 119 | + ) |
| 120 | + guard exitCode == 0 else { |
| 121 | + throw WorktrunkInstallerError.extractFailed(stderr: stderr) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private static func replaceFile(source: URL, dest: URL) throws { |
| 126 | + let fm = FileManager.default |
| 127 | + if fm.fileExists(atPath: dest.path) { |
| 128 | + try fm.removeItem(at: dest) |
| 129 | + } |
| 130 | + do { |
| 131 | + try fm.copyItem(at: source, to: dest) |
| 132 | + } catch { |
| 133 | + throw WorktrunkInstallerError.installFailed(message: "Failed to install Worktrunk to \(dest.path).") |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private static func makeExecutable(url: URL) throws { |
| 138 | + try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: url.path) |
| 139 | + } |
| 140 | + |
| 141 | + private static func removeQuarantine(url: URL) throws { |
| 142 | + _ = try runProcess( |
| 143 | + executable: URL(fileURLWithPath: "/usr/bin/xattr"), |
| 144 | + args: ["-d", "com.apple.quarantine", url.path] |
| 145 | + ) |
| 146 | + } |
| 147 | + |
| 148 | + private static func runProcess(executable: URL, args: [String]) throws -> (Int32, String) { |
| 149 | + let process = Process() |
| 150 | + process.executableURL = executable |
| 151 | + process.arguments = args |
| 152 | + |
| 153 | + let stdinPipe = Pipe() |
| 154 | + stdinPipe.fileHandleForWriting.closeFile() |
| 155 | + process.standardInput = stdinPipe |
| 156 | + |
| 157 | + let stderrPipe = Pipe() |
| 158 | + process.standardError = stderrPipe |
| 159 | + process.standardOutput = FileHandle.nullDevice |
| 160 | + |
| 161 | + try process.run() |
| 162 | + let stderrData = stderrPipe.fileHandleForReading.readDataToEndOfFile() |
| 163 | + process.waitUntilExit() |
| 164 | + |
| 165 | + let stderr = String(data: stderrData, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "" |
| 166 | + return (process.terminationStatus, stderr) |
| 167 | + } |
| 168 | +} |
| 169 | + |
0 commit comments