migrated to kotlin 2.2.0

better support for shebangs in CLI lyng
added jlyng local release
This commit is contained in:
Sergey Chernov 2025-07-07 23:44:21 +03:00
parent 612c0fb7b9
commit ce4ed5c819
7 changed files with 50 additions and 13 deletions

14
bin/local_jrelease Executable file
View File

@ -0,0 +1,14 @@
#!/bin/bash
set -e
root=./lyng/build/install/lyng-jvm/
./gradlew :lyng:installJvmDist
#strip $file
#upx $file
rm -rf ~/bin/jlyng-jvm || true
rm ~/bin/jlyng 2>/dev/null || true
mkdir -p ~/bin/jlyng-jvm
cp -R $root ~/bin/jlyng-jvm
ln -s ~/bin/jlyng-jvm/lyng-jvm/bin/lyng ~/bin/jlyng

3
docs/samples/helloworld.lyng Normal file → Executable file
View File

@ -1,2 +1,3 @@
#!/bin/env jlyng
println("Hello, world!");
println("Hello, world2! "+ARGV);

View File

@ -1,7 +1,7 @@
[versions]
agp = "8.5.2"
clikt = "5.0.3"
kotlin = "2.1.21"
kotlin = "2.2.0"
android-minSdk = "24"
android-compileSdk = "34"
kotlinx-coroutines = "1.10.1"

View File

@ -1,5 +1,5 @@
plugins {
kotlin("multiplatform") version "2.1.21"
kotlin("multiplatform") version "2.2.0"
}
group = "net.sergeych"

View File

@ -1,11 +1,14 @@
package net.sergeych
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.Context
import com.github.ajalt.clikt.core.main
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.arguments.multiple
import com.github.ajalt.clikt.parameters.arguments.optional
import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.*
import okio.FileSystem
import okio.Path.Companion.toPath
@ -41,7 +44,23 @@ val baseScope = Scope().apply {
// }
}
class Lyng(val launcher: (suspend () -> Unit) -> Unit) : CliktCommand() {
fun runMain(args: Array<String>) {
if(args.isNotEmpty()) {
if( args.size >= 2 && args[0] == "--" ) {
// -- -file.lyng <args>
executeFileWithArgs(args[1], args.drop(2))
return
} else if( args[0][0] != '-') {
// file.lyng <args>
executeFileWithArgs(args[0], args.drop(1))
return
}
}
// normal processing
Lyng { runBlocking { it() } }.main(args)
}
private class Lyng(val launcher: (suspend () -> Unit) -> Unit) : CliktCommand() {
override val printHelpOnEmptyArgs = true
@ -55,7 +74,7 @@ class Lyng(val launcher: (suspend () -> Unit) -> Unit) : CliktCommand() {
val args by argument(help = "arguments for script").multiple()
override fun help(context: com.github.ajalt.clikt.core.Context): String =
override fun help(context: Context): String =
"""
The Lyng script language interpreter, language version is $LyngVersion.
@ -106,6 +125,13 @@ class Lyng(val launcher: (suspend () -> Unit) -> Unit) : CliktCommand() {
}
}
fun executeFileWithArgs(fileName: String, args: List<String>) {
runBlocking {
baseScope.addConst("ARGV", ObjList(args.map { ObjString(it) }.toMutableList()))
executeFile(fileName)
}
}
suspend fun executeFile(fileName: String) {
var text = FileSystem.SYSTEM.source(fileName.toPath()).use { fileSource ->
fileSource.buffer().use { bs ->

View File

@ -1,9 +1,7 @@
package net.sergeych.lyng_cli
import com.github.ajalt.clikt.core.main
import kotlinx.coroutines.runBlocking
import net.sergeych.Lyng
import net.sergeych.runMain
fun main(args: Array<String>) {
Lyng({ runBlocking { it() } }).main(args)
runMain(args)
}

View File

@ -1,7 +1,5 @@
import com.github.ajalt.clikt.core.main
import kotlinx.coroutines.runBlocking
import net.sergeych.Lyng
import net.sergeych.runMain
fun main(args: Array<String>) {
Lyng( { runBlocking { it() } }).main(args)
runMain(args)
}