Skip to content

Commit 62ba6fb

Browse files
oshaiholgerbrandl
authored andcommitted
Avoid dependency duplications (#151)
1 parent 9fd9d87 commit 62ba6fb

File tree

6 files changed

+37
-11
lines changed

6 files changed

+37
-11
lines changed

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import java.net.URL
1111
*/
1212

1313
const val PACKAGE_STATEMENT_PREFIX = "package "
14-
const val IMPORT_STATMENT_PREFIX = "import " // todo make more solid by using operator including regex
14+
const val IMPORT_STATEMENT_PREFIX = "import " // todo make more solid by using operator including regex
1515

1616
data class IncludeResult(val scriptFile: File, val includes: List<URL> = emptyList())
1717

@@ -25,30 +25,35 @@ fun resolveIncludes(template: File, includeContext: URI = template.parentFile.to
2525
}
2626

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

2930
// resolve as long as it takes. YAGNI but we do because we can!
3031
while (script.any { isIncludeDirective(it) }) {
31-
script = script.flatMap {
32-
if (isIncludeDirective(it)) {
33-
val include = extractIncludeTarget(it)
32+
script = script.flatMap { line ->
33+
if (isIncludeDirective(line)) {
34+
val include = extractIncludeTarget(line)
3435

3536
val includeURL = when {
3637
isUrl(include) -> URL(include)
3738
include.startsWith("/") -> File(include).toURI().toURL()
3839
else -> includeContext.resolve(URI(include.removePrefix("./"))).toURL()
3940
}
41+
if (includeLines.contains(includeURL.path)) {
42+
emptyList()
43+
} else {
44+
includes.add(includeURL)
45+
includeLines.add(includeURL.path)
4046

41-
includes.add(includeURL)
42-
43-
try {
47+
try {
4448
includeURL.readText().lines()
45-
} catch (e: FileNotFoundException) {
49+
} catch (e: FileNotFoundException) {
4650
errorMsg("Failed to resolve //INCLUDE '${include}'")
4751
System.err.println(e.message?.lines()!!.map { it.prependIndent("[kscript] [ERROR] ") })
4852
quit(1)
53+
}
4954
}
5055
} else {
51-
listOf(it)
56+
listOf(line)
5257
}
5358
}.let { script.copy(it) }
5459
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ data class Script(val lines: List<String>, val extension: String = "kts") : Iter
3434
val annotations = emptySet<String>().toMutableSet()
3535

3636
stripShebang().forEach {
37-
if (it.startsWith(IMPORT_STATMENT_PREFIX)) {
37+
if (it.startsWith(IMPORT_STATEMENT_PREFIX)) {
3838
imports.add(it)
3939
} else if (isKscriptAnnotation(it)) {
4040
annotations.add(it)

src/test/kotlin/Tests.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,14 @@ class Tests {
168168
result.includes.filter { it.protocol == "file" }.map { File(it.toURI()).name } shouldBe List(4) { "include_${it + 1}.kt" }
169169
result.includes.filter { it.protocol != "file" }.size shouldBe 1
170170
}
171-
}
171+
172+
@Test
173+
fun `test include detection - should not include dependency twice`() {
174+
val result = resolveIncludes(File("test/resources/includes/dup_include/dup_include.kts"))
175+
176+
result.includes.map { File(it.toURI()).name } shouldBe listOf(
177+
"dup_include_1.kt",
178+
"dup_include_2.kt"
179+
)
180+
}
181+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env kscript
2+
3+
4+
@file:Include("dup_include_1.kt")
5+
@file:Include("dup_include_2.kt")
6+
7+
dup_include_2()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
@file:Include("dup_include_2.kt")
3+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fun dup_include_2() = println("dup_include")

0 commit comments

Comments
 (0)