Skip to content

Instantly share code, notes, and snippets.

@indraniel
Created July 18, 2017 18:48
Show Gist options
  • Select an option

  • Save indraniel/ac93ab3d22c1d2264246fd1aec49ddd9 to your computer and use it in GitHub Desktop.

Select an option

Save indraniel/ac93ab3d22c1d2264246fd1aec49ddd9 to your computer and use it in GitHub Desktop.
Goofing around with embedding the scala REPL inside a sbt project
import Dependencies._
lazy val root = (project in file(".")).
settings(
inThisBuild(List(
organization := "com.example",
scalaVersion := "2.12.1",
version := "0.1.0-SNAPSHOT"
)),
name := "Hello",
libraryDependencies ++= Seq(
scalaTest % Test,
scalaCompiler,
jline
// ammonite
)
)
import sbt._
// project/Dependencies.scala
object Dependencies {
lazy val scalaTest = "org.scalatest" %% "scalatest" % "3.0.1"
// lazy val ammonite = "com.lihaoyi" %% "ammonite" % "1.0.0" cross CrossVersion.full
// lazy val scalaCompiler = "org.scala-lang" % "scala-compiler" % scalaVersion.value
lazy val scalaCompiler = "org.scala-lang" % "scala-compiler" % "2.12.1"
lazy val jline = "org.scala-lang.modules" % "scala-jline" % "2.12.1"
}
package example
// src/main/scala/example/Hello.scala
// import scala.reflect.ClassTag
import scala.tools.nsc.Settings
import scala.tools.nsc.interpreter.{ILoop, NamedParam}
//import ammonite._
trait Greeting {
lazy val greeting: String = "hello"
}
// Ammonite v1 example
// object Hello extends Greeting with App {
// def dude(name: String) = {
// println(s"Howdy, ${name} !")
// }
// ammonite.Main( predefCode = "println(\"Starting Debugging\")" ).run( "dude" -> dude _ )
// println(greeting)
// }
// Ammonite v2 example
// object Hello extends Greeting {
// def dude(name: String) = {
// println(s"Howdy, ${name} !")
// }
//
// def main(args: Array[String]) {
// println(greeting)
// ammonite.Main( predefCode = "println(\"Starting Debugging\")" ).run( "dude" -> dude _ )
// dude("Frank")
// }
// }
/* crap */
// // basic repl integration (why can't I get these to work with scala 2.12.x ?!?!?!)
// // https://stackoverflow.com/questions/40322771/iloop-tab-completion
// // https://stackoverflow.com/questions/18628516/embedded-scala-repl-interpreter-example-for-2-10
// // https://stackoverflow.com/questions/18628516/embedded-scala-repl-interpreter-example-for-2-10/18628517#18628517
// // http://techqa.info/programming/question/18628516/embedded-scala-repl-interpreter-example-for-2.10
// // https://groups.google.com/forum/#!topic/scala-internals/bM_2ze5QMXY
// // https://stackoverflow.com/questions/16920884/custom-scala-repl-issues
// // https://stackoverflow.com/questions/8916699/how-to-write-scala-2-9-code-that-will-allow-dropping-into-an-interpreter
// // https://stackoverflow.com/questions/2160355/drop-into-interpreter-during-arbitrary-scala-code-location
// // http://www.scala-lang.org/api/2.12.2/scala-compiler/scala/tools/nsc/interpreter/ILoop.html
// object Hello extends Greeting with App {
// val ss = new Settings {
// // usejavacp.value = true
// deprecation.value = true
// Yreplsync.value = true
// }
//
// def repl = new ILoop {
// override def createInterpreter(): Unit = {
// super.createInterpreter()
// }
// }
//
// def dude(name: String) = {
// println(s"Howdy, ${name} !")
// }
//
// println(greeting)
// ss.embeddedDefaults[Hello.type]
// repl.process(ss)
// dude("Frank")
// }
// object Hello extends Greeting with App {
// def breakIf[T](assertion: => Boolean, args: NamedParam*)(implicit tag: ClassTag[T]) = {
// // val repl = new ILoop() {
// // override protected def postInitialization(): Unit = {
// // addThunk(args.foreach(p => intp.bind(p)))
// // //super.postInitialization()
// // }
// // }
// val repl = new ILoop() {
// override def printWelcome() = {
// processLine("")
// super.printWelcome()
// echo("Customized...")
// }
// }
//
// val settings = new Settings()
//
// settings.Yreplsync.value = true
// settings.usejavacp.value = true
// settings.embeddedDefaults[T]
//
// // args.foreach(repl.intp.rebind)
//
// repl.process(settings)
// }
//
// def dude(name: String) = {
// println(s"Howdy, ${name} !")
// }
//
// println(greeting)
// breakIf[App](assertion = true, NamedParam("greeting", greeting))
// dude("Frank")
// }
// object Hello extends Greeting with App {
// def repl = new ILoop {
// override def loop(): Unit = {
// intp.bind("e", "Double", 2.71828)
// super.loop()
// }
// }
//
// val settings = new Settings
// settings.Yreplsync.value = true
//
// // the 'usejavacp' setting when launching within SBT
// settings.embeddedDefaults[Repl.type]
//
// def dude(name: String) = {
// repl.process(settings)
// println(s"Howdy, ${name} !")
// }
//
// println(greeting)
// dude("Frank")
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment