From e32a1ca5629943e8ebe515da06fb8540aef7b353 Mon Sep 17 00:00:00 2001 From: Ilya Goncharov Date: Mon, 22 Nov 2021 14:19:52 +0300 Subject: [PATCH] [Gradle, JS] Not resolve configuration with nodejs and yarn if it should not be downloaded --- .../kotlin/gradle/Kotlin2JsGradlePluginIT.kt | 34 ++++++++++++++- .../nodeJsDownload/build.gradle.kts | 15 +++++++ .../src.main.resources/index.html | 11 +++++ .../nodeJsDownload/src/main/kotlin/main.kt | 10 +++++ .../targets/js/nodejs/NodeJsSetupTask.kt | 42 ++++++++++--------- .../gradle/targets/js/yarn/YarnSetupTask.kt | 42 ++++++++++--------- 6 files changed, 114 insertions(+), 40 deletions(-) create mode 100644 libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/nodeJsDownload/build.gradle.kts create mode 100644 libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/nodeJsDownload/src.main.resources/index.html create mode 100644 libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/nodeJsDownload/src/main/kotlin/main.kt diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/Kotlin2JsGradlePluginIT.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/Kotlin2JsGradlePluginIT.kt index 3d1f349614a..77de995b9c3 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/Kotlin2JsGradlePluginIT.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/Kotlin2JsGradlePluginIT.kt @@ -1045,14 +1045,44 @@ class GeneralKotlin2JsGradlePluginIT : KGPBaseTest() { build("checkDownloadedFolder") build("checkIfLastModifiedNotNow", "--rerun-tasks") + + buildGradle.modify { + it + "\n" + + """ + rootProject.plugins.withType { + rootProject.the().nodeVersion = "unspecified" + rootProject.the().download = false + } + """ + } + } + } + + @DisplayName("Disable download should not download Node.JS and Yarn") + @GradleTest + fun testNodeJsAndYarnNotDownloaded(gradleVersion: GradleVersion) { + project("nodeJsDownload", gradleVersion) { + buildGradleKts.modify { + it + "\n" + + """ + rootProject.plugins.withType { + rootProject.the().nodeVersion = "unspecified" + rootProject.the().download = false + } + rootProject.plugins.withType { + rootProject.the().version = "unspecified" + rootProject.the().download = false + } + """ + } + build("assemble") } } @DisplayName("Yarn.lock persistence") @GradleTest fun testYarnLockStore(gradleVersion: GradleVersion) { - project("cleanTask", gradleVersion) { - buildGradle.modify(::transformBuildScriptWithPluginsDsl) + project("nodeJsDownload", gradleVersion) { build("assemble") { assertFileExists(projectPath.resolve("kotlin-js-store").resolve("yarn.lock")) assert( diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/nodeJsDownload/build.gradle.kts b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/nodeJsDownload/build.gradle.kts new file mode 100644 index 00000000000..9cb1961b77a --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/nodeJsDownload/build.gradle.kts @@ -0,0 +1,15 @@ +plugins { + id("org.jetbrains.kotlin.js") +} + +repositories { + mavenLocal() + mavenCentral() +} + + +kotlin { + js { + nodejs() + } +} \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/nodeJsDownload/src.main.resources/index.html b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/nodeJsDownload/src.main.resources/index.html new file mode 100644 index 00000000000..e8d598a131f --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/nodeJsDownload/src.main.resources/index.html @@ -0,0 +1,11 @@ + + + + + CleanTask + + + + + + \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/nodeJsDownload/src/main/kotlin/main.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/nodeJsDownload/src/main/kotlin/main.kt new file mode 100644 index 00000000000..77fd603f7a9 --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/nodeJsDownload/src/main/kotlin/main.kt @@ -0,0 +1,10 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +import kotlinx.browser.document + +fun main() { + document.write("Hello, world!") +} \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/nodejs/NodeJsSetupTask.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/nodejs/NodeJsSetupTask.kt index acee86234ba..e6c486cebd9 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/nodejs/NodeJsSetupTask.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/nodejs/NodeJsSetupTask.kt @@ -50,26 +50,29 @@ abstract class NodeJsSetupTask : DefaultTask() { internal lateinit var configuration: Provider @get:Classpath - val nodeJsDist: File by lazy { - val repo = project.repositories.ivy { repo -> - repo.name = "Node Distributions at ${downloadBaseUrl}" - repo.url = URI(downloadBaseUrl) + @get:Optional + val nodeJsDist: File? by lazy { + if (shouldDownload) { + val repo = project.repositories.ivy { repo -> + repo.name = "Node Distributions at ${downloadBaseUrl}" + repo.url = URI(downloadBaseUrl) - repo.patternLayout { - it.artifact("v[revision]/[artifact](-v[revision]-[classifier]).[ext]") + repo.patternLayout { + it.artifact("v[revision]/[artifact](-v[revision]-[classifier]).[ext]") + } + repo.metadataSources { it.artifact() } + repo.content { it.includeModule("org.nodejs", "node") } } - repo.metadataSources { it.artifact() } - repo.content { it.includeModule("org.nodejs", "node") } - } - val startDownloadTime = System.currentTimeMillis() - val dist = configuration.get().files.single() - val downloadDuration = System.currentTimeMillis() - startDownloadTime - if (downloadDuration > 0) { - KotlinBuildStatsService.getInstance() - ?.report(NumericalMetrics.ARTIFACTS_DOWNLOAD_SPEED, dist.length() * 1000 / downloadDuration) - } - project.repositories.remove(repo) - dist + val startDownloadTime = System.currentTimeMillis() + val dist = configuration.get().files.single() + val downloadDuration = System.currentTimeMillis() - startDownloadTime + if (downloadDuration > 0) { + KotlinBuildStatsService.getInstance() + ?.report(NumericalMetrics.ARTIFACTS_DOWNLOAD_SPEED, dist.length() * 1000 / downloadDuration) + } + project.repositories.remove(repo) + dist + } else null } init { @@ -81,6 +84,7 @@ abstract class NodeJsSetupTask : DefaultTask() { @Suppress("unused") @TaskAction fun exec() { + if (!shouldDownload) return logger.kotlinInfo("Using node distribution from '$nodeJsDist'") var dirHash: String? = null @@ -93,7 +97,7 @@ abstract class NodeJsSetupTask : DefaultTask() { } val tmpDir = temporaryDir - unpackNodeArchive(nodeJsDist, tmpDir) + unpackNodeArchive(nodeJsDist!!, tmpDir) if (upToDate && fileHasher.calculateDirHash(tmpDir.resolve(destination.name))!! == dirHash) { tmpDir.deleteRecursively() diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/yarn/YarnSetupTask.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/yarn/YarnSetupTask.kt index 70aa2eaff1f..fc3b48e140a 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/yarn/YarnSetupTask.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/yarn/YarnSetupTask.kt @@ -62,25 +62,28 @@ open class YarnSetupTask : DefaultTask() { internal lateinit var configuration: Provider @get:Classpath - val yarnDist: File by lazy { - val repo = project.repositories.ivy { repo -> - repo.name = "Yarn Distributions at ${downloadUrl}" - repo.url = URI(downloadUrl) - repo.patternLayout { - it.artifact("v[revision]/[artifact](-v[revision]).[ext]") + @get:Optional + val yarnDist: File? by lazy { + if (shouldDownload) { + val repo = project.repositories.ivy { repo -> + repo.name = "Yarn Distributions at ${downloadUrl}" + repo.url = URI(downloadUrl) + repo.patternLayout { + it.artifact("v[revision]/[artifact](-v[revision]).[ext]") + } + repo.metadataSources { it.artifact() } + repo.content { it.includeModule("com.yarnpkg", "yarn") } } - repo.metadataSources { it.artifact() } - repo.content { it.includeModule("com.yarnpkg", "yarn") } - } - val startDownloadTime = System.currentTimeMillis() - val dist = configuration.get().files.single() - val downloadDuration = System.currentTimeMillis() - startDownloadTime - if (downloadDuration > 0) { - KotlinBuildStatsService.getInstance() - ?.report(NumericalMetrics.ARTIFACTS_DOWNLOAD_SPEED, dist.length() * 1000 / downloadDuration) - } - project.repositories.remove(repo) - dist + val startDownloadTime = System.currentTimeMillis() + val dist = configuration.get().files.single() + val downloadDuration = System.currentTimeMillis() - startDownloadTime + if (downloadDuration > 0) { + KotlinBuildStatsService.getInstance() + ?.report(NumericalMetrics.ARTIFACTS_DOWNLOAD_SPEED, dist.length() * 1000 / downloadDuration) + } + project.repositories.remove(repo) + dist + } else null } init { @@ -91,6 +94,7 @@ open class YarnSetupTask : DefaultTask() { @TaskAction fun setup() { + if (!shouldDownload) return logger.kotlinInfo("Using yarn distribution from '$yarnDist'") var dirHash: String? = null @@ -103,7 +107,7 @@ open class YarnSetupTask : DefaultTask() { } val tmpDir = temporaryDir - extract(yarnDist, tmpDir) // parent because archive contains name already + extract(yarnDist!!, tmpDir) // parent because archive contains name already if (upToDate && fileHasher.calculateDirHash(tmpDir.resolve(destination.name))!! == dirHash) { tmpDir.deleteRecursively()