@@ -85,40 +85,46 @@ fun main(args: Array<String>) {
8585 quit(0 )
8686 }
8787
88- // note: with current impt we still don't support `kscript -1` where "-1" is a valid kotlin expression
88+ // note: with current implementation we still don't support `kscript -1` where "-1" is a valid kotlin expression
8989 val userArgs = args.dropWhile { it.startsWith(" -" ) && it != " -" }.drop(1 )
9090 val kscriptArgs = args.take(args.size - userArgs.size)
9191
9292 val docopt = DocOptWrapper (kscriptArgs, USAGE )
9393 val loggingEnabled = ! docopt.getBoolean(" silent" )
9494
9595
96- // create cache dir if it does not yet exist
97- if (! KSCRIPT_CACHE_DIR .isDirectory) {
98- KSCRIPT_CACHE_DIR .mkdir()
99- }
100-
10196 // optionally clear up the jar cache
10297 if (docopt.getBoolean(" clear-cache" )) {
10398 info(" Cleaning up cache..." )
104- KSCRIPT_CACHE_DIR .listFiles().forEach { it.delete() }
105- // evalBash("rm -f ${KSCRIPT_CACHE_DIR}/*")
99+ KSCRIPT_CACHE_DIR .listFiles()?.forEach { it.delete() }
106100 quit(0 )
107101 }
108102
103+ // create cache dir if it does not yet exist
104+ if (! KSCRIPT_CACHE_DIR .isDirectory) {
105+ KSCRIPT_CACHE_DIR .mkdir()
106+ }
107+
109108 // Resolve the script resource argument into an actual file
110109 val scriptResource = docopt.getString(" script" )
111110
112- val enableSupportApi = docopt.getBoolean(" text" )
113- val (rawScript, includeContext) = prepareScript(scriptResource)
111+ val (rawUri, includeContext) = prepareScript(scriptResource)
114112
115113 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+
116120 errorIf(! rawScript.canWrite()) {
117121 " Script file not writable: $rawScript "
118122 }
123+
119124 errorIf(rawScript.parentFile == SCRIPT_TEMP_DIR ) {
120125 " Temporary script file detected: $rawScript , created from $scriptResource "
121126 }
127+
122128 val scriptLines = rawScript.readLines().dropWhile {
123129 it.startsWith(" #!/" ) && it != " #!/bin/bash"
124130 }
@@ -140,25 +146,25 @@ fun main(args: Array<String>) {
140146 quit(0 )
141147 }
142148
149+ val enableSupportApi = docopt.getBoolean(" text" )
150+
143151 // post process script (text-processing mode, custom dsl preamble, resolve includes)
144152 // and finally resolve all includes (see https://github.com/holgerbrandl/kscript/issues/34)
145- val (scriptFile, includeURLs) = resolveIncludes(resolvePreambles(rawScript, enableSupportApi), includeContext)
146-
153+ val (scriptFile, includeURLs) = resolveIncludes(resolvePreambles(rawUri, enableSupportApi), includeContext)
147154
148155 val script = Script (scriptFile)
149156
150-
151157 // Find all //DEPS directives and concatenate their values
152- val dependencies = ( script.collectDependencies() + Script (rawScript).collectDependencies() ).distinct()
153- val customRepos = ( script.collectRepos() + Script (rawScript).collectRepos() ).distinct()
158+ val dependencies = script.collectDependencies().distinct()
159+ val customRepos = script.collectRepos().distinct()
154160
155161 // Extract kotlin arguments
156162 val kotlinOpts = script.collectRuntimeOptions()
157163 val compilerOpts = script.collectCompilerOptions()
158164
159165 // Create temporary dev environment
160166 if (docopt.getBoolean(" idea" )) {
161- println (launchIdeaWithKscriptlet(rawScript , userArgs, dependencies, customRepos, includeURLs, compilerOpts))
167+ println (launchIdeaWithKscriptlet(scriptFile , userArgs, dependencies, customRepos, includeURLs, compilerOpts))
162168 exitProcess(0 )
163169 }
164170
@@ -283,6 +289,7 @@ fun main(args: Array<String>) {
283289 }
284290
285291 var extClassPath = " ${jarFile}${CP_SEPARATOR_CHAR }${KOTLIN_HOME }${File .separatorChar} lib${File .separatorChar} kotlin-script-runtime.jar"
292+
286293 if (classpath.isNotEmpty())
287294 extClassPath + = CP_SEPARATOR_CHAR + classpath
288295
@@ -321,8 +328,7 @@ private fun versionCheck() {
321328 }
322329}
323330
324-
325- fun prepareScript (scriptResource : String ): Pair <File , URI > {
331+ fun prepareScript (scriptResource : String ): Pair <URI , URI > {
326332 var scriptFile: File ?
327333
328334 // we need to keep track of the scripts dir or the working dir in case of stdin script to correctly resolve includes
@@ -382,13 +388,16 @@ fun prepareScript(scriptResource: String): Pair<File, URI> {
382388
383389 // note script file must be not null at this point
384390
385- return Pair (scriptFile!! , includeContext)
391+ return Pair (scriptFile!! .toURI() , includeContext)
386392}
387393
394+ private fun resolvePreambles (rawUri : URI , enableSupportApi : Boolean ): URI {
395+ if (! isFile(rawUri)) {
396+ return rawUri
397+ }
388398
389- private fun resolvePreambles (rawScript : File , enableSupportApi : Boolean ): File {
390399 // include preamble for custom interpreters (see https://github.com/holgerbrandl/kscript/issues/67)
391- var scriptFile = rawScript
400+ var scriptFile = File (rawUri)
392401
393402 System .getenv(" CUSTOM_KSCRIPT_PREAMBLE" )?.let { interpPreamble ->
394403 // rawScript = Script(rawScript!!).prependWith(interpPreamble).createTmpScript()
@@ -416,9 +425,6 @@ private fun resolvePreambles(rawScript: File, enableSupportApi: Boolean): File {
416425
417426 scriptFile = Script (scriptFile).prependWith(" //INCLUDE ${preambleFile.absolutePath} " ).createTmpScript()
418427 }
419- return scriptFile
420- }
421-
422428
423- private fun postProcessScript ( inputFile : File ? , includeContext : URI ): IncludeResult =
424- resolveIncludes(inputFile !! , includeContext)
429+ return scriptFile.toURI()
430+ }
0 commit comments