diff --git a/simple_test.kts b/simple_test.kts new file mode 100644 index 00000000..90bc5b55 --- /dev/null +++ b/simple_test.kts @@ -0,0 +1,7 @@ +package kscript.scriplet + +fun main() { + println("Hello from simple_test.kts") + println("Kotlin version: ${System.getProperty("kotlin.version")}") + println("Java version: ${System.getProperty("java.version")}") +} diff --git a/src/main/kotlin/io/github/kscripting/kscript/code/Templates.kt b/src/main/kotlin/io/github/kscripting/kscript/code/Templates.kt index d64cfbea..43b7301f 100644 --- a/src/main/kotlin/io/github/kscripting/kscript/code/Templates.kt +++ b/src/main/kotlin/io/github/kscripting/kscript/code/Templates.kt @@ -46,14 +46,18 @@ object Templates { } fun createWrapperForScript(packageName: PackageName, className: String): String { - val classReference = packageName.value + "." + className + // val classReference = packageName.value + "." + className // Original + // Assuming the script class compiled by kotlinc will be named Kt + // and be at the root of the JAR if the input script had no package declaration + // and ScriptUtils.resolveCode also doesn't add one (if packageName is null/empty). + val targetClassName = "${className}Kt" return """ |class Main_${className}{ | companion object { | @JvmStatic | fun main(args: Array) { - | val script = Main_${className}::class.java.classLoader.loadClass("$classReference") + | val script = Main_${className}::class.java.classLoader.loadClass("$targetClassName") | script.getDeclaredConstructor(Array::class.java).newInstance(args); | } | } diff --git a/src/main/kotlin/io/github/kscripting/kscript/model/ConfigBuilder.kt b/src/main/kotlin/io/github/kscripting/kscript/model/ConfigBuilder.kt index a9468977..a984b25d 100644 --- a/src/main/kotlin/io/github/kscripting/kscript/model/ConfigBuilder.kt +++ b/src/main/kotlin/io/github/kscripting/kscript/model/ConfigBuilder.kt @@ -61,7 +61,16 @@ class ConfigBuilder( val kotlinHomeDir: OsPath = kotlinHomeDir ?: environment.getEnvVariableOrNull("KOTLIN_HOME")?.toOsPathFromNative() ?: ShellUtils.guessKotlinHome(osType)?.toOsPathFromNative() - ?: throw IllegalStateException("KOTLIN_HOME is not set and could not be inferred from context.") + ?: throw IllegalStateException( + "kscript requires a Kotlin installation.\n" + + "KOTLIN_HOME is not set and it could not be inferred from the system PATH.\n\n" + + "Please take one of the following actions:\n" + + " 1. Set the KOTLIN_HOME environment variable to point to your Kotlin installation directory.\n" + + " Example: export KOTLIN_HOME=/path/to/your/kotlin/kotlinc\n" + + " 2. Ensure 'kotlinc' is available in your system PATH. Installing Kotlin via SDKMAN (sdk install kotlin) or Homebrew (brew install kotlin) usually handles this.\n\n" + + "Note: Even if your script specifies a specific Kotlin version using the '//KOTLIN_VERSION' directive, \n" + + "kscript currently needs a base Kotlin installation to be discoverable to operate." + ) val configFile: OsPath = configFile ?: kscriptDir?.resolve("kscript.properties") ?: when { osType.isWindowsLike() -> environment.getEnvVariableOrNull("LOCALAPPDATA")?.toOsPathFromNative() diff --git a/src/main/kotlin/io/github/kscripting/kscript/resolver/CommandResolver.kt b/src/main/kotlin/io/github/kscripting/kscript/resolver/CommandResolver.kt index 8aa30d1a..5c4d5d3a 100644 --- a/src/main/kotlin/io/github/kscripting/kscript/resolver/CommandResolver.kt +++ b/src/main/kotlin/io/github/kscripting/kscript/resolver/CommandResolver.kt @@ -7,6 +7,7 @@ import io.github.kscripting.kscript.model.OsConfig import io.github.kscripting.shell.model.OsPath import io.github.kscripting.shell.model.OsType import io.github.kscripting.shell.model.toNativeOsPath +import java.io.File // ADDED IMPORT class CommandResolver(val osConfig: OsConfig) { private val classPathSeparator = @@ -49,9 +50,23 @@ class CommandResolver(val osConfig: OsConfig) { val compilerOptsStr = resolveCompilerOpts(compilerOpts) val classpath = resolveClasspath(dependencies) val jarFile = resolveJarFile(jar) - val files = resolveFiles(filePaths) + val files = resolveFiles(filePaths) // This 'files' variable contains the paths to the script(s) to be compiled val kotlinc = resolveKotlinBinary("kotlinc") + // START OF MODIFICATION: Print content of files to be compiled + filePaths.forEach { osPath -> + try { + val actualFilePath = osPath.toNativeOsPath().stringPath() + val content = File(actualFilePath).readText(Charsets.UTF_8) // Use actualFilePath + println(" KOTLINC_INPUT_CODE_START ") + println(content) + println(" KOTLINC_INPUT_CODE_END ") + } catch (e: Exception) { + println(" KOTLINC_INPUT_CODE_ERROR Failed to read content of $osPath: ${e.message} ") + } + } + // END OF MODIFICATION + return "$kotlinc $compilerOptsStr $classpath -d $jarFile $files" } diff --git a/src/main/kotlin/io/github/kscripting/kscript/util/ScriptUtils.kt b/src/main/kotlin/io/github/kscripting/kscript/util/ScriptUtils.kt index 93a8b25e..68bf0c16 100644 --- a/src/main/kotlin/io/github/kscripting/kscript/util/ScriptUtils.kt +++ b/src/main/kotlin/io/github/kscripting/kscript/util/ScriptUtils.kt @@ -40,6 +40,7 @@ object ScriptUtils { return if (code.contains("fun main")) ScriptType.KT else ScriptType.KTS } + // Restored original logic fun resolveCode(packageName: PackageName?, importNames: Set, scriptNode: ScriptNode): String { val sortedImports = importNames.sortedBy { it.value }.toList() val sb = StringBuilder() diff --git a/test_script_1.kts b/test_script_1.kts new file mode 100644 index 00000000..6bcd8d62 --- /dev/null +++ b/test_script_1.kts @@ -0,0 +1,3 @@ +//KOTLIN_ARGS -J-Dkotlin.main.kts.jvm.target=11 +//KOTLIN_VERSION = 2.2.1-RC +println("Hello from Kotlin 2.2.1-RC!") diff --git a/test_script_5.kts b/test_script_5.kts new file mode 100644 index 00000000..1d30e684 --- /dev/null +++ b/test_script_5.kts @@ -0,0 +1,6 @@ +//KOTLIN_ARGS -J-Dkotlin.main.kts.jvm.target=11 +//KOTLIN_VERSION = 2.2.1-RC +data class Message(val text: String) +fun createMessage(name: String): Message = Message("Hello ${'$'}name from Kotlin 2.2.1-RC!") +val message = createMessage("kscript") +println(message.text) diff --git a/version_test.kts b/version_test.kts new file mode 100644 index 00000000..cd3c7a95 --- /dev/null +++ b/version_test.kts @@ -0,0 +1,3 @@ +//KOTLIN_VERSION 2.2.0-RC +println("Hello from Kotlin ${System.getProperty("kotlin.version")}") +println("Java version: ${System.getProperty("java.version")}")