Skip to content

Commit 7ea436b

Browse files
committed
added supported for user-provided KOTLIN_OPTS (fixes #8)
1 parent c853ce3 commit 7ea436b

File tree

8 files changed

+61
-36
lines changed

8 files changed

+61
-36
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.gradle
22
build/
3+
target/
34

45
# Ignore Gradle GUI config
56
gradle-app.setting

NEWS.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
## v1.1
44

55
* Support for stdin and process substitution as script source. See [examples](examples/unit_tests.sh)
6-
* TBD: versioning and auto-update
7-
* TBD: basic command-line help
6+
* versioning and auto-update
7+
* basic command-line help
8+
* Added support for `KOTLIN_OPTS` (see [#8](https://github.com/holgerbrandl/kscript/issues/8))
89

910

10-
##
11+
## v1.0
1112
Initial Release

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,20 @@ The initial version of `kscript` was kindly contributed by Oscar Gonzalez.
110110

111111
`kscript` works better with [Intellij](https://www.jetbrains.com/idea/), since extended multi-line shebang-headers are not (yet?) supported by Intellij's' Kotlin plugin (even if the kotlin-script parser seems to be able to handle them). However, for dependency resolution, `kotlin script` relies on such mutli-line shebang headers (see [here](https://github.com/andrewoma/kotlin-script#mvncp)). In contrast, since `kscript` just works with just a standard shebang line, code parsing works very well in Intellij.
112112

113+
FAQ
114+
============
115+
116+
### How to adjust the memory the JVM running my scriptlets?
117+
118+
`kscript` allows to provide a `//KOTLIN_OPTS` line followed by parameters passed on to `kotlin` similar to how dependencies are defined:
119+
```kotlin
120+
#!/usr/bin/env kscript
121+
//KOTLIN_OPTS -J-Xmx5g -J-server
122+
123+
println("Hello from Kotlin with 5g of heap memory in server mode!")
124+
```
125+
126+
113127
Issues
114128
=======
115129

examples/more_examples.sh

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ https://www.biostars.org/p/52698/
4646

4747

4848

49-
fun main(args: Array<String>) {
50-
kutils.KscriptHelpers.processStdin { "huhu" + it }
51-
}
52-
53-
5449
kscript -s '
5550
//DEPS de.mpicbg.scicomp:kutils:0.2-SNAPSHOT
5651
kutils.KscriptHelpers.processStdin { "huhu" + it }
@@ -62,11 +57,10 @@ kscript -st '"huhu" + it'
6257

6358

6459
## REPL test: Filter-Join fasta files by ID
65-
kotlinc -classpath $(expandcp.kts de.mpicbg.scicomp:kutils:0.2-SNAPSHOT)
60+
kotlinc -classpath $(expandcp.kts de.mpicbg.scicomp:kutils:0.2)
6661
<<"EOF"
62+
import de.mpicbg.scicomp.kscript.*
6763
68-
69-
kutils.KscriptHelpers.processStdin { "huhu" + it }
70-
kscript
64+
"house\nasdf".processLines { "huhu" + it }
7165
7266
EOF

examples/scratchpad.kts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1 @@
11
#!/usr/bin/env kscript
2-
3-
import de.mpicbg.scicomp.kscript.processLines
4-
5-
println("ok")
6-
println(args.joinToString())
7-
8-
// test
9-
10-
11-
//generateSequence() { readLine() }.map {
12-
//File(args[0]).readLines().map {}
13-
14-
java.io.File(args[0]).useLines {
15-
it.map {
16-
if (!it.startsWith(">")) {
17-
it.substring(0, 20)
18-
} else it
19-
}.forEach { println(it) }
20-
}
21-
22-
23-
java.io.File(args[0]).processLines { it + "foo" }
24-

examples/unit_tests.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,31 @@ println((1+3).toString() + " test")
4747

4848
kscript <(cat .test.kts)
4949

50+
51+
52+
## (4) KOTLIN_OPTS (
53+
54+
kscript - <<"EOF" | grep "^-ea"
55+
//KOTLIN_OPTS -J-Xmx5g -J-server -J-ea
56+
57+
import java.lang.management.ManagementFactory
58+
import java.lang.management.RuntimeMXBean
59+
60+
println("Hello from Kotlin with 5g of heap memory in server mode!")
61+
62+
val bean = ManagementFactory.getRuntimeMXBean()
63+
val aList = bean.inputArguments
64+
65+
for (i in aList.indices) {
66+
println(aList[i])
67+
}
68+
69+
EOF
70+
71+
72+
73+
74+
75+
public void runtimeParameters() {
76+
77+
}

expandcp.kts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xs
8080
<artifactId>resdep_template</artifactId>
8181
<version>1.0-SNAPSHOT</version>
8282
83+
<repositories>
84+
<repository>
85+
<id>jcenter</id>
86+
<url>http://jcenter.bintray.com/</url>
87+
</repository>
88+
</repositories>
89+
8390
<dependencies>
8491
${depTags.joinToString("\n")}
8592
</dependencies>

kscript

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ if ! which expandcp.kts &> /dev/null; then
6565
fi
6666

6767
dependencies=$(grep -F "//DEPS" ${scriptFile} | head -n1 | cut -f2- -d' ' | tr ',;' ' ')
68+
kotlin_opts=$(grep -F "//KOTLIN_OPTS" ${scriptFile} | head -n1 | cut -f2- -d' ')
69+
6870
#dependencies=" org.docopt:docopt:0.6.0-SNAPSHOT log4j:log4j:1.2.14 "
6971

7072
if [ -n "$dependencies" ]; then
@@ -132,4 +134,5 @@ fi
132134
#jar ufm ${jarFile} ${mainJava}.manimain
133135
#jar tf ${jarFile}
134136

135-
exec kotlin -classpath ${jarFile}:"$classpath" Main_${className} "$@"
137+
echo KOTLIN_OPTS are ${kotlin_opts}
138+
exec kotlin ${kotlin_opts} -classpath ${jarFile}:"$classpath" Main_${className} "$@"

0 commit comments

Comments
 (0)