-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
collects imports, removes bang line, and prepends at top of script #75
collects imports, removes bang line, and prepends at top of script #75
Conversation
@@ -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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the scriptfile can not be null, we should rather use !! or latinit here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have a null check later in the code, so I thought maybe it could be null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
file can be null as per "I do not exist" test that fails if I replace with !!, so this is necessary.
.travis.yml
Outdated
@@ -26,6 +26,7 @@ install: | |||
- sudo add-apt-repository -y ppa:openjdk-r/ppa | |||
- sudo apt-get update | |||
- sudo apt-get install openjdk-8-jdk | |||
- sudo apt-get install maven2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
always worked for me using sdk install. imho should not be part of PR since it does not relate to it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps something has changed lately with sdk install? The travis ci wont work without this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've just rerun the build using the current repo head and it run through without problems. See https://travis-ci.org/holgerbrandl/kscript/builds/309752820
So maybe it was temporary problem?
@@ -9,21 +9,20 @@ 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By doing so you removed the recursiveness. Before included files could contain @Include
s themselfes, but you refactored that away. But maybe this was too far-fetched anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think its a YAGNI thing. Do you want it back?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally think it also better to leave only at the top level and not allow recursion as it avoids the possibility of circular includes. When we add the feature to allow a "preamble" file for the DSL functionality then all includes would only go in there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you could list includes in the preamble.
And you're right about recursion YAGNIness. Not sure why I was obsessed enough in the first place to implement it that way.
sb.appendln(includeURL.readText()) | ||
// collect the import or emit | ||
includeURL.readText().lines().forEach { | ||
if (it.startsWith("import")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- it must be "import " at least (with a tailing space) to make it more robust. Otherwise we might mess up logic such as
fun importStuff() = ""
importStuff()
Do you see my point?
I'm still a bit worried that such a predicate might create false positives, but since I can't come with example, I like your solution (plus the missing space).
- You could use
partition instead
of the loop to get rid of the loop. Example
val (left, right) = listOf(1,2,3).partition{ it ==2}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. It needs a trailing space. I will add one. Partition is neater and cleaner too.
// 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("#")) { } else { sb.appendln(it) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
imho should be else if(!it.startsWith("#!/"))
( to get rid of the ugly { }
and to streamline the code
Great work! I know it's asking a lot, but could you also create a unit test to test this new feature? Just to make sure that we don't break it in the future. |
I'll add a unit test. |
@holgerbrandl added unit test and trailing space for the predicate. Is this good now? |
Looks great. thanks a lot for your contribution. |
I don't think that we can fix it, but it still would break if the user does
Both is valid kotlin code. |
fixes #72