Skip to content

Commit 97174ab

Browse files
committed
Better model to auto-enable kscript.text.* mode (fixes #28)
1 parent 162a169 commit 97174ab

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

kscript

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#!/bin/bash
22

3-
KSCRIPT_VERSION=1.4
3+
KSCRIPT_VERSION=1.4.1
44

55
## make sure that all requirements (java, kotlinc, maven) to run kscript are met
66
assertInPath(){ if [ -z "$(which $1)" ]; then echo "[ERROR] Could not locate '$1'" 1>&2; exit 1; fi }
77

8+
## trim whitespace from a string
9+
trim() { while read -r line; do echo "$line"; done; }
10+
811
assertInPath kotlinc
912
assertInPath mvn
1013
assertInPath curl
@@ -144,8 +147,11 @@ fi
144147
## note: -e also supports non-regular files in contrast to -f
145148
if [[ "$scriptFile" != *kts ]] && [[ ! -e ${scriptFile} ]]; then
146149
## auto-prefix one-liners with kscript-support api
147-
if [[ $(echo "${scriptFile}" | wc -l) -eq 1 ]]; then
148-
scriptFile=$'//DEPS com.github.holgerbrandl:kscript:1.2\nimport kscript.text.*\nval lines = resolveArgFile(args)\n\n'${scriptFile}
150+
# if [[ $(echo "${scriptFile}" | wc -l) -eq 1 ]]; then ## old approach
151+
# http://stackoverflow.com/questions/2172352/in-bash-how-can-i-check-if-a-string-begins-with-some-value
152+
trimmedScript=$(echo "${scriptFile}" | tr -d '\n' )
153+
if [[ $trimmedScript == lines.* ]] || [[ $trimmedScript == stdin.* ]] ; then
154+
scriptFile=$'//DEPS com.github.holgerbrandl:kscript:1.2.1\nimport kscript.text.*\nval lines = resolveArgFile(args)\n\n'${scriptFile}
149155
fi
150156

151157
scriptHash=$(kscript_md5 <(echo "$scriptFile"))
@@ -173,7 +179,6 @@ fi
173179
if [ $(grep "^// DEPS" ${scriptFile} | wc -l) -gt 0 ]; then
174180
echo "[ERROR] Dependencies must be declared by using the line prefix //DEPS" 1>&2; exit 1;
175181
fi
176-
trim() { while read -r line; do echo "$line"; done; }
177182
dependencies=$(grep "^//DEPS" ${scriptFile} | cut -f2- -d' ' | trim | tr ',;\n' ' ')
178183

179184

test/test_suite.sh

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,10 @@ assert 'echo "foo${NL}bar" | kscript "stdin.print()"' $'foo\nbar'
102102
#echo "$'foo\nbar' | kscript 'stdin.print()'
103103

104104
assert 'echo "foo${NL}bar" | kscript "stdin.print()"' $'foo\nbar'
105+
assert 'kscript "println(1+1)"' '2'
105106

106-
## todo renable once support v1.3 is live on jcenter
107-
#assert_statement 'echo "foo${NL}bar" | kscript "stdin.split().select(1, 2, -3)"' "" "[ERROR] Can not mix positive and negative selections" 1
108107

109-
110-
111-
## requirement checkig using support api
112-
assert_statement 'kscript "kscript.stopIfNot(1==23){\"condition not met\"}"' "" "[ERROR] condition not met" 1
108+
assert_statement 'echo "foo${NL}bar" | kscript "stdin.split().select(1, 2, -3)"' "" "[ERROR] Can not mix positive and negative selections" 1
113109

114110
assert_end support_api
115111

0 commit comments

Comments
 (0)