Browse Source

Clean code-gen directory if there was deleted a dependency on the res library (#4257)

fixes https://github.com/JetBrains/compose-multiplatform/issues/4242
pull/4264/head
Konstantin 10 months ago committed by GitHub
parent
commit
8ee7531c42
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 52
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt
  2. 6
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesGenerator.kt
  3. 50
      gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/ResourcesTest.kt
  4. 4
      gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/utils/assertUtils.kt
  5. 1
      gradle-plugins/compose/src/test/test-projects/misc/commonResources/build.gradle.kts

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

@ -12,10 +12,13 @@ import kotlin.io.path.relativeTo
/**
* This task should be FAST and SAFE! Because it is being run during IDE import.
*/
abstract class GenerateResClassTask : DefaultTask() {
internal abstract class GenerateResClassTask : DefaultTask() {
@get:Input
abstract val packageName: Property<String>
@get:Input
abstract val shouldGenerateResClass: Property<Boolean>
@get:InputFiles
@get:PathSensitive(PathSensitivity.RELATIVE)
abstract val resDir: Property<File>
@ -26,32 +29,37 @@ abstract class GenerateResClassTask : DefaultTask() {
@TaskAction
fun generate() {
try {
val rootResDir = resDir.get()
logger.info("Generate resources for $rootResDir")
val kotlinDir = codeDir.get().asFile
logger.info("Clean directory $kotlinDir")
kotlinDir.deleteRecursively()
kotlinDir.mkdirs()
//get first level dirs
val dirs = rootResDir.listNotHiddenFiles()
if (shouldGenerateResClass.get()) {
val rootResDir = resDir.get()
logger.info("Generate resources for $rootResDir")
dirs.forEach { f ->
if (!f.isDirectory) {
error("${f.name} is not directory! Raw files should be placed in '${rootResDir.name}/files' directory.")
}
}
//get first level dirs
val dirs = rootResDir.listNotHiddenFiles()
//type -> id -> resource item
val resources: Map<ResourceType, Map<String, List<ResourceItem>>> = dirs
.flatMap { dir ->
dir.listNotHiddenFiles()
.mapNotNull { it.fileToResourceItems(rootResDir.toPath()) }
.flatten()
dirs.forEach { f ->
if (!f.isDirectory) {
error("${f.name} is not directory! Raw files should be placed in '${rootResDir.name}/files' directory.")
}
}
.groupBy { it.type }
.mapValues { (_, items) -> items.groupBy { it.name } }
val kotlinDir = codeDir.get().asFile
kotlinDir.deleteRecursively()
kotlinDir.mkdirs()
getResFileSpec(resources, packageName.get()).writeTo(kotlinDir)
//type -> id -> resource item
val resources: Map<ResourceType, Map<String, List<ResourceItem>>> = dirs
.flatMap { dir ->
dir.listNotHiddenFiles()
.mapNotNull { it.fileToResourceItems(rootResDir.toPath()) }
.flatten()
}
.groupBy { it.type }
.mapValues { (_, items) -> items.groupBy { it.name } }
getResFileSpec(resources, packageName.get()).writeTo(kotlinDir)
} else {
logger.info("Generation Res class is disabled")
}
} catch (e: Exception) {
//message must contain two ':' symbols to be parsed by IDE UI!
logger.error("e: GenerateResClassTask was failed:", e)

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

@ -54,7 +54,7 @@ private fun Project.configureResourceGenerator(commonComposeResourcesDir: File,
fun buildDir(path: String) = layout.dir(layout.buildDirectory.map { File(it.asFile, path) })
//lazy check a dependency on the Resources library
val shouldGenerateResourceAccessors: Provider<Boolean> = provider {
val shouldGenerateResClass: Provider<Boolean> = provider {
if (ComposeProperties.alwaysGenerateResourceAccessors(project).get()) {
true
} else {
@ -72,9 +72,9 @@ private fun Project.configureResourceGenerator(commonComposeResourcesDir: File,
GenerateResClassTask::class.java
) {
it.packageName.set(packageName)
it.shouldGenerateResClass.set(shouldGenerateResClass)
it.resDir.set(commonComposeResources)
it.codeDir.set(buildDir("$RES_GEN_DIR/kotlin"))
it.onlyIf { shouldGenerateResourceAccessors.get() }
}
//register generated source set
@ -93,7 +93,7 @@ private fun Project.configureResourceGenerator(commonComposeResourcesDir: File,
configureAndroidResources(
commonComposeResources,
buildDir("$RES_GEN_DIR/androidFonts").map { it.asFile },
shouldGenerateResourceAccessors
shouldGenerateResClass
)
}
}

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

@ -117,6 +117,56 @@ class ResourcesTest : GradlePluginTestBase() {
}
}
@Test
fun testUpToDateChecks(): Unit = with(testProject("misc/commonResources")) {
gradle("prepareKotlinIdeaImport").checks {
check.taskSuccessful(":generateComposeResClass")
assert(file("build/generated/compose/resourceGenerator/kotlin/app/group/resources_test/generated/resources/Res.kt").exists())
}
gradle("prepareKotlinIdeaImport").checks {
check.taskUpToDate(":generateComposeResClass")
}
modifyText("build.gradle.kts") { str ->
str.replace(
"implementation(compose.components.resources)",
"//implementation(compose.components.resources)"
)
}
gradle("prepareKotlinIdeaImport").checks {
check.taskSuccessful(":generateComposeResClass")
assert(!file("build/generated/compose/resourceGenerator/kotlin/app/group/resources_test/generated/resources/Res.kt").exists())
}
gradle("prepareKotlinIdeaImport", "-Pcompose.resources.always.generate.accessors=true").checks {
check.taskSuccessful(":generateComposeResClass")
assert(file("build/generated/compose/resourceGenerator/kotlin/app/group/resources_test/generated/resources/Res.kt").exists())
}
modifyText("build.gradle.kts") { str ->
str.replace(
"//implementation(compose.components.resources)",
"implementation(compose.components.resources)"
)
}
gradle("prepareKotlinIdeaImport").checks {
check.taskUpToDate(":generateComposeResClass")
assert(file("build/generated/compose/resourceGenerator/kotlin/app/group/resources_test/generated/resources/Res.kt").exists())
}
modifyText("build.gradle.kts") { str ->
str.replace(
"group = \"app.group\"",
"group = \"io.company\""
)
}
gradle("prepareKotlinIdeaImport").checks {
check.taskSuccessful(":generateComposeResClass")
assert(!file("build/generated/compose/resourceGenerator/kotlin/app/group/resources_test/generated/resources/Res.kt").exists())
assert(file("build/generated/compose/resourceGenerator/kotlin/io/company/resources_test/generated/resources/Res.kt").exists())
}
}
@Test
fun testEmptyResClass(): Unit = with(testProject("misc/emptyResources")) {
gradle("generateComposeResClass").checks {

4
gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/utils/assertUtils.kt

@ -57,6 +57,10 @@ internal class BuildResultChecks(private val result: BuildResult) {
taskOutcome(task, TaskOutcome.FAILED)
}
fun taskUpToDate(task: String) {
taskOutcome(task, TaskOutcome.UP_TO_DATE)
}
fun taskFromCache(task: String) {
taskOutcome(task, TaskOutcome.FROM_CACHE)
}

1
gradle-plugins/compose/src/test/test-projects/misc/commonResources/build.gradle.kts

@ -21,7 +21,6 @@ kotlin {
dependencies {
implementation(compose.runtime)
implementation(compose.material)
@OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class)
implementation(compose.components.resources)
}
}

Loading…
Cancel
Save