Skip to content

Commit d600d2f

Browse files
scala binary can execute scripts
1 parent 7912002 commit d600d2f

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

dist/bin/scala

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ addDotcOptions () {
3838
source "$PROG_HOME/bin/common"
3939

4040
declare -a residual_args
41+
declare -a script_args
4142
execute_repl=false
4243
execute_run=false
44+
execute_script=false
4345
with_compiler=false
4446
class_path_count=0
4547
CLASS_PATH=""
@@ -79,13 +81,28 @@ while [[ $# -gt 0 ]]; do
7981
addDotcOptions "${1}"
8082
shift ;;
8183
*)
82-
residual_args+=("$1")
84+
if [ $execute_script == false ]; then
85+
if [[ "$1" == *.scala ]]; then
86+
execute_script=true
87+
target_script="$1"
88+
else
89+
residual_args+=("$1")
90+
fi
91+
else
92+
script_args+=("$1")
93+
fi
8394
shift
8495
;;
8596

8697
esac
8798
done
88-
if [ $execute_repl == true ] || ([ $execute_run == false ] && [ $options_indicator == 0 ]); then
99+
100+
if [ $execute_script == true ]; then
101+
if [ "$CLASS_PATH" ]; then
102+
cp_arg="-classpath \"$CLASS_PATH\""
103+
fi
104+
eval "\"$PROG_HOME/bin/scalac\" $cp_arg ${java_options[@]} ${residual_args[@]} -script $target_script ${script_args[@]}"
105+
elif [ $execute_repl == true ] || ([ $execute_run == false ] && [ $options_indicator == 0 ]); then
89106
if [ "$CLASS_PATH" ]; then
90107
cp_arg="-classpath \"$CLASS_PATH\""
91108
fi

dist/bin/scalac

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ withCompiler=true
3333
CompilerMain=dotty.tools.dotc.Main
3434
DecompilerMain=dotty.tools.dotc.decompiler.Main
3535
ReplMain=dotty.tools.repl.Main
36+
ScriptingMain=dotty.tools.scripting.Main
3637

3738
PROG_NAME=$CompilerMain
3839

@@ -45,6 +46,9 @@ addScala () {
4546
addResidual () {
4647
residual_args+=("'$1'")
4748
}
49+
addScripting () {
50+
scripting_args+=("'$1'")
51+
}
4852

4953
classpathArgs () {
5054
# echo "dotty-compiler: $DOTTY_COMP"
@@ -74,6 +78,7 @@ classpathArgs () {
7478
jvm_cp_args="-classpath \"$toolchain\""
7579
}
7680

81+
in_scripting_args=false
7782
while [[ $# -gt 0 ]]; do
7883
case "$1" in
7984
--) shift; for arg; do addResidual "$arg"; done; set -- ;;
@@ -85,6 +90,7 @@ case "$1" in
8590
# Optimize for short-running applications, see https://github.com/lampepfl/dotty/issues/222
8691
-Oshort) addJava "-XX:+TieredCompilation -XX:TieredStopAtLevel=1" && shift ;;
8792
-repl) PROG_NAME="$ReplMain" && shift ;;
93+
-script) PROG_NAME="$ScriptingMain" && target_script="$2" && in_scripting_args=true && shift && shift ;;
8894
-compile) PROG_NAME="$CompilerMain" && shift ;;
8995
-decompile) PROG_NAME="$DecompilerMain" && shift ;;
9096
-print-tasty) PROG_NAME="$DecompilerMain" && addScala "-print-tasty" && shift ;;
@@ -98,12 +104,22 @@ case "$1" in
98104
# will be available as system properties.
99105
-D*) addJava "$1" && shift ;;
100106
-J*) addJava "${1:2}" && shift ;;
101-
*) addResidual "$1" && shift ;;
107+
*) if [ $in_scripting_args == false ]; then
108+
addResidual "$1"
109+
else
110+
addScripting "$1"
111+
fi
112+
shift
113+
;;
102114
esac
103115
done
104116

105117
classpathArgs
106118

119+
if [ "$PROG_NAME" == "$ScriptingMain" ]; then
120+
scripting_string="-script $target_script ${scripting_args[@]}"
121+
fi
122+
107123
eval exec "\"$JAVACMD\"" \
108124
${JAVA_OPTS:-$default_java_opts} \
109125
"$DEBUG" \
@@ -112,5 +128,6 @@ eval exec "\"$JAVACMD\"" \
112128
-Dscala.usejavacp=true \
113129
"$PROG_NAME" \
114130
"${scala_args[@]}" \
115-
"${residual_args[@]}"
131+
"${residual_args[@]}" \
132+
"$scripting_string"
116133
exit $?

0 commit comments

Comments
 (0)