-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
ui-build.sbt
108 lines (83 loc) · 3.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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(firstScript: String, otherScripts: String*)(implicit
dir: File
): Int = {
def command(script: String) = {
if (isWindows) { Process("cmd /c set CI=true&&" + script, dir) }
else { Process("env CI=true " + script, dir) }
}
otherScripts
.foldLeft(command(firstScript)) { (acc, other) =>
acc #&& command(other)
}
} !
// 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
def executeUiCodeStyle(implicit dir: File): Int = ifNodeModulesInstalled(
runOnCommandline(FrontendCommands.prettier, FrontendCommands.eslint)
)
def executeUiCodeStyleCheck(implicit dir: File): Int = ifNodeModulesInstalled(
runOnCommandline(FrontendCommands.prettierCheck, FrontendCommands.eslintCheck)
)
// 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-code-style` = taskKey[Unit]("Apply code style.")
`ui-code-style` := {
implicit val userInterfaceRoot = baseDirectory.value / "ui"
if (executeUiCodeStyle != Success)
throw new Exception("UI code style failed!")
}
lazy val `ui-code-style-check` =
taskKey[Unit]("Check if code style was applied.")
`ui-code-style-check` := {
implicit val userInterfaceRoot = baseDirectory.value / "ui"
if (executeUiCodeStyleCheck != Success)
throw new Exception("UI code style check failed!")
}
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