Skip to content

Commit

Permalink
Remove usage of Dukat
Browse files Browse the repository at this point in the history
Dukat results are still very suboptimal and in Kotlin 1.8.20-Beta it was disabled.
As most usages were already replaced by kotlin-wrappers or self-written class,
now the last remaining three classes are hand-written too and Dukat is not necessary,
including the massive post-processing just to make it compile.
This will save the time for running Dukat, for doing the post processing,
and for compiling quite some unncessary classes.
If Dukat is improved and added back to the Gradle plugin integration,
this decision can be reconsidered, but for now it saves much time and is better
future proof in the light of 1.8.20-Beta.
  • Loading branch information
Vampire committed Feb 16, 2023
1 parent d374368 commit aadcf83
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 575 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,5 @@ dependencies {
implementation(platform(libs.kotlin.wrappers.bom))
implementation(libs.kotlin.wrapper.js)
implementation(libs.kotlin.wrapper.node)
implementation(npm(libs.build.vercel.ncc, generateExternals = false))
implementation(npm(libs.build.vercel.ncc))
}
129 changes: 1 addition & 128 deletions gradle/build-logic/src/main/kotlin/net/kautler/node.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@

package net.kautler

import java.security.MessageDigest
import kotlin.text.RegexOption.MULTILINE
import net.kautler.dao.action.GitHubAction
import net.kautler.util.npm
import org.gradle.accessors.dm.LibrariesForLibs
import org.jetbrains.kotlin.gradle.targets.js.dukat.IntegratedDukatTask
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsExec
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension
import org.yaml.snakeyaml.Yaml
Expand Down Expand Up @@ -83,50 +80,6 @@ val libs = the<LibrariesForLibs>()

configure<NodeJsRootExtension> {
nodeVersion = libs.versions.build.node.get()
versions.dukat.version = libs.versions.build.dukat.get()

// work-around for https://github.com/Kotlin/dukat/issues/103
npmInstallTaskProvider!!.configure {
val patchedDukat0012CliJs = layout
.projectDirectory
.file("resources/dukat-cli-0.0.12.js")

val patchedDukat057CliJs = layout
.projectDirectory
.file("resources/dukat-cli-0.5.7.js")

inputs
.files(patchedDukat0012CliJs, patchedDukat057CliJs)
.withPropertyName("patched dukat-cli.js files")

doLast {
val dukatCliJs = rootPackageDir.resolve("node_modules/dukat/bin/dukat-cli.js")

if (dukatCliJs.exists()) {
val sha256 = MessageDigest
.getInstance("SHA-256")
.digest(dukatCliJs.readBytes())
.joinToString("") { "%02x".format(it) }

when (sha256) {
// already a patched version
"32b7c91ecf8c94b8f1e554da870a617437e421db07e40d558dbb959bd221cb5e",
"e16d684be9b90c2d5d78f90077dc9690253fd4d7bc6b48f94bbd48b1692bf2d5" ->
return@doLast

// original 0.0.12 version
"ceebdfbb94d103eb44da265b572e51fb69ff4bb1c524b94f621c6a6e94716ac8" ->
patchedDukat0012CliJs.asFile.copyTo(dukatCliJs, overwrite = true)

// original 0.5.7 version
"12645e1491b68638d2c1c4146502ae9ed5037baa82cc12e6c55070471d962187" ->
patchedDukat057CliJs.asFile.copyTo(dukatCliJs, overwrite = true)

else -> throw RuntimeException("dukat-cli.js has unexpected checksum $sha256")
}
}
}
}
}

dependencies {
Expand All @@ -135,94 +88,14 @@ dependencies {
implementation(libs.kotlin.wrapper.actions.toolkit)
implementation(libs.kotlin.wrapper.js)
implementation(libs.kotlin.wrapper.node)
implementation(npm(libs.types.semver))
implementation(npm(libs.semver, generateExternals = false))
implementation(npm(libs.semver))
implementation(npm(libs.nullWritable))
}

tasks.withType(IntegratedDukatTask::class).configureEach {
doLast {
// work-around for https://github.com/Kotlin/dukat/issues/397
deleteExternalsFiles(
this,
"*.module_node.kt",
"*.nonDeclarations.kt"
)
fixExternalsFiles(
this,
"lib.dom.kt" to listOf(
"""\Qimport url.URL as _URL\E$""" to "import node.url.URL as _URL",
"""\Qimport url.URLSearchParams as _URLSearchParams\E$""" to "import node.url.URLSearchParams as _URLSearchParams",
"""\Qoverride fun addEventListener(type: String, listener: EventListenerObject\E""" to "fun addEventListener(type: String, listener: EventListenerObject",
"""\Qoverride fun removeEventListener(type: String, callback: EventListenerObject\E""" to "fun removeEventListener(type: String, callback: EventListenerObject",
"""\Q`T$19`\E""" to """`T\$19`<R, T>"""
),
// work-around for https://github.com/Kotlin/dukat/issues/402
"lib.es2018.asynciterable.module_dukat.kt" to listOf(
"""\Qval `return`: ((value: TReturn) -> Promise<dynamic /* IteratorYieldResult<T> | IteratorReturnResult<TReturn> */>)?\E$""" to "val `return`: ((value: dynamic) -> Promise<dynamic /* IteratorYieldResult<T> | IteratorReturnResult<TReturn> */>)?",
"""\Qval `return`: ((value: PromiseLike<TReturn>) -> Promise<dynamic /* IteratorYieldResult<T> | IteratorReturnResult<TReturn> */>)?\E\r?\n\Q get() = definedExternally\E\r?\n""" to ""
),
"lib.es2020.bigint.module_dukat.kt" to listOf(
"""\Q : RelativeIndexable<Any>\E""" to ""
),
"lib.es5.kt" to listOf(
"""\Qimport NodeJS.CallSite\E$""" to "import node.CallSite",
"""\Qval resolve: ((specified: String, parent: URL) -> Promise<String>)?\E$""" to "val resolve2: ((specified: String, parent: URL) -> Promise<String>)?"
),
// work-around for https://github.com/Kotlin/dukat/issues/401
"null-writable.module_null-writable.kt" to listOf(
"""\Qimport stream.internal.`T$17`\E$""" to "",
"""\Qimport stream.internal.Writable\E$""" to "import node.stream.Writable",
"""\Qoverride fun _write(_chunk: Any, _encoding: String, callback: (error: Error?) -> Unit)\E$""" to "override fun _write(chunk: Any, encoding: node.buffer.BufferEncoding, callback: (error: Error?) -> Unit)",
"""\Qopen fun _writev(_chunks: Array<`T$3`>, callback: (error: Error?) -> Unit)\E$""" to "",
"""\Qoverride fun _writev(chunks: Array<`T$17`>, callback: (error: Error?) -> Unit)\E$""" to ""
),
"semver.module_semver.kt" to listOf(
"""\Q@JsModule("semver")\E$""" to """@JsModule("semver/classes/semver")"""
)
)
}
}

tasks.assemble {
dependsOn(project(":ncc-packer").tasks.named("nodeProductionRun"))
}

fun deleteExternalsFiles(task: Task, vararg files: String) {
if (files.isNotEmpty()) {
task
.outputs
.files
.asFileTree
.matching {
for (file in files) {
include("**/$file")
}
}
.forEach { it.delete() }
}
}

fun fixExternalsFiles(task: Task, vararg pairs: Pair<String, List<Pair<String, String>>>) {
for ((file, fixups) in pairs) {
task
.outputs
.files
.asFileTree
.matching { include("**/$file") }
.singleFile
.apply {
writeText(
fixups
.map { (pattern, replacement) -> pattern.toRegex(MULTILINE) to replacement }
.fold(readText()) { current, (regex, replacement) ->
regex.replace(current, replacement)
}
)
}
}
}

fun plugin(plugin: Provider<PluginDependency>) = plugin.map {
"${it.pluginId}:${it.pluginId}.gradle.plugin:${it.version.requiredVersion}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ import org.jetbrains.kotlin.gradle.targets.js.npm.NpmDependency
import org.jetbrains.kotlin.gradle.targets.js.npm.NpmDependencyExtension

fun DependencyHandler.npm(
dependency: Provider<MinimalExternalModuleDependency>,
generateExternals: Boolean = true
dependency: Provider<MinimalExternalModuleDependency>
): NpmDependency {
val dep = dependency.get()
return (extensions.getByName("npm") as NpmDependencyExtension)(
name = if (dep.group == "<unscoped>") dep.name else "@${dep.group}/${dep.name}",
version = dep.version!!,
generateExternals = generateExternals
version = dep.version!!
)
}
3 changes: 0 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.

[versions]
build-dukat = "0.5.7"
build-github-api = "1.117"
build-gradle-plugin-dependency-analysis = "1.19.0"
build-gradle-plugin-github = "1.4.0"
Expand All @@ -32,7 +31,6 @@ kotlin-wrappers = "1.0.0-pre.498"
kotlinx-coroutines = "1.6.4"
nullWritable = "1.0.5"
semver = "7.3.8"
types-semver = "7.3.13"
workflows-kotlin = "1.8.10"

[libraries]
Expand All @@ -50,7 +48,6 @@ kotlin-wrappers-bom = { module = "org.jetbrains.kotlin-wrappers:kotlin-wrappers-
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" }
nullWritable = { module = "<unscoped>:null-writable", version.ref = "nullWritable" }
semver = { module = "<unscoped>:semver", version.ref = "semver" }
types-semver = { module = "types:semver", version.ref = "types-semver" }
workflows-kotlin-compiler = { module = "org.jetbrains.kotlin:kotlin-compiler", version.ref = "workflows-kotlin" }
workflows-kotlin-main-kts = { module = "org.jetbrains.kotlin:kotlin-main-kts", version.ref = "workflows-kotlin" }
workflows-kotlin-scripting-compiler = { module = "org.jetbrains.kotlin:kotlin-scripting-compiler", version.ref = "workflows-kotlin" }
Expand Down
13 changes: 0 additions & 13 deletions kotlin-js-store/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,6 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.13.0.tgz#0400d1e6ce87e9d3032c19eb6c58205b0d3f7850"
integrity sha512-gC3TazRzGoOnoKAhUx+Q0t8S9Tzs74z7m0ipwGpSqQrleP14hKxP4/JUeEQcD3W1/aIpnWl8pHowI7WokuZpXg==

"@types/semver@7.3.13":
version "7.3.13"
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.3.13.tgz#da4bfd73f49bd541d28920ab0e2bf0ee80f71c91"
integrity sha512-21cFJr9z3g5dW8B0CVI9g2O9beqaThGQ6ZFBqHfwhzLDKUxaqTIy3vnfah/UPkfOiF2pLq+tGz+W8RyCskuslw==

"@types/tunnel@^0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@types/tunnel/-/tunnel-0.0.3.tgz#f109e730b072b3136347561fc558c9358bb8c6e9"
Expand Down Expand Up @@ -399,14 +394,6 @@ diff@5.0.0:
resolved "https://registry.yarnpkg.com/diff/-/diff-5.0.0.tgz#7ed6ad76d859d030787ec35855f5b1daf31d852b"
integrity sha512-/VTCrvm5Z0JGty/BWHljh+BAiw3IK+2j87NGMu8Nwc/f48WoDAC395uomO9ZD117ZOBaHmkX1oyLvkVM/aIT3w==

dukat@0.5.7:
version "0.5.7"
resolved "https://registry.yarnpkg.com/dukat/-/dukat-0.5.7.tgz#4b3981f3fbde993546cc6f674b9027923f7293c6"
integrity sha512-PBM+B1oIE/aHbcrtdqX8yHfwdfdJ5ejJzkbU2LOW7bbAC3r1EcEyrS9ytFdHMPv06gVkF2QO77zGcMbGcsmXaA==
dependencies:
google-protobuf "3.12.2"
typescript "3.9.5"

dukat@0.5.8-rc.4:
version "0.5.8-rc.4"
resolved "https://registry.yarnpkg.com/dukat/-/dukat-0.5.8-rc.4.tgz#90384dcb50b14c26f0e99dae92b2dea44f5fce21"
Expand Down
Loading

0 comments on commit aadcf83

Please sign in to comment.