How to pass scalacOptions (Xelide-below) to sbt via command line
Asked Answered
K

1

7

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).

Katricekatrina answered 6/1, 2016 at 6:45 Comment(2)
This seems to work: $>sbt "set scalacOptions in ThisBuild ++=Seq(\"-Xelide-below\",\"0\")" assembly. But that's just nasty IMHO.Katricekatrina
I think the best would be to add a new configuration. However, I couldn't figure out how yet.Collotype
H
7

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.

Hildredhildreth answered 6/1, 2017 at 12:35 Comment(1)
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 scala-js.org/api/scalajs-library/latest/…Chadwickchae

© 2022 - 2024 — McMap. All rights reserved.