Skip to content

Jules/kscript investigation #426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions simple_test.kts
Original file line number Diff line number Diff line change
@@ -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")}")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <className>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<String>) {
| val script = Main_${className}::class.java.classLoader.loadClass("$classReference")
| val script = Main_${className}::class.java.classLoader.loadClass("$targetClassName")
| script.getDeclaredConstructor(Array<String>::class.java).newInstance(args);
| }
| }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ImportName>, scriptNode: ScriptNode): String {
val sortedImports = importNames.sortedBy { it.value }.toList()
val sb = StringBuilder()
Expand Down
3 changes: 3 additions & 0 deletions version_test.kts
Original file line number Diff line number Diff line change
@@ -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")}")
Loading