Skip to content

Commit

Permalink
collects imports, removes bang line, and prepends at top of script (#75)
Browse files Browse the repository at this point in the history
* collects imports, removes bang line, and prepends at top of script

* install maven with apt-get rather than sdkman

* remove unneccessary code that does nothing

* add failing unit test for import without trailing space

* change file names so ide does not have falase positive squiqqlies

* made test pass for import with trailing space only

* changed way of checking null, switched if else around as per review

* added back specific null test, as "i do not exist.kts" test fails

* revert travis.yml change so sdkman now installs maven again (I hope)
  • Loading branch information
ilanpillemer authored and holgerbrandl committed Dec 1, 2017
1 parent 7fc29bb commit f759ec4
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/kscript/app/Kscript.kt
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ fun prepareScript(scriptResource: String, enableSupportApi: Boolean): File {


// support //INCLUDE directive (see https://github.com/holgerbrandl/kscript/issues/34)
scriptFile = resolveIncludes(scriptFile)
if (scriptFile != null) scriptFile = resolveIncludes(scriptFile)

// just proceed if the script file is a regular file at this point
errorIf(scriptFile == null || !scriptFile.canRead()) {
Expand Down
40 changes: 26 additions & 14 deletions src/main/kotlin/kscript/app/ResolveIncludes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ import java.net.URL
*/

/** Resolve include declarations in a script file. Resolved script will be put into another temporary script */
fun resolveIncludes(template: File?): File? {
if (template == null) return null
fun resolveIncludes(template: File): File = resolveIncludesInternal(template)

// recursively replace //INCLUDES
return resolveIncludesInternal(template)?.run { resolveIncludes(this) } ?: template
}

private fun resolveIncludesInternal(template: File): File? {
private fun resolveIncludesInternal(template: File): File {
val IMPORT_TEXT = "import "
val scriptLines = template.readText().lines()

// don't do a thing if there's not INCLUDE in the script
if (scriptLines.find { it.startsWith("//INCLUDE ") } == null) return null
if (scriptLines.find { it.startsWith("//INCLUDE ") } == null) {
return template
}

val sb = StringBuilder()

// collect up the set of imports in this
val imports : MutableSet<String> = emptySet<String>().toMutableSet()

scriptLines.map {
if (it.startsWith("//INCLUDE")) {
val include = it.split("[ ]+".toRegex()).last()
Expand All @@ -37,20 +37,32 @@ private fun resolveIncludesInternal(template: File): File? {
}

try {
sb.appendln(includeURL.readText())
// collect the import or emit
includeURL.readText().lines().forEach {
if (it.startsWith(IMPORT_TEXT)) {
imports.add(it)
} else {
sb.appendln(it)
}
}
} catch (e: FileNotFoundException) {
errorMsg("Failed to resolve //INCLUDE '${include}'")
System.err.println(e.message?.lines()!!.map { it.prependIndent("[ERROR] ") })

quit(1)
}
} else if (it.startsWith(IMPORT_TEXT)) {
imports.add(it)
} else {
// if it's not a directive we simply skip emit the line as it is
sb.appendln(it)
// if its not an include directive or an import or a bang line, emit as is
if (!it.startsWith("#!/")) { sb.appendln(it) } else { }
}
}

return createTmpScript(sb.toString())
val impsb = StringBuilder()
imports.map { impsb.appendln(it) }

val final = impsb.appendln(sb.toString())
return createTmpScript(final.toString())
}


Expand Down
19 changes: 19 additions & 0 deletions src/test/kotlin/ConsolidateImports.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import io.kotlintest.matchers.shouldBe
import kscript.app.*
import org.junit.Test
import java.io.File
import org.hamcrest.core.Is.*
import org.junit.Assert.*

class ConsolidateImports {

@Test
fun test_consolidate_imports() {
val classLoader = javaClass.classLoader
val file = this.javaClass.getResource("/consolidate_includes/input.script").file
val expected = this.javaClass.getResource("/consolidate_includes/expected.script")
val result = resolveIncludes(File(file))

assertThat(result.readText(),`is`(expected.readText()))
}
}
52 changes: 52 additions & 0 deletions src/test/resources/consolidate_includes/expected.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import here.we.go
import we.go.here
import go.here.we
import if.not.now.when
import if.not.me.then.who
import my.cool.class
//DEPS com.eclipsesource.minimal-json:minimal-json:0.9.4

file 1

Lastly, she pictured to herself how this same little sister of
hers would, in the after-time, be herself a grown woman; and how
she would keep, through all her riper years, the simple and
loving heart of her childhood: and how she would gather about
her other little children, and make THEIR eyes bright and eager
with many a strange tale, perhaps even with the dream of
Wonderland of long ago: and how she would feel with all their
simple sorrows, and find a pleasure in all their simple joys,
remembering her own child-life, and the happy summer days.


file 2

Alice waited a little, half expecting to see it again, but it
did not appear, and after a minute or two she walked on in the
direction in which the March Hare was said to live. `I've seen
hatters before,' she said to herself; `the March Hare will be
much the most interesting, and perhaps as this is May it won't be
raving mad--at least not so mad as it was in March.' As she said
this, she looked up, and there was the Cat again, sitting on a
branch of a tree.

file 3
important to not treat me as import

This time Alice waited patiently until it chose to speak again.
In a minute or two the Caterpillar took the hookah out of its
mouth and yawned once or twice, and shook itself. Then it got
down off the mushroom, and crawled away in the grass, merely
remarking as it went, `One side will make you grow taller, and
the other side will make you grow shorter.'


Script
important to also not treat me as import

Alice was beginning to get very tired of sitting by her sister
on the bank, and of having nothing to do: once or twice she had
peeped into the book her sister was reading, but it had no
pictures or conversations in it, `and what is the use of a book,'
thought Alice `without pictures or conversation?'

15 changes: 15 additions & 0 deletions src/test/resources/consolidate_includes/file1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import here.we.go
import we.go.here
import go.here.we

file 1

Lastly, she pictured to herself how this same little sister of
hers would, in the after-time, be herself a grown woman; and how
she would keep, through all her riper years, the simple and
loving heart of her childhood: and how she would gather about
her other little children, and make THEIR eyes bright and eager
with many a strange tale, perhaps even with the dream of
Wonderland of long ago: and how she would feel with all their
simple sorrows, and find a pleasure in all their simple joys,
remembering her own child-life, and the happy summer days.
14 changes: 14 additions & 0 deletions src/test/resources/consolidate_includes/file2
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import here.we.go
import we.go.here
import if.not.now.when

file 2

Alice waited a little, half expecting to see it again, but it
did not appear, and after a minute or two she walked on in the
direction in which the March Hare was said to live. `I've seen
hatters before,' she said to herself; `the March Hare will be
much the most interesting, and perhaps as this is May it won't be
raving mad--at least not so mad as it was in March.' As she said
this, she looked up, and there was the Cat again, sitting on a
branch of a tree.
12 changes: 12 additions & 0 deletions src/test/resources/consolidate_includes/file3
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import go.here.we
import if.not.me.then.who

file 3
important to not treat me as import

This time Alice waited patiently until it chose to speak again.
In a minute or two the Caterpillar took the hookah out of its
mouth and yawned once or twice, and shook itself. Then it got
down off the mushroom, and crawled away in the grass, merely
remarking as it went, `One side will make you grow taller, and
the other side will make you grow shorter.'
16 changes: 16 additions & 0 deletions src/test/resources/consolidate_includes/input.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env kscript
//DEPS com.eclipsesource.minimal-json:minimal-json:0.9.4
//INCLUDE file1
//INCLUDE file2
//INCLUDE file3

import my.cool.class

Script
important to also not treat me as import

Alice was beginning to get very tired of sitting by her sister
on the bank, and of having nothing to do: once or twice she had
peeped into the book her sister was reading, but it had no
pictures or conversations in it, `and what is the use of a book,'
thought Alice `without pictures or conversation?'

0 comments on commit f759ec4

Please sign in to comment.