Skip to content

Commit 23e514e

Browse files
committed
frontend build hook logic
1 parent 318c1f3 commit 23e514e

File tree

3 files changed

+107
-2
lines changed

3 files changed

+107
-2
lines changed

build.sbt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
name := """play-java-starter-example"""
1+
name := """pjava-play-angular-seed"""
22

33
version := "1.0-SNAPSHOT"
44

5-
lazy val root = (project in file(".")).enablePlugins(PlayJava)
5+
lazy val root = (project in file(".")).enablePlugins(PlayJava).settings(
6+
watchSources ++= (baseDirectory.value / "public/ui" ** "*").get
7+
)
68

79
scalaVersion := "2.12.2"
810

project/AngularBuild.scala

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.net.InetSocketAddress
2+
3+
import play.sbt.PlayRunHook
4+
import sbt._
5+
6+
import scala.sys.process.Process
7+
8+
object AngularBuild {
9+
def apply(base: File): PlayRunHook = {
10+
object UIBuildHook extends PlayRunHook {
11+
12+
var process: Option[Process] = None
13+
14+
var npmInstall: String = "npm install"
15+
var npmRun: String = "npm run start"
16+
17+
// Windows requires npm commands prefixed with cmd /c
18+
if (System.getProperty("os.name").toLowerCase().contains("win")){
19+
npmInstall = "cmd /c" + npmInstall
20+
npmRun = "cmd /c" + npmRun
21+
}
22+
23+
override def beforeStarted(): Unit = {
24+
if (!(base / "ui" / "node_modules").exists()) Process(npmInstall, base / "ui").!
25+
}
26+
27+
override def afterStarted(addr: InetSocketAddress): Unit = {
28+
process = Option(
29+
Process(npmRun, base / "ui").run
30+
)
31+
}
32+
33+
override def afterStopped(): Unit = {
34+
process.foreach(_.destroy())
35+
process = None
36+
}
37+
38+
}
39+
40+
UIBuildHook
41+
}
42+
}

ui-build.sbt

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import scala.sys.process.Process
2+
3+
/*
4+
* UI Build Scripts
5+
*/
6+
val Success = 0 // 0 exit code
7+
val Error = 1 // 1 exit code
8+
9+
PlayKeys.playRunHooks += baseDirectory.map(AngularBuild.apply).value
10+
11+
val isWindows = System.getProperty("os.name").toLowerCase().contains("win")
12+
13+
def runScript(script: String)(implicit dir: File): Int = {
14+
if(isWindows){ Process("cmd /c " + script, dir) } else { Process(script, dir) } }!
15+
16+
def uiWasInstalled(implicit dir: File): Boolean = (dir / "node_modules").exists()
17+
18+
def runNpmInstall(implicit dir: File): Int =
19+
if (uiWasInstalled) Success else runScript("npm install")
20+
21+
def ifUiInstalled(task: => Int)(implicit dir: File): Int =
22+
if (runNpmInstall == Success) task
23+
else Error
24+
25+
// Include UI production build task here.
26+
def runProdBuild(implicit dir: File): Int = ifUiInstalled(runScript("npm run build-prod"))
27+
28+
// Include UI development build task here.
29+
def runDevBuild(implicit dir: File): Int = ifUiInstalled(runScript("npm run build-dev"))
30+
31+
// Include UI test build task here.
32+
def runUiTests(implicit dir: File): Int = ifUiInstalled(runScript("npm run test-no-watch"))
33+
34+
lazy val `ui-dev-build` = TaskKey[Unit]("Run UI build when developing the application.")
35+
36+
`ui-dev-build` := {
37+
implicit val userInterfaceRoot = baseDirectory.value / "ui"
38+
if (runDevBuild != Success) throw new Exception("Oops! UI Build crashed.")
39+
}
40+
41+
lazy val `ui-prod-build` = TaskKey[Unit]("Run UI build when packaging the application.")
42+
43+
`ui-prod-build` := {
44+
implicit val userInterfaceRoot = baseDirectory.value / "ui"
45+
if (runProdBuild != Success) throw new Exception("Oops! UI Build crashed.")
46+
}
47+
48+
lazy val `ui-test` = TaskKey[Unit]("Run UI tests when testing application.")
49+
50+
`ui-test` := {
51+
implicit val userInterfaceRoot = baseDirectory.value / "ui"
52+
if (runUiTests != Success) throw new Exception("UI tests failed!")
53+
}
54+
55+
`ui-test` := (`ui-test` dependsOn `ui-dev-build`).value
56+
57+
dist := (dist dependsOn `ui-prod-build`).value
58+
59+
stage := (stage dependsOn `ui-prod-build`).value
60+
61+
test := ((test in Test) dependsOn `ui-test`).value

0 commit comments

Comments
 (0)