[Gradle, JS] Fix afterEvaluate not inside task configuration

This commit is contained in:
Ilya Goncharov
2021-11-15 17:27:29 +03:00
committed by TeamCityServer
parent 8cdfec7c40
commit fb6eb414ad
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.gradle.targets.js.yarn
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.BasePlugin
import org.gradle.api.tasks.TaskProvider
import org.jetbrains.kotlin.gradle.targets.js.MultiplePluginDeclarationDetector
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin
import org.jetbrains.kotlin.gradle.targets.js.npm.RequiresNpmDependencies
@@ -44,30 +45,10 @@ open class YarnPlugin : Plugin<Project> {
task.description = "Create root package.json"
task.mustRunAfter(rootClean)
// Yes, we need to break Task Configuration Avoidance here
// In case when we need to create package.json's files and execute kotlinNpmInstall,
// We need to configure all RequiresNpmDependencies tasks to install them,
// Because we need to persist yarn.lock
// We execute this block in configure phase of rootPackageJson to be sure,
// That Task Configuration Avoidance will not be broken for tasks not related with NPM installing
// https://youtrack.jetbrains.com/issue/KT-48241
project.allprojects
.forEach {
val fn: (Project) -> Unit = {
it.tasks.implementing(RequiresNpmDependencies::class)
.forEach {}
}
if (it.state.executed) {
fn(it)
} else {
it.afterEvaluate {
fn(it)
}
}
}
}
configureRequiresNpmDependencies(project, rootPackageJson)
val kotlinNpmInstall = tasks.named(KotlinNpmInstallTask.NAME)
kotlinNpmInstall.configure {
it.dependsOn(rootPackageJson)
@@ -109,6 +90,42 @@ open class YarnPlugin : Plugin<Project> {
}
}
// Yes, we need to break Task Configuration Avoidance here
// In case when we need to create package.json's files and execute kotlinNpmInstall,
// We need to configure all RequiresNpmDependencies tasks to install them,
// Because we need to persist yarn.lock
// We execute this block in configure phase of rootPackageJson to be sure,
// That Task Configuration Avoidance will not be broken for tasks not related with NPM installing
// https://youtrack.jetbrains.com/issue/KT-48241
private fun configureRequiresNpmDependencies(
project: Project,
rootPackageJson: TaskProvider<RootPackageJsonTask>
) {
val fn: (Project) -> Unit = {
it.tasks.implementing(RequiresNpmDependencies::class)
.forEach {}
}
rootPackageJson.configure {
project.allprojects
.forEach { project ->
if (it.state.executed) {
fn(project)
}
}
}
project.allprojects
.forEach {
if (!it.state.executed) {
it.afterEvaluate { project ->
rootPackageJson.configure {
fn(project)
}
}
}
}
}
companion object {
fun apply(project: Project): YarnRootExtension {
val rootProject = project.rootProject