Skip to content

Commit 0b16b7a

Browse files
authored
Merge pull request #789 from icerockdev/#768-unpacked-klibs-support
#768 unpacked klibs support
2 parents ddbf5ed + 9bac033 commit 0b16b7a

File tree

6 files changed

+314
-502
lines changed

6 files changed

+314
-502
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ buildscript {
6464
}
6565
6666
dependencies {
67-
classpath "dev.icerock.moko:resources-generator:0.24.3"
67+
classpath "dev.icerock.moko:resources-generator:0.24.4"
6868
}
6969
}
7070
@@ -82,10 +82,10 @@ project build.gradle
8282
apply plugin: "dev.icerock.mobile.multiplatform-resources"
8383
8484
dependencies {
85-
commonMainApi("dev.icerock.moko:resources:0.24.3")
86-
commonMainApi("dev.icerock.moko:resources-compose:0.24.3") // for compose multiplatform
85+
commonMainApi("dev.icerock.moko:resources:0.24.4")
86+
commonMainApi("dev.icerock.moko:resources-compose:0.24.4") // for compose multiplatform
8787
88-
commonTestImplementation("dev.icerock.moko:resources-test:0.24.3")
88+
commonTestImplementation("dev.icerock.moko:resources-test:0.24.4")
8989
}
9090
9191
multiplatformResources {
@@ -132,7 +132,7 @@ should [add `export` declarations](https://kotlinlang.org/docs/multiplatform-bui
132132

133133
```
134134
framework {
135-
export("dev.icerock.moko:resources:0.24.3")
135+
export("dev.icerock.moko:resources:0.24.4")
136136
export("dev.icerock.moko:graphics:0.9.0") // toUIColor here
137137
}
138138
```

resources-generator/src/main/kotlin/dev/icerock/gradle/actions/apple/CopyResourcesFromKLibsAction.kt

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package dev.icerock.gradle.actions.apple
77
import dev.icerock.gradle.utils.klibs
88
import org.gradle.api.Action
99
import org.gradle.api.Task
10+
import org.gradle.api.logging.Logger
1011
import org.jetbrains.kotlin.gradle.tasks.KotlinNativeLink
1112
import org.jetbrains.kotlin.library.KotlinLibraryLayout
1213
import org.jetbrains.kotlin.library.impl.KotlinLibraryLayoutImpl
@@ -18,25 +19,50 @@ internal abstract class CopyResourcesFromKLibsAction : Action<Task> {
1819
linkTask: KotlinNativeLink,
1920
outputDir: File
2021
) {
21-
linkTask.klibs
22+
val packedKlibs: List<File> = linkTask.klibs
23+
.filter { it.exists() }
2224
.filter { it.extension == "klib" }
25+
.map { it }
26+
val unpackedKlibs: List<File> = linkTask.klibs
2327
.filter { it.exists() }
28+
// we need only unpacked klibs
29+
.filter { it.name == "manifest" && it.parentFile.name == "default" }
30+
// manifest stored in klib inside directory default
31+
.map { it.parentFile.parentFile }
32+
33+
(packedKlibs + unpackedKlibs)
2434
.forEach { inputFile ->
25-
linkTask.logger.info("copy resources from $inputFile into $outputDir")
26-
val klibKonan = org.jetbrains.kotlin.konan.file.File(inputFile.path)
27-
val klib = KotlinLibraryLayoutImpl(klib = klibKonan, component = "default")
28-
val layout: KotlinLibraryLayout = klib.extractingToTemp
29-
30-
try {
31-
File(layout.resourcesDir.path).copyRecursively(
32-
target = outputDir,
33-
overwrite = true
34-
)
35-
} catch (@Suppress("SwallowedException") exc: NoSuchFileException) {
36-
linkTask.logger.info("resources in $inputFile not found")
37-
} catch (@Suppress("SwallowedException") exc: java.nio.file.NoSuchFileException) {
38-
linkTask.logger.info("resources in $inputFile not found (empty lib)")
39-
}
35+
linkTask.logger.info("found dependency $inputFile, try to copy resources")
36+
37+
val layout: KotlinLibraryLayout = getKotlinLibraryLayout(inputFile)
38+
39+
copyResourcesFromKlib(
40+
logger = linkTask.logger,
41+
layout = layout,
42+
outputDir = outputDir,
43+
)
4044
}
4145
}
46+
47+
private fun copyResourcesFromKlib(logger: Logger, layout: KotlinLibraryLayout, outputDir: File) {
48+
logger.info("copy resources from $layout into $outputDir")
49+
50+
try {
51+
File(layout.resourcesDir.path).copyRecursively(
52+
target = outputDir,
53+
overwrite = true
54+
)
55+
} catch (@Suppress("SwallowedException") exc: NoSuchFileException) {
56+
logger.info("resources in $layout not found")
57+
} catch (@Suppress("SwallowedException") exc: java.nio.file.NoSuchFileException) {
58+
logger.info("resources in $layout not found (empty lib)")
59+
}
60+
}
61+
62+
private fun getKotlinLibraryLayout(file: File): KotlinLibraryLayout {
63+
val klibKonan = org.jetbrains.kotlin.konan.file.File(file.path)
64+
val klib = KotlinLibraryLayoutImpl(klib = klibKonan, component = "default")
65+
66+
return if (klib.isZipped) klib.extractingToTemp else klib
67+
}
4268
}

resources-generator/src/main/kotlin/dev/icerock/gradle/actions/apple/PackAppleResourcesToKLibAction.kt

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinNativeCompile
1515
import org.jetbrains.kotlin.konan.file.zipDirAs
1616
import java.io.File
1717
import java.util.Properties
18+
import org.jetbrains.kotlin.konan.file.File as KonanFile
1819

1920
internal class PackAppleResourcesToKLibAction(
2021
private val assetsDirectory: Provider<File>,
@@ -43,11 +44,48 @@ internal class PackAppleResourcesToKLibAction(
4344

4445
val klibFile: File = task.outputFile.get()
4546
val repackDir = File(klibFile.parent, klibFile.nameWithoutExtension)
46-
val defaultDir = File(repackDir, "default")
47-
val resRepackDir = File(defaultDir, "resources")
4847

49-
task.logger.info("Adding resources to klib file `{}`", klibFile)
50-
unzipTo(zipFile = klibFile, outputDirectory = repackDir)
48+
if (klibFile.isDirectory) {
49+
task.logger.info("Adding resources to unpacked klib directory `{}`", klibFile)
50+
51+
addResourcesToUnpackedKlib(
52+
klibDir = klibFile,
53+
resourcesGenerationDir = resourcesGenerationDir,
54+
assetsDirectory = assetsDirectory,
55+
task = task
56+
)
57+
} else {
58+
task.logger.info("Adding resources to packed klib directory `{}`", klibFile)
59+
60+
unzipTo(zipFile = klibFile, outputDirectory = repackDir)
61+
62+
addResourcesToUnpackedKlib(
63+
klibDir = repackDir,
64+
resourcesGenerationDir = resourcesGenerationDir,
65+
assetsDirectory = assetsDirectory,
66+
task = task
67+
)
68+
69+
val repackKonan = KonanFile(repackDir.path)
70+
val klibKonan = KonanFile(klibFile.path)
71+
72+
klibFile.delete()
73+
repackKonan.zipDirAs(klibKonan)
74+
75+
repackDir.deleteRecursively()
76+
}
77+
}
78+
79+
private fun addResourcesToUnpackedKlib(
80+
klibDir: File,
81+
resourcesGenerationDir: File,
82+
assetsDirectory: File,
83+
task: KotlinNativeCompile
84+
) {
85+
assert(klibDir.isDirectory) { "should be used directory as KLib" }
86+
87+
val defaultDir = File(klibDir, "default")
88+
val resRepackDir = File(defaultDir, "resources")
5189

5290
val manifestFile = File(defaultDir, "manifest")
5391
val manifest = Properties()
@@ -83,14 +121,6 @@ internal class PackAppleResourcesToKLibAction(
83121
} else {
84122
task.logger.info("assets not found, compilation not required")
85123
}
86-
87-
val repackKonan = org.jetbrains.kotlin.konan.file.File(repackDir.path)
88-
val klibKonan = org.jetbrains.kotlin.konan.file.File(klibFile.path)
89-
90-
klibFile.delete()
91-
repackKonan.zipDirAs(klibKonan)
92-
93-
repackDir.deleteRecursively()
94124
}
95125

96126
private fun compileAppleAssets(

samples/compose-resources-gallery/gradle.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ org.jetbrains.compose.experimental.uikit.enabled=true
1919
kotlin.mpp.androidSourceSetLayoutVersion=2
2020
kotlin.mpp.androidGradlePluginCompatibility.nowarn=true
2121

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

0 commit comments

Comments
 (0)