ref: simplify readEntitlements() + unzip to tmp-dir shortcut
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
if let exe = bundleExecutable {
|
||||
switch meta.type {
|
||||
case .IPA:
|
||||
let tmpPath = NSTemporaryDirectory() + "/" + UUID().uuidString
|
||||
try! FileManager.default.createDirectory(atPath: tmpPath, withIntermediateDirectories: true)
|
||||
if let tmpPath = try? meta.zipFile!.unzipFileToTempDir("Payload/*.app/\(exe)") {
|
||||
defer {
|
||||
try? FileManager.default.removeItem(atPath: tmpPath)
|
||||
}
|
||||
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(forBinary: tmpPath + "/" + exe)
|
||||
}
|
||||
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`
|
||||
|
||||
@@ -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) {
|
||||
@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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user