Skip to content

Commit 1a9ba3a

Browse files
committed
Add REPL flag to quit after evaluating init script
1 parent 93af7b8 commit 1a9ba3a

File tree

7 files changed

+215
-4
lines changed

7 files changed

+215
-4
lines changed

Diff for: 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 "$@"

Diff for: compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

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

133134
/** -P "plugin" settings. Various tools might support plugins. */

Diff for: compiler/src/dotty/tools/repl/ReplDriver.scala

+3-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ class ReplDriver(settings: Array[String],
153153
*
154154
* Possible reason for unsuccessful run are raised flags in CLI like --help or --version
155155
*/
156-
final def tryRunning = if shouldStart then runUntilQuit()
156+
final def tryRunning = if shouldStart then
157+
if rootCtx.settings.replEvalOnly.value(using rootCtx) then initialState
158+
else runUntilQuit()
157159

158160
/** Run REPL with `state` until `:quit` command found
159161
*

Diff for: compiler/test/dotty/tools/scripting/BashExitCodeTests.scala

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ class BashExitCodeTests:
3232
s"expected $expectedExitCode but got $exitCode${pp("out", stdout)}${pp("err", stderr)}"
3333
}, expectedExitCode, exitCode)
3434

35-
// Helpers for running scala, scalac, and scalac without the output directory ("raw")
35+
// Helpers for running scala, scalac, scalac, and repl without the output directory ("raw")
3636
def scala(args: String*) = verifyExit(scalaPath, ("--power" +: args :+ "--offline" :+ "--server=false")*)
3737
def scalacRaw(args: String*) = verifyExit(scalacPath, args*)
3838
def scalac(args: String*) = scalacRaw(("-d" +: tmpDir +: args)*)
39+
def repl(args: String*) = verifyExit(replPath, args*)
3940

4041
/** The path to the test file for this class. */
4142
def f(body: String, suffix: String = ".scala"): String =
@@ -72,6 +73,8 @@ class BashExitCodeTests:
7273
@Test def xPluginList = scala("-Xplugin-list")(0)
7374
@Test def vPhases = scala("-Vphases")(0)
7475

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

Diff for: compiler/test/dotty/tools/scripting/ScriptTestEnv.scala

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

293293
lazy val cwd: Path = Paths.get(".").toAbsolutePath.normalize
294294

295-
lazy val (scalacPath: String, scalaPath: String) = {
295+
lazy val (scalacPath: String, scalaPath: String, replPath: String) = {
296296
val scalac = s"$workingDirectory/$packBinDir/scalac".toPath.normalize
297297
val scala = s"$workingDirectory/$packBinDir/scala".toPath.normalize
298-
(scalac.norm, scala.norm)
298+
val repl = s"$workingDirectory/$packBinDir/repl".toPath.normalize
299+
(scalac.norm, scala.norm, repl.norm)
299300
}
300301

301302

Diff for: 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

Diff for: 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)