From 035276dcfc91aa3b347d33e66faaf117cbf6ff60 Mon Sep 17 00:00:00 2001 From: relikd Date: Sat, 29 Nov 2025 01:59:38 +0100 Subject: [PATCH] ref: simplify readEntitlements() + unzip to tmp-dir shortcut --- src/Preview+Entitlements.swift | 31 ++++++++++++++----------------- src/Zip.swift | 23 +++++++++++++++++------ 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/Preview+Entitlements.swift b/src/Preview+Entitlements.swift index cd3f39b..14851ee 100644 --- a/src/Preview+Entitlements.swift +++ b/src/Preview+Entitlements.swift @@ -3,25 +3,22 @@ import Foundation extension PreviewGenerator { /// Search for app binary and run `codesign` on it. private func readEntitlements(_ meta: MetaInfo, _ bundleExecutable: String?) -> Entitlements { - guard let bundleExecutable else { - return Entitlements.withoutBinary() - } - - switch meta.type { - case .IPA: - let tmpPath = NSTemporaryDirectory() + "/" + UUID().uuidString - try! FileManager.default.createDirectory(atPath: tmpPath, withIntermediateDirectories: true) - defer { - try? FileManager.default.removeItem(atPath: tmpPath) + if let exe = bundleExecutable { + switch meta.type { + case .IPA: + if let tmpPath = try? meta.zipFile!.unzipFileToTempDir("Payload/*.app/\(exe)") { + defer { + try? FileManager.default.removeItem(atPath: tmpPath) + } + return Entitlements(forBinary: tmpPath + "/" + exe) + } + case .Archive, .Extension: + return Entitlements(forBinary: meta.effectiveUrl("MacOS", exe).path) + case .APK: + break // not applicable for Android } - try! meta.zipFile!.unzipFile("Payload/*.app/\(bundleExecutable)", toDir: tmpPath) - return Entitlements(forBinary: tmpPath + "/" + bundleExecutable) - case .Archive, .Extension: - return Entitlements(forBinary: meta.effectiveUrl("MacOS", bundleExecutable).path) - case .APK: - // not applicable for Android - return Entitlements.withoutBinary() } + return Entitlements.withoutBinary() } /// Process compiled binary and provision plist to extract `Entitlements` diff --git a/src/Zip.swift b/src/Zip.swift index 11f1729..d358f30 100644 --- a/src/Zip.swift +++ b/src/Zip.swift @@ -354,12 +354,23 @@ struct ZipFile { /// Unzip file to filesystem. /// @param filePath File path inside zip file. /// @param targetDir Directory in which to unzip the file. - func unzipFile(_ filePath: String, toDir targetDir: String) throws { - if let data = self.unzipFile(filePath) { - let filename = filePath.components(separatedBy: "/").last! - let outputPath = targetDir.appending("/" + filename) - os_log(.debug, log: log, "[unzip] write to %{public}@", outputPath) - try data.write(to: URL(fileURLWithPath: outputPath), options: .atomic) + @discardableResult + func unzipFile(_ filePath: String, toDir targetDir: String) throws -> String? { + guard let data = self.unzipFile(filePath) else { + return nil } + let filename = filePath.components(separatedBy: "/").last! + let outputPath = targetDir.appending("/" + filename) + os_log(.debug, log: log, "[unzip] write to %{public}@", outputPath) + try data.write(to: URL(fileURLWithPath: outputPath), options: .atomic) + return outputPath + } + + /// Extract selected `filePath` inside zip to a new temporary directory and return path to that file. + /// @return Path to extracted data. Returns `nil` or throws exception if data could not be extracted. + func unzipFileToTempDir(_ filePath: String) throws -> String? { + let tmpPath = NSTemporaryDirectory() + "/" + UUID().uuidString + try! FileManager.default.createDirectory(atPath: tmpPath, withIntermediateDirectories: true) + return try unzipFile(filePath, toDir: tmpPath) } }