|
| 1 | +import Foundation |
| 2 | + |
| 3 | +protocol AppUpdateInstalling: Sendable { |
| 4 | + func install(update: AppUpdateRelease) async throws |
| 5 | +} |
| 6 | + |
| 7 | +struct SourceArchiveAppUpdateInstaller: AppUpdateInstalling { |
| 8 | + var session: URLSession = .shared |
| 9 | + |
| 10 | + func install(update: AppUpdateRelease) async throws { |
| 11 | + guard let archiveURL = update.sourceArchiveURL else { |
| 12 | + throw AppUpdateInstallError.missingArchive |
| 13 | + } |
| 14 | + |
| 15 | + let fileManager = FileManager.default |
| 16 | + let workDirectory = fileManager.temporaryDirectory |
| 17 | + .appendingPathComponent("agentic-secrets-update-\(UUID().uuidString)", isDirectory: true) |
| 18 | + let archive = workDirectory.appendingPathComponent("source.zip") |
| 19 | + let extractDirectory = workDirectory.appendingPathComponent("source", isDirectory: true) |
| 20 | + let logURL = workDirectory.appendingPathComponent("install.log") |
| 21 | + |
| 22 | + try fileManager.createDirectory(at: extractDirectory, withIntermediateDirectories: true) |
| 23 | + do { |
| 24 | + let (downloadedURL, response) = try await session.download(from: archiveURL) |
| 25 | + if let http = response as? HTTPURLResponse, !(200..<300).contains(http.statusCode) { |
| 26 | + throw AppUpdateInstallError.downloadHTTPStatus(http.statusCode) |
| 27 | + } |
| 28 | + try fileManager.moveItem(at: downloadedURL, to: archive) |
| 29 | + try await runProcess( |
| 30 | + executable: URL(fileURLWithPath: "/usr/bin/ditto"), |
| 31 | + arguments: ["-x", "-k", archive.path, extractDirectory.path], |
| 32 | + currentDirectory: nil, |
| 33 | + logURL: logURL |
| 34 | + ) |
| 35 | + let sourceRoot = try findSourceRoot(in: extractDirectory) |
| 36 | + try validateVersion(update: update, sourceRoot: sourceRoot) |
| 37 | + try await runProcess( |
| 38 | + executable: sourceRoot.appendingPathComponent("scripts/install_local.sh"), |
| 39 | + arguments: ["--load", "--cleanup-path", workDirectory.path], |
| 40 | + currentDirectory: sourceRoot, |
| 41 | + logURL: logURL |
| 42 | + ) |
| 43 | + try? fileManager.removeItem(at: workDirectory) |
| 44 | + } catch { |
| 45 | + throw AppUpdateInstallError.failed(error, logURL: logURL) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private func findSourceRoot(in directory: URL) throws -> URL { |
| 50 | + let fileManager = FileManager.default |
| 51 | + let contents = try fileManager.contentsOfDirectory( |
| 52 | + at: directory, |
| 53 | + includingPropertiesForKeys: [.isDirectoryKey], |
| 54 | + options: [.skipsHiddenFiles] |
| 55 | + ) |
| 56 | + for candidate in contents { |
| 57 | + let script = candidate.appendingPathComponent("scripts/install_local.sh") |
| 58 | + let version = candidate.appendingPathComponent("version.env") |
| 59 | + if fileManager.isExecutableFile(atPath: script.path), |
| 60 | + fileManager.fileExists(atPath: version.path) { |
| 61 | + return candidate |
| 62 | + } |
| 63 | + } |
| 64 | + throw AppUpdateInstallError.sourceRootNotFound |
| 65 | + } |
| 66 | + |
| 67 | + private func validateVersion(update: AppUpdateRelease, sourceRoot: URL) throws { |
| 68 | + let versionFile = sourceRoot.appendingPathComponent("version.env") |
| 69 | + let text = try String(contentsOf: versionFile, encoding: .utf8) |
| 70 | + let expected = update.versionLabel |
| 71 | + guard text.components(separatedBy: .newlines).contains("MARKETING_VERSION=\(expected)") else { |
| 72 | + throw AppUpdateInstallError.versionMismatch(expected: expected) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private func runProcess(executable: URL, arguments: [String], currentDirectory: URL?, logURL: URL) async throws { |
| 77 | + try await Task.detached(priority: .userInitiated) { |
| 78 | + FileManager.default.createFile(atPath: logURL.path, contents: nil) |
| 79 | + let logHandle = try FileHandle(forWritingTo: logURL) |
| 80 | + defer { try? logHandle.close() } |
| 81 | + |
| 82 | + let process = Process() |
| 83 | + process.executableURL = executable |
| 84 | + process.arguments = arguments |
| 85 | + process.currentDirectoryURL = currentDirectory |
| 86 | + process.standardOutput = logHandle |
| 87 | + process.standardError = logHandle |
| 88 | + try process.run() |
| 89 | + process.waitUntilExit() |
| 90 | + guard process.terminationReason == .exit, process.terminationStatus == 0 else { |
| 91 | + throw AppUpdateInstallError.processFailed( |
| 92 | + executable.lastPathComponent, |
| 93 | + status: process.terminationStatus |
| 94 | + ) |
| 95 | + } |
| 96 | + }.value |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +enum AppUpdateInstallError: Error, CustomStringConvertible { |
| 101 | + case missingArchive |
| 102 | + case downloadHTTPStatus(Int) |
| 103 | + case sourceRootNotFound |
| 104 | + case versionMismatch(expected: String) |
| 105 | + case processFailed(String, status: Int32) |
| 106 | + case failed(Error, logURL: URL) |
| 107 | + |
| 108 | + var description: String { |
| 109 | + switch self { |
| 110 | + case .missingArchive: |
| 111 | + "This release does not expose a downloadable source archive." |
| 112 | + case .downloadHTTPStatus(let status): |
| 113 | + "Update download failed with HTTP \(status)." |
| 114 | + case .sourceRootNotFound: |
| 115 | + "Downloaded archive does not contain the Agentic Secrets installer." |
| 116 | + case .versionMismatch(let expected): |
| 117 | + "Downloaded source does not match expected version \(expected)." |
| 118 | + case .processFailed(let command, let status): |
| 119 | + "\(command) exited with status \(status)." |
| 120 | + case .failed(let error, let logURL): |
| 121 | + "\(error). Installer log: \(logURL.path)" |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments