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 {
/// 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`

View File

@@ -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)
}
}