Skip to content

Commit a4d80f3

Browse files
committed
impl support for @file:DependsOn (towards #42)
1 parent 7bf014c commit a4d80f3

File tree

5 files changed

+69
-4
lines changed

5 files changed

+69
-4
lines changed

misc/docker_tests.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ cd ${KSCRIPT_HOME}/misc
88
docker build -t kscript_tester .
99
docker run -it kscript_tester
1010

11+
12+
#docker rm `docker ps -q -l` # restart it in the background
13+
docker start `docker ps -q -l` # restart it in the background
14+
docker attach `docker ps -q -l` # reattach the terminal & stdin
15+
1116
#curl -Lso /bin/kscript https://raw.githubusercontent.com/holgerbrandl/kscript/abb5f4c6ee72ec90d22c0fe913284e92363cad0e/kscript && chmod u+x /bin/kscript
1217
#curl -Lso /bin/kscript https://www.dropbox.com/s/l5g8vr0wz78y3zy/kscript?dl=1 && chmod u+x /bin/kscript
1318

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ fun main(args: Array<String>) {
117117
// Optionally enter interactive mode
118118
if (docopt.getBoolean("interactive")) {
119119
System.err.println("Creating REPL from ${scriptFile}")
120-
System.err.println("kotlinc ${kotlinOpts} -classpath '${classpath}'")
120+
// System.err.println("kotlinc ${kotlinOpts} -classpath '${classpath}'")
121121

122122
println("kotlinc ${kotlinOpts} -classpath ${classpath}")
123123
exitProcess(0)
@@ -221,13 +221,31 @@ fun main(args: Array<String>) {
221221
}
222222

223223
fun collectDependencies(scriptText: List<String>): List<String> {
224-
return scriptText
224+
val deps = scriptText
225225
.filter { it.startsWith("//DEPS ") }
226226
.flatMap { it.split("[ ;,]+".toRegex()).drop(1) }
227227
.map(String::trim)
228+
229+
val annotatonPrefix = "@file:DependsOn("
230+
var annotDeps = scriptText
231+
.filter { it.startsWith(annotatonPrefix) }
232+
.map { it.replaceFirst(annotatonPrefix, "").split(")")[0] }
233+
.flatMap { it.split(",") }
234+
.map { it.trim(' ', '"') }
235+
236+
// if annotations are used add dependency
237+
if (scriptText.any { containsKscriptAnnotation(it) }) {
238+
annotDeps += "com.github.holgerbrandl:kscript-annotations:1.0"
239+
}
240+
241+
return (deps + annotDeps).distinct()
228242
}
229243

230244

245+
fun containsKscriptAnnotation(line: String) =
246+
listOf("DependsOn", "KotlinOpts", "Include", "EntryPoint").any { line.startsWith("@file:${it}(") }
247+
248+
231249
fun collectRuntimeOptions(scriptText: List<String>): String {
232250
val koptsPrefix = "//KOTLIN_OPTS "
233251

@@ -305,7 +323,7 @@ fun prepareScript(scriptResource: String, enableSupportApi: Boolean): File {
305323
var script = scriptResource.trim()
306324

307325
//auto-prefix one-liners with kscript-support api
308-
// if (numLines(script) == 1 && (script.startsWith("lines") || script.startsWith("stdin"))) {
326+
// if (numLines(script) == 1 && (script.startsWith("lines") || script.startsWith("stdin"))) {
309327
if (enableSupportApi) {
310328
val prefix = """
311329
//DEPS com.github.holgerbrandl:kscript:1.2.2

src/test/kotlin/Tests.kt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Tests {
1111

1212
// "comma separated dependencies should be parsed correctly"
1313
@Test
14-
fun dependencyCollect() {
14+
fun directiveDependencyCollect() {
1515
val lines = listOf(
1616
"//DEPS de.mpicbg.scicomp.joblist:joblist-kotlin:1.1, de.mpicbg.scicomp:kutils:0.7",
1717
"//DEPS log4j:log4j:1.2.14"
@@ -26,6 +26,25 @@ class Tests {
2626
collectDependencies(lines) shouldBe expected
2727
}
2828

29+
@Test
30+
fun mixedDependencyCollect() {
31+
val lines = listOf(
32+
"//DEPS de.mpicbg.scicomp.joblist:joblist-kotlin:1.1, de.mpicbg.scicomp:kutils:0.7",
33+
"""@file:DependsOn("log4j:log4j:1.2.14")"""
34+
)
35+
36+
val expected = listOf(
37+
"de.mpicbg.scicomp.joblist:joblist-kotlin:1.1",
38+
"de.mpicbg.scicomp:kutils:0.7",
39+
"log4j:log4j:1.2.14",
40+
"com.github.holgerbrandl:kscript-annotations:1.0"
41+
)
42+
43+
collectDependencies(lines) shouldBe expected
44+
}
45+
46+
47+
2948
// combine kotlin opts spread over multiple lines
3049
@Test
3150
fun optsCollect() {

test/resources/annotation_deps.kts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env kscript
2+
3+
@file:DependsOn("log4j:log4j:1.2.14", "com.offbytwo:docopt:0.6.0.20150202")
4+
5+
// some pointless comment
6+
7+
import org.docopt.Docopt
8+
9+
// test the docopt dependency
10+
val docopt = Docopt("Usage: jl <command> [options] [<joblist_file>]")
11+
12+
// instantiate a logger to test the log4j dependency
13+
org.apache.log4j.Logger.getRootLogger()
14+
15+
println("kscript with annotations rocks!")

test/test_suite.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ assert_raises "resolve_deps org.org.docopt:org.docopt:0.9.0-SNAPSHOT log4j:log4j
132132

133133
assert_end dependency_lookup
134134

135+
########################################################################################################################
136+
## annotation-drive configuration
137+
138+
# make sure that @file:DependsOn is parsed correctly
139+
assert "kscript ${KSCRIPT_HOME}/test/resources/annotation_deps.kts" "kscript with annotations rocks!"
140+
141+
142+
135143
########################################################################################################################
136144
## support_api
137145

0 commit comments

Comments
 (0)