Skip to content

Commit fb6eb41

Browse files
ilgonmicTeamCityServer
authored and
TeamCityServer
committed
[Gradle, JS] Fix afterEvaluate not inside task configuration
1 parent 8cdfec7 commit fb6eb41

File tree

1 file changed

+39
-22
lines changed
  • libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/yarn

1 file changed

+39
-22
lines changed

libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/yarn/YarnPlugin.kt

+39-22
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.gradle.targets.js.yarn
88
import org.gradle.api.Plugin
99
import org.gradle.api.Project
1010
import org.gradle.api.plugins.BasePlugin
11+
import org.gradle.api.tasks.TaskProvider
1112
import org.jetbrains.kotlin.gradle.targets.js.MultiplePluginDeclarationDetector
1213
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin
1314
import org.jetbrains.kotlin.gradle.targets.js.npm.RequiresNpmDependencies
@@ -44,30 +45,10 @@ open class YarnPlugin : Plugin<Project> {
4445
task.description = "Create root package.json"
4546

4647
task.mustRunAfter(rootClean)
47-
48-
// Yes, we need to break Task Configuration Avoidance here
49-
// In case when we need to create package.json's files and execute kotlinNpmInstall,
50-
// We need to configure all RequiresNpmDependencies tasks to install them,
51-
// Because we need to persist yarn.lock
52-
// We execute this block in configure phase of rootPackageJson to be sure,
53-
// That Task Configuration Avoidance will not be broken for tasks not related with NPM installing
54-
// https://youtrack.jetbrains.com/issue/KT-48241
55-
project.allprojects
56-
.forEach {
57-
val fn: (Project) -> Unit = {
58-
it.tasks.implementing(RequiresNpmDependencies::class)
59-
.forEach {}
60-
}
61-
if (it.state.executed) {
62-
fn(it)
63-
} else {
64-
it.afterEvaluate {
65-
fn(it)
66-
}
67-
}
68-
}
6948
}
7049

50+
configureRequiresNpmDependencies(project, rootPackageJson)
51+
7152
val kotlinNpmInstall = tasks.named(KotlinNpmInstallTask.NAME)
7253
kotlinNpmInstall.configure {
7354
it.dependsOn(rootPackageJson)
@@ -109,6 +90,42 @@ open class YarnPlugin : Plugin<Project> {
10990
}
11091
}
11192

93+
// Yes, we need to break Task Configuration Avoidance here
94+
// In case when we need to create package.json's files and execute kotlinNpmInstall,
95+
// We need to configure all RequiresNpmDependencies tasks to install them,
96+
// Because we need to persist yarn.lock
97+
// We execute this block in configure phase of rootPackageJson to be sure,
98+
// That Task Configuration Avoidance will not be broken for tasks not related with NPM installing
99+
// https://youtrack.jetbrains.com/issue/KT-48241
100+
private fun configureRequiresNpmDependencies(
101+
project: Project,
102+
rootPackageJson: TaskProvider<RootPackageJsonTask>
103+
) {
104+
val fn: (Project) -> Unit = {
105+
it.tasks.implementing(RequiresNpmDependencies::class)
106+
.forEach {}
107+
}
108+
rootPackageJson.configure {
109+
project.allprojects
110+
.forEach { project ->
111+
if (it.state.executed) {
112+
fn(project)
113+
}
114+
}
115+
}
116+
117+
project.allprojects
118+
.forEach {
119+
if (!it.state.executed) {
120+
it.afterEvaluate { project ->
121+
rootPackageJson.configure {
122+
fn(project)
123+
}
124+
}
125+
}
126+
}
127+
}
128+
112129
companion object {
113130
fun apply(project: Project): YarnRootExtension {
114131
val rootProject = project.rootProject

0 commit comments

Comments
 (0)