Skip to content

Commit 84a923d

Browse files
committedApr 11, 2025·
update demo-project
1 parent bff2625 commit 84a923d

File tree

8 files changed

+73
-42
lines changed

8 files changed

+73
-42
lines changed
 

‎README.md

+21-14
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,27 @@ libraryDependencies += "com.michaelpollmeier" % "scala-repl-pp_3.6.4" % "<versio
2222
* `srp` is published with the full scala version suffix (e.g. `_3.6.4` instead of just `_3`) because the stock Scala REPL often has binary incompatible changes between minor version changes - different Scala patch versions typically work though, e.g. if your build uses Scala 3.6.3 you can use `scala-repl-pp_3.6.4`
2323
* `srp` has only one direct dependency: the scala3-compiler[(*)](#fineprint)
2424

25+
As an example take a look at the demo project ["string calculator"](src/test/resources/demo-project) in this repository:
26+
```bash
27+
cd core/src/test/resources/demo-project
28+
sbt stage
29+
./stringcalc
30+
31+
Welcome to the magical world of string calculation!
32+
Type `help` for help
33+
34+
stringcalc> add(One, Two)
35+
val res0: stringcalc.Number = Number(3)
36+
37+
stringcalc> :exit // or press Ctrl-D
38+
39+
40+
./stringcalc --script plus.sc
41+
executing plus.sc
42+
Number(3)
43+
```
44+
45+
2546
## Table of contents
2647
<!-- generated with:
2748
markdown-toc --maxdepth 3 README.md|tail -n +5
@@ -499,20 +520,6 @@ Server-specific configuration options as per `srp --help`:
499520
--server-auth-password <value> Basic auth password for the REPL server
500521
```
501522

502-
## Embed into your own project
503-
Try out the working [string calculator example](src/test/resources/demo-project) in this repo:
504-
```bash
505-
cd core/src/test/resources/demo-project
506-
sbt stage
507-
./stringcalc
508-
509-
Welcome to the magical world of string calculation!
510-
Type `help` for help
511-
512-
stringcalc> add(One, Two)
513-
val res0: stringcalc.Number = Number(3)
514-
```
515-
516523
## Verbose mode
517524
If verbose mode is enabled, you'll get additional information about classpaths and complete scripts etc.
518525
To enable it, you can either pass `--verbose` or set the environment variable `SCALA_REPL_PP_VERBOSE=true`.

‎core/src/main/scala/replpp/Config.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ object Config {
292292
* equality etc. differently, which breaks common case class conventions.
293293
*/
294294
case class ForClasspath(additionalClasspathEntries: Seq[String] = Seq.empty,
295-
inheritClasspath: Boolean = false,
295+
inheritClasspath: Boolean = true,
296296
inheritClasspathIncludes: Seq[String] = ForClasspath.DefaultInheritClasspathIncludes,
297297
inheritClasspathExcludes: Seq[String] = Seq.empty,
298298
dependencies: Seq[String] = Seq.empty,

‎core/src/main/scala/replpp/JLineTerminal.scala

-2
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ import org.jline.terminal.Terminal
1717
import org.jline.terminal.Terminal.Signal
1818
import org.jline.terminal.TerminalBuilder
1919
import org.jline.utils.AttributedString
20-
import org.slf4j.LoggerFactory
2120

2221
import scala.util.Try
2322

2423
/** Based on https://github.com/lampepfl/dotty/blob/3.4.1/compiler/src/dotty/tools/repl/JLineTerminal.scala
2524
* and adapted for our needs */
2625
class JLineTerminal extends java.io.Closeable {
27-
private val logger = LoggerFactory.getLogger(getClass)
2826

2927
// MP: adapted here
3028
// if env var TERM=dumb is defined, instantiate as 'dumb' straight away, otherwise try to instantiate as a

‎core/src/main/scala/replpp/Main.scala

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package replpp
33
import replpp.scripting.ScriptRunner
44

55
object Main {
6-
def main(args: Array[String]): Unit = {
7-
val config = Config.parse(args)
6+
def main(args: Array[String]): Unit =
7+
run(Config.parse(args))
88

9+
def run(config: Config): Unit = {
910
if (config.scriptFile.isDefined) {
10-
ScriptRunner.main(args)
11+
ScriptRunner.exec(config).get
1112
} else {
1213
InteractiveShell.run(config)
1314
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
name := "stringcalc"
22

3-
scalaVersion := "3.3.0"
3+
scalaVersion := "3.6.4"
4+
val srpVersion = "0.5.4"
45

56
libraryDependencies ++= Seq(
6-
"com.michaelpollmeier" %% "scala-repl-pp" % "0.1.56"
7+
"com.michaelpollmeier" % "scala-repl-pp" % srpVersion cross CrossVersion.full,
8+
"com.github.scopt" %% "scopt" % "4.1.0",
79
)
810

911
enablePlugins(JavaAppPackaging)
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version=1.9.4
1+
sbt.version=1.10.11

‎core/src/test/resources/demo-project/src/main/scala/stringcalc/Domain.scala

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ object One extends Number(1)
77
object Two extends Number(2)
88
object Three extends Number(3)
99

10+
1011
object StringCalculator {
1112
def add(number1: Number, number2: Number): Number =
1213
(number1, number2) match {
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,48 @@
11
package stringcalc
22

3-
import java.nio.file.Files
4-
import replpp.{Config, InteractiveShell}
3+
import java.nio.file.{Files, Path}
4+
import scopt.OParser
55

6-
object Main {
7-
@main def startRepl(): Unit = {
8-
val predefFileTmp = Files.createTempFile("scala-repl-pp-demo-project-predef", ".sc")
9-
Files.writeString(
10-
predefFileTmp,
11-
s"""import stringcalc._
12-
|import StringCalculator._
13-
|
14-
|def help: Unit = println("try this: `add(One, Two)`")
15-
""".stripMargin
16-
)
17-
18-
InteractiveShell.run(
19-
Config(
20-
prompt = Some("stringcalc"),
21-
greeting = "Welcome to the magical world of string calculation! \nType `help` for help",
22-
predefFiles = Seq(predefFileTmp),
6+
def main(args: Array[String]) = {
7+
Config.parse(args) match {
8+
case Some(config) =>
9+
replpp.Main.run(
10+
replpp.Config(
11+
scriptFile = config.scriptFile,
12+
prompt = Some("stringcalc"),
13+
greeting = Some("Welcome to the magical world of string calculation! \nType `help` for help"),
14+
verbose = config.verbose,
15+
runBefore = Seq(
16+
"import stringcalc.*",
17+
"import StringCalculator.*",
18+
"""def help: Unit = println("try this: `add(One, Two)`")"""
19+
)
20+
)
2321
)
22+
case None => System.exit(1)
23+
}
24+
}
25+
26+
case class Config(verbose: Boolean = false, scriptFile: Option[Path] = None)
27+
object Config {
28+
def parse(args: Array[String]): Option[Config] =
29+
OParser.parse(parser, args, Config())
30+
31+
private val builder = OParser.builder[Config]
32+
private val parser = {
33+
import builder._
34+
OParser.sequence(
35+
programName("stringcalc"),
36+
opt[Boolean]('v', "verbose")
37+
.action((x, c) => c.copy(verbose = x))
38+
.text("enable verbose mode"),
39+
opt[Path]("script")
40+
.action((x, c) => c.copy(scriptFile = Option(x)))
41+
.text("path to script file")
42+
.validate(path =>
43+
if (Files.exists(path)) success
44+
else failure(s"script file $path does not exist")
45+
)
2446
)
2547
}
2648
}

0 commit comments

Comments
 (0)
Please sign in to comment.