Skip to content

Commit 8d80e31

Browse files
committed
added support for stdin and process substiution as script source
1 parent 5615b7f commit 8d80e31

File tree

4 files changed

+109
-11
lines changed

4 files changed

+109
-11
lines changed

NEWS.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
3+
## v1.1
4+
5+
* Support for stdin and process substitution as script source. See [examples](examples/unit_tests.sh)
6+
* TBD: versioning and auto-update
7+
* TBD: basic command-line help
8+
9+
10+
##
11+
Initial Release

examples/unit_tests.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env bash
2+
3+
4+
##
5+
## Unit tests for kscript
6+
##
7+
8+
9+
## todo make this more streamlined. Learn from http://stackoverflow.com/questions/1339416/unit-testing-bash-scripts
10+
11+
## make sure that expandcp.kts gives an exitcode 1 if some dependencies are not found
12+
expandcp.kts org.docopt:docopt:0.6.0-FOOBAR
13+
14+
expandcp.kts org.docopt:docopt:0.6.0-SNAPSHOT
15+
16+
17+
18+
## make sure that scripts can be provided on the fly ...
19+
20+
## (1) via pipe
21+
echo '
22+
println(1+1)
23+
' | kscript
24+
25+
26+
## (2) heredoc
27+
kscript - <<"EOF"
28+
println(1+1)
29+
EOF
30+
31+
## (2b) heredoc with dependencies
32+
kscript - <<"EOF"
33+
//DEPS org.docopt:docopt:0.6.0-SNAPSHOT log4j:log4j:1.2.14
34+
35+
import org.docopt.Docopt
36+
val docopt = Docopt("Usage: jl <command> [options] [<joblist_file>]")
37+
38+
println(1+1)
39+
EOF
40+
41+
42+
43+
## (3) process substitution
44+
echo '
45+
println((1+3).toString() + " test")
46+
' > .test.kts
47+
48+
kscript <(cat .test.kts)
49+

kscript

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,56 @@
22

33
KSCRIPT_VERSION=1.0
44

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+
539
### auto-install expandcp.kts into same dir as kscript for automatic dependency resolution if not yet in PATH
640
if ! which expandcp.kts &> /dev/null; then
741
installDir=$(dirname $(which kscript))
842
curl -s https://raw.githubusercontent.com/holgerbrandl/kscript/master/expandcp.kts > ${installDir}/expandcp.kts
943
chmod u+x ${installDir}/expandcp.kts
1044
fi
1145

12-
dependencies=$(grep -F "//DEPS" $1 | head -n1 | cut -f2- -d' ' | tr ',;' ' ')
46+
dependencies=$(grep -F "//DEPS" ${scriptFile} | head -n1 | cut -f2- -d' ' | tr ',;' ' ')
1347
#dependencies=" org.docopt:docopt:0.6.0-SNAPSHOT log4j:log4j:1.2.14 "
1448

1549
if [ -n "$dependencies" ]; then
1650
classpath=$(expandcp.kts ${dependencies})
1751
if [ $? -eq 1 ]; then exit 1; fi
1852
fi
1953

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}"
3055

3156
scriptCheckSum=$(md5sum $scriptFile | cut -c1-6)
3257

notes.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,16 @@ Also see https://en.wikipedia.org/wiki/Shebang_(Unix)#Portability
8686
Some systems, including Linux, do not split up the arguments;[10] for example, when running the script with the first line like,
8787

8888
Also see http://stackoverflow.com/questions/4303128/how-to-use-multiple-arguments-with-a-shebang-i-e
89+
90+
91+
## directly run scripts with kotlinc
92+
93+
```
94+
## directly run the script...
95+
echo kotlinc -script -classpath "$classpath" "$@"
96+
97+
```
98+
99+
## how does process substitution work?
100+
101+
http://unix.stackexchange.com/questions/156084/why-does-process-substitution-result-in-a-file-called-dev-fd-63-which-is-a-pipe

0 commit comments

Comments
 (0)