0

I want to define an sbt task that would start the scala console with project's compiled classes on the classpath and some initial commands executed.

I want to start that REPL session like this

sbt session

Here is my sorry attempt that I put together based on other answers, but it neither puts the project's classes on the classpath, nor doesn't execute the initial commands:

// extend Test in hope to include compiled sources on the classpath.
val ReplSession = config("repl-session") extend(Test)

val root = project.in(file("."))
  .configs(ReplSession)
  .settings(inConfig(ReplSession)(initialCommands := """
    | import foo._
    | """.stripMargin))

// define task that starts the REPL session
lazy val session = TaskKey[Unit]("session")
session <<= Seq(
  console in (root, ReplSession)
).dependOn
Tomas Mikula
  • 6,537
  • 25
  • 39
  • doesn't the console action already do what you want? Along with `initialCommands in console`? But you seem to already be using that, but trying to do something extra, so I feel like there is more to your question that I don't understand. (more details here: http://www.scala-sbt.org/0.13/docs/Howto-Scala.html) – Imran Rashid Mar 03 '17 at 04:15
  • @ImranRashid `sbt console` starts the console, but without project classes on the classpath, thus the execution of `initialCommands` fails. What works is `sbt test:console` together with `initialCommands in console`, but that still sets `initialCommands` for the `Compile` config as well, where they still fail. Plus I want a one word command instead of `test:console`. – Tomas Mikula Mar 06 '17 at 20:22
  • Ah I see -- `console` does get the project classes added to the classpath *inside* the console, but not when you invoke `initialCommands`. So you can't reference classes from your project in `initialCommands`. – Imran Rashid Mar 19 '17 at 14:05
  • But, somehow it seems to work in the spark build ... not sure how that works. https://github.com/apache/spark/blob/master/project/SparkBuild.scala#L495 – Imran Rashid Mar 19 '17 at 14:07

1 Answers1

0

Note: If someone has a better solution, I will mark it as the correct answer.

What somewhat worked for me was changing

inConfig(ReplSession)(initialCommands := ...)

in my original snippet to

inConfig(Test)(initialCommands := ...)

or, alternatively,

initialCommands in (Test, console) := ...

For completeness, here's what I use now:

val ReplSession = config("repl-session") extend(Test)

val root = project.in(file("."))
  .configs(ReplSession)

lazy val session = TaskKey[Unit]("session")
session <<= Seq(
  console in (root, ReplSession)
).dependOn

initialCommands in (Test, console) := """
  | import foo._
  | """.stripMargin

I don't find this optimal, because I'm setting initialCommands in config Test (which my ReplSession extends), instead of setting initialCommands only in config ReplSession.

Tomas Mikula
  • 6,537
  • 25
  • 39