Skip to content

Commit

Permalink
Merge pull request #706 from icerockdev/#700-fix-assets-path-on-windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex009 authored May 15, 2024
2 parents 2f6c8a7 + 2394524 commit 0b1b6cc
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,25 @@ 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
)
}

override fun generateResourceFiles(data: List<AssetMetadata>) {
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<AssetMetadata>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ internal class JsAssetResourceGenerator(
override fun imports(): List<ClassName> = 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
)
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 0b1b6cc

Please sign in to comment.