|
5 | 5 | package dev.icerock.moko.resources.utils |
6 | 6 |
|
7 | 7 | import platform.Foundation.NSBundle |
| 8 | +import platform.Foundation.NSDirectoryEnumerator |
| 9 | +import platform.Foundation.NSFileManager |
| 10 | +import platform.Foundation.NSURL |
| 11 | +import platform.Foundation.pathExtension |
8 | 12 |
|
9 | 13 | fun NSBundle.Companion.loadableBundle(identifier: String): NSBundle { |
10 | 14 | // try get already loaded bundle |
11 | 15 | NSBundle.bundleWithIdentifier(identifier)?.let { return it } |
12 | 16 |
|
13 | | - // try load from app framework |
14 | | - NSBundle.mainBundle |
15 | | - .pathsForResourcesOfType(ext = "framework", inDirectory = "Frameworks") |
16 | | - .filterIsInstance<String>() |
17 | | - .mapNotNull { NSBundle.bundleWithPath(it) } |
18 | | - .flatMap { it.pathsForResourcesOfType(ext = "bundle", inDirectory = null) } |
19 | | - .filterIsInstance<String>() |
20 | | - // load each loadable bundle to correct load by identifier later |
21 | | - .forEach { NSBundle.bundleWithPath(it)?.bundleIdentifier } |
| 17 | + val bundlePath: String = NSBundle.mainBundle.bundlePath |
| 18 | + val enumerator: NSDirectoryEnumerator = requireNotNull(NSFileManager.defaultManager.enumeratorAtPath(bundlePath)) |
| 19 | + while (true) { |
| 20 | + val relativePath: String = enumerator.nextObject() as? String ?: break |
| 21 | + val url = NSURL(fileURLWithPath = relativePath) |
| 22 | + if (url.pathExtension == "bundle") { |
| 23 | + val fullPath = "$bundlePath/$relativePath" |
| 24 | + val loadedIdentifier: String? = NSBundle.bundleWithPath(fullPath)?.bundleIdentifier |
| 25 | + if (isBundleSearchLogEnabled) { |
| 26 | + println("moko-resources auto-load bundle with identifier $loadedIdentifier at path $fullPath") |
| 27 | + } |
| 28 | + } |
| 29 | + } |
22 | 30 |
|
23 | | - return NSBundle.bundleWithIdentifier(identifier)!! |
| 31 | + val resultBundle = NSBundle.bundleWithIdentifier(identifier) |
| 32 | + if (resultBundle == null) { |
| 33 | + throw IllegalArgumentException("bundle with identifier $identifier not found") |
| 34 | + } |
| 35 | + |
| 36 | + return resultBundle |
24 | 37 | } |
| 38 | + |
| 39 | +var isBundleSearchLogEnabled = false |
0 commit comments