@@ -8,6 +8,7 @@ import java.io.File
88import java.io.FileInputStream
99import java.io.InputStreamReader
1010import java.lang.IllegalArgumentException
11+ import java.net.URI
1112import java.net.URL
1213import java.net.UnknownHostException
1314import java.util.*
@@ -92,31 +93,38 @@ fun main(args: Array<String>) {
9293 val loggingEnabled = ! docopt.getBoolean(" silent" )
9394
9495
95- // create cache dir if it does not yet exist
96- if (! KSCRIPT_CACHE_DIR .isDirectory) {
97- KSCRIPT_CACHE_DIR .mkdir()
98- }
99-
10096 // optionally clear up the jar cache
10197 if (docopt.getBoolean(" clear-cache" )) {
10298 info(" Cleaning up cache..." )
103- KSCRIPT_CACHE_DIR .listFiles().forEach { it.delete() }
99+ KSCRIPT_CACHE_DIR .listFiles()? .forEach { it.delete() }
104100 quit(0 )
105101 }
106102
103+ // create cache dir if it does not yet exist
104+ if (! KSCRIPT_CACHE_DIR .isDirectory) {
105+ KSCRIPT_CACHE_DIR .mkdir()
106+ }
107+
107108 // Resolve the script resource argument into an actual file
108109 val scriptResource = docopt.getString(" script" )
109110
110- val enableSupportApi = docopt.getBoolean(" text" )
111- val rawScript = prepareScript(scriptResource)
111+ val rawUri = prepareScript(scriptResource)
112112
113113 if (docopt.getBoolean(" add-bootstrap-header" )) {
114+ errorIf(! isFile(rawUri)) {
115+ " Can not add bootstrap header to URL resources: $rawUri "
116+ }
117+
118+ val rawScript = File (rawUri)
119+
114120 errorIf(! rawScript.canWrite()) {
115121 " Script file not writable: $rawScript "
116122 }
123+
117124 errorIf(rawScript.parentFile == SCRIPT_TEMP_DIR ) {
118125 " Temporary script file detected: $rawScript , created from $scriptResource "
119126 }
127+
120128 val scriptLines = rawScript.readLines().dropWhile {
121129 it.startsWith(" #!/" ) && it != " #!/bin/bash"
122130 }
@@ -138,23 +146,25 @@ fun main(args: Array<String>) {
138146 quit(0 )
139147 }
140148
149+ val enableSupportApi = docopt.getBoolean(" text" )
150+
141151 // post process script (text-processing mode, custom dsl preamble, resolve includes)
142152 // and finally resolve all includes (see https://github.com/holgerbrandl/kscript/issues/34)
143- val (scriptFile, includeURLs) = resolveIncludes(resolvePreambles(rawScript , enableSupportApi))
153+ val (scriptFile, includeURLs) = resolveIncludes(resolvePreambles(rawUri , enableSupportApi))
144154
145155 val script = Script (scriptFile)
146156
147157 // Find all //DEPS directives and concatenate their values
148- val dependencies = ( script.collectDependencies() + Script (rawScript).collectDependencies() ).distinct()
149- val customRepos = ( script.collectRepos() + Script (rawScript).collectRepos() ).distinct()
158+ val dependencies = script.collectDependencies().distinct()
159+ val customRepos = script.collectRepos().distinct()
150160
151161 // Extract kotlin arguments
152162 val kotlinOpts = script.collectRuntimeOptions()
153163 val compilerOpts = script.collectCompilerOptions()
154164
155165 // Create temporary dev environment
156166 if (docopt.getBoolean(" idea" )) {
157- println (launchIdeaWithKscriptlet(rawScript , userArgs, dependencies, customRepos, includeURLs, compilerOpts))
167+ println (launchIdeaWithKscriptlet(scriptFile , userArgs, dependencies, customRepos, includeURLs, compilerOpts))
158168 exitProcess(0 )
159169 }
160170
@@ -319,63 +329,64 @@ private fun versionCheck() {
319329}
320330
321331
322- fun prepareScript (scriptResource : String ):File {
323- var scriptFile: File ?
332+ fun prepareScript (scriptResource : String ): URI {
333+ if (isUrl(scriptResource)) {
334+ return URI (scriptResource)
335+ }
336+
337+ var scriptUri: URI ?
324338
325339 // map script argument to script file
326- scriptFile = with (File (scriptResource)) {
340+ scriptUri = with (File (scriptResource)) {
327341 if (! canRead()) {
328342 // not a file so let's keep the script-file undefined here
329343 null
330344 } else if (listOf (" kts" , " kt" ).contains(extension)) {
331- this
345+ this .toURI()
332346 } else {
333347 // if we can "just" read from script resource create tmp file
334348 // i.e. script input is process substitution file handle
335349 // not FileInputStream(this).bufferedReader().use{ readText()} does not work nor does this.readText
336- createTmpScript(FileInputStream (this ).bufferedReader().readText())
350+ createTmpScript(FileInputStream (this ).bufferedReader().readText()).toURI()
337351 }
338352 }
339353
340354 // support stdin
341355 if (scriptResource == " -" || scriptResource == " /dev/stdin" ) {
342356 val scriptText = generateSequence() { readLine() }.joinToString(" \n " ).trim()
343- scriptFile = createTmpScript(scriptText)
357+ scriptUri = createTmpScript(scriptText).toURI( )
344358 }
345359
346-
347- // Support URLs as script files
348- if (scriptResource.startsWith(" http://" ) || scriptResource.startsWith(" https://" )) {
349- scriptFile = fetchFromURL(scriptResource)
350- }
351-
352-
353360 // Support for support process substitution and direct script arguments
354- if (scriptFile == null && ! scriptResource.endsWith(" .kts" ) && ! scriptResource.endsWith(" .kt" )) {
361+ if (scriptUri == null && ! scriptResource.endsWith(" .kts" ) && ! scriptResource.endsWith(" .kt" )) {
355362 val scriptText = if (File (scriptResource).canRead()) {
356363 File (scriptResource).readText().trim()
357364 } else {
358365 // the last resort is to assume the input to be a kotlin program
359366 scriptResource.trim()
360367 }
361368
362- scriptFile = createTmpScript(scriptText)
369+ scriptUri = createTmpScript(scriptText).toURI( )
363370 }
364371
365372 // just proceed if the script file is a regular file at this point
366- errorIf(scriptFile == null || ! scriptFile.canRead() ) {
373+ errorIf(scriptUri == null ) {
367374 " Could not read script argument '$scriptResource '"
368375 }
369376
370377 // note script file must be not null at this point
371378
372- return scriptFile !!
379+ return scriptUri !!
373380}
374381
375382
376- private fun resolvePreambles (rawScript : File , enableSupportApi : Boolean ): File {
383+ private fun resolvePreambles (rawUri : URI , enableSupportApi : Boolean ): URI {
384+ if (! isFile(rawUri)) {
385+ return rawUri
386+ }
387+
377388 // include preamble for custom interpreters (see https://github.com/holgerbrandl/kscript/issues/67)
378- var scriptFile = rawScript
389+ var scriptFile = File (rawUri)
379390
380391 System .getenv(" CUSTOM_KSCRIPT_PREAMBLE" )?.let { interpPreamble ->
381392 // rawScript = Script(rawScript!!).prependWith(interpPreamble).createTmpScript()
@@ -404,5 +415,5 @@ private fun resolvePreambles(rawScript: File, enableSupportApi: Boolean): File {
404415 scriptFile = Script (scriptFile).prependWith(" //INCLUDE ${preambleFile.absolutePath} " ).createTmpScript()
405416 }
406417
407- return scriptFile
418+ return scriptFile.toURI()
408419}
0 commit comments