[Gradle, JS] Unique name for NPM projects based on project path

^KT-56025 fixed
^KT-32209 fixed
This commit is contained in:
Ilya Goncharov
2023-03-21 11:43:37 +00:00
committed by Space Team
parent 5ce3132f9f
commit bb09395952
3 changed files with 75 additions and 17 deletions
@@ -128,8 +128,8 @@ open class NpmProject(@Transient val compilation: KotlinJsCompilation) : Seriali
val rootProjectName = project.rootProject.name
val localName = if (project != project.rootProject) {
project.name
} else null
(rootProjectName + project.path).replace(":", "-")
} else rootProjectName
val targetName = if (target.name.isNotEmpty() && target.name.toLowerCaseAsciiOnly() != "js") {
target.name
@@ -142,7 +142,6 @@ open class NpmProject(@Transient val compilation: KotlinJsCompilation) : Seriali
} else null
return sequenceOf(
rootProjectName,
localName,
targetName,
compilationName
@@ -93,15 +93,7 @@ class PackageJson(
.setPrettyPrinting()
.disableHtmlEscaping()
.serializeNulls()
.addSerializationExclusionStrategy(
object : ExclusionStrategy {
override fun shouldSkipField(f: FieldAttributes?): Boolean =
f?.name == this@PackageJson::customFields.name
override fun shouldSkipClass(clazz: Class<*>?): Boolean =
false
}
)
.registerTypeAdapterFactory(PackageJsonTypeAdapter())
.create()
packageJsonFile.ensureParentDirsCreated()
@@ -112,11 +104,6 @@ class PackageJson(
}
} else null
customFields
.forEach { (key, value) ->
val valueElement = gson.toJsonTree(value)
jsonTree.asJsonObject.add(key, valueElement)
}
if (jsonTree != previous) {
packageJsonFile.writer().use {
gson.toJson(jsonTree, it)
@@ -0,0 +1,72 @@
/*
* Copyright 2010-2023 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 org.jetbrains.kotlin.gradle.targets.js.npm
import com.google.gson.*
import com.google.gson.annotations.SerializedName
import com.google.gson.reflect.TypeToken
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonWriter
import java.lang.reflect.Field
// Necessary to get rid of all fields with null value (private, workspace etc)
// But we need to leave customFields because it is user's null value and it is ok
// Here we remove all null-valuable fields, read customFields, add them to jsonObject as elements and remove customFields field
// It helps to not get such json
// { name: "foo", private: null, customFields: { customField1: null }, customField1: null }
// but just
// { name: "foo", customField1: "foo" }
class PackageJsonTypeAdapter : TypeAdapterFactory {
override fun <T : Any?> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? {
if (type.rawType != PackageJson::class.java) return null
fun Field.serializedName() = declaredAnnotations
.filterIsInstance<SerializedName>()
.firstOrNull()?.value ?: name
val declaredFields = type.rawType.declaredFields
val customFieldsField = declaredFields
.single { it.name == PackageJson::customFields.name }
return object : TypeAdapter<T>() {
private val delegateAdapter = gson.getDelegateAdapter(this@PackageJsonTypeAdapter, type)
private val elementAdapter = gson.getAdapter(JsonElement::class.java)
override fun write(writer: JsonWriter, value: T?) {
val jsonObject = delegateAdapter.toJsonTree(value).asJsonObject
customFieldsField.isAccessible = true
val customFields = customFieldsField.get(value) as Map<*, *>
customFields
.forEach { (key, value) ->
val valueElement = gson.toJsonTree(value)
key as String
jsonObject.add(key, valueElement)
}
declaredFields
.map { it.serializedName() }
.filter { jsonObject.get(it) is JsonNull }
.forEach { jsonObject.remove(it) }
jsonObject.remove(customFieldsField.serializedName())
val originalSerializeNulls = writer.serializeNulls
writer.serializeNulls = true
elementAdapter.write(writer, jsonObject)
writer.serializeNulls = originalSerializeNulls
}
override fun read(reader: JsonReader): T {
return delegateAdapter.read(reader)
}
}
}
}