@@ -11,7 +11,22 @@ trim() { while read -r line; do echo "$line"; done; }
1111
1212assertInPath kotlinc
1313assertInPath mvn
14- assertInPath curl
14+
15+
16+ # # cheap curl replacement to avoid dependency on curl
17+ # # https://superuser.com/questions/238987/how-does-unix-search-for-executable-files
18+ kurl (){
19+ kscript ' import java.io.*
20+
21+ try {
22+ BufferedReader(InputStreamReader(java.net.URL(args[0]).openStream())).useLines { it.forEach { println(it) } }
23+ }catch(e: Throwable){
24+ System.err.println("[ERROR] Could not read from ' " + e.message + " ' ")
25+ kotlin.system.exitProcess(1)
26+ }
27+ ' " $@ "
28+ }
29+ # kurl https://git.io/v9R73
1530
1631# # prefer JAVA_HOME over PATH for javac and jar (to stay in sync with kotlinc; see #6)
1732if [ -n " $JAVA_HOME " -a -x " $JAVA_HOME /bin/java" ]; then
@@ -37,7 +52,7 @@ if [ $# == 0 ] || [ "$1" == "-v" ] || [ "$1" == "--version" ] || [ "$1" == "-h"
3752 echo " Website : https://github.com/holgerbrandl/kscript" >&2
3853
3954 # # determine the latest version
40- latestVersion=$( curl -Ls https://git.io/v9R73 | grep ' ^KSCRIPT_VERSION' | cut -f2 -d' =' | tr -d ' .' )
55+ latestVersion=$( kurl https://git.io/v9R73 | grep ' ^KSCRIPT_VERSION' | cut -f2 -d' =' | tr -d ' .' )
4156 installedVersion=$( echo $KSCRIPT_VERSION | tr -d ' .' )
4257
4358 # # http://stackoverflow.com/questions/15224581/floating-point-comparison-with-variable-in-bash
@@ -64,24 +79,21 @@ if [ "$1" == "--clear-cache" ]; then
6479 exit 0
6580fi
6681
67-
68-
69- # # auto-install expandcp.kts into same dir as kscript for automatic dependency resolution if not yet in PATH
70- if ! which expandcp.kts & > /dev/null; then
71- installDir=$( dirname $( which kscript) )
72- curl -Lso ${installDir} /expandcp.kts https://git.io/v9R7n
73- chmod u+x ${installDir} /expandcp.kts
82+ # # look up resdeps from repo unless we're having a full local test clone of it
83+ if [[ -z " $( which resdeps.kts) " ]]; then
84+ resolve_script_deps (){ kscript " https://raw.githubusercontent.com/holgerbrandl/kscript/v${KSCRIPT_VERSION} /resdeps.kts" " $@ " ; }
85+ else
86+ resolve_script_deps (){ resdeps.kts " $@ " ; }
7487fi
88+ # resolve_script_deps log4j:log4j:1.2.14
7589
7690
77- # # optionally self-update kscript ot the newest version
78- if [ " $1 " == " --self-update" ]; then
91+ # # optionally self-update kscript ot the newest version (if not local copy is not being maintained by sdkman)
92+ if [[ " $1 " == " --self-update" ]] && [[ -z " $( which kotlin | grep .sdkman ) " ] ]; then
7993 echo " Installing latest version of kscript..."
80- curl -Lso $( which kscript) https://git.io/v9R73 && chmod u+x $( which kscript)
81- curl -Lso $( which expandcp.kts) https://git.io/v9R7n && chmod u+x $( which expandcp.kts)
94+ kurl https://git.io/v9R73 > $( which kscript) && chmod u+x $( which kscript)
8295
8396 echo " You're now running kscript " $( kscript --help 2>&1 | grep Version | cut -f2- | tr -d ' :' )
84-
8597 exit 0
8698fi
8799
@@ -119,7 +131,9 @@ if [[ "$scriptFile" == "http://"* ]] || [[ "$scriptFile" == "https://"* ]]; then
119131
120132 if [ ! -f " $urlCache " ]; then
121133# echo "fetching kscript from url $scriptFile into ${tmpScript}..."
122- curl -L ${scriptFile} 2> /dev/null > ${urlCache}
134+ # curl -L ${scriptFile} 2>/dev/null > ${urlCache}
135+ kurl ${scriptFile} > ${urlCache}
136+ if [ $? -eq 1 ]; then rm ${urlCache} ; exit 1; fi
123137 fi
124138
125139 scriptFile=${urlCache}
184198dependencies=$( grep " ^//DEPS" ${scriptFile} | cut -f2- -d' ' | trim | tr ' ,;\n' ' ' )
185199
186200
187- # # First try dependency cache directly to avoid jvm launch for expandcp .kts
201+ # # First try dependency cache directly to avoid jvm launch for resdeps .kts
188202# fix me more consistent use of ${TMPDIR}
189203dependency_cache=" ${KSCRIPT_CACHE_DIR} /dependency_cache.txt"
190204if [ -n " $dependencies " ] && [ -f " $dependency_cache " ]; then
191205 classpath=$( grep -F $( echo ${dependencies} | tr ' ' ' ;' ) " " ${dependency_cache} | cut -d' ' -f2)
192206fi
193207
194- # # If there are dependencies but cache-lookup failed we run expandcp .kts
208+ # # If there are dependencies but cache-lookup failed we run resdeps .kts
195209if [ -n " $dependencies " ] && [ -z " $classpath " ]; then
196- classpath=$( expandcp.kts ${dependencies} )
210+ classpath=$( resolve_script_deps ${dependencies} )
197211 if [ $? -eq 1 ]; then exit 1; fi
198212fi
199213
@@ -204,7 +218,7 @@ kotlin_opts=$(grep -F "//KOTLIN_OPTS" ${scriptFile} | head -n1 | cut -f2- -d' ')
204218# # Optionally enter interactive mode
205219if [ " $is_interactive " = true ]; then
206220 echo " To create a shell with script dependencies run:"
207- echo " kotlinc ${kotlin_opts} -classpath '$( expandcp .kts ${dependencies} ) '"
221+ echo " kotlinc ${kotlin_opts} -classpath '$( resdeps .kts ${dependencies} ) '"
208222 exit 0
209223fi
210224
0 commit comments