fix(ios/engine): migration of preload-sourced resources

This commit is contained in:
jahorton 2020-06-17 08:35:35 +07:00
parent 2361fa1871
commit e0f2e512d4
3 changed files with 52 additions and 2 deletions

View file

@ -344,7 +344,24 @@ public class Manager: NSObject, UIGestureRecognizerDelegate {
/// The keyboard must be downloaded (see `downloadKeyboard()`) or preloaded (see `preloadLanguageFile()`)
@available(*, deprecated, message: "Deprecated in favor of ResourceFileManager.install(resourceWithID:from:)")
public func addKeyboard(_ keyboard: InstallableKeyboard) {
ResourceFileManager.shared.addResource(keyboard)
var kbdToInstall = keyboard
// 3rd party installation of keyboards (13.0 and before):
// - Manager.shared.preloadFiles was called first to import the file resources
// - Then Manager.shared.addKeyboard installs the keyboard.
//
// Since 3rd-party uses never provided kmp.json files, we need to instant-migrate
// them here.
if !Migrations.resourceHasPackageMetadata(keyboard) {
let wrappedKbds = Migrations.migrateToKMPFormat([keyboard])
guard wrappedKbds.count == 1 else {
log.error("Could not properly import keyboard")
return
}
kbdToInstall = wrappedKbds[0]
} else {
ResourceFileManager.shared.addResource(kbdToInstall)
}
}
@ -391,7 +408,24 @@ public class Manager: NSObject, UIGestureRecognizerDelegate {
*/
@available(*, deprecated, message: "Deprecated in favor of ResourceFileManager.install(resourceWithID:from:)")
static public func addLexicalModel(_ lexicalModel: InstallableLexicalModel) {
ResourceFileManager.shared.addResource(lexicalModel)
var modelToInstall = lexicalModel
// Potential 3rd party installation of lexical models (12.0, 13.0):
// - Manager.shared.preloadFiles was called first to import the file resources
// - Then Manager.addLexicalModel installs the lexical model.
//
// Since 3rd-party uses never provided kmp.json files, we need to instant-migrate
// them here.
if !Migrations.resourceHasPackageMetadata(lexicalModel) {
let wrappedModels = Migrations.migrateToKMPFormat([lexicalModel])
guard wrappedModels.count == 1 else {
log.error("Could not properly import lexical model")
return
}
modelToInstall = wrappedModels[0]
} else {
ResourceFileManager.shared.addResource(modelToInstall)
}
}
/// Removes a keyboard from the list in the keyboard picker if it exists.

View file

@ -698,4 +698,10 @@ public enum Migrations {
return matched + mappedResources
}
internal static func resourceHasPackageMetadata<Resource: LanguageResource>(_ resource: Resource) -> Bool {
var resourceDir = Storage.active.resourceDir(for: resource)!
resourceDir.appendPathComponent("kmp.json")
return FileManager.default.fileExists(atPath: resourceDir.path)
}
}

View file

@ -217,6 +217,12 @@ public class ResourceFileManager {
}
}
/**
* Searches the specified package for a language resource with the indicated resource-language-code "full ID" key,
* importing the package's files and installing the indicated resource-language pairing upon success.
*
* The`resourcesWithIDs:` variant is better optimized for installing multiple resources from the same package.
*/
public func install<ResourceType: LanguageResource,
PackageType: TypedKeymanPackage<ResourceType>> (
resourceWithID fullID: ResourceType.FullID,
@ -225,6 +231,10 @@ public class ResourceFileManager {
try install(resourcesWithIDs: [fullID], from: package)
}
/**
* Searches the specified package for language resources with the indicated resource-language-code "full ID" keys
* importing the package's files and installing the indicated resource-language pairings upon success.
*/
public func install<Resource: LanguageResource,
Package: TypedKeymanPackage<Resource>> (
resourcesWithIDs fullIDs: [Resource.FullID], from package: Package) throws {