-
Notifications
You must be signed in to change notification settings - Fork 1k
/
scala
executable file
·154 lines (138 loc) · 3.78 KB
/
scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env bash
# Try to autodetect real location of the script
if [ -z "$PROG_HOME" ] ; then
## resolve links - $0 may be a link to PROG_HOME
PRG="$0"
# need this for relative symlinks
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG="`dirname "$PRG"`/$link"
fi
done
saveddir=`pwd`
PROG_HOME=`dirname "$PRG"`/..
# make it fully qualified
PROG_HOME=`cd "$PROG_HOME" && pwd`
cd "$saveddir"
fi
addJvmOptions () {
jvm_options+=("'$1'")
}
addDotcOptions () {
java_options+=("'$1'")
}
source "$PROG_HOME/bin/common"
declare -a residual_args
declare -a script_args
execute_repl=false
execute_run=false
execute_script=false
with_compiler=false
class_path_count=0
CLASS_PATH=""
save_compiled=false
# Little hack to check if all arguments are options
all_params="$*"
truncated_params="${*#-}"
# options_indicator != 0 if at least one parameter is not an option
options_indicator=$(( ${#all_params} - ${#truncated_params} - $# ))
[ -n "$SCALA_OPTS" ] && set -- $SCALA_OPTS "$@"
while [[ $# -gt 0 ]]; do
case "$1" in
-repl)
execute_repl=true
shift
;;
-run)
execute_run=true
shift
;;
-cp | -classpath)
CLASS_PATH="$2"
class_path_count+=1
shift
shift
;;
-with-compiler)
with_compiler=true
shift
;;
@*|-color:*)
addDotcOptions "${1}"
shift
;;
-save|-savecompiled)
save_compiled=true
dotc_script_options+=("$1")
shift
;;
-compile-only)
dotc_script_options+=("$1")
shift
;;
-d)
DEBUG="$DEBUG_STR"
shift
;;
-J*)
addJvmOptions "${1:2}"
addDotcOptions "${1}"
shift ;;
*)
if [ $execute_script == false ]; then
# is a script if extension .scala or .sc or if has scala hash bang
if [[ -e "$1" && ("$1" == *.scala || "$1" == *.sc || -f "$1" && `head -n 1 -- "$1" | grep '#!.*scala'`) ]]; then
execute_script=true
target_script="$1"
else
residual_args+=("$1")
fi
else
script_args+=("$1")
fi
shift
;;
esac
done
if [ $execute_script == true ]; then
[ -n "$script_trace" ] && set -x
if [ "$CLASS_PATH" ]; then
cp_arg="-classpath \"$CLASS_PATH\""
fi
java_options+=(${dotc_script_options})
setScriptName="-Dscript.path=$target_script"
target_jar="${target_script%.*}.jar"
if [[ $save_compiled == true && "$target_jar" -nt "$target_script" ]]; then
java $setScriptName -jar "$target_jar" "${script_args[@]}"
else
[[ $save_compiled == true ]] && rm -f $target_jar
residual_args+=($setScriptName)
eval "\"$PROG_HOME/bin/scalac\" $cp_arg ${java_options[@]} ${residual_args[@]} -script $target_script ${script_args[@]}"
fi
elif [ $execute_repl == true ] || ([ $execute_run == false ] && [ $options_indicator == 0 ]); then
if [ "$CLASS_PATH" ]; then
cp_arg="-classpath \"$CLASS_PATH\""
fi
echo "Starting scala3 REPL..."
eval "\"$PROG_HOME/bin/scalac\" $cp_arg ${java_options[@]} -repl ${residual_args[@]}"
elif [ $execute_repl == true ] || [ ${#residual_args[@]} -ne 0 ]; then
cp_arg="$DOTTY_LIB$PSEP$SCALA_LIB"
if [ -z "$CLASS_PATH" ]; then
cp_arg+="$PSEP."
else
cp_arg+="$PSEP$CLASS_PATH"
fi
if [ "$class_path_count" -gt 1 ]; then
echo "warning: multiple classpaths are found, scala only use the last one."
fi
if [ $with_compiler == true ]; then
cp_arg+="$PSEP$DOTTY_COMP$PSEP$TASTY_CORE$PSEP$DOTTY_INTF$PSEP$SCALA_ASM$PSEP$DOTTY_STAGING$PSEP$DOTTY_TASTY_INSPECTOR"
fi
eval exec "\"$JAVACMD\"" "$DEBUG" "-classpath \"$cp_arg\"" "${jvm_options[@]}" "${residual_args[@]}"
else
echo "warning: command option is not correct."
fi