From b42043a64b8f6d13311c251564e75aaca6ad8fcc Mon Sep 17 00:00:00 2001 From: Eugene Susla Date: Mon, 3 Dec 2018 19:51:07 -0800 Subject: [PATCH] Expose script file name to script itself See #196 and #195 for details Test Plan: test/test_suite.sh --- src/kscript | 3 +++ test/resources/uses_self_file_name.kts | 2 ++ test/test_suite.sh | 4 ++++ 3 files changed, 9 insertions(+) create mode 100755 test/resources/uses_self_file_name.kts diff --git a/src/kscript b/src/kscript index 97bc5158..1fbede4a 100755 --- a/src/kscript +++ b/src/kscript @@ -27,5 +27,8 @@ if [[ $(uname) == CYGWIN* ]]; then jarPath=$(cygpath -w ${jarPath}); fi ## prefer KOTLIN_HOME instead of PATH to resolve `kotlin` location (see #145) if [[ -z "$KOTLIN_HOME" ]]; then KOTLIN_EXEC="kotlin"; else KOTLIN_EXEC="$KOTLIN_HOME/bin/kotlin"; fi +## expose the name of the script being run to the script itself +export KSCRIPT_FILE="$1" + ## run it using command substitution to have just the user process once kscript is done eval "exec $(${KOTLIN_EXEC} -classpath ${jarPath} kscript.app.KscriptKt "$@")" diff --git a/test/resources/uses_self_file_name.kts b/test/resources/uses_self_file_name.kts new file mode 100755 index 00000000..bd8668d0 --- /dev/null +++ b/test/resources/uses_self_file_name.kts @@ -0,0 +1,2 @@ +#!/usr/bin/env kscript +println("Usage: ${System.getenv("KSCRIPT_FILE")} [-ae] [--foo] file+") \ No newline at end of file diff --git a/test/test_suite.sh b/test/test_suite.sh index c72a4d2b..a948d784 100755 --- a/test/test_suite.sh +++ b/test/test_suite.sh @@ -115,6 +115,10 @@ assert "unset KOTLIN_HOME; echo 'println(99)' | kscript -" "99" ## todo test what happens if kotlin/kotlinc/java/maven is not in PATH +## run script that tries to find out its own filename via environment variable +f="${KSCRIPT_HOME}/test/resources/uses_self_file_name.kts" +assert "$f" "Usage: $f [-ae] [--foo] file+" + assert_end environment_tests