-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui-build.sbt
66 lines (46 loc) · 2.59 KB
/
ui-build.sbt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import scala.sys.process.Process
/*
* UI Build hook Scripts
*/
// Execution status success.
val Success = 0
// Execution status failure.
val Error = 1
// Run serve task when Play runs in dev mode, that is, when using 'sbt run'
// https://www.playframework.com/documentation/2.8.x/SBTCookbook
PlayKeys.playRunHooks += baseDirectory.map(FrontendRunHook.apply).value
// True if build running operating system is windows.
val isWindows = System.getProperty("os.name").toLowerCase().contains("win")
// Execute on commandline, depending on the operating system. Used to execute npm commands.
def runOnCommandline(script: String)(implicit dir: File): Int = {
if(isWindows){ Process("cmd /c set CI=true&&" + script, dir) } else { Process("env CI=true " + script, dir) } }!
// Check of node_modules directory exist in given directory.
def isNodeModulesInstalled(implicit dir: File): Boolean = (dir / "node_modules").exists()
// Execute `npm install` command to install all node module dependencies. Return Success if already installed.
def runNpmInstall(implicit dir: File): Int =
if (isNodeModulesInstalled) Success else runOnCommandline(FrontendCommands.dependencyInstall)
// Execute task if node modules are installed, else return Error status.
def ifNodeModulesInstalled(task: => Int)(implicit dir: File): Int =
if (runNpmInstall == Success) task
else Error
// Execute frontend test task. Update to change the frontend test task.
def executeUiTests(implicit dir: File): Int = ifNodeModulesInstalled(runOnCommandline(FrontendCommands.test))
// Execute frontend prod build task. Update to change the frontend prod build task.
def executeProdBuild(implicit dir: File): Int = ifNodeModulesInstalled(runOnCommandline(FrontendCommands.build))
// Create frontend build tasks for prod, dev and test execution.
lazy val `ui-test` = taskKey[Unit]("Run UI tests when testing application.")
`ui-test` := {
implicit val userInterfaceRoot = baseDirectory.value / "ui"
if (executeUiTests != Success) throw new Exception("UI tests failed!")
}
lazy val `ui-prod-build` = taskKey[Unit]("Run UI build when packaging the application.")
`ui-prod-build` := {
implicit val userInterfaceRoot = baseDirectory.value / "ui"
if (executeProdBuild != Success) throw new Exception("Oops! UI Build crashed.")
}
// 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
// Execute frontend test task prior to play test execution.
test := ((test in Test) dependsOn `ui-test`).value