Skip to content

Commit fc61936

Browse files
authored
Fix #21242: Add REPL flag to quit after evaluating init script (#22636)
Adding a new REPL flag `--repl-quit-after-init`: evaluate the init script and skip the interactive mode. Fix the remaining part of #21242 Test with flag at REPL startup: ```scala > ./bin/replQ --repl-quit-after-init --repl-init-script 'println("Hello from init script!"); val i = 2 * 2' Hello from init script! val i: Int = 4 > ```
2 parents ed2f0ad + fbd34ec commit fc61936

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

bin/replQ

Lines changed: 6 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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 replQuitAfterInit: Setting[Boolean] = BooleanSetting(RootSetting, "repl-quit-after-init", "Quit REPL after evaluating the init script.", aliases = List("--repl-quit-after-init"))
131132
end CommonScalaSettings
132133

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

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

Lines changed: 3 additions & 1 deletion
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.replQuitAfterInit.value(using rootCtx) then initialState
158+
else runUntilQuit()
157159

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

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

Lines changed: 4 additions & 1 deletion
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 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(scalaPath, ("--power" +: "repl" +: "--offline" +: "--" +: 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-quit-after-init", "--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

0 commit comments

Comments
 (0)