Browse Source

[gradle] Add `getAsUri(path: String): String` function to the generated Res class

k.tskh/resource-uri
Konstantin Tskhovrebov 2 months ago
parent
commit
940cf47d49
  1. 21
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesSpec.kt
  2. 13
      gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected/Res.kt
  3. 11
      gradle-plugins/compose/src/test/test-projects/misc/emptyResources/expected/Res.kt
  4. 13
      gradle-plugins/compose/src/test/test-projects/misc/jvmOnlyResources/expected/Res.kt

21
gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesSpec.kt

@ -131,7 +131,6 @@ internal fun getResFileSpecs(
resObject.addModifiers(KModifier.INTERNAL)
resObject.addAnnotation(experimentalAnnotation)
//readFileBytes
val readResourceBytes = MemberName("org.jetbrains.compose.resources", "readResourceBytes")
resObject.addFunction(
FunSpec.builder("readBytes")
@ -151,6 +150,26 @@ internal fun getResFileSpecs(
.addStatement("""return %M("$moduleDir" + path)""", readResourceBytes)
.build()
)
val convertPathToUri = MemberName("org.jetbrains.compose.resources", "convertPathToUri")
resObject.addFunction(
FunSpec.builder("getAsUri")
.addKdoc(
"""
Converts a given file path to a platform dependent URI string.
Example: `val uri = Res.getAsUri("files/key.bin")`
@param path The file path to be converted to a URI.
@return The URI representation of the given file path.
""".trimIndent()
)
.addParameter("path", String::class)
.addModifiers(KModifier.SUSPEND)
.returns(String::class)
.addStatement("""return %M("$moduleDir" + path)""", convertPathToUri)
.build()
)
ResourceType.values().forEach { type ->
resObject.addType(TypeSpec.objectBuilder(type.typeName).build())
}

13
gradle-plugins/compose/src/test/test-projects/misc/commonResources/expected/Res.kt

@ -9,6 +9,7 @@ import kotlin.ByteArray
import kotlin.OptIn
import kotlin.String
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.convertPathToUri
import org.jetbrains.compose.resources.readResourceBytes
@ExperimentalResourceApi
@ -23,9 +24,19 @@ internal object Res {
*/
public suspend fun readBytes(path: String): ByteArray = readResourceBytes("" + path)
/**
* Converts a given file path to a platform dependent URI string.
*
* Example: `val uri = Res.getAsUri("files/key.bin")`
*
* @param path The file path to be converted to a URI.
* @return The URI representation of the given file path.
*/
public suspend fun getAsUri(path: String): String = convertPathToUri("" + path)
public object drawable
public object string
public object font
}
}

11
gradle-plugins/compose/src/test/test-projects/misc/emptyResources/expected/Res.kt

@ -9,6 +9,7 @@ import kotlin.ByteArray
import kotlin.OptIn
import kotlin.String
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.convertPathToUri
import org.jetbrains.compose.resources.readResourceBytes
@ExperimentalResourceApi
@ -23,6 +24,16 @@ internal object Res {
*/
public suspend fun readBytes(path: String): ByteArray = readResourceBytes("" + path)
/**
* Converts a given file path to a platform dependent URI string.
*
* Example: `val uri = Res.getAsUri("files/key.bin")`
*
* @param path The file path to be converted to a URI.
* @return The URI representation of the given file path.
*/
public suspend fun getAsUri(path: String): String = convertPathToUri("" + path)
public object drawable
public object string

13
gradle-plugins/compose/src/test/test-projects/misc/jvmOnlyResources/expected/Res.kt

@ -9,6 +9,7 @@ import kotlin.ByteArray
import kotlin.OptIn
import kotlin.String
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.convertPathToUri
import org.jetbrains.compose.resources.readResourceBytes
@ExperimentalResourceApi
@ -23,9 +24,19 @@ internal object Res {
*/
public suspend fun readBytes(path: String): ByteArray = readResourceBytes("" + path)
/**
* Converts a given file path to a platform dependent URI string.
*
* Example: `val uri = Res.getAsUri("files/key.bin")`
*
* @param path The file path to be converted to a URI.
* @return The URI representation of the given file path.
*/
public suspend fun getAsUri(path: String): String = convertPathToUri("" + path)
public object drawable
public object string
public object font
}
}
Loading…
Cancel
Save