Skip to content

Commit d0b84a7

Browse files
authored
dependencies for server mode (#130)
1 parent 7eb5586 commit d0b84a7

File tree

7 files changed

+39
-23
lines changed

7 files changed

+39
-23
lines changed

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,16 @@ curl -XPOST http://localhost:8080/query-sync -d '{"query":"val baz = foo + 1"}'
415415
# {"success":true,"stdout":"val baz: Int = 100\n",...}
416416
```
417417

418-
There's also has an asynchronous mode:
418+
Adding dependencies:
419+
```
420+
echo '//> using dep com.michaelpollmeier:versionsort:1.0.7' > foo.sc
421+
./srp-server --predef foo.sc
422+
423+
curl http://localhost:8080/query-sync -X POST -d '{"query": "versionsort.VersionHelper.compare(\"1.0\", \"0.9\")"}'
424+
# {"success":true,"stdout":"val res0: Int = 1\n",...}%
425+
```
426+
427+
srp-server can be used in an asynchronous mode:
419428
```
420429
./srp-server
421430

core/src/main/scala/replpp/InteractiveShell.scala

-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package replpp
22

3-
import dotty.tools.Settings
4-
import dotty.tools.io.{ClassPath, Directory, PlainDirectory}
53
import dotty.tools.repl.State
64

7-
import java.lang.System.lineSeparator
85
import scala.util.control.NoStackTrace
96

107
object InteractiveShell {

server/src/it/scala/replpp/server/EmbeddedReplTests.scala

+12-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import scala.concurrent.duration.Duration
1313
class EmbeddedReplTests extends AnyWordSpec with Matchers {
1414

1515
"execute commands synchronously" in {
16-
val repl = new EmbeddedRepl()
16+
val repl = new EmbeddedRepl(defaultCompilerArgs)
1717

1818
repl.query("val x = 0").output.trim shouldBe "val x: Int = 0"
1919
repl.query("x + 1").output.trim shouldBe "val res0: Int = 1"
@@ -22,11 +22,21 @@ class EmbeddedReplTests extends AnyWordSpec with Matchers {
2222
}
2323

2424
"execute a command asynchronously" in {
25-
val repl = new EmbeddedRepl()
25+
val repl = new EmbeddedRepl(defaultCompilerArgs)
2626
val (uuid, futureResult) = repl.queryAsync("val x = 0")
2727
val result = Await.result(futureResult, Duration.Inf)
2828
result.trim shouldBe "val x: Int = 0"
2929
repl.shutdown()
3030
}
3131

32+
val defaultCompilerArgs = {
33+
val inheritedClasspath = System.getProperty("java.class.path")
34+
Array(
35+
"-classpath", inheritedClasspath,
36+
"-explain", // verbose scalac error messages
37+
"-deprecation",
38+
"-color", "never"
39+
)
40+
}
41+
3242
}

server/src/main/scala/replpp/server/Config.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ object Config {
5252
help("help").text("Print this help text"),
5353
)
5454

55-
OParser.parse(parser, args, Config(replpp.Config()))
55+
OParser.parse(parser, args, Config(replpp.Config(nocolors = true)))
5656
.getOrElse(throw new AssertionError("error while parsing commandline args - see errors above"))
5757
}
5858
}

server/src/main/scala/replpp/server/EmbeddedRepl.scala

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,24 @@
11
package replpp.server
22

3-
import dotty.tools.dotc.config.Printers.config
43
import dotty.tools.repl.State
54
import org.slf4j.{Logger, LoggerFactory}
65
import replpp.Colors.BlackWhite
7-
import replpp.{Config, ReplDriverBase, pwd}
6+
import replpp.{ReplDriverBase, pwd}
87

98
import java.io.*
109
import java.nio.charset.StandardCharsets
1110
import java.util.UUID
12-
import java.util.concurrent.{BlockingQueue, Executors, LinkedBlockingQueue, Semaphore}
11+
import java.util.concurrent.Executors
1312
import scala.concurrent.duration.Duration
14-
import scala.concurrent.impl.Promise
1513
import scala.concurrent.{Await, ExecutionContext, ExecutionContextExecutorService, Future}
16-
import scala.util.{Failure, Success}
1714

18-
class EmbeddedRepl(predefLines: IterableOnce[String] = Seq.empty) {
15+
class EmbeddedRepl(compilerArgs: Array[String], predefLines: IterableOnce[String] = Seq.empty) {
1916
private val logger: Logger = LoggerFactory.getLogger(getClass)
2017

2118
/** repl and compiler output ends up in this replOutputStream */
2219
private val replOutputStream = new ByteArrayOutputStream()
2320

2421
private val replDriver: ReplDriver = {
25-
val inheritedClasspath = System.getProperty("java.class.path")
26-
val compilerArgs = Array(
27-
"-classpath", inheritedClasspath,
28-
"-explain", // verbose scalac error messages
29-
"-deprecation",
30-
"-color", "never"
31-
)
3222
new ReplDriver(compilerArgs, new PrintStream(replOutputStream), classLoader = None)
3323
}
3424

server/src/main/scala/replpp/server/ReplServer.scala

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ object ReplServer {
2121
password <- config.serverAuthPassword
2222
} yield UsernamePasswordAuth(username, password)
2323

24-
val embeddedRepl = new EmbeddedRepl(allPredefLines(config.baseConfig))
24+
val predefLines = replpp.allPredefLines(config.baseConfig)
25+
val compilerArgs = replpp.compilerArgs(config.baseConfig)
26+
val embeddedRepl = new EmbeddedRepl(compilerArgs, predefLines)
2527
Runtime.getRuntime.addShutdownHook(new Thread(() => {
2628
logger.info("Shutting down server...")
2729
embeddedRepl.shutdown()

server/src/test/scala/replpp/server/ReplServerTests.scala

+10-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import cask.util.Logger.Console.*
44
import castor.Context.Simple.global
55
import org.scalatest.matchers.should.Matchers
66
import org.scalatest.wordspec.AnyWordSpec
7-
import replpp.Config
87
import requests.RequestFailedException
98
import ujson.Value.Value
109

@@ -371,9 +370,18 @@ class ReplServerTests extends AnyWordSpec with Matchers {
371370
}
372371

373372
object Fixture {
373+
val defaultCompilerArgs = {
374+
val inheritedClasspath = System.getProperty("java.class.path")
375+
Array(
376+
"-classpath", inheritedClasspath,
377+
"-explain", // verbose scalac error messages
378+
"-deprecation",
379+
"-color", "never"
380+
)
381+
}
374382

375383
def apply[T](predefCode: String = "")(urlToResult: String => T): T = {
376-
val embeddedRepl = new EmbeddedRepl(predefLines = predefCode.linesIterator)
384+
val embeddedRepl = new EmbeddedRepl(defaultCompilerArgs, predefLines = predefCode.linesIterator)
377385

378386
val host = "localhost"
379387
val port = 8081

0 commit comments

Comments
 (0)