1
+
1
2
import org.gradle.api.*
2
3
import org.gradle.api.artifacts.dsl.*
3
4
import org.gradle.api.artifacts.repositories.*
4
5
import org.gradle.api.initialization.dsl.*
5
6
import org.gradle.kotlin.dsl.*
6
7
import org.jetbrains.kotlin.gradle.targets.js.nodejs.*
7
- import org.jetbrains.kotlin.gradle.targets.js.npm.tasks.*
8
8
import org.jetbrains.kotlin.gradle.targets.js.yarn.*
9
9
import java.net.*
10
10
@@ -100,16 +100,28 @@ private fun Project.checkRedirect(repositories: RepositoryHandler, containerName
100
100
}
101
101
}
102
102
103
- private fun Project.configureYarnAndNodeRedirects () {
103
+ private fun Project.configureYarnRedirects () {
104
104
if (CacheRedirector .isEnabled) {
105
- val yarnRootExtension = extensions.findByType<YarnRootExtension >()
106
- yarnRootExtension?.downloadBaseUrl?.let {
107
- yarnRootExtension.downloadBaseUrl = CacheRedirector .maybeRedirect(it)
105
+ plugins.withType(YarnPlugin ::class ) {
106
+ extensions.configure(YarnRootEnvSpec ::class .java) {
107
+ // no API to modify the value in-place keeping it lazy: https://github.com/gradle/gradle/issues/27227
108
+ downloadBaseUrl.orNull?.let {
109
+ downloadBaseUrl = CacheRedirector .maybeRedirect(it)
110
+ }
111
+ }
108
112
}
113
+ }
114
+ }
109
115
110
- val nodeJsExtension = rootProject.extensions.findByType<NodeJsRootExtension >()
111
- nodeJsExtension?.downloadBaseUrl?.let {
112
- nodeJsExtension.downloadBaseUrl = CacheRedirector .maybeRedirect(it)
116
+ private fun Project.configureNodeJsRedirects () {
117
+ if (CacheRedirector .isEnabled) {
118
+ plugins.withType(NodeJsPlugin ::class ) {
119
+ extensions.configure(NodeJsEnvSpec ::class .java) {
120
+ // no API to modify the value in-place keeping it lazy: https://github.com/gradle/gradle/issues/27227
121
+ downloadBaseUrl.orNull?.let {
122
+ downloadBaseUrl = CacheRedirector .maybeRedirect(it)
123
+ }
124
+ }
113
125
}
114
126
}
115
127
}
@@ -128,14 +140,16 @@ object CacheRedirector {
128
140
@JvmStatic
129
141
fun configure (project : Project ) {
130
142
project.checkRedirect(project.repositories, project.displayName)
143
+ project.configureNodeJsRedirects()
131
144
}
132
145
133
146
/* *
134
- * Configures JS-specific extensions to use
147
+ * Configures JS-specific extensions defined on the root project to use cache redirector
148
+ * For example, KGP provides Yarn configuration only globally for the entire build via the root project.
135
149
*/
136
150
@JvmStatic
137
- fun configureJsPackageManagers ( project : Project ) {
138
- project.configureYarnAndNodeRedirects ()
151
+ fun configureRootJsPackageManagers ( rootProject : Project ) {
152
+ rootProject.configureYarnRedirects ()
139
153
}
140
154
141
155
@JvmStatic
0 commit comments