Skip to content

Commit 582acdb

Browse files
committed
fixed #81: Allow package declarations for scripts
1 parent fe55554 commit 582acdb

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/main/kotlin/kscript/app/Kscript.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,15 @@ fun main(args: Array<String>) {
204204

205205
val wrapperSrcArg = if (scriptFileExt == "kts") {
206206
val mainKotlin = File(createTempDir("kscript"), execClassName + ".kt")
207+
208+
val classReference = (script.pckg ?: "") + className
209+
207210
mainKotlin.writeText("""
208211
class Main_${className}{
209212
companion object {
210213
@JvmStatic
211214
fun main(args: Array<String>) {
212-
val script = Main_${className}::class.java.classLoader.loadClass("${className}")
215+
val script = Main_${className}::class.java.classLoader.loadClass("${classReference}")
213216
script.getDeclaredConstructor(Array<String>::class.java).newInstance(args);
214217
}
215218
}
@@ -235,7 +238,6 @@ fun main(args: Array<String>) {
235238
}
236239

237240

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

test/resources/script_in_pckg.kts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env kscript
2+
3+
@file:KotlinOpts("-J-Xmx5g")
4+
5+
package foo
6+
7+
import KotlinOpts
8+
9+
//import kscript.annotations.*
10+
11+
12+
println("I live in a package!")

test/test_suite.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,17 @@ assert "kscript ${KSCRIPT_HOME}/test/resources/kt_tests/default_entry_nopckg.kt"
187187

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

190+
191+
## also make sure that kts in package can be run via kscript
192+
assert "${KSCRIPT_HOME}/test/resources/script_in_pckg.kts" "I live in a package!"
193+
194+
190195
assert_end kt_support
191196

192197

193198

199+
200+
194201
########################################################################################################################
195202
## custom interpreters
196203

0 commit comments

Comments
 (0)