ref: simplify readEntitlements() + unzip to tmp-dir shortcut

This commit is contained in:
relikd
2025-11-29 01:59:38 +01:00
parent 3a16277867
commit 035276dcfc
2 changed files with 31 additions and 23 deletions

View File

@@ -3,25 +3,22 @@ import Foundation
extension PreviewGenerator { extension PreviewGenerator {
/// Search for app binary and run `codesign` on it. /// Search for app binary and run `codesign` on it.
private func readEntitlements(_ meta: MetaInfo, _ bundleExecutable: String?) -> Entitlements { private func readEntitlements(_ meta: MetaInfo, _ bundleExecutable: String?) -> Entitlements {
guard let bundleExecutable else { if let exe = bundleExecutable {
return Entitlements.withoutBinary()
}
switch meta.type { switch meta.type {
case .IPA: case .IPA:
let tmpPath = NSTemporaryDirectory() + "/" + UUID().uuidString if let tmpPath = try? meta.zipFile!.unzipFileToTempDir("Payload/*.app/\(exe)") {
try! FileManager.default.createDirectory(atPath: tmpPath, withIntermediateDirectories: true)
defer { defer {
try? FileManager.default.removeItem(atPath: tmpPath) try? FileManager.default.removeItem(atPath: tmpPath)
} }
try! meta.zipFile!.unzipFile("Payload/*.app/\(bundleExecutable)", toDir: tmpPath) return Entitlements(forBinary: tmpPath + "/" + exe)
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()
} }
case .Archive, .Extension:
return Entitlements(forBinary: meta.effectiveUrl("MacOS", exe).path)
case .APK:
break // not applicable for Android
}
}
return Entitlements.withoutBinary()
} }
/// Process compiled binary and provision plist to extract `Entitlements` /// Process compiled binary and provision plist to extract `Entitlements`

View File

@@ -354,12 +354,23 @@ struct ZipFile {
/// Unzip file to filesystem. /// Unzip file to filesystem.
/// @param filePath File path inside zip file. /// @param filePath File path inside zip file.
/// @param targetDir Directory in which to unzip the file. /// @param targetDir Directory in which to unzip the file.
func unzipFile(_ filePath: String, toDir targetDir: String) throws { @discardableResult
if let data = self.unzipFile(filePath) { 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 filename = filePath.components(separatedBy: "/").last!
let outputPath = targetDir.appending("/" + filename) let outputPath = targetDir.appending("/" + filename)
os_log(.debug, log: log, "[unzip] write to %{public}@", outputPath) os_log(.debug, log: log, "[unzip] write to %{public}@", outputPath)
try data.write(to: URL(fileURLWithPath: outputPath), options: .atomic) 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)
} }
} }