|
2 | 2 |
|
3 | 3 | KSCRIPT_VERSION=1.0 |
4 | 4 |
|
| 5 | + |
| 6 | +## ... or use a cached version of it if possible |
| 7 | +#scriptFile="./test.kts" |
| 8 | +scriptFile=$1 |
| 9 | +shift |
| 10 | + |
| 11 | + |
| 12 | +## work around missing md5sum on mac |
| 13 | +if [ "${OSTYPE//[0-9.]/}" == "darwin" ]; then md5sum(){ md5 -r $1; }; fi |
| 14 | + |
| 15 | + |
| 16 | +## If script is provided from stdin create a temporary file |
| 17 | +## note we cannot support empty args list here because this would confuse with actual script args |
| 18 | +if [ "$scriptFile" == "-" ]; then scriptFile="/dev/stdin"; fi |
| 19 | +#if [ "$scriptFile"=="/dev/stdin" ]; then |
| 20 | + |
| 21 | +## Rather Test if script ends with kts to also support process substitution here. Wrap stdin |
| 22 | +# http://serverfault.com/questions/52034/what-is-the-difference-between-double-and-single-square-brackets-in-bash |
| 23 | +# https://viewsby.wordpress.com/2013/09/06/bash-string-ends-with/ |
| 24 | +if [[ "$scriptFile" != *kts ]]; then |
| 25 | + tmpScript=${TMPDIR=/tmp}/kscript_stdin_$(mktemp -d XXXXXXXXX).kts # odd but works on macos as well |
| 26 | +# tmpScript=/tmp/kscript_stdin_$(mktemp -d XXXXXXXXX).kts # odd but works on macos as well |
| 27 | + cat ${scriptFile} | tee > ${tmpScript} |
| 28 | + |
| 29 | + ## rename to use checksum as name to allow for jar-caching also when using stdin |
| 30 | + stdinMD5=$(md5sum ${tmpScript} | cut -c1-6) |
| 31 | + |
| 32 | + ## replace script file with md5 hash file copy of stdin |
| 33 | + scriptFile=$(dirname ${tmpScript})/kscript_stdin_${stdinMD5}.kts |
| 34 | + mv ${tmpScript} $scriptFile |
| 35 | +fi |
| 36 | + |
| 37 | + |
| 38 | + |
5 | 39 | ### auto-install expandcp.kts into same dir as kscript for automatic dependency resolution if not yet in PATH |
6 | 40 | if ! which expandcp.kts &> /dev/null; then |
7 | 41 | installDir=$(dirname $(which kscript)) |
8 | 42 | curl -s https://raw.githubusercontent.com/holgerbrandl/kscript/master/expandcp.kts > ${installDir}/expandcp.kts |
9 | 43 | chmod u+x ${installDir}/expandcp.kts |
10 | 44 | fi |
11 | 45 |
|
12 | | -dependencies=$(grep -F "//DEPS" $1 | head -n1 | cut -f2- -d' ' | tr ',;' ' ') |
| 46 | +dependencies=$(grep -F "//DEPS" ${scriptFile} | head -n1 | cut -f2- -d' ' | tr ',;' ' ') |
13 | 47 | #dependencies=" org.docopt:docopt:0.6.0-SNAPSHOT log4j:log4j:1.2.14 " |
14 | 48 |
|
15 | 49 | if [ -n "$dependencies" ]; then |
16 | 50 | classpath=$(expandcp.kts ${dependencies}) |
17 | 51 | if [ $? -eq 1 ]; then exit 1; fi |
18 | 52 | fi |
19 | 53 |
|
20 | | -## directly run the script... |
21 | | -#echo kotlinc -script -classpath "$classpath" "$@" |
22 | | - |
23 | | -## ... or use a cached version of it if possible |
24 | | -#scriptFile="./test.kts" |
25 | | -scriptFile=$1 |
26 | | -shift |
27 | | - |
28 | | -## work around missing md5sum on mac |
29 | | -if [ "${OSTYPE//[0-9.]/}" == "darwin" ]; then md5sum(){ md5 -r $1; }; fi |
| 54 | +#echo "used classpath is ${classpath}" |
30 | 55 |
|
31 | 56 | scriptCheckSum=$(md5sum $scriptFile | cut -c1-6) |
32 | 57 |
|
|
0 commit comments