6

I am trying to call sbt assembly from the command line passing it a scalac compiler flag to elides (elide-below 1).

I have managed to get the flag working in the build.sbt by adding this line to the build.sbt

scalacOptions ++= Seq("-Xelide-below", "1")

And also it's working fine when I start sbt and run the following:

$> sbt                                                                                                                        
$> set scalacOptions in ThisBuild ++=Seq("-Xelide-below", "0")

But I would like to know how to pass this in when starting sbt, so that my CI jobs can use it while doing different assembly targets (ie. dev/test/prod).

Armin
  • 1,367
  • 1
  • 12
  • 17
  • 1
    This seems to work: $>sbt "set scalacOptions in ThisBuild ++=Seq(\"-Xelide-below\",\"0\")" assembly. But that's just nasty IMHO. – Armin Jan 06 '16 at 06:50
  • I think the best would be to add a new configuration. However, [I couldn't figure out how yet](https://stackoverflow.com/questions/34633489/sbt-add-new-configuration-to-refine-compile-flags). – 0__ Jan 06 '16 at 12:48

1 Answers1

6

One way to pass the elide level as a command line option is to use system properties

scalacOptions ++= Seq("-Xelide-below", sys.props.getOrElse("elide.below", "0"))

and run sbt -Delide.below=20 assembly. Quick, dirty and easy.

Another more verbose way to accomplish the same thing is to define different commands for producing test/prod artifacts.

lazy val elideLevel = settingKey[Int]("elide code below this level.")
elideLevel in Global := 0
scalacOptions ++= Seq("-Xelide-below", elideLevel.value.toString)
def assemblyCommand(name: String, level: Int) =
  Command.command(s"${name}Assembly") { s =>
    s"set elideLevel in Global := $level" ::
      "assembly" ::
      s"set elideLevel in Global := 0" ::
      s
  }
commands += assemblyCommand("test", 10)
commands += assemblyCommand("prod", 1000)

and you can run sbt testAssembly prodAssembly. This buys you a cleaner command name in combination with the fact that you don't have to exit an active sbt-shell session to call for example testAssembly. My sbt-shell sessions tend to live for a long time so I personally prefer the second option.

  • FWIW. You may not need this trick at all if you only need a boolean dev/prod switch for Scala.js, `scala.scalajs.LinkingInfo.developmentMode` already does that for you, see https://www.scala-js.org/api/scalajs-library/latest/#scala.scalajs.LinkingInfo$ – Ólafur Páll Geirsson Jan 07 '17 at 09:23