Browse Source

Configure android resources after the agp is applied and ignore hidden files in resources

pull/4173/head
Konstantin 11 months ago committed by GitHub
parent
commit
3040ea85bb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 8
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt
  2. 12
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesGenerator.kt
  3. 11
      gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/ResourcesTest.kt
  4. 11
      gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/kotlin/App.kt

8
gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt

@ -34,7 +34,7 @@ abstract class GenerateResClassTask : DefaultTask() {
logger.info("Generate resources for $rootResDir") logger.info("Generate resources for $rootResDir")
//get first level dirs //get first level dirs
val dirs = rootResDir.listFiles().orEmpty() val dirs = rootResDir.listNotHiddenFiles()
dirs.forEach { f -> dirs.forEach { f ->
if (!f.isDirectory) { if (!f.isDirectory) {
@ -45,8 +45,7 @@ abstract class GenerateResClassTask : DefaultTask() {
//type -> id -> resource item //type -> id -> resource item
val resources: Map<ResourceType, Map<String, List<ResourceItem>>> = dirs val resources: Map<ResourceType, Map<String, List<ResourceItem>>> = dirs
.flatMap { dir -> .flatMap { dir ->
dir.listFiles() dir.listNotHiddenFiles()
.orEmpty()
.mapNotNull { it.fileToResourceItems(rootResDir.toPath()) } .mapNotNull { it.fileToResourceItems(rootResDir.toPath()) }
.flatten() .flatten()
} }
@ -105,6 +104,9 @@ abstract class GenerateResClassTask : DefaultTask() {
.map { it.attributes.getNamedItem("name").nodeValue } .map { it.attributes.getNamedItem("name").nodeValue }
return ids.toSet() return ids.toSet()
} }
private fun File.listNotHiddenFiles(): List<File> =
listFiles()?.filter { !it.isHidden }.orEmpty()
} }
internal fun String.asUnderscoredIdentifier(): String = internal fun String.asUnderscoredIdentifier(): String =

12
gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesGenerator.kt

@ -11,9 +11,13 @@ import java.io.File
internal const val COMPOSE_RESOURCES_DIR = "composeResources" internal const val COMPOSE_RESOURCES_DIR = "composeResources"
private const val RES_GEN_DIR = "generated/compose/resourceGenerator" private const val RES_GEN_DIR = "generated/compose/resourceGenerator"
private val androidPluginIds = listOf(
"com.android.application",
"com.android.library"
)
internal fun Project.configureComposeResources() { internal fun Project.configureComposeResources() {
pluginManager.withPlugin(KOTLIN_MPP_PLUGIN_ID) { plugins.withId(KOTLIN_MPP_PLUGIN_ID) {
val kotlinExtension = project.extensions.getByType(KotlinMultiplatformExtension::class.java) val kotlinExtension = project.extensions.getByType(KotlinMultiplatformExtension::class.java)
kotlinExtension.sourceSets.all { sourceSet -> kotlinExtension.sourceSets.all { sourceSet ->
val sourceSetName = sourceSet.name val sourceSetName = sourceSet.name
@ -76,12 +80,14 @@ private fun Project.configureResourceGenerator(commonComposeResourcesDir: File,
} }
} }
val androidExtension = project.extensions.findByName("android") //when applied AGP then configure android resources
if (androidExtension != null) { androidPluginIds.forEach { pluginId ->
plugins.withId(pluginId) {
configureAndroidResources( configureAndroidResources(
commonComposeResources, commonComposeResources,
buildDir("$RES_GEN_DIR/androidFonts").map { it.asFile }, buildDir("$RES_GEN_DIR/androidFonts").map { it.asFile },
shouldGenerateResourceAccessors shouldGenerateResourceAccessors
) )
} }
}
} }

11
gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/ResourcesTest.kt

@ -9,7 +9,7 @@ import kotlin.io.path.Path
class ResourcesTest : GradlePluginTestBase() { class ResourcesTest : GradlePluginTestBase() {
@Test @Test
fun testGeneratedAccessorsAndCopiedFonts() = with(testProject("misc/commonResources")) { fun testGeneratedAccessorsAndCopiedFonts(): Unit = with(testProject("misc/commonResources")) {
//check generated resource's accessors //check generated resource's accessors
gradle("generateComposeResClass").checks { gradle("generateComposeResClass").checks {
assertEqualTextFiles( assertEqualTextFiles(
@ -108,11 +108,12 @@ class ResourcesTest : GradlePluginTestBase() {
file("src/commonMain/composeResources/drawable/vector_3.xml").renameTo( file("src/commonMain/composeResources/drawable/vector_3.xml").renameTo(
file("src/commonMain/composeResources/drawable/vector_2.xml") file("src/commonMain/composeResources/drawable/vector_2.xml")
) )
}
//TODO: check a real build after a release a new version of the resources library @Test
//because generated accessors depend on classes from the new version fun testCopyFontsInAndroidApp(): Unit = with(testProject("misc/commonResources")) {
gradle("assembleDebug", "--dry-run").checks { gradle("assembleDebug").checks {
check.taskSkipped("copyFontsToAndroidAssets") check.taskSuccessful(":copyFontsToAndroidAssets")
} }
} }
} }

11
gradle-plugins/compose/src/test/test-projects/misc/commonResources/src/commonMain/kotlin/App.kt

@ -1,8 +1,11 @@
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.foundation.Image
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontFamily
import generated.resources.Res import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import app.group.resources_test.generated.resources.Res
import org.jetbrains.compose.resources.* import org.jetbrains.compose.resources.*
@OptIn(ExperimentalResourceApi::class) @OptIn(ExperimentalResourceApi::class)
@ -11,10 +14,10 @@ fun App() {
Column { Column {
Image( Image(
modifier = Modifier.size(100.dp), modifier = Modifier.size(100.dp),
painter = painterResource(Res.images.vector), painter = painterResource(Res.drawable.vector),
contentDescription = null contentDescription = null
) )
Text(getString(Res.strings.app_name)) Text(stringResource(Res.string.app_name))
val font = FontFamily(Font(Res.fonts.emptyfont)) val font = FontFamily(Font(Res.font.emptyfont))
} }
} }

Loading…
Cancel
Save