From 5da48904e5c24aa567da17fb780707073f920af3 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Wed, 10 Jul 2024 16:51:32 +0200 Subject: [PATCH] [gradle] Disable code generation tasks if they are not needed. (#5091) Small refactor: instead of the run tasks in a dumb mode just skip them if the resource accessors generation is disabled. --- .../resources/ComposeResourcesGeneration.kt | 32 +++++-- .../compose/resources/GenerateResClassTask.kt | 17 ++-- .../GenerateResourceAccessorsTask.kt | 54 +++++------- .../GenerateResourceCollectorsTask.kt | 87 ++++++++----------- .../test/tests/integration/ResourcesTest.kt | 13 ++- 5 files changed, 98 insertions(+), 105 deletions(-) diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ComposeResourcesGeneration.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ComposeResourcesGeneration.kt index 672a479d0d..9b45b9be32 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ComposeResourcesGeneration.kt +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ComposeResourcesGeneration.kt @@ -98,17 +98,21 @@ private fun Project.configureResClassGeneration( GenerateResClassTask::class.java ) { task -> task.packageName.set(packageName) - task.shouldGenerateCode.set(shouldGenerateCode) task.makeAccessorsPublic.set(makeAccessorsPublic) task.codeDir.set(layout.buildDirectory.dir("$RES_GEN_DIR/kotlin/commonResClass")) if (generateModulePath) { task.packagingDir.set(packagingDir) } + task.onlyIf { shouldGenerateCode.get() } } //register generated source set - resClassSourceSet.kotlin.srcDir(genTask.map { it.codeDir }) + resClassSourceSet.kotlin.srcDir( + genTask.zip(shouldGenerateCode) { task, flag -> + if (flag) listOf(task.codeDir) else emptyList() + } + ) } private fun Project.configureResourceAccessorsGeneration( @@ -128,7 +132,6 @@ private fun Project.configureResourceAccessorsGeneration( ) { task -> task.packageName.set(packageName) task.sourceSetName.set(sourceSet.name) - task.shouldGenerateCode.set(shouldGenerateCode) task.makeAccessorsPublic.set(makeAccessorsPublic) task.resDir.set(resourcesDir) task.codeDir.set(layout.buildDirectory.dir("$RES_GEN_DIR/kotlin/${sourceSet.name}ResourceAccessors")) @@ -136,10 +139,15 @@ private fun Project.configureResourceAccessorsGeneration( if (generateModulePath) { task.packagingDir.set(packagingDir) } + task.onlyIf { shouldGenerateCode.get() } } //register generated source set - sourceSet.kotlin.srcDir(genTask.map { it.codeDir }) + sourceSet.kotlin.srcDir( + genTask.zip(shouldGenerateCode) { task, flag -> + if (flag) listOf(task.codeDir) else emptyList() + } + ) } private fun KotlinSourceSet.getResourceAccessorsGenerationTaskName(): String { @@ -219,13 +227,17 @@ private fun Project.configureExpectResourceCollectorsGeneration( GenerateExpectResourceCollectorsTask::class.java ) { task -> task.packageName.set(packageName) - task.shouldGenerateCode.set(shouldGenerateCode) task.makeAccessorsPublic.set(makeAccessorsPublic) task.codeDir.set(layout.buildDirectory.dir("$RES_GEN_DIR/kotlin/${sourceSet.name}ResourceCollectors")) + task.onlyIf { shouldGenerateCode.get() } } //register generated source set - sourceSet.kotlin.srcDir(genTask.map { it.codeDir }) + sourceSet.kotlin.srcDir( + genTask.zip(shouldGenerateCode) { task, flag -> + if (flag) listOf(task.codeDir) else emptyList() + } + ) } private fun Project.configureActualResourceCollectorsGeneration( @@ -257,13 +269,17 @@ private fun Project.configureActualResourceCollectorsGeneration( GenerateActualResourceCollectorsTask::class.java ) { task -> task.packageName.set(packageName) - task.shouldGenerateCode.set(shouldGenerateCode) task.makeAccessorsPublic.set(makeAccessorsPublic) task.useActualModifier.set(useActualModifier) task.resourceAccessorDirs.from(accessorDirs) task.codeDir.set(layout.buildDirectory.dir("$RES_GEN_DIR/kotlin/${sourceSet.name}ResourceCollectors")) + task.onlyIf { shouldGenerateCode.get() } } //register generated source set - sourceSet.kotlin.srcDir(genTask.map { it.codeDir }) + sourceSet.kotlin.srcDir( + genTask.zip(shouldGenerateCode) { task, flag -> + if (flag) listOf(task.codeDir) else emptyList() + } + ) } \ No newline at end of file diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt index 5ce79a5268..676715343f 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt @@ -20,9 +20,6 @@ internal abstract class GenerateResClassTask : IdeaImportTask() { @get:Optional abstract val packagingDir: Property - @get:Input - abstract val shouldGenerateCode: Property - @get:Input abstract val makeAccessorsPublic: Property @@ -34,15 +31,11 @@ internal abstract class GenerateResClassTask : IdeaImportTask() { dir.deleteRecursively() dir.mkdirs() - if (shouldGenerateCode.get()) { - logger.info("Generate $RES_FILE_NAME.kt") + logger.info("Generate $RES_FILE_NAME.kt") - val pkgName = packageName.get() - val moduleDirectory = packagingDir.getOrNull()?.let { it.invariantSeparatorsPath + "/" } ?: "" - val isPublic = makeAccessorsPublic.get() - getResFileSpec(pkgName, RES_FILE_NAME, moduleDirectory, isPublic).writeTo(dir) - } else { - logger.info("Generation Res class is disabled") - } + val pkgName = packageName.get() + val moduleDirectory = packagingDir.getOrNull()?.let { it.invariantSeparatorsPath + "/" } ?: "" + val isPublic = makeAccessorsPublic.get() + getResFileSpec(pkgName, RES_FILE_NAME, moduleDirectory, isPublic).writeTo(dir) } } diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResourceAccessorsTask.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResourceAccessorsTask.kt index 9aca01b3ff..6d346c5069 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResourceAccessorsTask.kt +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResourceAccessorsTask.kt @@ -9,7 +9,6 @@ import org.gradle.api.tasks.OutputDirectory import org.gradle.api.tasks.PathSensitive import org.gradle.api.tasks.PathSensitivity import org.gradle.api.tasks.SkipWhenEmpty -import org.gradle.api.tasks.TaskAction import org.jetbrains.compose.internal.IdeaImportTask import java.io.File import java.nio.file.Path @@ -26,9 +25,6 @@ internal abstract class GenerateResourceAccessorsTask : IdeaImportTask() { @get:Optional abstract val packagingDir: Property - @get:Input - abstract val shouldGenerateCode: Property - @get:Input abstract val makeAccessorsPublic: Property @@ -49,37 +45,33 @@ internal abstract class GenerateResourceAccessorsTask : IdeaImportTask() { kotlinDir.deleteRecursively() kotlinDir.mkdirs() - if (shouldGenerateCode.get()) { - logger.info("Generate accessors for $rootResDir") + logger.info("Generate accessors for $rootResDir") - //get first level dirs - val dirs = rootResDir.listNotHiddenFiles() + //get first level dirs + val dirs = rootResDir.listNotHiddenFiles() - dirs.forEach { f -> - if (!f.isDirectory) { - error("${f.name} is not directory! Raw files should be placed in '${rootResDir.name}/files' directory.") - } + dirs.forEach { f -> + if (!f.isDirectory) { + error("${f.name} is not directory! Raw files should be placed in '${rootResDir.name}/files' directory.") } - - //type -> id -> resource item - val resources: Map>> = dirs - .flatMap { dir -> - dir.listNotHiddenFiles() - .mapNotNull { it.fileToResourceItems(rootResDir.toPath()) } - .flatten() - } - .groupBy { it.type } - .mapValues { (_, items) -> items.groupBy { it.name } } - - val pkgName = packageName.get() - val moduleDirectory = packagingDir.getOrNull()?.let { it.invariantSeparatorsPath + "/" } ?: "" - val isPublic = makeAccessorsPublic.get() - getAccessorsSpecs( - resources, pkgName, sourceSet, moduleDirectory, isPublic - ).forEach { it.writeTo(kotlinDir) } - } else { - logger.info("Generation accessors for $rootResDir is disabled") } + + //type -> id -> resource item + val resources: Map>> = dirs + .flatMap { dir -> + dir.listNotHiddenFiles() + .mapNotNull { it.fileToResourceItems(rootResDir.toPath()) } + .flatten() + } + .groupBy { it.type } + .mapValues { (_, items) -> items.groupBy { it.name } } + + val pkgName = packageName.get() + val moduleDirectory = packagingDir.getOrNull()?.let { it.invariantSeparatorsPath + "/" } ?: "" + val isPublic = makeAccessorsPublic.get() + getAccessorsSpecs( + resources, pkgName, sourceSet, moduleDirectory, isPublic + ).forEach { it.writeTo(kotlinDir) } } private fun File.fileToResourceItems( diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResourceCollectorsTask.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResourceCollectorsTask.kt index bd67c804f5..19b9a371a1 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResourceCollectorsTask.kt +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResourceCollectorsTask.kt @@ -15,9 +15,6 @@ internal abstract class GenerateExpectResourceCollectorsTask : IdeaImportTask() @get:Input abstract val packageName: Property - @get:Input - abstract val shouldGenerateCode: Property - @get:Input abstract val makeAccessorsPublic: Property @@ -31,14 +28,12 @@ internal abstract class GenerateExpectResourceCollectorsTask : IdeaImportTask() kotlinDir.deleteRecursively() kotlinDir.mkdirs() - if (shouldGenerateCode.get()) { - logger.info("Generate expect ResourceCollectors for $kotlinDir") + logger.info("Generate expect ResourceCollectors for $kotlinDir") - val pkgName = packageName.get() - val isPublic = makeAccessorsPublic.get() - val spec = getExpectResourceCollectorsFileSpec(pkgName, "ExpectResourceCollectors", isPublic) - spec.writeTo(kotlinDir) - } + val pkgName = packageName.get() + val isPublic = makeAccessorsPublic.get() + val spec = getExpectResourceCollectorsFileSpec(pkgName, "ExpectResourceCollectors", isPublic) + spec.writeTo(kotlinDir) } } @@ -46,9 +41,6 @@ internal abstract class GenerateActualResourceCollectorsTask : IdeaImportTask() @get:Input abstract val packageName: Property - @get:Input - abstract val shouldGenerateCode: Property - @get:Input abstract val makeAccessorsPublic: Property @@ -73,44 +65,39 @@ internal abstract class GenerateActualResourceCollectorsTask : IdeaImportTask() val inputFiles = inputDirs.flatMap { dir -> dir.walkTopDown().filter { !it.isHidden && it.isFile && it.extension == "kt" }.toList() } - - if (shouldGenerateCode.get()) { - logger.info("Generate actual ResourceCollectors for $kotlinDir") - val funNames = inputFiles.mapNotNull { inputFile -> - if (inputFile.nameWithoutExtension.contains('.')) { - val (fileName, suffix) = inputFile.nameWithoutExtension.split('.') - val type = ResourceType.values().firstOrNull { fileName.startsWith(it.accessorName, true) } - val name = "_collect${suffix.uppercaseFirstChar()}${fileName}Resources" - - if (type == null) { - logger.warn("Unknown resources type: `$inputFile`") - null - } else if (!inputFile.readText().contains(name)) { - logger.warn("A function '$name' is not found in the `$inputFile` file!") - null - } else { - logger.info("Found collector function: `$name`") - type to name - } - } else { - logger.warn("Unknown file name: `$inputFile`") + logger.info("Generate actual ResourceCollectors for $kotlinDir") + val funNames = inputFiles.mapNotNull { inputFile -> + if (inputFile.nameWithoutExtension.contains('.')) { + val (fileName, suffix) = inputFile.nameWithoutExtension.split('.') + val type = ResourceType.values().firstOrNull { fileName.startsWith(it.accessorName, true) } + val name = "_collect${suffix.uppercaseFirstChar()}${fileName}Resources" + + if (type == null) { + logger.warn("Unknown resources type: `$inputFile`") + null + } else if (!inputFile.readText().contains(name)) { + logger.warn("A function '$name' is not found in the `$inputFile` file!") null + } else { + logger.info("Found collector function: `$name`") + type to name } - }.groupBy({ it.first }, { it.second }) - - val pkgName = packageName.get() - val isPublic = makeAccessorsPublic.get() - val useActual = useActualModifier.get() - val spec = getActualResourceCollectorsFileSpec( - pkgName, - "ActualResourceCollectors", - isPublic, - useActual, - funNames - ) - spec.writeTo(kotlinDir) - } else { - logger.info("Generation ResourceCollectors for $kotlinDir is disabled") - } + } else { + logger.warn("Unknown file name: `$inputFile`") + null + } + }.groupBy({ it.first }, { it.second }) + + val pkgName = packageName.get() + val isPublic = makeAccessorsPublic.get() + val useActual = useActualModifier.get() + val spec = getActualResourceCollectorsFileSpec( + pkgName, + "ActualResourceCollectors", + isPublic, + useActual, + funNames + ) + spec.writeTo(kotlinDir) } } diff --git a/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/ResourcesTest.kt b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/ResourcesTest.kt index 0aeb94a2d8..95b599bfcb 100644 --- a/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/ResourcesTest.kt +++ b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/ResourcesTest.kt @@ -488,8 +488,7 @@ class ResourcesTest : GradlePluginTestBase() { ) } gradle("prepareKotlinIdeaImport").checks { - check.taskSuccessful(":generateComposeResClass") - assertFalse(file("build/generated/compose/resourceGenerator/kotlin/commonResClass/app/group/resources_test/generated/resources/Res.kt").exists()) + check.taskSkipped(":generateComposeResClass") } modifyText("build.gradle.kts") { str -> @@ -571,8 +570,14 @@ class ResourcesTest : GradlePluginTestBase() { } """.trimIndent() ) - gradle("generateComposeResClass").checks { - check.logContains("Generation Res class is disabled") + gradle("prepareKotlinIdeaImport").checks { + check.taskSkipped(":generateComposeResClass") + check.taskSkipped(":generateResourceAccessorsForCommonMain") + check.taskSkipped(":generateResourceAccessorsForDesktopMain") + check.taskSkipped(":generateResourceAccessorsForAndroidMain") + check.taskSkipped(":generateExpectResourceCollectorsForCommonMain") + check.taskSkipped(":generateActualResourceCollectorsForDesktopMain") + check.taskSkipped(":generateActualResourceCollectorsForAndroidMain") } }