Browse Source

[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.
pull/5095/head
Konstantin 4 months ago committed by GitHub
parent
commit
5da48904e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 32
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ComposeResourcesGeneration.kt
  2. 7
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt
  3. 8
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResourceAccessorsTask.kt
  4. 13
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResourceCollectorsTask.kt
  5. 13
      gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/ResourcesTest.kt

32
gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ComposeResourcesGeneration.kt

@ -98,17 +98,21 @@ private fun Project.configureResClassGeneration(
GenerateResClassTask::class.java GenerateResClassTask::class.java
) { task -> ) { task ->
task.packageName.set(packageName) task.packageName.set(packageName)
task.shouldGenerateCode.set(shouldGenerateCode)
task.makeAccessorsPublic.set(makeAccessorsPublic) task.makeAccessorsPublic.set(makeAccessorsPublic)
task.codeDir.set(layout.buildDirectory.dir("$RES_GEN_DIR/kotlin/commonResClass")) task.codeDir.set(layout.buildDirectory.dir("$RES_GEN_DIR/kotlin/commonResClass"))
if (generateModulePath) { if (generateModulePath) {
task.packagingDir.set(packagingDir) task.packagingDir.set(packagingDir)
} }
task.onlyIf { shouldGenerateCode.get() }
} }
//register generated source set //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( private fun Project.configureResourceAccessorsGeneration(
@ -128,7 +132,6 @@ private fun Project.configureResourceAccessorsGeneration(
) { task -> ) { task ->
task.packageName.set(packageName) task.packageName.set(packageName)
task.sourceSetName.set(sourceSet.name) task.sourceSetName.set(sourceSet.name)
task.shouldGenerateCode.set(shouldGenerateCode)
task.makeAccessorsPublic.set(makeAccessorsPublic) task.makeAccessorsPublic.set(makeAccessorsPublic)
task.resDir.set(resourcesDir) task.resDir.set(resourcesDir)
task.codeDir.set(layout.buildDirectory.dir("$RES_GEN_DIR/kotlin/${sourceSet.name}ResourceAccessors")) task.codeDir.set(layout.buildDirectory.dir("$RES_GEN_DIR/kotlin/${sourceSet.name}ResourceAccessors"))
@ -136,10 +139,15 @@ private fun Project.configureResourceAccessorsGeneration(
if (generateModulePath) { if (generateModulePath) {
task.packagingDir.set(packagingDir) task.packagingDir.set(packagingDir)
} }
task.onlyIf { shouldGenerateCode.get() }
} }
//register generated source set //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 { private fun KotlinSourceSet.getResourceAccessorsGenerationTaskName(): String {
@ -219,13 +227,17 @@ private fun Project.configureExpectResourceCollectorsGeneration(
GenerateExpectResourceCollectorsTask::class.java GenerateExpectResourceCollectorsTask::class.java
) { task -> ) { task ->
task.packageName.set(packageName) task.packageName.set(packageName)
task.shouldGenerateCode.set(shouldGenerateCode)
task.makeAccessorsPublic.set(makeAccessorsPublic) task.makeAccessorsPublic.set(makeAccessorsPublic)
task.codeDir.set(layout.buildDirectory.dir("$RES_GEN_DIR/kotlin/${sourceSet.name}ResourceCollectors")) task.codeDir.set(layout.buildDirectory.dir("$RES_GEN_DIR/kotlin/${sourceSet.name}ResourceCollectors"))
task.onlyIf { shouldGenerateCode.get() }
} }
//register generated source set //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( private fun Project.configureActualResourceCollectorsGeneration(
@ -257,13 +269,17 @@ private fun Project.configureActualResourceCollectorsGeneration(
GenerateActualResourceCollectorsTask::class.java GenerateActualResourceCollectorsTask::class.java
) { task -> ) { task ->
task.packageName.set(packageName) task.packageName.set(packageName)
task.shouldGenerateCode.set(shouldGenerateCode)
task.makeAccessorsPublic.set(makeAccessorsPublic) task.makeAccessorsPublic.set(makeAccessorsPublic)
task.useActualModifier.set(useActualModifier) task.useActualModifier.set(useActualModifier)
task.resourceAccessorDirs.from(accessorDirs) task.resourceAccessorDirs.from(accessorDirs)
task.codeDir.set(layout.buildDirectory.dir("$RES_GEN_DIR/kotlin/${sourceSet.name}ResourceCollectors")) task.codeDir.set(layout.buildDirectory.dir("$RES_GEN_DIR/kotlin/${sourceSet.name}ResourceCollectors"))
task.onlyIf { shouldGenerateCode.get() }
} }
//register generated source set //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()
}
)
} }

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

@ -20,9 +20,6 @@ internal abstract class GenerateResClassTask : IdeaImportTask() {
@get:Optional @get:Optional
abstract val packagingDir: Property<File> abstract val packagingDir: Property<File>
@get:Input
abstract val shouldGenerateCode: Property<Boolean>
@get:Input @get:Input
abstract val makeAccessorsPublic: Property<Boolean> abstract val makeAccessorsPublic: Property<Boolean>
@ -34,15 +31,11 @@ internal abstract class GenerateResClassTask : IdeaImportTask() {
dir.deleteRecursively() dir.deleteRecursively()
dir.mkdirs() dir.mkdirs()
if (shouldGenerateCode.get()) {
logger.info("Generate $RES_FILE_NAME.kt") logger.info("Generate $RES_FILE_NAME.kt")
val pkgName = packageName.get() val pkgName = packageName.get()
val moduleDirectory = packagingDir.getOrNull()?.let { it.invariantSeparatorsPath + "/" } ?: "" val moduleDirectory = packagingDir.getOrNull()?.let { it.invariantSeparatorsPath + "/" } ?: ""
val isPublic = makeAccessorsPublic.get() val isPublic = makeAccessorsPublic.get()
getResFileSpec(pkgName, RES_FILE_NAME, moduleDirectory, isPublic).writeTo(dir) getResFileSpec(pkgName, RES_FILE_NAME, moduleDirectory, isPublic).writeTo(dir)
} else {
logger.info("Generation Res class is disabled")
}
} }
} }

8
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.PathSensitive
import org.gradle.api.tasks.PathSensitivity import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.SkipWhenEmpty import org.gradle.api.tasks.SkipWhenEmpty
import org.gradle.api.tasks.TaskAction
import org.jetbrains.compose.internal.IdeaImportTask import org.jetbrains.compose.internal.IdeaImportTask
import java.io.File import java.io.File
import java.nio.file.Path import java.nio.file.Path
@ -26,9 +25,6 @@ internal abstract class GenerateResourceAccessorsTask : IdeaImportTask() {
@get:Optional @get:Optional
abstract val packagingDir: Property<File> abstract val packagingDir: Property<File>
@get:Input
abstract val shouldGenerateCode: Property<Boolean>
@get:Input @get:Input
abstract val makeAccessorsPublic: Property<Boolean> abstract val makeAccessorsPublic: Property<Boolean>
@ -49,7 +45,6 @@ internal abstract class GenerateResourceAccessorsTask : IdeaImportTask() {
kotlinDir.deleteRecursively() kotlinDir.deleteRecursively()
kotlinDir.mkdirs() kotlinDir.mkdirs()
if (shouldGenerateCode.get()) {
logger.info("Generate accessors for $rootResDir") logger.info("Generate accessors for $rootResDir")
//get first level dirs //get first level dirs
@ -77,9 +72,6 @@ internal abstract class GenerateResourceAccessorsTask : IdeaImportTask() {
getAccessorsSpecs( getAccessorsSpecs(
resources, pkgName, sourceSet, moduleDirectory, isPublic resources, pkgName, sourceSet, moduleDirectory, isPublic
).forEach { it.writeTo(kotlinDir) } ).forEach { it.writeTo(kotlinDir) }
} else {
logger.info("Generation accessors for $rootResDir is disabled")
}
} }
private fun File.fileToResourceItems( private fun File.fileToResourceItems(

13
gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResourceCollectorsTask.kt

@ -15,9 +15,6 @@ internal abstract class GenerateExpectResourceCollectorsTask : IdeaImportTask()
@get:Input @get:Input
abstract val packageName: Property<String> abstract val packageName: Property<String>
@get:Input
abstract val shouldGenerateCode: Property<Boolean>
@get:Input @get:Input
abstract val makeAccessorsPublic: Property<Boolean> abstract val makeAccessorsPublic: Property<Boolean>
@ -31,7 +28,6 @@ internal abstract class GenerateExpectResourceCollectorsTask : IdeaImportTask()
kotlinDir.deleteRecursively() kotlinDir.deleteRecursively()
kotlinDir.mkdirs() kotlinDir.mkdirs()
if (shouldGenerateCode.get()) {
logger.info("Generate expect ResourceCollectors for $kotlinDir") logger.info("Generate expect ResourceCollectors for $kotlinDir")
val pkgName = packageName.get() val pkgName = packageName.get()
@ -39,16 +35,12 @@ internal abstract class GenerateExpectResourceCollectorsTask : IdeaImportTask()
val spec = getExpectResourceCollectorsFileSpec(pkgName, "ExpectResourceCollectors", isPublic) val spec = getExpectResourceCollectorsFileSpec(pkgName, "ExpectResourceCollectors", isPublic)
spec.writeTo(kotlinDir) spec.writeTo(kotlinDir)
} }
}
} }
internal abstract class GenerateActualResourceCollectorsTask : IdeaImportTask() { internal abstract class GenerateActualResourceCollectorsTask : IdeaImportTask() {
@get:Input @get:Input
abstract val packageName: Property<String> abstract val packageName: Property<String>
@get:Input
abstract val shouldGenerateCode: Property<Boolean>
@get:Input @get:Input
abstract val makeAccessorsPublic: Property<Boolean> abstract val makeAccessorsPublic: Property<Boolean>
@ -73,8 +65,6 @@ internal abstract class GenerateActualResourceCollectorsTask : IdeaImportTask()
val inputFiles = inputDirs.flatMap { dir -> val inputFiles = inputDirs.flatMap { dir ->
dir.walkTopDown().filter { !it.isHidden && it.isFile && it.extension == "kt" }.toList() dir.walkTopDown().filter { !it.isHidden && it.isFile && it.extension == "kt" }.toList()
} }
if (shouldGenerateCode.get()) {
logger.info("Generate actual ResourceCollectors for $kotlinDir") logger.info("Generate actual ResourceCollectors for $kotlinDir")
val funNames = inputFiles.mapNotNull { inputFile -> val funNames = inputFiles.mapNotNull { inputFile ->
if (inputFile.nameWithoutExtension.contains('.')) { if (inputFile.nameWithoutExtension.contains('.')) {
@ -109,8 +99,5 @@ internal abstract class GenerateActualResourceCollectorsTask : IdeaImportTask()
funNames funNames
) )
spec.writeTo(kotlinDir) spec.writeTo(kotlinDir)
} else {
logger.info("Generation ResourceCollectors for $kotlinDir is disabled")
}
} }
} }

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

@ -488,8 +488,7 @@ class ResourcesTest : GradlePluginTestBase() {
) )
} }
gradle("prepareKotlinIdeaImport").checks { gradle("prepareKotlinIdeaImport").checks {
check.taskSuccessful(":generateComposeResClass") check.taskSkipped(":generateComposeResClass")
assertFalse(file("build/generated/compose/resourceGenerator/kotlin/commonResClass/app/group/resources_test/generated/resources/Res.kt").exists())
} }
modifyText("build.gradle.kts") { str -> modifyText("build.gradle.kts") { str ->
@ -571,8 +570,14 @@ class ResourcesTest : GradlePluginTestBase() {
} }
""".trimIndent() """.trimIndent()
) )
gradle("generateComposeResClass").checks { gradle("prepareKotlinIdeaImport").checks {
check.logContains("Generation Res class is disabled") check.taskSkipped(":generateComposeResClass")
check.taskSkipped(":generateResourceAccessorsForCommonMain")
check.taskSkipped(":generateResourceAccessorsForDesktopMain")
check.taskSkipped(":generateResourceAccessorsForAndroidMain")
check.taskSkipped(":generateExpectResourceCollectorsForCommonMain")
check.taskSkipped(":generateActualResourceCollectorsForDesktopMain")
check.taskSkipped(":generateActualResourceCollectorsForAndroidMain")
} }
} }

Loading…
Cancel
Save