Skip to content

Commit

Permalink
fixed #81: Allow package declarations for scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerbrandl committed Dec 9, 2017
1 parent fe55554 commit 582acdb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/main/kotlin/kscript/app/Kscript.kt
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,15 @@ fun main(args: Array<String>) {

val wrapperSrcArg = if (scriptFileExt == "kts") {
val mainKotlin = File(createTempDir("kscript"), execClassName + ".kt")

val classReference = (script.pckg ?: "") + className

mainKotlin.writeText("""
class Main_${className}{
companion object {
@JvmStatic
fun main(args: Array<String>) {
val script = Main_${className}::class.java.classLoader.loadClass("${className}")
val script = Main_${className}::class.java.classLoader.loadClass("${classReference}")
script.getDeclaredConstructor(Array<String>::class.java).newInstance(args);
}
}
Expand All @@ -235,7 +238,6 @@ fun main(args: Array<String>) {
}



/** Determine the latest version by checking github repo and print info if newer version is available. */
private fun versionCheck() {

Expand Down
12 changes: 12 additions & 0 deletions test/resources/script_in_pckg.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env kscript

@file:KotlinOpts("-J-Xmx5g")

package foo

import KotlinOpts

//import kscript.annotations.*


println("I live in a package!")
7 changes: 7 additions & 0 deletions test/test_suite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,17 @@ assert "kscript ${KSCRIPT_HOME}/test/resources/kt_tests/default_entry_nopckg.kt"

assert "kscript ${KSCRIPT_HOME}/test/resources/kt_tests/default_entry_withpckg.kt" "main was called"


## also make sure that kts in package can be run via kscript
assert "${KSCRIPT_HOME}/test/resources/script_in_pckg.kts" "I live in a package!"


assert_end kt_support





########################################################################################################################
## custom interpreters

Expand Down

0 comments on commit 582acdb

Please sign in to comment.