Skip to content

Commit

Permalink
Merge pull request #103 from icerockdev/#97-bundle-not-available
Browse files Browse the repository at this point in the history
#97 bundle not available
  • Loading branch information
Alex009 authored Aug 29, 2020
2 parents 2f85d3b + e5bd0f2 commit 979a23e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 35 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ This is a Kotlin MultiPlatform library that provides access to the resources on
- 0.10.0
- 0.10.1
- 0.11.0
- 0.11.1

## Installation
root build.gradle
Expand All @@ -57,7 +58,7 @@ buildscript {
}
dependencies {
classpath "dev.icerock.moko:resources-generator:0.11.0"
classpath "dev.icerock.moko:resources-generator:0.11.1"
}
}
Expand All @@ -74,12 +75,12 @@ project build.gradle
apply plugin: "dev.icerock.mobile.multiplatform-resources"
dependencies {
commonMainApi("dev.icerock.moko:resources:0.11.0")
commonMainApi("dev.icerock.moko:resources:0.11.1")
}
multiplatformResources {
multiplatformResourcesPackage = "org.example.library"
iosBaseLocalizationRegion = "en" //optional, default "en"
multiplatformResourcesPackage = "org.example.library" // required
iosBaseLocalizationRegion = "en" // optional, default "en"
multiplatformResourcesSourceSet = "commonClientMain" // optional, default "commonMain"
}
```
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object Versions {
const val kotlin = "1.3.72"
const val detekt = "1.7.4"

private const val mokoResources = "0.11.0"
private const val mokoResources = "0.11.1"

object Plugins {
const val android = "3.6.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,27 +132,27 @@ class IosMRGenerator(
private fun setupFrameworkResources() {
val kotlinNativeTarget = compilation.target as KotlinNativeTarget

val frameworkBinaries: List<Framework> = kotlinNativeTarget.binaries
.filterIsInstance<Framework>()
.filter { it.compilation == compilation }

frameworkBinaries.forEach { framework ->
val linkTask = framework.linkTask

linkTask.doLast {
linkTask.libraries
.plus(linkTask.intermediateLibrary.get())
.filter { it.extension == "klib" }
.forEach {
project.logger.info("copy resources from $it")
val klibKonan = org.jetbrains.kotlin.konan.file.File(it.path)
val klib = KotlinLibraryLayoutImpl(klibKonan)
val layout = klib.extractingToTemp

File(layout.resourcesDir.path).copyRecursively(framework.outputFile, overwrite = true)
}
kotlinNativeTarget.binaries
.matching { it is Framework && it.compilation == compilation }
.configureEach {
val framework = this as Framework

val linkTask = framework.linkTask

linkTask.doLast {
linkTask.libraries
.plus(linkTask.intermediateLibrary.get())
.filter { it.extension == "klib" }
.forEach {
project.logger.info("copy resources from $it")
val klibKonan = org.jetbrains.kotlin.konan.file.File(it.path)
val klib = KotlinLibraryLayoutImpl(klibKonan)
val layout = klib.extractingToTemp

File(layout.resourcesDir.path).copyRecursively(framework.outputFile, overwrite = true)
}
}
}
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,35 @@
package dev.icerock.moko.resources.utils

import platform.Foundation.NSBundle
import platform.Foundation.NSDirectoryEnumerator
import platform.Foundation.NSFileManager
import platform.Foundation.NSURL
import platform.Foundation.pathExtension

fun NSBundle.Companion.loadableBundle(identifier: String): NSBundle {
// try get already loaded bundle
NSBundle.bundleWithIdentifier(identifier)?.let { return it }

// try load from app framework
NSBundle.mainBundle
.pathsForResourcesOfType(ext = "framework", inDirectory = "Frameworks")
.filterIsInstance<String>()
.mapNotNull { NSBundle.bundleWithPath(it) }
.flatMap { it.pathsForResourcesOfType(ext = "bundle", inDirectory = null) }
.filterIsInstance<String>()
// load each loadable bundle to correct load by identifier later
.forEach { NSBundle.bundleWithPath(it)?.bundleIdentifier }
val bundlePath: String = NSBundle.mainBundle.bundlePath
val enumerator: NSDirectoryEnumerator = requireNotNull(NSFileManager.defaultManager.enumeratorAtPath(bundlePath))
while (true) {
val relativePath: String = enumerator.nextObject() as? String ?: break
val url = NSURL(fileURLWithPath = relativePath)
if (url.pathExtension == "bundle") {
val fullPath = "$bundlePath/$relativePath"
val loadedIdentifier: String? = NSBundle.bundleWithPath(fullPath)?.bundleIdentifier
if (isBundleSearchLogEnabled) {
println("moko-resources auto-load bundle with identifier $loadedIdentifier at path $fullPath")
}
}
}

return NSBundle.bundleWithIdentifier(identifier)!!
val resultBundle = NSBundle.bundleWithIdentifier(identifier)
if (resultBundle == null) {
throw IllegalArgumentException("bundle with identifier $identifier not found")
}

return resultBundle
}

var isBundleSearchLogEnabled = false

0 comments on commit 979a23e

Please sign in to comment.