Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions src/main/kotlin/kscript/app/ResolveIncludes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import java.net.URL
*/

const val PACKAGE_STATEMENT_PREFIX = "package "
const val IMPORT_STATMENT_PREFIX = "import " // todo make more solid by using operator including regex
const val IMPORT_STATEMENT_PREFIX = "import " // todo make more solid by using operator including regex

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

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

val includes = emptyList<URL>().toMutableList()
val includeLines = emptySet<String>().toMutableSet()

// resolve as long as it takes. YAGNI but we do because we can!
while (script.any { isIncludeDirective(it) }) {
script = script.flatMap {
if (isIncludeDirective(it)) {
val include = extractIncludeTarget(it)
script = script.flatMap { line ->
if (isIncludeDirective(line)) {
val include = extractIncludeTarget(line)

val includeURL = when {
isUrl(include) -> URL(include)
include.startsWith("/") -> File(include).toURI().toURL()
else -> includeContext.resolve(URI(include.removePrefix("./"))).toURL()
}
if (includeLines.contains(includeURL.path)) {
emptyList()
} else {
includes.add(includeURL)
includeLines.add(includeURL.path)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just

if(includes.contains(includeURL)) emptyList<String>()

// or depending how clever URL.equals is implemented
if(includes.map{it.path}.contains(includeURL.path)) emptyList<String>()

?
The logic seems the same but it's much simpler and works without another list for url-bookkeeping?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can do that, but note that equals and hashcode of URL are very weird methods, that includes accessing network to resolve names, see this for more details: http://errorprone.info/bugpattern/URLEqualsHashCode

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All right, let's compare via url.path instead then, although URI.equals seems to be more popular (see https://stackoverflow.com/questions/5402485/how-to-compare-two-urls-in-java)


includes.add(includeURL)

try {
try {
includeURL.readText().lines()
} catch (e: FileNotFoundException) {
} catch (e: FileNotFoundException) {
errorMsg("Failed to resolve //INCLUDE '${include}'")
System.err.println(e.message?.lines()!!.map { it.prependIndent("[kscript] [ERROR] ") })
quit(1)
}
}
} else {
listOf(it)
listOf(line)
}
}.let { script.copy(it) }
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/kscript/app/Script.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ data class Script(val lines: List<String>, val extension: String = "kts") : Iter
val annotations = emptySet<String>().toMutableSet()

stripShebang().forEach {
if (it.startsWith(IMPORT_STATMENT_PREFIX)) {
if (it.startsWith(IMPORT_STATEMENT_PREFIX)) {
imports.add(it)
} else if (isKscriptAnnotation(it)) {
annotations.add(it)
Expand Down
12 changes: 11 additions & 1 deletion src/test/kotlin/Tests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,14 @@ class Tests {
result.includes.filter { it.protocol == "file" }.map { File(it.toURI()).name } shouldBe List(4) { "include_${it + 1}.kt" }
result.includes.filter { it.protocol != "file" }.size shouldBe 1
}
}

@Test
fun `test include detection - should not include dependency twice`() {
val result = resolveIncludes(File("test/resources/includes/dup_include/dup_include.kts"))

result.includes.map { File(it.toURI()).name } shouldBe listOf(
"dup_include_1.kt",
"dup_include_2.kt"
)
}
}
7 changes: 7 additions & 0 deletions test/resources/includes/dup_include/dup_include.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env kscript


@file:Include("dup_include_1.kt")
@file:Include("dup_include_2.kt")

dup_include_2()
3 changes: 3 additions & 0 deletions test/resources/includes/dup_include/dup_include_1.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

@file:Include("dup_include_2.kt")

1 change: 1 addition & 0 deletions test/resources/includes/dup_include/dup_include_2.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fun dup_include_2() = println("dup_include")