Skip to content

Commit 58f9b23

Browse files
committed
restored separate CLI parsing for bin/scala and bin/scalac ; refactor and cleanup
1 parent 2290dbc commit 58f9b23

File tree

4 files changed

+172
-129
lines changed

4 files changed

+172
-129
lines changed

dist/bin/common

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@ classpathArgs () {
185185
toolchain+="$JLINE_READER$PSEP"
186186
toolchain+="$JLINE_TERMINAL$PSEP"
187187
toolchain+="$JLINE_TERMINAL_JNA$PSEP"
188-
toolchain+="$JNA$PSEP"
188+
[ -n "${JNA-}" ] && toolchain+="$JNA$PSEP"
189189

190-
if [ -n "$jvm_cp_args" ]; then
190+
if [ -n "${jvm_cp_args-}" ]; then
191191
jvm_cp_args="$toolchain$jvm_cp_args"
192192
else
193193
jvm_cp_args="$toolchain$PSEP"
@@ -201,6 +201,9 @@ DecompilerMain=dotty.tools.dotc.decompiler.Main
201201
ReplMain=dotty.tools.repl.Main
202202
ScriptingMain=dotty.tools.scripting.Main
203203

204+
declare -a residual_args
205+
declare -a script_args
206+
204207
addJava () {
205208
java_args+=("'$1'")
206209
}
@@ -210,44 +213,6 @@ addScala () {
210213
addResidual () {
211214
residual_args+=("'$1'")
212215
}
213-
addScripting () {
214-
scripting_args+=("'$1'")
215-
}
216-
prepScalacCommandLine () {
217-
withCompiler=true
218-
219-
while [[ $# -gt 0 ]]; do
220-
case "$1" in
221-
--) shift; for arg; do addResidual "$arg"; done; set -- ;;
222-
-v|-verbose) verbose=true && addScala "-verbose" && shift ;;
223-
-debug) DEBUG="$DEBUG_STR" && shift ;;
224-
-q|-quiet) quiet=true && shift ;;
225-
226-
# Optimize for short-running applications, see https://github.com/lampepfl/dotty/issues/222
227-
-Oshort) addJava "-XX:+TieredCompilation -XX:TieredStopAtLevel=1" && shift ;;
228-
-repl) PROG_NAME="$ReplMain" && shift ;;
229-
-script) PROG_NAME="$ScriptingMain" && target_script="$2" && shift && shift
230-
while [[ $# -gt 0 ]]; do addScripting "$1" && shift ; done ;;
231-
-compile) PROG_NAME="$CompilerMain" && shift ;;
232-
-decompile) PROG_NAME="$DecompilerMain" && shift ;;
233-
-print-tasty) PROG_NAME="$DecompilerMain" && addScala "-print-tasty" && shift ;;
234-
-run) PROG_NAME="$ReplMain" && shift ;;
235-
-colors) colors=true && shift ;;
236-
-no-colors) unset colors && shift ;;
237-
-with-compiler) jvm_cp_args="$PSEP$DOTTY_COMP$PSEP$TASTY_CORE" && shift ;;
238-
239-
# break out -D and -J options and add them to java_args so
240-
# they reach the JVM in time to do some good. The -D options
241-
# will be available as system properties.
242-
-D*) addJava "$1" && shift ;;
243-
-J*) addJava "${1:2}" && shift ;;
244-
*) addResidual "$1" && shift ;;
245-
esac
246-
done
247-
248-
classpathArgs
249-
250-
if [ "$PROG_NAME" == "$ScriptingMain" ]; then
251-
scripting_string="-script $target_script ${scripting_args[@]}"
252-
fi
216+
addScript () {
217+
script_args+=("'$1'")
253218
}

dist/bin/scala

Lines changed: 107 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -26,77 +26,81 @@ if [ -z "${PROG_HOME-}" ] ; then
2626
cd "$saveddir"
2727
fi
2828

29-
addJava () {
30-
java_args+=("'$1'")
31-
}
29+
source "$PROG_HOME/bin/common"
3230

33-
addScalacOptions () {
34-
java_options+=("'$1'")
31+
# This script operates in one of 3 usage modes:
32+
# script
33+
# repl
34+
# run
35+
# execute_mode replaces mutually exclusive booleans:
36+
# execute_repl=false
37+
# execute_run=false
38+
# execute_script=false
39+
setExecuteMode () {
40+
case "${execute_mode-}" in
41+
"") execute_mode="$1" ; shift ;;
42+
*) echo "execute_mode==[${execute_mode-}], attempted overwrite by [$1]" 1>&2
43+
exit 1
44+
;;
45+
esac
3546
}
3647

37-
source "$PROG_HOME/bin/common"
3848

39-
declare -a residual_args
40-
declare -a script_args
41-
execute_repl=false
42-
execute_run=false
43-
execute_script=false
44-
with_compiler=false
45-
class_path_count=0
46-
CLASS_PATH=""
47-
save_compiled=false
49+
with_compiler=false # to add compiler jars to repl classpath
50+
let class_path_count=0 || true # count classpath args, warning if more than 1
51+
save_compiled=false # to save compiled script jar in script directory
52+
CLASS_PATH="" || true # scala classpath
4853

4954
# Little hack to check if all arguments are options
5055
all_params="$*"
5156
truncated_params="${*#-}"
5257
# options_indicator != 0 if at least one parameter is not an option
5358
options_indicator=$(( ${#all_params} - ${#truncated_params} - $# ))
5459

55-
[ -n "$SCALA_OPTS" ] && set -- $SCALA_OPTS "$@"
60+
[ -n "${SCALA_OPTS-}" ] && set -- $SCALA_OPTS "$@"
5661

5762
while [[ $# -gt 0 ]]; do
5863
case "$1" in
5964
-repl)
60-
execute_repl=true
65+
setExecuteMode 'repl'
6166
shift
6267
;;
6368
-run)
64-
execute_run=true
69+
setExecuteMode 'run'
6570
shift
6671
;;
6772
-cp | -classpath)
6873
CLASS_PATH="$2${PSEP}"
69-
class_path_count+=1
74+
let class_path_count+=1
7075
shift
7176
shift
7277
;;
73-
-cp*|-classpath*)
78+
-cp*|-classpath*) # partial fix for #10761
7479
# hashbang can combine args, e.g. "-classpath 'lib/*'"
7580
CLASS_PATH="${1#* *}${PSEP}"
76-
class_path_count+=1
81+
let class_path_count+=1
7782
shift
7883
;;
7984
-with-compiler)
8085
with_compiler=true
8186
shift
8287
;;
8388
@*|-color:*)
84-
addScalacOptions "${1}"
89+
addScala "${1}"
8590
shift
8691
;;
8792
-save|-savecompiled)
8893
save_compiled=true
89-
scala_script_options+=("$1")
94+
addScala "$1"
9095
shift
9196
;;
9297
-compile-only)
93-
scala_script_options+=("$1")
98+
addScala "$1"
9499
shift
95100
;;
96-
-d)
101+
-d|-debug)
97102
DEBUG="$DEBUG_STR"
98-
shift
99-
;;
103+
shift ;;
100104
-version)
101105
# defer to scalac, then exit
102106
shift
@@ -106,17 +110,32 @@ while [[ $# -gt 0 ]]; do
106110
;;
107111
-J*)
108112
addJava "${1:2}"
109-
addScalacOptions "${1}"
113+
addScala "${1}"
114+
shift ;;
115+
-v|-verbose)
116+
verbose=true
117+
addScala "-verbose"
110118
shift ;;
119+
-run|-repl)
120+
PROG_NAME="$ReplMain"
121+
shift ;;
122+
111123
*)
112-
if [ $execute_script == false ]; then
124+
if [ "${execute_mode-}" == 'script' ]; then
125+
addScript "$1"
126+
else
127+
# script if extension .scala or .sc, or if has scala hashbang line
128+
129+
# no -f test, issue meaningful error message (file not found)
113130
if [[ "$1" == *.scala || "$1" == *.sc ]]; then
114-
# is a script if extension .scala or .sc
115-
# (even if file not found, to avoid adding likely typo to residual_args)
116-
execute_script=true
117-
target_script="$1"
131+
setExecuteMode 'script' # execute_script=true
132+
133+
# -f test needed before we examine the hashbang line
118134
elif [[ (-f "$1" && `head -n 1 -- "$1" | grep '#!.*scala'`) ]]; then
119-
execute_script=true
135+
setExecuteMode 'script' # execute_script=true
136+
fi
137+
138+
if [ "${execute_mode-}" == 'script' ]; then
120139
target_script="$1"
121140
if [ ! -f $target_script ]; then
122141
# likely a typo or missing script file, quit early
@@ -125,70 +144,93 @@ while [[ $# -gt 0 ]]; do
125144
onExit
126145
fi
127146
else
128-
# unrecognized args appearing prior to a script name
129-
residual_args+=("$1")
147+
# all unrecognized args appearing prior to a script name
148+
addResidual "$1"
130149
fi
131-
else
132-
script_args+=("$1")
133150
fi
134151
shift
135152
;;
136153

137154
esac
138155
done
139156

157+
#[ -n "${dump_args}" ] && dumpArgs ; exit 2
158+
159+
if [ -z "${execute_mode-}" ]; then
160+
# no script was specified, set run or repl mode
161+
if [[ $options_indicator -ne 0 || ${#residual_args[@]} -ne 0 ]]; then
162+
setExecuteMode 'run'
163+
else
164+
setExecuteMode 'repl'
165+
fi
166+
fi
167+
140168
[ -n "${script_trace-}" ] && set -x
141-
if [ $execute_script == true ]; then
169+
170+
case "${execute_mode-}" in
171+
script)
142172
if [ "$CLASS_PATH" ]; then
143-
cp_arg="-classpath \"$CLASS_PATH\""
173+
script_cp_arg="-classpath '$CLASS_PATH'"
144174
fi
145-
java_options+=(${scala_script_options})
146175
setScriptName="-Dscript.path=$target_script"
147176
target_jar="${target_script%.*}.jar"
148177
if [[ $save_compiled == true && "$target_jar" -nt "$target_script" ]]; then
149178
eval "\"$JAVACMD\"" $setScriptName -jar "$target_jar" "${script_args[@]}"
150179
scala_exit_status=$?
151180
else
152181
[[ $save_compiled == true ]] && rm -f $target_jar
153-
set -- ${cp_arg-} ${java_options[@]} ${residual_args[@]} -script "$target_script" ${script_args[@]}
154-
PROG_MAIN=$ScriptingMain
155-
prepScalacCommandLine "$@"
156-
# exec here would prevent onExit from being called, leaving terminal in unusable state
182+
PROG_NAME=$ScriptingMain
183+
classpathArgs # initialize jvm_cp_args with toolchain classpath
184+
scripting_string="-script $target_script ${script_args[@]}"
185+
# use eval instead of exec, to insure that onExit is subsequently called
186+
187+
# $script_cp_arg must be the first argument to $ScriptingMain
188+
# $scripting_string must be last
157189
eval "\"$JAVACMD\"" \
158-
${JAVA_OPTS:-$default_java_opts} \
159-
"${DEBUG-}" \
160-
"${java_args[@]}" \
161-
"-classpath \"$jvm_cp_args\"" \
162-
-Dscala.usejavacp=true \
163-
"$PROG_NAME" \
164-
"${scala_args[@]}" \
165-
"${residual_args[@]}" \
166-
"${scripting_string-}"
190+
${JAVA_OPTS:-$default_java_opts} \
191+
"${DEBUG-}" \
192+
"${java_args[@]}" \
193+
"-classpath \"$jvm_cp_args\"" \
194+
-Dscala.usejavacp=true \
195+
"$setScriptName" \
196+
"$ScriptingMain" \
197+
${script_cp_arg-} \
198+
"${scala_args[@]}" \
199+
"${residual_args[@]}" \
200+
"${scripting_string-}" # must be the last arguments
167201
scala_exit_status=$?
168202
fi
169-
elif [ $execute_repl == true ] || ([ $execute_run == false ] && [ $options_indicator == 0 ]); then
203+
;;
204+
205+
repl)
170206
if [ "$CLASS_PATH" ]; then
171-
cp_arg="-classpath \"$CLASS_PATH\""
207+
repl_cparg="-classpath \"$CLASS_PATH\""
172208
fi
173-
eval "\"$PROG_HOME/bin/scalac\" ${cp_arg-} ${java_options[@]} -repl ${residual_args[@]}"
209+
eval "\"$PROG_HOME/bin/scalac\" ${repl_cparg-} ${scalac_options[@]} -repl ${residual_args[@]}"
174210
scala_exit_status=$?
175-
elif [ ${#residual_args[@]} -ne 0 ]; then
176-
cp_arg="$DOTTY_LIB$PSEP$SCALA_LIB"
211+
;;
212+
213+
run)
214+
repl_cparg="$DOTTY_LIB$PSEP$SCALA_LIB"
177215
if [ -z "$CLASS_PATH" ]; then
178-
cp_arg+="$PSEP."
216+
repl_cparg+="$PSEP."
179217
else
180-
cp_arg+="$PSEP$CLASS_PATH"
218+
repl_cparg+="$PSEP$CLASS_PATH"
181219
fi
182220
if [ "$class_path_count" -gt 1 ]; then
183221
echo "warning: multiple classpaths are found, scala only use the last one."
184222
fi
185223
if [ $with_compiler == true ]; then
186-
cp_arg+="$PSEP$DOTTY_COMP$PSEP$TASTY_CORE$PSEP$DOTTY_INTF$PSEP$SCALA_ASM$PSEP$DOTTY_STAGING$PSEP$DOTTY_TASTY_INSPECTOR"
224+
repl_cparg+="$PSEP$DOTTY_COMP$PSEP$TASTY_CORE$PSEP$DOTTY_INTF$PSEP$SCALA_ASM$PSEP$DOTTY_STAGING$PSEP$DOTTY_TASTY_INSPECTOR"
187225
fi
188226
# exec here would prevent onExit from being called, leaving terminal in unusable state
189-
eval "\"$JAVACMD\"" "$DEBUG" "-classpath \"$cp_arg\"" "${java_args[@]}" "${residual_args[@]}"
227+
eval "\"$JAVACMD\"" "$DEBUG" "-classpath \"$repl_cparg\"" "${java_args[@]}" "${residual_args[@]}"
190228
scala_exit_status=$?
191-
else
229+
;;
230+
231+
*)
192232
echo "warning: command option is not correct."
193-
fi
233+
;;
234+
esac
235+
194236
onExit

0 commit comments

Comments
 (0)