scalaplayframeworkgruntjstypesafe-activator

Accessing command line arguments in play sbt project


So here's the situation; I have a play application which is integrated with activator. In order to run my Grunt task I added Grunt.scala to my project

import java.net.InetSocketAddress

import play.sbt.PlayRunHook
import sbt._

object Grunt {
  def apply(base: File): PlayRunHook = {

    object GruntProcess extends PlayRunHook {

      var process: Option[Process] = None

      def osCommand(command: String): String = {
        System.getProperty("os.name").toLowerCase().startsWith("win") match{
          case true => s"cmd.exe /c $command"
          case false => command
        }
      }

      override def beforeStarted(): Unit = {
        Process(osCommand("grunt dist"), base).run
      }

      override def afterStarted(addr: InetSocketAddress): Unit = {
        process = Some(Process(osCommand("grunt watch"), base).run)
      }

      override def afterStopped(): Unit = {
        process.map(p => p.destroy())
        process = None
      }
    }

    GruntProcess
  }
}

then added the following line to my build.sbt :

PlayKeys.playRunHooks += Grunt(baseDirectory.value)

basically followed Play's documentation. So now in order to run my project I use the following activator command :

activator -Dsbt.log.noformat=true clean dist universal:packageZipTarball

the question is how can I access the "dist" command line argument from the scala class. I'd like to use that argument to determine which Grunt task(dist, test etc.) I should run.


Solution

  • I found the SBT file in this repo very educational. Different environments are basically handled from the SBT file(not scala hooks) as you can see at the bottom of that file :

    ....
    // Execute frontend prod build task prior to play dist execution.
    dist := (dist dependsOn `ui-prod-build`).value
    
    // Execute frontend prod build task prior to play stage execution.
    stage := (stage dependsOn `ui-prod-build`).value
    ....