Skip to content

Commit 2cc5bba

Browse files
committed
simplified include duplication check and fixed indent
1 parent 62ba6fb commit 2cc5bba

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ fun resolveIncludes(template: File, includeContext: URI = template.parentFile.to
2525
}
2626

2727
val includes = emptyList<URL>().toMutableList()
28-
val includeLines = emptySet<String>().toMutableSet()
2928

3029
// resolve as long as it takes. YAGNI but we do because we can!
3130
while (script.any { isIncludeDirective(it) }) {
@@ -38,19 +37,21 @@ fun resolveIncludes(template: File, includeContext: URI = template.parentFile.to
3837
include.startsWith("/") -> File(include).toURI().toURL()
3938
else -> includeContext.resolve(URI(include.removePrefix("./"))).toURL()
4039
}
41-
if (includeLines.contains(includeURL.path)) {
42-
emptyList()
40+
41+
// test if include was processed already (aka include duplication, see #151)
42+
if (includes.map { it.path }.contains(includeURL.path)) {
43+
// include was already resolved, so we return an emtpy result here to avoid duplication errors
44+
emptyList()
4345
} else {
44-
includes.add(includeURL)
45-
includeLines.add(includeURL.path)
46-
47-
try {
48-
includeURL.readText().lines()
49-
} catch (e: FileNotFoundException) {
50-
errorMsg("Failed to resolve //INCLUDE '${include}'")
51-
System.err.println(e.message?.lines()!!.map { it.prependIndent("[kscript] [ERROR] ") })
52-
quit(1)
53-
}
46+
includes.add(includeURL)
47+
48+
try {
49+
includeURL.readText().lines()
50+
} catch (e: FileNotFoundException) {
51+
errorMsg("Failed to resolve //INCLUDE '${include}'")
52+
System.err.println(e.message?.lines()!!.map { it.prependIndent("[kscript] [ERROR] ") })
53+
quit(1)
54+
}
5455
}
5556
} else {
5657
listOf(line)

0 commit comments

Comments
 (0)