Skip to content

Commit 42e1ada

Browse files
noti0na1tgodzik
authored andcommitted
Add REPL flag to quit after evaluating init script
1 parent 05b01e7 commit 42e1ada

File tree

7 files changed

+214
-3
lines changed

7 files changed

+214
-3
lines changed

bin/replQ

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
3+
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" >& /dev/null && pwd)/.."
4+
. $ROOT/bin/commonQ
5+
6+
java -Dscala.usejavacp=true -cp $cp dotty.tools.repl.Main -usejavacp "$@"

compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ trait CommonScalaSettings:
135135
val usejavacp: Setting[Boolean] = BooleanSetting("-usejavacp", "Utilize the java.class.path in classpath resolution.", aliases = List("--use-java-class-path"))
136136
val scalajs: Setting[Boolean] = BooleanSetting("-scalajs", "Compile in Scala.js mode (requires scalajs-library.jar on the classpath).", aliases = List("--scalajs"))
137137
val replInitScript: Setting[String] = StringSetting("-repl-init-script", "code", "The code will be run on REPL startup.", "", aliases = List("--repl-init-script"))
138+
val replEvalOnly: Setting[Boolean] = BooleanSetting("-repl-eval", "Quit REPL after evaluating the init script.", aliases = List("--repl-eval"))
138139

139140
end CommonScalaSettings
140141

compiler/src/dotty/tools/repl/ReplDriver.scala

+3-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ class ReplDriver(settings: Array[String],
139139
*
140140
* Possible reason for unsuccessful run are raised flags in CLI like --help or --version
141141
*/
142-
final def tryRunning = if shouldStart then runUntilQuit()
142+
final def tryRunning = if shouldStart then
143+
if rootCtx.settings.replEvalOnly.value(using rootCtx) then initialState
144+
else runUntilQuit()
143145

144146
/** Run REPL with `state` until `:quit` command found
145147
*

compiler/test/dotty/tools/scripting/BashExitCodeTests.scala

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class BashExitCodeTests:
3030
def scala(args: String*) = verifyExit(scalaPath, args*)
3131
def scalacRaw(args: String*) = verifyExit(scalacPath, args*)
3232
def scalac(args: String*) = scalacRaw(("-d" +: tmpDir +: args)*)
33+
def repl(args: String*) = verifyExit(replPath, args*)
3334

3435
/** The path to the test file for this class. */
3536
def f(body: String, suffix: String = ".scala"): String =
@@ -62,6 +63,8 @@ class BashExitCodeTests:
6263
@Test def xPluginList = scala("-Xplugin-list")(0)
6364
@Test def vPhases = scala("-Vphases")(0)
6465

66+
@Test def replEval = repl("--repl-eval", "--repl-init-script", "\'println(\"Hello from init script!\"); val i = 2 * 2\'")(0)
67+
6568
/** A utility for running two commands in a row, like you do in bash. */
6669
extension (inline u1: Unit) inline def & (inline u2: Unit): Unit = { u1; u2 }
6770
end BashExitCodeTests

compiler/test/dotty/tools/scripting/ScriptTestEnv.scala

+3-2
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,11 @@ object ScriptTestEnv {
234234

235235
lazy val cwd: Path = Paths.get(".").toAbsolutePath.normalize
236236

237-
lazy val (scalacPath: String, scalaPath: String) = {
237+
lazy val (scalacPath: String, scalaPath: String, replPath: String) = {
238238
val scalac = s"$workingDirectory/dist/target/pack/bin/scalac".toPath.normalize
239239
val scala = s"$workingDirectory/dist/target/pack/bin/scala".toPath.normalize
240-
(scalac.norm, scala.norm)
240+
val repl = s"$workingDirectory/dist/target/pack/bin/repl".toPath.normalize
241+
(scalac.norm, scala.norm, repl.norm)
241242
}
242243

243244

dist/bin/repl

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env bash
2+
3+
#set -o nounset ; set -o errexit
4+
5+
# Try to autodetect real location of the script
6+
if [ -z "${PROG_HOME-}" ] ; then
7+
## resolve links - $0 may be a link to PROG_HOME
8+
PRG="$0"
9+
10+
# need this for relative symlinks
11+
while [ -h "$PRG" ] ; do
12+
ls=`ls -ld "$PRG"`
13+
link=`expr "$ls" : '.*-> \(.*\)$'`
14+
if expr "$link" : '/.*' > /dev/null; then
15+
PRG="$link"
16+
else
17+
PRG="`dirname "$PRG"`/$link"
18+
fi
19+
done
20+
21+
saveddir=`pwd`
22+
23+
PROG_HOME=`dirname "$PRG"`/..
24+
25+
# make it fully qualified
26+
PROG_HOME=`cd "$PROG_HOME" && pwd`
27+
28+
cd "$saveddir"
29+
fi
30+
31+
source "$PROG_HOME/libexec/common"
32+
default_java_opts="-Xmx768m -Xms768m"
33+
withCompiler=true
34+
35+
CompilerMain=dotty.tools.dotc.Main
36+
DecompilerMain=dotty.tools.dotc.decompiler.Main
37+
ReplMain=dotty.tools.repl.Main
38+
ScriptingMain=dotty.tools.scripting.Main
39+
JVM_CP_ARGS="$PROG_HOME/lib/scaladoc.jar"
40+
41+
PROG_NAME=$CompilerMain
42+
43+
addJava () {
44+
java_args+=("'$1'")
45+
}
46+
addScala () {
47+
scala_args+=("'$1'")
48+
}
49+
addResidual () {
50+
residual_args+=("'$1'")
51+
}
52+
addScrip() {
53+
script_args+=("'$1'")
54+
}
55+
56+
#for A in "$@" ; do echo "A[$A]" ; done ; exit 2
57+
58+
while [[ $# -gt 0 ]]; do
59+
case "$1" in
60+
--) shift; for arg; do addResidual "$arg"; done; set -- ;;
61+
-v|-verbose) verbose=true && addScala "-verbose" && shift ;;
62+
-q|-quiet) quiet=true && shift ;;
63+
64+
-colors) colors=true && shift ;;
65+
-no-colors) unset colors && shift ;;
66+
# break out -D and -J options and add them to java_args so
67+
# they reach the JVM in time to do some good. The -D options
68+
# will be available as system properties.
69+
-D*) addJava "$1" && shift ;;
70+
-J*) addJava "${1:2}" && shift ;;
71+
*) addResidual "$1"
72+
shift
73+
;;
74+
esac
75+
done
76+
77+
eval "\"$JAVACMD\"" \
78+
${JAVA_OPTS:-$default_java_opts} \
79+
"${java_args[@]}" \
80+
-classpath "${JVM_CP_ARGS}" \
81+
-Dscala.expandjavacp=true \
82+
-Dscala.usejavacp=true \
83+
"dotty.tools.repl.Main" \
84+
"${scala_args[@]}" \
85+
"${residual_args[@]}" \
86+
"${scripting_string-}"
87+
scala_exit_status=$?
88+
onExit

dist/bin/repl.bat

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
@echo off
2+
setlocal enabledelayedexpansion
3+
4+
@rem #########################################################################
5+
@rem ## Environment setup
6+
7+
set _EXITCODE=0
8+
9+
for %%f in ("%~dp0.") do (
10+
set "_PROG_HOME=%%~dpf"
11+
@rem get rid of the trailing slash
12+
set "_PROG_HOME=!_PROG_HOME:~0,-1!"
13+
)
14+
call "%_PROG_HOME%\libexec\common.bat"
15+
if not %_EXITCODE%==0 goto end
16+
17+
set _DEFAULT_JAVA_OPTS=-Xmx768m -Xms768m
18+
19+
call :args %*
20+
21+
@rem #########################################################################
22+
@rem ## Main
23+
24+
if defined JAVA_OPTS ( set _JAVA_OPTS=%JAVA_OPTS%
25+
) else ( set _JAVA_OPTS=%_DEFAULT_JAVA_OPTS%
26+
)
27+
28+
@rem we need to escape % in the java command path, for some reason this doesnt work in common.bat
29+
set "_JAVACMD=!_JAVACMD:%%=%%%%!"
30+
31+
call "%_JAVACMD%" %_JAVA_OPTS% %_JAVA_DEBUG% %_JAVA_ARGS% ^
32+
-classpath "%_LIB_DIR%\scaladoc.jar" ^
33+
-Dscala.expandjavacp=true ^
34+
-Dscala.usejavacp=true ^
35+
dotty.tools.repl.Main %_SCALA_ARGS% %_RESIDUAL_ARGS%
36+
if not %ERRORLEVEL%==0 (
37+
@rem echo Error: Scaladoc execution failed 1>&2
38+
set _EXITCODE=1
39+
goto end
40+
)
41+
goto end
42+
43+
@rem #########################################################################
44+
@rem ## Subroutines
45+
46+
:args
47+
set _JAVA_DEBUG=
48+
set _HELP=
49+
set _VERBOSE=
50+
set _QUIET=
51+
set _COLORS=
52+
set _SCALA_ARGS=
53+
set _JAVA_ARGS=
54+
set _RESIDUAL_ARGS=
55+
56+
:args_loop
57+
if "%~1"=="" goto args_done
58+
set "__ARG=%~1"
59+
if "%__ARG%"=="--" (
60+
@rem for arg; do addResidual "$arg"; done; set -- ;;
61+
) else if "%__ARG%"=="-h" (
62+
set _HELP=true
63+
call :addScala "-help"
64+
) else if "%__ARG%"=="-help" (
65+
set _HELP=true
66+
call :addScala "-help"
67+
) else if "%__ARG%"=="-v" (
68+
set _VERBOSE=true
69+
call :addScala "-verbose"
70+
) else if "%__ARG%"=="-verbose" (
71+
set _VERBOSE=true
72+
call :addScala "-verbose"
73+
) else if "%__ARG%"=="-debug" ( set "_JAVA_DEBUG=%_DEBUG_STR%"
74+
) else if "%__ARG%"=="-q" ( set _QUIET=true
75+
) else if "%__ARG%"=="-quiet" ( set _QUIET=true
76+
) else if "%__ARG%"=="-colors" ( set _COLORS=true
77+
) else if "%__ARG%"=="-no-colors" ( set _COLORS=
78+
) else if "%__ARG:~0,2%"=="-D" ( call :addJava "%__ARG%"
79+
) else if "%__ARG:~0,2%"=="-J" ( call :addJava "%__ARG:~2%"
80+
) else (
81+
if defined _IN_SCRIPTING_ARGS ( call :addScripting "%__ARG%"
82+
) else ( call :addResidual "%__ARG%"
83+
)
84+
)
85+
shift
86+
goto args_loop
87+
:args_done
88+
goto :eof
89+
90+
@rem output parameter: _SCALA_ARGS
91+
:addScala
92+
set _SCALA_ARGS=%_SCALA_ARGS% %~1
93+
goto :eof
94+
95+
@rem output parameter: _JAVA_ARGS
96+
:addJava
97+
set _JAVA_ARGS=%_JAVA_ARGS% %~1
98+
goto :eof
99+
100+
@rem output parameter: _RESIDUAL_ARGS
101+
:addResidual
102+
set _RESIDUAL_ARGS=%_RESIDUAL_ARGS% %~1
103+
goto :eof
104+
105+
@rem #########################################################################
106+
@rem ## Cleanups
107+
108+
:end
109+
exit /b %_EXITCODE%
110+
endlocal

0 commit comments

Comments
 (0)