From 23945240dabe1d1cbebd0b3ad9e69eaa533d5424 Mon Sep 17 00:00:00 2001 From: Aleksey Mikhailov Date: Wed, 15 May 2024 22:03:38 +0700 Subject: [PATCH] #700 fix assets reading on windows --- .../asset/AndroidAssetResourceGenerator.kt | 5 +++- .../asset/AppleAssetResourceGenerator.kt | 13 ++++++---- .../asset/JsAssetResourceGenerator.kt | 4 +-- .../asset/JvmAssetResourceGenerator.kt | 2 +- .../kotlin/com/icerock/library/AssetsTests.kt | 25 +++++++++++++++++++ .../kotlin/AssetResourceExt.android.kt | 12 +++++++++ .../src/commonMain/kotlin/AssetResourceExt.kt | 7 ++++++ .../iosMain/kotlin/AssetResourceExt.ios.kt | 9 +++++++ .../src/jsMain/kotlin/AssetResourceExt.js.kt | 9 +++++++ .../jvmMain/kotlin/AssetResourceExt.jvm.kt | 9 +++++++ .../kotlin/AssetResourceExt.macos.kt | 9 +++++++ 11 files changed, 95 insertions(+), 9 deletions(-) create mode 100644 samples/resources-gallery/mpp-library/src/commonTest/kotlin/com/icerock/library/AssetsTests.kt create mode 100644 samples/resources-gallery/mpp-library/test-utils/src/androidMain/kotlin/AssetResourceExt.android.kt create mode 100644 samples/resources-gallery/mpp-library/test-utils/src/commonMain/kotlin/AssetResourceExt.kt create mode 100644 samples/resources-gallery/mpp-library/test-utils/src/iosMain/kotlin/AssetResourceExt.ios.kt create mode 100644 samples/resources-gallery/mpp-library/test-utils/src/jsMain/kotlin/AssetResourceExt.js.kt create mode 100644 samples/resources-gallery/mpp-library/test-utils/src/jvmMain/kotlin/AssetResourceExt.jvm.kt create mode 100644 samples/resources-gallery/mpp-library/test-utils/src/macosMain/kotlin/AssetResourceExt.macos.kt diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/resources/asset/AndroidAssetResourceGenerator.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/resources/asset/AndroidAssetResourceGenerator.kt index e2b780e38..5d2e44de1 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/resources/asset/AndroidAssetResourceGenerator.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/resources/asset/AndroidAssetResourceGenerator.kt @@ -24,7 +24,10 @@ internal class AndroidAssetResourceGenerator( ) override fun generateInitializer(metadata: AssetMetadata): CodeBlock { - return CodeBlock.of("AssetResource(path = %S)", metadata.pathRelativeToBase.path) + return CodeBlock.of( + "AssetResource(path = %S)", + metadata.pathRelativeToBase.invariantSeparatorsPath + ) } override fun generateBeforeProperties( diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/resources/asset/AppleAssetResourceGenerator.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/resources/asset/AppleAssetResourceGenerator.kt index e468bf2d9..abb658795 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/resources/asset/AppleAssetResourceGenerator.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/resources/asset/AppleAssetResourceGenerator.kt @@ -23,10 +23,8 @@ internal class AppleAssetResourceGenerator( override fun generateInitializer(metadata: AssetMetadata): CodeBlock { return CodeBlock.of( "AssetResource(originalPath = %S, fileName = %S, extension = %S, bundle = %L)", - metadata.pathRelativeToBase.path, - metadata.pathRelativeToBase.path - .replace('/', PATH_DELIMITER) - .substringBeforeLast('.'), + metadata.pathRelativeToBase.invariantSeparatorsPath, + processedFilePath(metadata).substringBeforeLast('.'), metadata.filePath.extension, Constants.Apple.platformContainerBundlePropertyName ) @@ -34,11 +32,16 @@ internal class AppleAssetResourceGenerator( override fun generateResourceFiles(data: List) { data.forEach { metadata -> - val newName: String = metadata.pathRelativeToBase.path.replace('/', PATH_DELIMITER) + val newName: String = processedFilePath(metadata) metadata.filePath.copyTo(File(resourcesGenerationDir, newName)) } } + private fun processedFilePath(metadata: AssetMetadata): String { + return metadata.pathRelativeToBase.invariantSeparatorsPath + .replace('/', PATH_DELIMITER) + } + override fun generateBeforeProperties( builder: Builder, metadata: List, diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/resources/asset/JsAssetResourceGenerator.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/resources/asset/JsAssetResourceGenerator.kt index a8b50d39e..1bc721a34 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/resources/asset/JsAssetResourceGenerator.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/resources/asset/JsAssetResourceGenerator.kt @@ -22,8 +22,8 @@ internal class JsAssetResourceGenerator( override fun imports(): List = emptyList() override fun generateInitializer(metadata: AssetMetadata): CodeBlock { - val filePath: String = File(ASSETS_DIR, metadata.pathRelativeToBase.path).path - .replace("\\", "/") + val filePath: String = File(ASSETS_DIR, metadata.pathRelativeToBase.path) + .invariantSeparatorsPath val requireDeclaration = """require("./$filePath")""" return CodeBlock.of( diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/resources/asset/JvmAssetResourceGenerator.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/resources/asset/JvmAssetResourceGenerator.kt index d3086b59d..b0e7cd3f4 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/resources/asset/JvmAssetResourceGenerator.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/resources/asset/JvmAssetResourceGenerator.kt @@ -26,7 +26,7 @@ internal class JvmAssetResourceGenerator( return CodeBlock.of( "AssetResource(resourcesClassLoader = %L, originalPath = %S, path = %S)", "${PlatformDetails.platformDetailsPropertyName}.${Jvm.resourcesClassLoaderPropertyName}", - metadata.pathRelativeToBase.path, + metadata.pathRelativeToBase.invariantSeparatorsPath, buildAssetPath(metadata) ) } diff --git a/samples/resources-gallery/mpp-library/src/commonTest/kotlin/com/icerock/library/AssetsTests.kt b/samples/resources-gallery/mpp-library/src/commonTest/kotlin/com/icerock/library/AssetsTests.kt new file mode 100644 index 000000000..28fb1178f --- /dev/null +++ b/samples/resources-gallery/mpp-library/src/commonTest/kotlin/com/icerock/library/AssetsTests.kt @@ -0,0 +1,25 @@ +/* + * Copyright 2024 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. + */ + +package com.icerock.library + +import BaseUnitTest +import com.icerockdev.library.MR +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.runTest +import readTextContent +import kotlin.test.Test +import kotlin.test.assertEquals + +class AssetsTests : BaseUnitTest() { + + @OptIn(ExperimentalCoroutinesApi::class) + @Test + fun checkAssetRead() = runTest { + assertEquals( + expected = "Text file2 from assets.", + actual = MR.assets.texts.test2_txt.readTextContent() + ) + } +} diff --git a/samples/resources-gallery/mpp-library/test-utils/src/androidMain/kotlin/AssetResourceExt.android.kt b/samples/resources-gallery/mpp-library/test-utils/src/androidMain/kotlin/AssetResourceExt.android.kt new file mode 100644 index 000000000..6904337f4 --- /dev/null +++ b/samples/resources-gallery/mpp-library/test-utils/src/androidMain/kotlin/AssetResourceExt.android.kt @@ -0,0 +1,12 @@ +/* + * Copyright 2024 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. + */ + +import android.content.Context +import androidx.test.core.app.ApplicationProvider +import dev.icerock.moko.resources.AssetResource + +public actual suspend fun AssetResource.readTextContent(): String { + val context: Context = ApplicationProvider.getApplicationContext() + return this.readText(context) +} diff --git a/samples/resources-gallery/mpp-library/test-utils/src/commonMain/kotlin/AssetResourceExt.kt b/samples/resources-gallery/mpp-library/test-utils/src/commonMain/kotlin/AssetResourceExt.kt new file mode 100644 index 000000000..8abfee285 --- /dev/null +++ b/samples/resources-gallery/mpp-library/test-utils/src/commonMain/kotlin/AssetResourceExt.kt @@ -0,0 +1,7 @@ +/* + * Copyright 2024 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. + */ + +import dev.icerock.moko.resources.AssetResource + +public expect suspend fun AssetResource.readTextContent(): String diff --git a/samples/resources-gallery/mpp-library/test-utils/src/iosMain/kotlin/AssetResourceExt.ios.kt b/samples/resources-gallery/mpp-library/test-utils/src/iosMain/kotlin/AssetResourceExt.ios.kt new file mode 100644 index 000000000..11096b953 --- /dev/null +++ b/samples/resources-gallery/mpp-library/test-utils/src/iosMain/kotlin/AssetResourceExt.ios.kt @@ -0,0 +1,9 @@ +/* + * Copyright 2024 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. + */ + +import dev.icerock.moko.resources.AssetResource + +public actual suspend fun AssetResource.readTextContent(): String { + return this.readText() +} diff --git a/samples/resources-gallery/mpp-library/test-utils/src/jsMain/kotlin/AssetResourceExt.js.kt b/samples/resources-gallery/mpp-library/test-utils/src/jsMain/kotlin/AssetResourceExt.js.kt new file mode 100644 index 000000000..6bf627cfa --- /dev/null +++ b/samples/resources-gallery/mpp-library/test-utils/src/jsMain/kotlin/AssetResourceExt.js.kt @@ -0,0 +1,9 @@ +/* + * Copyright 2024 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. + */ + +import dev.icerock.moko.resources.AssetResource + +public actual suspend fun AssetResource.readTextContent(): String { + return this.getText() +} diff --git a/samples/resources-gallery/mpp-library/test-utils/src/jvmMain/kotlin/AssetResourceExt.jvm.kt b/samples/resources-gallery/mpp-library/test-utils/src/jvmMain/kotlin/AssetResourceExt.jvm.kt new file mode 100644 index 000000000..11096b953 --- /dev/null +++ b/samples/resources-gallery/mpp-library/test-utils/src/jvmMain/kotlin/AssetResourceExt.jvm.kt @@ -0,0 +1,9 @@ +/* + * Copyright 2024 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. + */ + +import dev.icerock.moko.resources.AssetResource + +public actual suspend fun AssetResource.readTextContent(): String { + return this.readText() +} diff --git a/samples/resources-gallery/mpp-library/test-utils/src/macosMain/kotlin/AssetResourceExt.macos.kt b/samples/resources-gallery/mpp-library/test-utils/src/macosMain/kotlin/AssetResourceExt.macos.kt new file mode 100644 index 000000000..11096b953 --- /dev/null +++ b/samples/resources-gallery/mpp-library/test-utils/src/macosMain/kotlin/AssetResourceExt.macos.kt @@ -0,0 +1,9 @@ +/* + * Copyright 2024 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. + */ + +import dev.icerock.moko.resources.AssetResource + +public actual suspend fun AssetResource.readTextContent(): String { + return this.readText() +}