Skip to content

Instantly share code, notes, and snippets.

@eugene-sy
Created September 4, 2014 07:17
Show Gist options
  • Save eugene-sy/1b1d4d9e79df1cee7dfc to your computer and use it in GitHub Desktop.
Save eugene-sy/1b1d4d9e79df1cee7dfc to your computer and use it in GitHub Desktop.
sbt-grunt integration for Play Framework apps
import play.PlayRunHook
import sbt._
import Keys._
import play.Play.autoImport._
import play.PlayImport.PlayKeys.playRunHooks
import java.net.InetSocketAddress
object ApplicationBuild extends Build {
import Grunt.GruntTasks
val appName = ""
val appVersion = ""
val projectScalaVersion = "2.11.1"
scalaVersion := projectScalaVersion
// Dependencies
val appDependencies = Seq(
// Add your project dependencies here,
)
// Grunt task to run on "sbt run"
val gruntDefault = TaskKey[Unit]("grunt default")
val gruntSettings = gruntDefault := GruntTasks.`grunt:default`
// Project configuration
val main = Project(appName, file(".")).enablePlugins(play.PlayScala)
.settings(gruntSettings).settings(
version := appVersion,
scalaVersion := projectScalaVersion,
libraryDependencies ++= appDependencies,
// Run "grunt watch" on "sbt run"
playRunHooks <+= baseDirectory.map(base => Grunt(base)),
compile in Compile <<= (compile in Compile).dependsOn(gruntDefault),
// commands to sbt interactive shell
commands ++= GruntTasks.all,
commands ++= CustomTasks.all,
// no docs please
sources in (Compile, doc) := Seq.empty,
publishArtifact in (Compile, packageDoc) := false
)
}
object Grunt {
def apply(base: File): PlayRunHook = {
object GruntProcess extends PlayRunHook {
var process: Option[Process] = None
override def beforeStarted(): Unit = {
Process("grunt default", base).run
}
override def afterStarted(addr: InetSocketAddress): Unit = {
process = Some(Process("grunt watch", base).run)
}
override def afterStopped(): Unit = {
process.map(p => p.destroy())
process = None
}
}
GruntProcess
}
object GruntTasks {
def grunt: Command = Command.args("grunt", "<task>") { (state, args) =>
("grunt " + args.mkString(" ")).!
state
}
def `grunt:watch`: Command = Command.command("grunt-watch") { state =>
"grunt watch".!
state
}
def `grunt:default`: Command = Command.command("grunt-default") { state =>
"grunt".!
state
}
def all: Seq[Command] = Seq(grunt, `grunt:watch`, `grunt:default`)
}
}
object CustomTasks {
def npm: Command = Command.args("npm", "<task>") { (state, args) =>
("npm " + args.mkString(" ")).!
state
}
def yo: Command = Command.args("yo", "<task>") { (state, args) =>
("yo " + args.mkString(" ")).!
state
}
def bower: Command = Command.args("bower", "<task>") { (state, args) =>
("bower " + args.mkString(" ")).!
state
}
def all: Seq[Command] = Seq(npm, yo, bower)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment