Skip to content

Commit

Permalink
Merge pull request #789 from icerockdev/#768-unpacked-klibs-support
Browse files Browse the repository at this point in the history
#768 unpacked klibs support
  • Loading branch information
Alex009 authored Dec 8, 2024
2 parents ddbf5ed + 9bac033 commit 0b16b7a
Show file tree
Hide file tree
Showing 6 changed files with 314 additions and 502 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ buildscript {
}
dependencies {
classpath "dev.icerock.moko:resources-generator:0.24.3"
classpath "dev.icerock.moko:resources-generator:0.24.4"
}
}
Expand All @@ -82,10 +82,10 @@ project build.gradle
apply plugin: "dev.icerock.mobile.multiplatform-resources"
dependencies {
commonMainApi("dev.icerock.moko:resources:0.24.3")
commonMainApi("dev.icerock.moko:resources-compose:0.24.3") // for compose multiplatform
commonMainApi("dev.icerock.moko:resources:0.24.4")
commonMainApi("dev.icerock.moko:resources-compose:0.24.4") // for compose multiplatform
commonTestImplementation("dev.icerock.moko:resources-test:0.24.3")
commonTestImplementation("dev.icerock.moko:resources-test:0.24.4")
}
multiplatformResources {
Expand Down Expand Up @@ -132,7 +132,7 @@ should [add `export` declarations](https://kotlinlang.org/docs/multiplatform-bui

```
framework {
export("dev.icerock.moko:resources:0.24.3")
export("dev.icerock.moko:resources:0.24.4")
export("dev.icerock.moko:graphics:0.9.0") // toUIColor here
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package dev.icerock.gradle.actions.apple
import dev.icerock.gradle.utils.klibs
import org.gradle.api.Action
import org.gradle.api.Task
import org.gradle.api.logging.Logger
import org.jetbrains.kotlin.gradle.tasks.KotlinNativeLink
import org.jetbrains.kotlin.library.KotlinLibraryLayout
import org.jetbrains.kotlin.library.impl.KotlinLibraryLayoutImpl
Expand All @@ -18,25 +19,50 @@ internal abstract class CopyResourcesFromKLibsAction : Action<Task> {
linkTask: KotlinNativeLink,
outputDir: File
) {
linkTask.klibs
val packedKlibs: List<File> = linkTask.klibs
.filter { it.exists() }
.filter { it.extension == "klib" }
.map { it }
val unpackedKlibs: List<File> = linkTask.klibs
.filter { it.exists() }
// we need only unpacked klibs
.filter { it.name == "manifest" && it.parentFile.name == "default" }
// manifest stored in klib inside directory default
.map { it.parentFile.parentFile }

(packedKlibs + unpackedKlibs)
.forEach { inputFile ->
linkTask.logger.info("copy resources from $inputFile into $outputDir")
val klibKonan = org.jetbrains.kotlin.konan.file.File(inputFile.path)
val klib = KotlinLibraryLayoutImpl(klib = klibKonan, component = "default")
val layout: KotlinLibraryLayout = klib.extractingToTemp

try {
File(layout.resourcesDir.path).copyRecursively(
target = outputDir,
overwrite = true
)
} catch (@Suppress("SwallowedException") exc: NoSuchFileException) {
linkTask.logger.info("resources in $inputFile not found")
} catch (@Suppress("SwallowedException") exc: java.nio.file.NoSuchFileException) {
linkTask.logger.info("resources in $inputFile not found (empty lib)")
}
linkTask.logger.info("found dependency $inputFile, try to copy resources")

val layout: KotlinLibraryLayout = getKotlinLibraryLayout(inputFile)

copyResourcesFromKlib(
logger = linkTask.logger,
layout = layout,
outputDir = outputDir,
)
}
}

private fun copyResourcesFromKlib(logger: Logger, layout: KotlinLibraryLayout, outputDir: File) {
logger.info("copy resources from $layout into $outputDir")

try {
File(layout.resourcesDir.path).copyRecursively(
target = outputDir,
overwrite = true
)
} catch (@Suppress("SwallowedException") exc: NoSuchFileException) {
logger.info("resources in $layout not found")
} catch (@Suppress("SwallowedException") exc: java.nio.file.NoSuchFileException) {
logger.info("resources in $layout not found (empty lib)")
}
}

private fun getKotlinLibraryLayout(file: File): KotlinLibraryLayout {
val klibKonan = org.jetbrains.kotlin.konan.file.File(file.path)
val klib = KotlinLibraryLayoutImpl(klib = klibKonan, component = "default")

return if (klib.isZipped) klib.extractingToTemp else klib
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinNativeCompile
import org.jetbrains.kotlin.konan.file.zipDirAs
import java.io.File
import java.util.Properties
import org.jetbrains.kotlin.konan.file.File as KonanFile

internal class PackAppleResourcesToKLibAction(
private val assetsDirectory: Provider<File>,
Expand Down Expand Up @@ -43,11 +44,48 @@ internal class PackAppleResourcesToKLibAction(

val klibFile: File = task.outputFile.get()
val repackDir = File(klibFile.parent, klibFile.nameWithoutExtension)
val defaultDir = File(repackDir, "default")
val resRepackDir = File(defaultDir, "resources")

task.logger.info("Adding resources to klib file `{}`", klibFile)
unzipTo(zipFile = klibFile, outputDirectory = repackDir)
if (klibFile.isDirectory) {
task.logger.info("Adding resources to unpacked klib directory `{}`", klibFile)

addResourcesToUnpackedKlib(
klibDir = klibFile,
resourcesGenerationDir = resourcesGenerationDir,
assetsDirectory = assetsDirectory,
task = task
)
} else {
task.logger.info("Adding resources to packed klib directory `{}`", klibFile)

unzipTo(zipFile = klibFile, outputDirectory = repackDir)

addResourcesToUnpackedKlib(
klibDir = repackDir,
resourcesGenerationDir = resourcesGenerationDir,
assetsDirectory = assetsDirectory,
task = task
)

val repackKonan = KonanFile(repackDir.path)
val klibKonan = KonanFile(klibFile.path)

klibFile.delete()
repackKonan.zipDirAs(klibKonan)

repackDir.deleteRecursively()
}
}

private fun addResourcesToUnpackedKlib(
klibDir: File,
resourcesGenerationDir: File,
assetsDirectory: File,
task: KotlinNativeCompile
) {
assert(klibDir.isDirectory) { "should be used directory as KLib" }

val defaultDir = File(klibDir, "default")
val resRepackDir = File(defaultDir, "resources")

val manifestFile = File(defaultDir, "manifest")
val manifest = Properties()
Expand Down Expand Up @@ -83,14 +121,6 @@ internal class PackAppleResourcesToKLibAction(
} else {
task.logger.info("assets not found, compilation not required")
}

val repackKonan = org.jetbrains.kotlin.konan.file.File(repackDir.path)
val klibKonan = org.jetbrains.kotlin.konan.file.File(klibFile.path)

klibFile.delete()
repackKonan.zipDirAs(klibKonan)

repackDir.deleteRecursively()
}

private fun compileAppleAssets(
Expand Down
3 changes: 2 additions & 1 deletion samples/compose-resources-gallery/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ org.jetbrains.compose.experimental.uikit.enabled=true
kotlin.mpp.androidSourceSetLayoutVersion=2
kotlin.mpp.androidGradlePluginCompatibility.nowarn=true

kotlin.version=2.0.0
# update to future versions after fix of https://youtrack.jetbrains.com/issue/CMP-7207
kotlin.version=2.1.0
agp.version=8.4.0
compose.version=1.6.10
Loading

0 comments on commit 0b16b7a

Please sign in to comment.