[Gradle, JS] Distinguish creating prepared resolution and writing package.json
^KT-61326 fixed
This commit is contained in:
committed by
Space Team
parent
2472999242
commit
355ad3e641
+35
@@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+19
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+13
@@ -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)
|
||||||
+16
@@ -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())
|
||||||
|
}
|
||||||
|
}
|
||||||
+20
@@ -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 {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+14
@@ -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
|
||||||
|
}
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
plugins {
|
||||||
|
kotlin("js").apply(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
group = "com.example"
|
||||||
|
version = "1.0"
|
||||||
|
|
||||||
|
allprojects {
|
||||||
|
repositories {
|
||||||
|
mavenLocal()
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
}
|
||||||
+16
@@ -0,0 +1,16 @@
|
|||||||
|
plugins {
|
||||||
|
kotlin("js")
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation(kotlin("stdlib-js"))
|
||||||
|
implementation(project(":base"))
|
||||||
|
}
|
||||||
|
|
||||||
|
kotlin {
|
||||||
|
js {
|
||||||
|
useCommonJs()
|
||||||
|
browser {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+14
@@ -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()
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
rootProject.name = "kotlin-js-browser"
|
||||||
|
|
||||||
|
include("base")
|
||||||
|
include("lib")
|
||||||
|
include("app")
|
||||||
+2
-2
@@ -16,6 +16,7 @@ import org.gradle.api.tasks.OutputFile
|
|||||||
import org.gradle.api.tasks.TaskAction
|
import org.gradle.api.tasks.TaskAction
|
||||||
import org.gradle.work.DisableCachingByDefault
|
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.NpmProject.Companion.PACKAGE_JSON
|
||||||
|
import org.jetbrains.kotlin.gradle.targets.js.npm.resolved.PreparedKotlinCompilationNpmResolution
|
||||||
import org.jetbrains.kotlin.gradle.utils.property
|
import org.jetbrains.kotlin.gradle.utils.property
|
||||||
import java.io.File
|
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()]
|
get() = npmResolutionManager.get().resolution.get()[projectPath][compilationDisambiguatedName.get()]
|
||||||
.getResolutionOrPrepare(
|
.getResolutionOrPrepare(
|
||||||
npmResolutionManager.get(),
|
npmResolutionManager.get(),
|
||||||
packageJsonHandlers,
|
|
||||||
logger
|
logger
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
+11
-3
@@ -16,7 +16,7 @@ import java.io.Serializable
|
|||||||
class KotlinRootNpmResolution(
|
class KotlinRootNpmResolution(
|
||||||
val projects: Map<String, KotlinProjectNpmResolution>,
|
val projects: Map<String, KotlinProjectNpmResolution>,
|
||||||
val rootProjectName: String,
|
val rootProjectName: String,
|
||||||
val rootProjectVersion: String
|
val rootProjectVersion: String,
|
||||||
) : Serializable {
|
) : Serializable {
|
||||||
operator fun get(project: String) = projects[project] ?: KotlinProjectNpmResolution.empty()
|
operator fun get(project: String) = projects[project] ?: KotlinProjectNpmResolution.empty()
|
||||||
|
|
||||||
@@ -27,12 +27,20 @@ class KotlinRootNpmResolution(
|
|||||||
logger: Logger,
|
logger: Logger,
|
||||||
npmEnvironment: NpmEnvironment,
|
npmEnvironment: NpmEnvironment,
|
||||||
yarnEnvironment: YarnEnvironment,
|
yarnEnvironment: YarnEnvironment,
|
||||||
npmResolutionManager: KotlinNpmResolutionManager
|
npmResolutionManager: KotlinNpmResolutionManager,
|
||||||
): Installation {
|
): Installation {
|
||||||
synchronized(projects) {
|
synchronized(projects) {
|
||||||
npmResolutionManager.parameters.gradleNodeModulesProvider.get().close()
|
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.packageManager.prepareRootProject(
|
||||||
npmEnvironment,
|
npmEnvironment,
|
||||||
rootProjectName,
|
rootProjectName,
|
||||||
|
|||||||
+21
-25
@@ -29,7 +29,7 @@ class KotlinCompilationNpmResolution(
|
|||||||
val npmProjectMain: String,
|
val npmProjectMain: String,
|
||||||
val npmProjectPackageJsonFile: File,
|
val npmProjectPackageJsonFile: File,
|
||||||
val npmProjectDir: File,
|
val npmProjectDir: File,
|
||||||
val tasksRequirements: TasksRequirements
|
val tasksRequirements: TasksRequirements,
|
||||||
) : Serializable {
|
) : Serializable {
|
||||||
|
|
||||||
val inputs: PackageJsonProducerInputs
|
val inputs: PackageJsonProducerInputs
|
||||||
@@ -45,17 +45,13 @@ class KotlinCompilationNpmResolution(
|
|||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
fun prepareWithDependencies(
|
fun prepareWithDependencies(
|
||||||
skipWriting: Boolean = false,
|
|
||||||
npmResolutionManager: KotlinNpmResolutionManager,
|
npmResolutionManager: KotlinNpmResolutionManager,
|
||||||
packageJsonHandlers: ListProperty<Action<PackageJson>>,
|
logger: Logger,
|
||||||
logger: Logger
|
|
||||||
): PreparedKotlinCompilationNpmResolution {
|
): PreparedKotlinCompilationNpmResolution {
|
||||||
check(resolution == null) { "$this already resolved" }
|
check(resolution == null) { "$this already resolved" }
|
||||||
|
|
||||||
return createPreparedResolution(
|
return createPreparedResolution(
|
||||||
skipWriting,
|
|
||||||
npmResolutionManager,
|
npmResolutionManager,
|
||||||
packageJsonHandlers,
|
|
||||||
logger
|
logger
|
||||||
).also {
|
).also {
|
||||||
resolution = it
|
resolution = it
|
||||||
@@ -65,30 +61,28 @@ class KotlinCompilationNpmResolution(
|
|||||||
@Synchronized
|
@Synchronized
|
||||||
fun getResolutionOrPrepare(
|
fun getResolutionOrPrepare(
|
||||||
npmResolutionManager: KotlinNpmResolutionManager,
|
npmResolutionManager: KotlinNpmResolutionManager,
|
||||||
packageJsonHandlers: ListProperty<Action<PackageJson>>,
|
|
||||||
logger: Logger,
|
logger: Logger,
|
||||||
): PreparedKotlinCompilationNpmResolution {
|
): PreparedKotlinCompilationNpmResolution {
|
||||||
|
|
||||||
return resolution ?: prepareWithDependencies(
|
return resolution ?: prepareWithDependencies(
|
||||||
skipWriting = true,
|
|
||||||
npmResolutionManager,
|
npmResolutionManager,
|
||||||
packageJsonHandlers,
|
|
||||||
logger
|
logger
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
fun close(): PreparedKotlinCompilationNpmResolution {
|
fun close(
|
||||||
|
npmResolutionManager: KotlinNpmResolutionManager,
|
||||||
|
logger: Logger,
|
||||||
|
): PreparedKotlinCompilationNpmResolution {
|
||||||
check(!closed) { "$this already closed" }
|
check(!closed) { "$this already closed" }
|
||||||
closed = true
|
closed = true
|
||||||
return resolution!!
|
return getResolutionOrPrepare(npmResolutionManager, logger)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun createPreparedResolution(
|
fun createPreparedResolution(
|
||||||
skipWriting: Boolean,
|
|
||||||
npmResolutionManager: KotlinNpmResolutionManager,
|
npmResolutionManager: KotlinNpmResolutionManager,
|
||||||
packageJsonHandlers: ListProperty<Action<PackageJson>>,
|
logger: Logger,
|
||||||
logger: Logger
|
|
||||||
): PreparedKotlinCompilationNpmResolution {
|
): PreparedKotlinCompilationNpmResolution {
|
||||||
val rootResolver = npmResolutionManager.parameters.resolution.get()
|
val rootResolver = npmResolutionManager.parameters.resolution.get()
|
||||||
|
|
||||||
@@ -97,7 +91,6 @@ class KotlinCompilationNpmResolution(
|
|||||||
val compilationNpmResolution: KotlinCompilationNpmResolution = rootResolver[it.projectPath][it.compilationName]
|
val compilationNpmResolution: KotlinCompilationNpmResolution = rootResolver[it.projectPath][it.compilationName]
|
||||||
compilationNpmResolution.getResolutionOrPrepare(
|
compilationNpmResolution.getResolutionOrPrepare(
|
||||||
npmResolutionManager,
|
npmResolutionManager,
|
||||||
packageJsonHandlers,
|
|
||||||
logger
|
logger
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -126,11 +119,22 @@ class KotlinCompilationNpmResolution(
|
|||||||
val otherNpmDependencies = toolsNpmDependencies + transitiveNpmDependencies
|
val otherNpmDependencies = toolsNpmDependencies + transitiveNpmDependencies
|
||||||
val allNpmDependencies = disambiguateDependencies(externalNpmDependencies, otherNpmDependencies, logger)
|
val allNpmDependencies = disambiguateDependencies(externalNpmDependencies, otherNpmDependencies, logger)
|
||||||
|
|
||||||
|
return PreparedKotlinCompilationNpmResolution(
|
||||||
|
npmProjectDir,
|
||||||
|
importedExternalGradleDependencies,
|
||||||
|
allNpmDependencies,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun createPackageJson(
|
||||||
|
resolution: PreparedKotlinCompilationNpmResolution,
|
||||||
|
packageJsonHandlers: ListProperty<Action<PackageJson>>,
|
||||||
|
) {
|
||||||
val packageJson = packageJson(
|
val packageJson = packageJson(
|
||||||
npmProjectName,
|
npmProjectName,
|
||||||
npmProjectVersion,
|
npmProjectVersion,
|
||||||
npmProjectMain,
|
npmProjectMain,
|
||||||
allNpmDependencies,
|
resolution.externalNpmDependencies,
|
||||||
packageJsonHandlers.get()
|
packageJsonHandlers.get()
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -138,15 +142,7 @@ class KotlinCompilationNpmResolution(
|
|||||||
it.execute(packageJson)
|
it.execute(packageJson)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!skipWriting) {
|
packageJson.saveTo(npmProjectPackageJsonFile)
|
||||||
packageJson.saveTo(npmProjectPackageJsonFile)
|
|
||||||
}
|
|
||||||
|
|
||||||
return PreparedKotlinCompilationNpmResolution(
|
|
||||||
npmProjectDir,
|
|
||||||
importedExternalGradleDependencies,
|
|
||||||
allNpmDependencies,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun disambiguateDependencies(
|
private fun disambiguateDependencies(
|
||||||
|
|||||||
+4
-2
@@ -112,12 +112,14 @@ abstract class KotlinPackageJsonTask :
|
|||||||
|
|
||||||
@TaskAction
|
@TaskAction
|
||||||
fun resolve() {
|
fun resolve() {
|
||||||
npmResolutionManager.get().resolution.get()[projectPath][compilationDisambiguatedName.get()]
|
val resolution = npmResolutionManager.get().resolution.get()[projectPath][compilationDisambiguatedName.get()]
|
||||||
|
val preparedResolution = resolution
|
||||||
.prepareWithDependencies(
|
.prepareWithDependencies(
|
||||||
npmResolutionManager = npmResolutionManager.get(),
|
npmResolutionManager = npmResolutionManager.get(),
|
||||||
packageJsonHandlers = packageJsonHandlers,
|
|
||||||
logger = logger
|
logger = logger
|
||||||
)
|
)
|
||||||
|
|
||||||
|
resolution.createPackageJson(preparedResolution, packageJsonHandlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|||||||
Reference in New Issue
Block a user