feat(ios/engine): temp package cleanup

This commit is contained in:
jahorton 2020-06-15 09:27:25 +07:00
parent bfd90f3280
commit 9bc385b8a4
4 changed files with 28 additions and 6 deletions

View file

@ -26,6 +26,8 @@ public class KeymanPackage {
public let sourceFolder: URL
public let id: String
internal let metadata: KMPMetadata
// Used to denote Packages pending installation; referenced by ResourceFileManager.
internal let isTemp: Bool
internal init(metadata: KMPMetadata, folder: URL) {

View file

@ -518,12 +518,7 @@ public enum Migrations {
allLocalPackages = kmpFiles.compactMap { file in
let filePath = cachedKMPsDirectory.appendingPathComponent(file)
do {
return try ResourceFileManager.shared.prepareKMPInstall(from: filePath)
} catch {
log.error("Error occurred when processing existing packages during cloud -> KMP migration: \(String(describing: error))")
return nil as KeymanPackage?
}
return ResourceFileManager.shared.getPackageInfo(for: filePath)
}
} catch {
log.error("Could not check contents of Documents directory for resource-migration assist")

View file

@ -55,6 +55,14 @@ public class PackageInstallViewController: UIViewController {
@objc func cancelBtnHandler() {
dismiss(animated: true, completion: nil)
// Note: package.sourceFolder is a temporary directory, as set by preparePackageInstall.
do {
try FileManager.default.removeItem(at: package.sourceFolder)
} catch {
// Our attempt to clean up the temporarily-extracted contents failed.
// At least we extracted to a temp (cache) folder; we'll let iOS
// handle it, then.
}
}
@objc func installBtnHandler() {

View file

@ -138,6 +138,23 @@ public class ResourceFileManager {
}
}
/**
* Similar to `preparePackageInstall`, but the resuting `KeymanPackage` cannot be used for installation. Use when you
* want information about a package's contents when not immediately looking to install its resources.
*/
public func getPackageInfo(for url: URL) -> KeymanPackage? {
// Facilitates clean retrieval of a package's metadata by temporarily extracting
// its contents just long enough to parse the kmp.json.
do {
let package = try self.prepareKMPInstall(from: url)
try FileManager.default.removeItem(at: package.sourceFolder)
return package
} catch {
log.error("Error occurred attempting to extract metadata for KMP at \(String(describing: url)): \(String(describing: error))")
return nil
}
}
public func promptPackageInstall(of package: KeymanPackage,
in rootVC: UIViewController,
isCustom: Bool,