@@ -11,7 +11,7 @@ import java.net.URL
1111 */
1212
1313const 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
1616data 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 }
0 commit comments