[K/JS] Introduce the ability to consume platform-specific cli arguments inside the main function ^KT-16981 Fixed

This commit is contained in:
Artem Kobzar
2023-11-18 11:01:34 +00:00
committed by Space Team
parent ceded87a3a
commit 1832f5a3f7
15 changed files with 93 additions and 3 deletions
@@ -47,6 +47,24 @@ class Kotlin2JsIrGradlePluginIT : KGPBaseTest() {
}
}
@DisplayName("nodejs main function arguments")
@GradleTest
fun testNodeJsMainArguments(gradleVersion: GradleVersion) {
project("kotlin-js-nodejs-project", gradleVersion) {
buildGradle.appendText(
"""
|
|tasks.withType(org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsExec).configureEach {
| args += ["test", "'Hello, World'"]
|}
""".trimMargin()
)
build("nodeRun") {
assertOutputContains("ACCEPTED: test;'Hello, World'")
}
}
}
@DisplayName("stale output is cleaned")
@GradleTest
fun testCleanOutputWithEmptySources(gradleVersion: GradleVersion) {
@@ -17,6 +17,7 @@ kotlin {
useCommonJs()
binaries.executable()
nodejs {
passProcessArgvToMainFunction()
}
}
}
@@ -11,4 +11,8 @@ fun best(): Int {
fun simpleBest(): Int {
return 73
}
fun main(args: Array<String>) {
println("ACCEPTED: ${args.drop(2).joinToString(";")}")
}
@@ -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.
*/
package org.jetbrains.kotlin.gradle.targets.js.dsl
@RequiresOptIn(level = RequiresOptIn.Level.WARNING)
@Target(AnnotationTarget.FUNCTION)
annotation class ExperimentalMainFunctionArgumentsDsl
@@ -47,6 +47,12 @@ interface KotlinJsTargetDsl : KotlinTarget, KotlinTargetWithNodeJsDsl {
fun useCommonJs()
fun useEsModules()
/**
* The function accepts [jsExpression] and puts this expression as the "args: Array<String>" argument in place of main-function call
*/
@ExperimentalMainFunctionArgumentsDsl
fun passAsArgumentToMainFunction(jsExpression: String)
fun generateTypeScriptDefinitions()
val binaries: KotlinJsBinaryContainer
@@ -98,4 +104,7 @@ interface KotlinJsBrowserDsl : KotlinJsSubTargetDsl {
interface KotlinJsNodeDsl : KotlinJsSubTargetDsl {
fun runTask(body: Action<NodeJsExec>)
@ExperimentalMainFunctionArgumentsDsl
fun passProcessArgvToMainFunction()
}
@@ -433,6 +433,19 @@ constructor(
}
override fun passAsArgumentToMainFunction(jsExpression: String) {
compilations
.all {
it.binaries
.withType(JsIrBinary::class.java)
.all {
it.linkTask.configure { linkTask ->
linkTask.compilerOptions.freeCompilerArgs.add("-Xplatform-arguments-in-main-function=$jsExpression")
}
}
}
}
private fun KotlinJsOptions.configureCommonJsOptions() {
moduleKind = "commonjs"
sourceMap = true
@@ -32,6 +32,10 @@ abstract class KotlinNodeJsIr @Inject constructor(target: KotlinJsIrTarget) :
project.tasks.withType<NodeJsExec>().named(runTaskName).configure(body)
}
override fun passProcessArgvToMainFunction() {
target.passAsArgumentToMainFunction("process.argv")
}
override fun locateOrRegisterRunTask(binary: JsIrBinary, name: String) {
if (project.locateTask<NodeJsExec>(name) != null) return