This repository has been archived by the owner on Sep 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.sc
496 lines (390 loc) · 15.5 KB
/
build.sc
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import $ivy.`com.lihaoyi::mill-contrib-bloop:$MILL_VERSION`
import coursier.Repository
import coursier.maven.MavenRepository
import coursier.core.Authentication
import mill._
import mill.define.{Sources, Task, Target}
import mill.scalajslib.ScalaJSModule
import mill.scalajslib.api.ModuleKind
import mill.scalalib._
import mill.scalalib.publish._
import ammonite.ops.Path
import coursier.core.Authentication
import mill.modules.Jvm
import os.RelPath
import $file.build_deps
import build_deps.UiDeps
import mill.api.Loose
// This import the mill-osgi plugin
import $ivy.`de.tototec::de.tobiasroeser.mill.osgi:0.3.0`
import de.tobiasroeser.mill.osgi._
// imports from the blended-mill plugin
import $ivy.`de.wayofquality.blended::blended-mill:0.3`
import de.wayofquality.blended.mill.versioning.GitModule
import de.wayofquality.blended.mill.publish.BlendedPublishModule
import de.wayofquality.blended.mill.webtools.WebTools
import de.wayofquality.blended.mill.modules._
import de.wayofquality.blended.mill.utils._
/** Project directory. */
val projectDir: os.Path = {
build.millSourcePath
}
object GitSupport extends GitModule {
override def millSourcePath: Path = projectDir
}
def akkaBundleRevision : String = "1"
def blendedVersion = T { GitSupport.publishVersion() }
trait WebUtils extends Module {
// The node modules directory to be used - This should be "node_modules" located in the base directory
// because normally webpack operations search the node_modules in parent directories as well, So all
// mill modules would find the npm modules
def npmModulesDir : Path = projectDir / "node_modules"
def nodeVersion() = T.command {
val result = os.proc("node", "-v").call().out.text().filter(_ >= ' ')
println(s"Using Node.js version [$result]")
result
}
// If the mill module should be packaged as a web application, we need to point the module to the output
// of a fastOptJS or fullOptJS step
def packagedWebApp : T[Option[PathRef]] = None
// The appname that is used in various generator steps
def appName : String
// The App title that will appear in a generated html if required
def appTitle : Option[String] = None
// The port a webpack dev server starts on
def webPackDevServerPort : Int = 9000
// Html sources that will be copied into the packaged web app folder
def htmlSources = T.sources(millSourcePath / "src" / "html")
def packagedJsLibs : Seq[String] = Seq()
// run yarn install and download all resources that are defined in the
// package.json of the project root
def yarnInstall : T[PathRef] = T {
val modules = npmModulesDir
val result = os.proc("yarn", "install", "--modules-folder", modules).call(cwd = projectDir)
T.log.info(new String(result.out.bytes))
val yarnErrors : os.Path = projectDir / "yarn-error.log"
if ( (yarnErrors.toIO.exists()) ) {
T.log.info("----- Yarn Error log -----")
T.log.info(new String(os.read.bytes(yarnErrors)))
T.log.info("----- Yarn Error log -----")
}
PathRef(modules)
}
// A simple index page that will reference the web app that has been run through webpack
// and additional libs
def simpleIndexPage(dir : Path) : String = {
val scriptLine : String => String = libName => s"""<script type="text/javascript" src="./$libName"></script>"""
val packagedLibs : String = os.walk(dir)
.filter(_.last.endsWith(".js"))
.map(_.relativeTo(dir))
.map(lib => scriptLine(lib.toString()))
.mkString("\n")
s"""<!DOCTYPE html>
|<!--suppress ALL -->
|<html>
|<head>
| <meta charset="UTF-8">
| <title>${appTitle.getOrElse(appName)}</title>
|</head>
|<body>
|
|<div id="content"></div>
""".stripMargin + packagedLibs +
s"""
|</body>
|</html>
|""".stripMargin
}
// Run webpack with a generated config (to cover the simplest case of running some fastOptJS or fullOptJS output
// thorugh webpack)
def webpack : T[PathRef] = T {
if (!nodeVersion()().startsWith("v12")) {
sys.error("Please install Node version 12 according to node install instructions for your environment.")
}
val cfgFromSource : Path = millSourcePath / "webpack.config.js"
val dist : Path = T.dest / "webpack"
val usedCfg : Path = if (cfgFromSource.toIO.exists()) {
cfgFromSource
} else {
val webpackConfig : String = packagedWebApp() match {
case None => throw new Exception("a packaged application must be defined to generate a webpack configuration")
case Some(app) =>
s"""const path = require('path');
|const webpack = require('webpack');
|
|module.exports = {
| entry: '${app.path.toIO.getAbsolutePath()}',
| plugins: [
| new webpack.HashedModuleIdsPlugin(), // so that file hashes don't change unexpectedly
| ],
| output: {
| path: '$dist',
| filename: '[name].[contenthash].js',
| },
| optimization: {
| runtimeChunk: 'single',
| splitChunks: {
| chunks: 'all',
| maxInitialRequests: Infinity,
| minSize: 0,
| cacheGroups: {
| vendor: {
| test: /[\\/]node_modules[\\/]/,
| name(module) {
| // get the name. E.g. node_modules/packageName/not/this/part.js
| // or node_modules/packageName
| const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$$)/)[1];
| // npm package names are URL-safe, but some servers don't like @ symbols
| return `npm.$${packageName.replace('@', '')}`;
| },
| },
| },
| },
| },
| devtool: "source-map",
| "module": {
| "rules": [{
| "test": new RegExp("\\.js$$"),
| "enforce": "pre",
| "use": ["source-map-loader"]
| }]
| }
|};
|""".stripMargin
}
val generatedCfg : Path = T.dest / "webpack.config.js"
os.write(generatedCfg, webpackConfig)
generatedCfg
}
val modules = yarnInstall().path
T.log.info(s"Using webpack info [${usedCfg}]")
val rc = os.proc(s"$modules/webpack-cli/bin/cli.js", "--progress", "--config", usedCfg.toIO.getAbsolutePath()).call(cwd = projectDir)
T.log.info(new String(rc.out.bytes))
PathRef(dist)
}
// create a directory that has everuthing to serve a webapp
def packageHtml : T[PathRef] = T {
val dist : Path = T.dest / "dist"
val bundledApp : Path = webpack().path
os.copy(from = bundledApp, to = dist)
htmlSources().foreach{ ref =>
if (ref.path.toIO.exists()) {
os.list(ref.path).iterator.foreach { p =>
os.copy.into(p, dist)
}
}
}
val modules : Path = npmModulesDir
packagedJsLibs.foreach{ lib =>
val file : Path = modules / RelPath(lib)
os.copy.into(file, dist)
}
val index : Path = dist / "index.html"
if (!index.toIO.exists()) {
T.log.info(s"Generating simple index.html to [$dist]")
os.write(index, simpleIndexPage(dist))
} else {
T.log.info("Using index.html from sources")
}
PathRef(dist)
}
def devServer : T[PathRef] = T {
val distDir = packageHtml().path.toIO.getAbsolutePath()
val rc = os.proc(s"$npmModulesDir/webpack-dev-server/bin/webpack-dev-server.js", "--content-base", distDir, "--port", s"$webPackDevServerPort").call(cwd = projectDir)
PathRef(T.dest)
}
}
object blended extends Cross[BlendedUiCross](UiDeps.scalaVersions.keys.toSeq:_*)
class BlendedUiCross(crossScalaVersion : String) extends GenIdeaModule { blended =>
val crossDeps = UiDeps.scalaVersions(crossScalaVersion)
def version = T.input {
os.read(projectDir / "version.txt").trim()
}
trait UiCoursierModule extends CoursierModule {
private def zincWorker: ZincWorkerModule = mill.scalalib.ZincWorkerModule
override def repositories: Seq[Repository] = zincWorker.repositories ++ Seq(
MavenRepository("https://oss.sonatype.org/content/repositories/snapshots/"),
MavenRepository(
s"https://u233308-sub2.your-storagebox.de/blended/${crossDeps.blendedCoreVersion}",
Some(Authentication("u233308-sub2", "px8Kumv98zIzSF7k"))
),
MavenRepository(
s"https://u233308-sub2.your-storagebox.de/akka-osgi/${akkaBundleRevision}",
Some(Authentication("u233308-sub2", "px8Kumv98zIzSF7k"))
)
)
}
trait UiPublishModule extends BlendedPublishModule {
def githubRepo : String = "blended.mgmt.ui"
def scpTargetDir : String = "mgmt-ui"
override def publishVersion = T { blendedVersion() }
}
trait UiModule extends BlendedJvmModule
with UiCoursierModule
with UiPublishModule { uiModule =>
override def description = s"Blended Module $blendedModule"
override def baseDir : os.Path = projectDir
override def scalaVersion = crossDeps.scalaVersion
override def scalacOptions = T { super.scalacOptions().filter(_ != "-Werror") }
trait UiJs extends super.BlendedJs with UiPublishModule with UiCoursierModule {
override def millSourcePath = uiModule.millSourcePath
}
override type ProjectDeps = UiDeps
override def deps = crossDeps
}
trait UiJsModule extends BlendedJsModule
with UiCoursierModule
with UiPublishModule {
override def description = s"Blended Module $blendedModule"
override def baseDir : os.Path = projectDir
override type ProjectDeps = UiDeps
override def deps = crossDeps
override def scalacOptions = T { super.scalacOptions().filter(_ != "-Werror") }
}
trait UiWebBundle extends BlendedWebModule with UiModule {
override def blendedCoreVersion = crossDeps.blendedCoreVersion
}
object mgmt extends Module {
object ui extends Module {
object common extends UiJsModule {
override def millSourcePath = projectDir / "common"
override def ivyDeps = T { super.ivyDeps() ++ Agg(
deps.Js.react4s,
deps.Js.scalaJsDom
)}
override def moduleDeps = super.moduleDeps ++ Seq(router)
}
object router extends UiJsModule {
override def millSourcePath = projectDir / "router"
object test extends super.BlendedJsTests
}
object materialGen extends UiModule {
override def description = "Generator classes for Material UI"
override def blendedModule = "blended.mgmt.ui.material.gen"
override def millSourcePath = projectDir / "materialGen"
override def mainClass = Some("blended.material.gen.MaterialGenerator")
override def ivyDeps = T { super.ivyDeps() ++ Agg(
deps.cmdOption,
deps.slf4j,
deps.logbackCore,
deps.logbackClassic
)}
}
object material extends UiJsModule with WebUtils {
override def millSourcePath = projectDir / "material"
override def appName = blendedModule
override def generatedSources = T {
val npmModules = yarnInstall()
val genTarget = T.dest / "generatedSources"
val generate = Jvm.runSubprocess(
mainClass = materialGen.mainClass().get,
classPath = materialGen.runClasspath().map(_.path),
mainArgs = Seq(
"-d", (npmModules.path / "@material-ui").toIO.getAbsolutePath(),
"-o", genTarget.toIO.getAbsolutePath()
)
)
super.generatedSources() ++ Seq(PathRef(genTarget))
}
override def ivyDeps = T {
super.ivyDeps() ++ Agg(
deps.Js.react4s
)
}
}
object theme extends UiJsModule {
override def millSourcePath = baseDir / "theme"
override def moduleDeps = super.moduleDeps ++ Seq(material)
}
object components extends UiJsModule {
override def millSourcePath = baseDir / "components"
override def moduleDeps = super.moduleDeps ++ Seq(material, theme)
override def ivyDeps = T {
super.ivyDeps() ++ Agg(
deps.Js.blendedJmx
)
}
}
object sampleApp extends UiJsModule with WebUtils {
override def millSourcePath = baseDir / "sampleApp"
override def appName = blendedModule
override def appTitle = Some("Blended Component Playground")
override def packagedJsLibs = Seq(
"react/umd/react.development.js",
"react-dom/umd/react-dom.development.js"
)
override def packagedWebApp = T {
Some(fastOpt())
}
override def moduleDeps = super.moduleDeps ++ Seq(common, components)
override def ivyDeps = T {
super.ivyDeps() ++ Agg(
deps.Js.react4s,
deps.Js.scalaJsDom
)
}
object webBundle extends UiWebBundle {
override def webContext = "sample"
override def exportPackages = Seq.empty
override def bundleSymbolicName = millModuleSegments.parts.filter(_ != crossDeps.scalaVersion).mkString(".")
override def bundleActivator = "blended.mgmt.ui.SampleActivator"
override def webContent = T {
sampleApp.packageHtml()
}
}
}
object mgmtApp extends UiJsModule with WebUtils {
override def millSourcePath = baseDir / "mgmt-app"
override def appName = blendedModule
override def appTitle = Some("Blended Management Console")
override def packagedJsLibs = Seq(
"react/umd/react.development.js",
"react-dom/umd/react-dom.development.js"
)
override def packagedWebApp = T {
Some(fastOpt())
}
override def moduleDeps = super.moduleDeps ++ Seq(common, components)
override def ivyDeps = T {
super.ivyDeps() ++ Agg(
deps.Js.blendedUpdaterConfig,
deps.Js.blendedJmx,
deps.Js.blendedSecurity,
deps.Js.akkaJsActor,
deps.Js.react4s,
deps.Js.scalaJsDom
)
}
object webBundle extends UiWebBundle {
override def webContext = "management"
override def exportPackages = Seq.empty
override def bundleActivator = "blended.mgmt.ui.MgmtActivator"
override def bundleSymbolicName = millModuleSegments.parts.filter(_ != crossDeps.scalaVersion).mkString(".")
override def webContent = T {
mgmtApp.packageHtml()
}
}
}
object mgmtAppSelenium extends UiModule {
override def description = "Selenium tests for the Management UI"
override def millSourcePath = baseDir / "mgmtAppSelenium"
object test extends super.BlendedJvmTests {
override def ivyDeps = T { super.ivyDeps() ++ Agg(
deps.akkaActor(akkaBundleRevision),
deps.akkaStream(akkaBundleRevision),
deps.akkaHttp(akkaBundleRevision),
deps.akkaHttpCore(akkaBundleRevision),
deps.akkaTestkit,
deps.selenium,
deps.scalatestSelenium
)}
override def forkArgs = T {
val appDir = mgmtApp.packageHtml().path
super.forkArgs() ++ Seq(
s"-DappUnderTest=$appDir"
)}
}
}
}
}
}