[Gradle, JS] Distinguish creating prepared resolution and writing package.json

^KT-61326 fixed
This commit is contained in:
Ilya Goncharov
2023-08-25 17:23:10 +00:00
committed by Space Team
parent 2472999242
commit 355ad3e641
14 changed files with 203 additions and 32 deletions
@@ -1702,4 +1702,39 @@ class Kotlin2JsIrGradlePluginIT : KGPBaseTest() {
}
}
}
@DisplayName("test package.jsom skipped with main package.json was up-to-date")
@GradleTest
fun testPackageJsonSkippedWithUpToDatePackageJsons(gradleVersion: GradleVersion) {
project("kotlin-js-browser-library-project", gradleVersion) {
subProject("app").buildGradleKts.modify {
it + """
tasks.named("testPackageJson") {
enabled = false
}
tasks.named("testPublicPackageJson") {
enabled = false
}
""".trimIndent()
}
build("assemble") {
assertTasksExecuted(":app:packageJson")
assertTasksExecuted(":base:packageJson")
assertTasksExecuted(":lib:packageJson")
}
build("check") {
assertTasksUpToDate(":app:packageJson")
assertTasksUpToDate(":base:packageJson")
assertTasksUpToDate(":lib:packageJson")
assertTasksSkipped(":app:testPackageJson")
assertTasksExecuted(":lib:testPackageJson")
assertTasksExecuted(":base:testPackageJson")
}
}
}
}
@@ -0,0 +1,19 @@
plugins {
kotlin("js")
}
dependencies {
implementation(kotlin("stdlib-js"))
implementation(project(":lib"))
testImplementation(kotlin("test-js"))
}
kotlin {
js {
browser {
testTask {
enabled = false
}
}
}
}
@@ -0,0 +1,13 @@
/*
* 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.
*/
package com.example
fun main() {
require("css/main.css")
println("Sheldon: ${sheldon()}")
}
external fun require(o: String)
@@ -0,0 +1,16 @@
/*
* Copyright 2010-2021 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.
*/
package com.example
import kotlin.test.Test
import kotlin.test.assertEquals
class AppTests {
@Test
fun testSheldon() {
assertEquals(73, sheldon())
}
}
@@ -0,0 +1,20 @@
import org.jetbrains.kotlin.gradle.plugin.KotlinJsCompilerType.IR as IR_TYPE
plugins {
kotlin("js")
}
dependencies {
implementation(kotlin("stdlib-js"))
}
@Suppress("DEPRECATION")
kotlin {
js("ir")
js(IR)
js(IR_TYPE) {
useCommonJs()
browser {
}
}
}
@@ -0,0 +1,14 @@
/*
* 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.
*/
package com.example
fun best(): Int {
return 42
}
fun simpleBest(): Int {
return 73
}
@@ -0,0 +1,13 @@
plugins {
kotlin("js").apply(false)
}
group = "com.example"
version = "1.0"
allprojects {
repositories {
mavenLocal()
mavenCentral()
}
}
@@ -0,0 +1,16 @@
plugins {
kotlin("js")
}
dependencies {
implementation(kotlin("stdlib-js"))
implementation(project(":base"))
}
kotlin {
js {
useCommonJs()
browser {
}
}
}
@@ -0,0 +1,14 @@
/*
* 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.
*/
package com.example
fun answer(): Int {
return best()
}
fun sheldon(): Int {
return simpleBest()
}
@@ -0,0 +1,5 @@
rootProject.name = "kotlin-js-browser"
include("base")
include("lib")
include("app")
@@ -16,6 +16,7 @@ import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import org.gradle.work.DisableCachingByDefault
import org.jetbrains.kotlin.gradle.targets.js.npm.NpmProject.Companion.PACKAGE_JSON
import org.jetbrains.kotlin.gradle.targets.js.npm.resolved.PreparedKotlinCompilationNpmResolution
import org.jetbrains.kotlin.gradle.utils.property
import java.io.File
@@ -54,11 +55,10 @@ abstract class PublicPackageJsonTask :
}
}
private val compilationResolution
private val compilationResolution: PreparedKotlinCompilationNpmResolution
get() = npmResolutionManager.get().resolution.get()[projectPath][compilationDisambiguatedName.get()]
.getResolutionOrPrepare(
npmResolutionManager.get(),
packageJsonHandlers,
logger
)
@@ -16,7 +16,7 @@ import java.io.Serializable
class KotlinRootNpmResolution(
val projects: Map<String, KotlinProjectNpmResolution>,
val rootProjectName: String,
val rootProjectVersion: String
val rootProjectVersion: String,
) : Serializable {
operator fun get(project: String) = projects[project] ?: KotlinProjectNpmResolution.empty()
@@ -27,12 +27,20 @@ class KotlinRootNpmResolution(
logger: Logger,
npmEnvironment: NpmEnvironment,
yarnEnvironment: YarnEnvironment,
npmResolutionManager: KotlinNpmResolutionManager
npmResolutionManager: KotlinNpmResolutionManager,
): Installation {
synchronized(projects) {
npmResolutionManager.parameters.gradleNodeModulesProvider.get().close()
val projectResolutions: List<PreparedKotlinCompilationNpmResolution> = projects.values.flatMap { it.npmProjects }.map { it.close() }
val projectResolutions: List<PreparedKotlinCompilationNpmResolution> = projects.values
.flatMap { it.npmProjects }
.map {
it.close(
npmResolutionManager,
logger
)
}
npmEnvironment.packageManager.prepareRootProject(
npmEnvironment,
rootProjectName,
@@ -29,7 +29,7 @@ class KotlinCompilationNpmResolution(
val npmProjectMain: String,
val npmProjectPackageJsonFile: File,
val npmProjectDir: File,
val tasksRequirements: TasksRequirements
val tasksRequirements: TasksRequirements,
) : Serializable {
val inputs: PackageJsonProducerInputs
@@ -45,17 +45,13 @@ class KotlinCompilationNpmResolution(
@Synchronized
fun prepareWithDependencies(
skipWriting: Boolean = false,
npmResolutionManager: KotlinNpmResolutionManager,
packageJsonHandlers: ListProperty<Action<PackageJson>>,
logger: Logger
logger: Logger,
): PreparedKotlinCompilationNpmResolution {
check(resolution == null) { "$this already resolved" }
return createPreparedResolution(
skipWriting,
npmResolutionManager,
packageJsonHandlers,
logger
).also {
resolution = it
@@ -65,30 +61,28 @@ class KotlinCompilationNpmResolution(
@Synchronized
fun getResolutionOrPrepare(
npmResolutionManager: KotlinNpmResolutionManager,
packageJsonHandlers: ListProperty<Action<PackageJson>>,
logger: Logger,
): PreparedKotlinCompilationNpmResolution {
return resolution ?: prepareWithDependencies(
skipWriting = true,
npmResolutionManager,
packageJsonHandlers,
logger
)
}
@Synchronized
fun close(): PreparedKotlinCompilationNpmResolution {
fun close(
npmResolutionManager: KotlinNpmResolutionManager,
logger: Logger,
): PreparedKotlinCompilationNpmResolution {
check(!closed) { "$this already closed" }
closed = true
return resolution!!
return getResolutionOrPrepare(npmResolutionManager, logger)
}
fun createPreparedResolution(
skipWriting: Boolean,
npmResolutionManager: KotlinNpmResolutionManager,
packageJsonHandlers: ListProperty<Action<PackageJson>>,
logger: Logger
logger: Logger,
): PreparedKotlinCompilationNpmResolution {
val rootResolver = npmResolutionManager.parameters.resolution.get()
@@ -97,7 +91,6 @@ class KotlinCompilationNpmResolution(
val compilationNpmResolution: KotlinCompilationNpmResolution = rootResolver[it.projectPath][it.compilationName]
compilationNpmResolution.getResolutionOrPrepare(
npmResolutionManager,
packageJsonHandlers,
logger
)
}
@@ -126,11 +119,22 @@ class KotlinCompilationNpmResolution(
val otherNpmDependencies = toolsNpmDependencies + transitiveNpmDependencies
val allNpmDependencies = disambiguateDependencies(externalNpmDependencies, otherNpmDependencies, logger)
return PreparedKotlinCompilationNpmResolution(
npmProjectDir,
importedExternalGradleDependencies,
allNpmDependencies,
)
}
fun createPackageJson(
resolution: PreparedKotlinCompilationNpmResolution,
packageJsonHandlers: ListProperty<Action<PackageJson>>,
) {
val packageJson = packageJson(
npmProjectName,
npmProjectVersion,
npmProjectMain,
allNpmDependencies,
resolution.externalNpmDependencies,
packageJsonHandlers.get()
)
@@ -138,15 +142,7 @@ class KotlinCompilationNpmResolution(
it.execute(packageJson)
}
if (!skipWriting) {
packageJson.saveTo(npmProjectPackageJsonFile)
}
return PreparedKotlinCompilationNpmResolution(
npmProjectDir,
importedExternalGradleDependencies,
allNpmDependencies,
)
packageJson.saveTo(npmProjectPackageJsonFile)
}
private fun disambiguateDependencies(
@@ -112,12 +112,14 @@ abstract class KotlinPackageJsonTask :
@TaskAction
fun resolve() {
npmResolutionManager.get().resolution.get()[projectPath][compilationDisambiguatedName.get()]
val resolution = npmResolutionManager.get().resolution.get()[projectPath][compilationDisambiguatedName.get()]
val preparedResolution = resolution
.prepareWithDependencies(
npmResolutionManager = npmResolutionManager.get(),
packageJsonHandlers = packageJsonHandlers,
logger = logger
)
resolution.createPackageJson(preparedResolution, packageJsonHandlers)
}
companion object {