-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.gradle.kts
1426 lines (1243 loc) · 43.9 KB
/
build.gradle.kts
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import org.jetbrains.dokka.DokkaConfiguration
import org.jetbrains.dokka.PluginConfigurationImpl
import org.jetbrains.dokka.gradle.DokkaTask
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpack
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpackConfig
import org.jetbrains.kotlin.gradle.targets.js.yarn.YarnPlugin
import org.jetbrains.kotlin.gradle.targets.js.yarn.YarnRootExtension
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.Files
import java.nio.file.StandardCopyOption
import java.net.URL
import java.util.Properties
import org.tomlj.Toml
plugins {
val kotlinVersion: String by System.getProperties()
kotlin("plugin.serialization") version kotlinVersion
kotlin("multiplatform") version kotlinVersion
id("org.jetbrains.dokka") version kotlinVersion
val kvisionVersion: String by System.getProperties()
id("kvision") version kvisionVersion
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.tomlj:tomlj:1.0.0")
}
}
group = "edu.duke.bartesaghi"
/**
* This version number describes the nextPYP API.
* It's not the same as the nextPYP version!
*
* This version number has defined formal semantics that helps nextPYP API clients
* decide if they're compatible with the API or not.
* The formal semantics are called Semantic Versioning: https://semver.org/
*
* Briefly, the version number should always have three integer components: major.minor.patch.
* If you make a change to the API that would break any existing clients, you must increment the major number.
* Otherwise, if you add new functionality without breaking existing funtionality, you must increment the minor number.
* Otherwise, if you fix a bug or patch a vulnerability without breaking compatibility, you must increment the patch number.
*
* Also, incrementing 9 yields 10. eg, incrementing the minor version of 1.9.0 yields 1.10.0
*/
val apiVersion = "1.0.0"
repositories {
mavenCentral()
jcenter() // needed for some JetBrains dependencies... still works somehow
}
// Versions
val javaVersion: String by project
val kotlinVersion: String by System.getProperties()
val kvisionVersion: String by System.getProperties()
val kotlinLanguageVersion: String by project
val serializationVersion: String by project
val coroutinesVersion: String by project
val ktorVersion: String by project
val exposedVersion: String by project
val hikariVersion: String by project
val h2Version: String by project
val pgsqlVersion: String by project
val kweryVersion: String by project
val logbackVersion: String by project
val commonsCodecVersion: String by project
val jdbcNamedParametersVersion: String by project
// create a default local.properties if needed
val localPropsPath = projectDir.resolve("local.properties")
if (!localPropsPath.exists()) {
localPropsPath.writeText("""
|
|# the path to the pyp sources
|pypDir=../pyp
|
""".trimMargin())
}
// read info about the local environment from local.properties, not gradle.properties
val localProps = Properties().apply {
localPropsPath.bufferedReader().use { reader ->
load(reader)
}
}
val pypDir = projectDir.resolve(localProps["pypDir"] as String)
val rawDataDir = projectDir.resolve(localProps["rawDataDir"] as? String ?: "../data")
val clientDir = (localProps["clientDir"] as? String)?.let { projectDir.resolve(it) }
val webDir = file("src/frontendMain/web")
val runDir = projectDir.resolve("run")
// read the version number from pyp, if possible
pypDir.resolve("nextpyp.toml")
.takeIf { it.exists() }
?.let { nextpypPath ->
// read the TOML file
val doc = Toml.parse(nextpypPath.readText())
if (doc.hasErrors()) {
throw Error("Failed to parse ${nextpypPath.absolutePath}:\n${doc.errors().joinToString("\n")}")
}
// read the version, if any
doc.getString("version")
?.let { version = it }
}
println("NextPYP version: $version")
// figure out if we're building a development build or not, so we can optimize the task dependencies
val isDevBuildTask = gradle.startParameter.taskNames.any { it in listOf(
"vmContainerRun",
"vmContainerRerun",
"containerRun",
"containerRerun"
) }
kotlin {
val sharedCompilerArgs = emptyList<String>()
jvm("backend") {
compilations.all {
kotlinOptions {
jvmTarget = javaVersion
apiVersion = kotlinLanguageVersion
languageVersion = kotlinLanguageVersion
// show compiler errors when we misuse nullable values from Java
// see: https://kotlinlang.org/docs/java-interop.html#jsr-305-support
// and: https://github.com/Kotlin/KEEP/blob/master/proposals/jsr-305-custom-nullability-qualifiers.md#compiler-configuration-for-jsr-305-support
freeCompilerArgs += listOf("-Xjsr305=strict")
freeCompilerArgs += sharedCompilerArgs
}
}
}
// The new Kotlin/JS IR compiler v1.8.22 can't deal with the older versions of the KVision libraries.
// The code itself compiles just fine now, but the CSS resources get omitted from the compiled version somehow.
// The new compiler docs say the reason might be because older libraries are incompatible with the new compiler.
// see: https://kotlinlang.org/docs/js-ir-compiler.html#current-limitations-of-the-ir-compiler
// We're a couple years behind the current KVision release, but upgrading KVision is such a pain in the ass
// it's REALLY not worth the trouble unless we have absolutely no other choice.
// So we'll continue to use the old legacy (non-IR) compiler for as long as we're using the old version of KVision
// and as long as JetBrains will continue supporting it.
// Tragically, the newest version of the legacy compiler v1.8.22 doesn't work on our code either. =(
// And neither does the v1.7.21 compiler. >8[
// Looks like the newest compiler version we can use is v1.6.x. *sigh*
js("frontend", compiler=LEGACY) {
compilations.all {
kotlinOptions {
apiVersion = kotlinLanguageVersion
languageVersion = kotlinLanguageVersion
freeCompilerArgs += sharedCompilerArgs
}
}
browser {
runTask {
outputFileName = "main.bundle.js"
sourceMaps = true
// NOTE: this dev server isn't used at all in container-land
devServer = KotlinWebpackConfig.DevServer(
open = false,
port = 3000,
proxy = mutableMapOf(
"/kv/*" to "http://localhost:8080",
"/kvws/*" to mapOf("target" to "ws://localhost:8080", "ws" to true)
),
contentBase = mutableListOf("$buildDir/processedResources/frontend/main")
)
}
webpackTask {
outputFileName = "main.bundle.js"
// see: https://webpack.js.org/configuration/devtool/
devtool = "eval-source-map"
// TODO: how to differentiate devtool between dev and prod?
}
testTask {
useKarma {
//useChromeHeadless()
useFirefoxHeadless()
}
}
}
binaries.executable()
}
sourceSets {
val commonMain by getting {
dependencies {
api("io.kvision:kvision-server-ktor:$kvisionVersion")
// force the full release version of coroutines to avoid IDE errors (not the release candidate that KVision depends on)
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
// override the kotlinx.serialization version provided by the gradle plugin to newer version to get a bugfix, see:
// https://github.com/Kotlin/kotlinx.serialization/issues/1488
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:$serializationVersion")
}
kotlin.srcDir("build/generated-src/common")
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val backendMain by getting {
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation(kotlin("reflect"))
implementation("io.ktor:ktor-server-netty:$ktorVersion") // Apache 2
implementation("io.ktor:ktor-auth:$ktorVersion") // Apache 2
implementation("io.ktor:ktor-client-cio:$ktorVersion") // Apache 2
implementation("ch.qos.logback:logback-classic:$logbackVersion") // LGPL
// TODO: we don't even use these things, they were included in KVision, should we get rid of them?
implementation("com.h2database:h2:$h2Version")
implementation("org.jetbrains.exposed:exposed:$exposedVersion")
implementation("org.postgresql:postgresql:$pgsqlVersion")
implementation("com.zaxxer:HikariCP:$hikariVersion")
implementation("commons-codec:commons-codec:$commonsCodecVersion")
implementation("com.axiomalaska:jdbc-named-parameters:$jdbcNamedParametersVersion")
implementation("com.github.andrewoma.kwery:core:$kweryVersion")
implementation("com.github.jai-imageio:jai-imageio-core:1.3.1") // BSD 3-clause
implementation("org.mongodb:mongodb-driver-sync:4.4.0") // Apache 2
implementation("org.tomlj:tomlj:1.0.0") // Apache 2
implementation("de.mkammerer:argon2-jvm:2.7") // LGPL
implementation("io.seruco.encoding:base62:0.1.3") // MIT
implementation("com.github.mwiede:jsch:0.1.66") // BSD
implementation("com.fasterxml.jackson.core:jackson-databind:2.12.3") // Apache 2
implementation("org.apache.commons:commons-vfs2:2.9.0") // Apache 2
implementation("org.reflections:reflections:0.10.2") // Apache 2
// library for WebP support in ImageIO
implementation("io.github.darkxanter:webp-imageio:0.2.3") // Apache 2
/* NOTE:
The latest version of this library is 0.3.2, but we can't use it because of dependency hell.
The 0.3.2 jar bundles a native libary that depends on libc 2.29.
To get libc 2.29+, we'd have to base the container on Rocky 9+.
BUT!
Mongo DB 4.4 isn't supported in Rocky 9. The mongodb-org v4.4 package is not present in the repo for RHEL/etc 9!
Mongo DB 5+ is present in the repo, and we could use that in theory,
but upgrading to a new DB version apparently requires a database migration, which we'd like to avoid.
So we'll use 0.2.3 of the WebP jar which depends on a libc that is present in Rocky 8.
*/
// CUDA libraries
// see: https://github.com/jcuda/jcuda-main/blob/master/USAGE.md
val jcudaVersion = "12.0.0"
implementation("org.jcuda:jcuda:$jcudaVersion") {
isTransitive = false
}
runtimeOnly("org.jcuda:jcuda-natives:$jcudaVersion:linux-x86_64")
// NOTE: if you change dependency libraries, run the `image` gradle task to update the `build/libs` folder
}
}
val backendTest by getting {
dependencies {
implementation(kotlin("test"))
implementation(kotlin("test-junit"))
}
}
val frontendMain by getting {
resources.srcDir(webDir)
dependencies {
// NOTE: KVision itself is MIT licensed
implementation("io.kvision:kvision:$kvisionVersion")
implementation("io.kvision:kvision-bootstrap:$kvisionVersion")
implementation("io.kvision:kvision-bootstrap-css:$kvisionVersion")
implementation("io.kvision:kvision-bootstrap-select:$kvisionVersion")
implementation("io.kvision:kvision-bootstrap-select-remote:$kvisionVersion")
implementation("io.kvision:kvision-bootstrap-datetime:$kvisionVersion")
implementation("io.kvision:kvision-bootstrap-spinner:$kvisionVersion")
implementation("io.kvision:kvision-bootstrap-upload:$kvisionVersion")
implementation("io.kvision:kvision-bootstrap-dialog:$kvisionVersion")
implementation("io.kvision:kvision-fontawesome:$kvisionVersion")
implementation("io.kvision:kvision-i18n:$kvisionVersion")
implementation("io.kvision:kvision-richtext:$kvisionVersion")
implementation("io.kvision:kvision-handlebars:$kvisionVersion")
implementation("io.kvision:kvision-datacontainer:$kvisionVersion")
implementation("io.kvision:kvision-redux:$kvisionVersion")
implementation("io.kvision:kvision-chart:$kvisionVersion")
implementation("io.kvision:kvision-tabulator:$kvisionVersion")
implementation("io.kvision:kvision-pace:$kvisionVersion")
implementation("io.kvision:kvision-moment:$kvisionVersion")
//implementation("io.kvision:kvision-routing-navigo-ng:$kvisionVersion")
implementation("io.kvision:navigo-kotlin-ng:0.0.3")
// NOTE: The above library uses a module from a newer KVision release
// to try to work around a bug in Navigo v8.8.12 that was fixed in a later release.
// It uses the Navigo wrapper rather than the KVision module around it,
// because we re-wrote the KVision shim code in our project anyway.
implementation("io.kvision:kvision-toast:$kvisionVersion")
implementation("org.jetbrains.kotlinx:kotlinx-html-js:0.7.2") // Apache 2
/* Always explicitly pick versions for all JS dependencies!!
The Kotlin front-end plugin will warn us if we try to add a dependency without a version.
Don't ignore those warnings, you'll regret it later if you do.
Tragically, the default behavior for npm is to automatically download newest
versions of libraries even when rebuilding an existing project!!
Naturally, this creates unexpected bugs when OF COURSE your code isn't
automatically compatible with newer versions of your dependencies.
Even worse, the version you didn't know you were depending on gets overwritten by the rebuild
so you can't go look it up after you suddenly realize you need it.
The only way to restore working order to your project is to guess what version you depended on
for each end every dependency that's now broken by an unexpected (and unwanted) update.
Thankfully, the newest Kotlin/JS gradle plugin uses yarn,
which will ossify dependency versions into a yarn.lock file.
Make sure to include the kotlin-js-store/yarn.lock file in the git repo.
*/
// add JavaScript dependencies here, then require() or @JsModule() them in kotlin source somewhere
implementation(npm("three", "0.138.3")) // MIT
implementation(npm("dat.gui", "0.7.9")) // Apache-2.0
implementation(npm("plotly.js", "1.58.1")) // MIT
implementation(npm("nouislider", "14.6.3")) // MIT
implementation(npm("photoswipe", "4.1.3")) // MIT
implementation(npm("hyperlist", "1.0.0")) // MIT
implementation(npm("js-cookie", "2.2.1")) // MIT
implementation(npm("webcola", "3.4.0")) // MIT
implementation(npm("@projectstorm/react-diagrams", "6.2.0")) // MIT
implementation(npm("@ltd/j-toml", "1.12.2")) // LGPL 3
implementation(npm("ansicolor", "1.1.100")) // Unlicense
// dependencies for react-diagrams, see: https://projectstorm.gitbook.io/react-diagrams/getting-started
implementation(npm("closest", "0.0.1")) // MIT
implementation(npm("lodash", "4.17.20")) // MIT
implementation(npm("react", "16.14.0")) // MIT
implementation(npm("react-dom", "16.14.0")) // MIT
implementation(npm("ml-matrix", "6.5.3")) // MIT
implementation(npm("dagre", "0.8.5")) // MIT
implementation(npm("pathfinding", "0.4.18")) // MIT
implementation(npm("paths-js", "0.4.11")) // Apache 2
implementation(npm("@emotion/core", "10.1.1")) // MIT
implementation(npm("@emotion/styled", "10.0.27")) // MIT
implementation(npm("resize-observer-polyfill", "1.5.1")) // MIT
}
kotlin.srcDir("build/generated-src/frontend")
}
val frontendTest by getting {
dependencies {
implementation(kotlin("test-js"))
implementation("io.kvision:kvision-testutils:$kvisionVersion")
}
}
}
}
// Tragically, the react-diagrams maintainer didn't get SemVer correct.
// I dont blame them, getting SemVer correct is a form of predicting the future.
// And predicting the future is hard. =/
// To fix it, we need to override version numbers for transitive dependencies, see:
// https://classic.yarnpkg.com/en/docs/selective-version-resolutions/
// https://blog.jetbrains.com/kotlin/2020/11/kotlin-1-4-20-released/
rootProject.plugins.withType<YarnPlugin> {
rootProject.the<YarnRootExtension>().apply {
resolution("@projectstorm/react-canvas-core", "6.2.0")
resolution("@projectstorm/react-diagrams-core", "6.2.0")
resolution("@projectstorm/react-diagrams-defaults", "6.2.0")
resolution("@projectstorm/react-diagrams-routing","6.2.0")
}
}
fun getNodeJsBinaryExecutable(): String {
val nodeDir = NodeJsRootPlugin.apply(rootProject).nodeJsSetupTaskProvider.get().destination
val isWindows = System.getProperty("os.name").toLowerCase().contains("windows")
val nodeBinDir = if (isWindows) nodeDir else nodeDir.resolve("bin")
val command = NodeJsRootPlugin.apply(rootProject).nodeCommand
val finalCommand = if (isWindows && command == "node") "node.exe" else command
return nodeBinDir.resolve(finalCommand).absolutePath
}
tasks {
create("generatePotFile", Exec::class) {
dependsOn("compileKotlinFrontend")
executable = getNodeJsBinaryExecutable()
args("${rootProject.buildDir}/js/node_modules/gettext-extract/bin/gettext-extract")
inputs.files(kotlin.sourceSets["frontendMain"].kotlin.files)
outputs.file("$projectDir/src/frontendMain/resources/i18n/messages.pot")
}
}
afterEvaluate {
tasks {
getByName("frontendProcessResources", Copy::class) {
dependsOn("compileKotlinFrontend")
exclude("**/*.pot")
doLast("Convert PO to JSON") {
destinationDir.walkTopDown().filter {
it.isFile && it.extension == "po"
}.forEach {
exec {
executable = getNodeJsBinaryExecutable()
args(
"${rootProject.buildDir}/js/node_modules/gettext.js/bin/po2json",
it.absolutePath,
"${it.parent}/${it.nameWithoutExtension}.json"
)
println("Converted ${it.name} to ${it.nameWithoutExtension}.json")
}
it.delete()
}
}
}
create("frontendArchive", Jar::class).apply {
// if we're building for development, put webpack in development mode
val webpackTask = if (isDevBuildTask) {
"frontendBrowserDevelopmentWebpack"
} else {
"frontendBrowserProductionWebpack"
}
dependsOn(webpackTask)
group = "package"
archiveAppendix.set("frontend")
val distribution =
project.tasks.getByName(webpackTask, KotlinWebpack::class).destinationDirectory!!
from(distribution) {
include("*.*")
}
from(webDir)
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
into("/assets")
inputs.files(distribution, webDir)
outputs.file(archiveFile)
manifest {
attributes(
mapOf(
"Implementation-Title" to rootProject.name,
"Implementation-Group" to rootProject.group,
"Implementation-Version" to rootProject.version,
"Timestamp" to System.currentTimeMillis()
)
)
}
}
getByName("backendProcessResources", Copy::class) {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
getByName("backendJar").group = "package"
val jarTask = create("jar", Jar::class).apply {
dependsOn("frontendArchive", "backendJar")
group = "package"
manifest {
attributes(
mapOf(
"Implementation-Title" to rootProject.name,
"Implementation-Group" to rootProject.group,
"Implementation-Version" to rootProject.version,
"Timestamp" to System.currentTimeMillis()
)
)
}
val dependencies = project.tasks["backendJar"].outputs.files +
project.tasks["frontendArchive"].outputs.files
dependencies.forEach {
if (it.isDirectory) from(it) else from(zipTree(it))
}
inputs.files(dependencies)
outputs.file(archiveFile)
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
create("backendRun", JavaExec::class) {
dependsOn("compileKotlinBackend")
group = "run"
main = "edu.duke.bartesaghi.micromon.MainKt"
classpath =
configurations["backendRuntimeClasspath"] + project.tasks["compileKotlinBackend"].outputs.files +
project.tasks["backendProcessResources"].outputs.files
workingDir = buildDir
}
getByName("compileKotlinBackend") {
dependsOn("compileKotlinMetadata", "allMetadataJar", "generateBuildSources")
}
getByName("compileKotlinFrontend") {
dependsOn("compileKotlinMetadata", "allMetadataJar", "generateBuildSources")
}
create("copyLocalModules", Copy::class) {
group = "nodejs"
description = "copy our js modules from the src folder to the build folder where the compiler can find them"
mustRunAfter("kotlinNpmInstall")
val frontendDir = kotlin.sourceSets.getByName("frontendMain").kotlin.srcDirs.first().parentFile
from(frontendDir.resolve("js"))
into(buildDir.resolve("js/node_modules"))
}
getByName("frontendBrowserDevelopmentWebpack") {
dependsOn("copyLocalModules")
if (isDevBuildTask) {
// force rebuilding webback in when in a dev task, otherwise the CSS doesn't get recompiled
// TODO: maybe there's a better way to detect changes in CSS files so we don't have to redo this every build?
outputs.upToDateWhen { false }
}
}
getByName("frontendBrowserProductionWebpack") {
dependsOn("copyLocalModules")
}
getByName("frontendBrowserTest") {
dependsOn("copyLocalModules")
}
/** get all the backend java runtime dependencies (ie jars) */
fun collectClasspath(): FileCollection =
project.tasks["jar"].outputs.files
.plus(configurations["backendRuntimeClasspath"])
.filter { it.name.endsWith(".jar") }
val classpathFileTask = create("classpathFile") {
group = "build"
description = "creates the classpath file needed by the website container"
doLast {
// resolve all the jar files against the libs folder
buildDir.resolve("classpath.txt")
.writeText("""
|-cp "\
|${collectClasspath().joinToString(":\\\n") { "libs/${it.name}" }}
|"
""".trimMargin())
}
}
create("image", Copy::class) {
dependsOn("jar", classpathFileTask)
group = "package"
description = "makes the server runtime image"
destinationDir = file("$buildDir/image")
// copy all the dependency jars
from(collectClasspath()) {
into("libs")
}
// copy the classpath file
from(buildDir.resolve("classpath.txt")) {
into("bin")
}
// copy the executable scripts
from(projectDir.resolve("config")) {
include("cli.sh")
include("micromon.sh")
into("bin")
makeExecutable()
}
// NOTE: KTor is configured via the src/backendMain/resources/application.conf file,
// rather than the command line, see:
// https://ktor.io/servers/configuration.html#available-config
}
/* TODO: do we need this anymore?
register<Tar>("dist") {
group = "package"
description = "build the distribution files for the container"
compression = Compression.GZIP
archiveBaseName.set("nextPyp")
destinationDirectory.set(buildDir.resolve("dist"))
// include all the singularity images
val containerNames = listOf("nextPYP.sif", "reverse-proxy.sif")
for (name in containerNames) {
val path = runDir.resolve(name)
from(path)
}
// include all the scripts
for (script in listOf("start", "stop", "config.toml")) {
from("config/$script") {
makeExecutable()
}
}
from("docs/install.md")
doFirst {
// make sure the container files actually exist before running the task
for (name in containerNames) {
checkContainer(name)
}
}
}
*/
fun checkContainer(name: String) {
val path = runDir.resolve(name)
if (!path.exists()) {
throw Error("""
|Missing container $name, try running the task to build it, eg vmBuild___.
|If you already have a pre-built container, move it to $path
""".trimMargin())
}
}
create("containerRun") {
group = "run"
dependsOn("jar", classpathFileTask)
mustRunAfter("containerStop")
doLast {
// make sure the config.toml exists
val configPath = runDir.resolve("config.toml")
if (!configPath.exists()) {
throw Error("create a config.toml in the run dir")
}
// make sure the container exists
checkContainer("nextPYP.sif")
// run the start script with the development jar
exec {
workingDir = runDir
executable = "./start"
environment("PYP_CONFIG", configPath.absolutePath)
environment("PYP_SRC", pypDir)
args(projectDir.absolutePath, project.version)
}
}
}
create("containerStop") {
group = "run"
doLast {
exec {
workingDir = runDir
executable = "./stop"
// if the container isn't running, Singularity will return an error exit code,
// which translates into a gradle exception by default
// except, if the container isn't running, we've already won! =D
// there's no need to stop it again, so just ignore errors from singularity entirely
isIgnoreExitValue = true
}
}
}
create("containerRerun") {
group = "run"
dependsOn("containerStop", "containerRun")
}
// pick a unique id for our VM and its sub-objects
val vmid = "streamPYP"
val storageId = "$vmid-storage"
val networkId = "vboxnet0"
// NOTE: we can't actually choose the name of the host-only network, the name is automatically generated
// using custom names isn't implemented for some reason, see: https://www.virtualbox.org/ticket/11919
val micromonId = "micromon"
val pypId = "pyp"
val rawDataId = "rawdata"
val devDir = buildDir.toPath().resolve("dev")
val drivePath = devDir.resolve("drive.vdi")
val interfacePrefix = "192.168.56"
val vmIp = "$interfacePrefix.5"
// translate paths into the VM filesystem
val vmMicromonDir = Paths.get("/media/$micromonId")
val vmRunDir = vmMicromonDir.resolve("run")
val vmPypDir = Paths.get("/media/$pypId")
// TODO: need to share pyp folder
create("vmCreate") {
group = "dev"
description = "Creates a VirtualBox virtual machine for development"
doLast {
// make a place to do VM things
devDir.createFolderIfNeeded()
// download the linux install ISO
// CentOS is basically abandoned now (CentOS Stream is *not* the same thing!)
// The next best thing to CentOS is now Rocky Linux, see for more info:
// https://computingforgeeks.com/rocky-linux-vs-centos-stream-vs-rhel-vs-oracle-linux/
val installIso = devDir.resolve("rocky.iso")
if (!installIso.exists()) {
URL("https://dl.rockylinux.org/vault/rocky/8.5/isos/x86_64/Rocky-8.5-x86_64-minimal.iso").let {
println("dowloading $it ...")
it.download(installIso)
}
}
// remove any old VMs
// but detatch the drive first so it doesn't get deleted too
vbox("storageattach", ignoreResult=true) {
add(vmid)
add("--storagectl", storageId)
add("--port", "1")
add("--medium", "none")
}
vbox("closemedium", ignoreResult=true) {
add("disk")
add(drivePath.toString())
}
vbox("unregistervm", ignoreResult=true) {
add(vmid)
add("--delete")
// NOTE: also deletes any attached drives
}
vbox("hostonlyif", ignoreResult=true) {
add("remove", networkId)
}
vbox("dhcpserver", ignoreResult=true) {
add("remove")
add("--network", vboxInterfaceNetworkName(networkId))
}
// make the vbox image for rocky, see:
// https://docs.rockylinux.org/guides/virtualization/vbox-rocky/
// https://zaufi.github.io/administration/2012/08/31/vbox-setup-new-vm
// https://docs.oracle.com/en/virtualization/virtualbox/6.1/user/vboxmanage.html#vboxmanage-intro
val vmDir = devDir.resolve("vbox")
vbox("createvm") {
add("--name", vmid)
add("--ostype", "RedHat_64")
add("--register")
add("--basefolder", vmDir.toString())
}
vbox("modifyvm") {
add(vmid)
// pick resource limits
// NOTE: the streaming daemons need at least 4 CPUs to run
// NOTE: micrograph processing seems to need more than 4 GiB of RAM now
// looks like `unblur` from cistem is using most of it, 8 GiB seems to be enough for now
add("--memory", (1024*8).toString())
add("--cpus", "4")
// add more than the default video memory, so we can run the rocky installer GUI
// tragically, headless/automated installations are far too cumbersome to do here
// see "kickstarting": https://docs.fedoraproject.org/en-US/Fedora/26/html/Installation_Guide/chap-kickstart-installations.html
add("--vram", "256")
// the vbox GUI seems to recommend using this option, but somehow it's not the default
// turning it on solves a lot of performance issues for me though
add("--graphicscontroller", "vmsvga")
// try to use modern CPU instructions, for performance
add("--hwvirtex", "on")
//add("--hwvirtexexcl", "on") // apparently not supported by my version of vbox
add("--vtxvpid", "on")
// turn off defaults we don't need
add("--accelerate3d", "off")
add("--audio", "none")
// configure the guest->host network path
add("--nic1", "nat")
// TODO
//add("--natpf1", "ssh,tcp,,2222,,22")
//add("--natpf2", "ssh,tcp,,8080,,8080")
}
// configure the host->guest network path
// virtualbox should assign ip4 address 192.168.56.1 to the host adapter
vbox("hostonlyif") {
add("create")
}
vbox("hostonlyif") {
add("ipconfig", networkId)
add("--ip", "$interfacePrefix.1")
}
vbox("modifyvm") {
add(vmid)
add("--nic2", "hostonly")
add("--hostonlyadapter2", networkId)
}
vbox("dhcpserver") {
add("add")
add("--network", vboxInterfaceNetworkName(networkId))
add("--enable")
add("--ip", "$interfacePrefix.2")
add("--netmask", "255.255.255.0")
// we won't actually use the dynamic IP range,
// but vbox still requires us to set it
add("--lowerip", "$interfacePrefix.10")
add("--upperip", "$interfacePrefix.20")
// give our VM a static lease on the IP
add("--vm", vmid)
add("--nic", "2")
add("--fixed-address", vmIp)
}
// create the virtual drive for the VM
val driveMiB = 30*1024 // 30 GiB, should be more than enough space
if (!drivePath.exists()) {
println("Creating drive for up to $driveMiB MiB ...")
vbox("createmedium") {
add("--filename", drivePath.toString())
add("--format", "VDI")
add("--size", driveMiB.toString())
add("--variant", "Standard") // aka dynamically-sized, don't actually allocate space until we need it
}
}
// attach the drive to the VM
vbox("storagectl") {
add(vmid)
add("--name", storageId)
add("--add", "sata")
add("--controller", "IntelAHCI")
add("--portcount", "4")
add("--hostiocache", "off")
add("--bootable", "on")
}
vbox("storageattach") {
add(vmid)
add("--storagectl", storageId)
add("--port", "1")
add("--medium", drivePath.toString())
add("--type", "hdd")
}
// attach the install iso
vbox("storageattach") {
add(vmid)
add("--storagectl", storageId)
add("--port", "2")
add("--medium", installIso.toString())
add("--type", "dvddrive")
}
vbox("modifyvm") {
add(vmid)
add("--boot1", "dvd")
}
// boot the VM
vbox("startvm") {
add(vmid)
add("--type", "gui") // need a human to run the Rocky installer
}
// notes for what to do in the installer GUI
// choose your language
// Installation Destination:
// the defaults are fine, just click done
// Network & Host Name
// choose a host name, eg `nextpyp`
// Root Password
// leave this alone, don't pick a root password
// User Creation
// might have to scroll down to see it
// make your user account, use the same name as the host user account!
// check the administrator option
// uncheck the password option, more annoying than useful for a dev vm
// Begin Installation!
// when done, don't click reboot button
// machine -> ACPI shutdown
}
}
create("vmUpdate") {
group = "dev"
description = "Updates the operating system software in the VM, including kernel updates"
doLast {
// detatch the install iso
vbox("storageattach", ignoreResult=true) {
add(vmid)
add("--storagectl", storageId)
add("--port", "2")
add("--medium", "none")
}
vbox("modifyvm") {
add(vmid)
add("--boot1", "disk")
}
vbox("startvm") {
add(vmid)
add("--type", "gui")
}
// log into the VM
// $ sudo dnf update -y
// $ shutdown now
}
}
create("vmGuestAdditions") {
group = "dev"
description = "Sets up folder sharing in the virtual machine"
doLast {
// download the guest additions if needed
// NOTE: v6.1.32 doesn't seem to work
val guestIso = devDir.resolve("guestAdditions.iso")
if (!guestIso.exists()) {
URL("https://download.virtualbox.org/virtualbox/6.1.30/VBoxGuestAdditions_6.1.30.iso").download(guestIso)
}
if (!guestIso.exists()) {
throw Error("can't find VirtualBox Guest Additions ISO at\n\t$guestIso")
}
// add guest additions media
vbox("storageattach") {
add(vmid)
add("--storagectl", storageId)
add("--port", "2")
add("--medium", guestIso.toString())
add("--type", "dvddrive")
}
vbox("startvm") {
add(vmid)
add("--type", "gui")
}
// $ sudo dnf install -y kernel-devel kernel-headers gcc make bzip2 perl elfutils-libelf-devel
// $ sudo mount /dev/sr0 /mnt
// $ sudo /mnt/VBoxLinuxAdditions.run
// $ shutdown now
// NOTE: rocky 9+ needs this too:
// sudo dnf install epel-release -y
// sudo dnf install -y dkms
}
}
create("vmSetup") {
group = "dev"
description = "performs all the remaining setup to make the VM usable for development"
doLast {
// detatch the guest additions iso
vbox("storageattach", ignoreResult=true) {
add(vmid)
add("--storagectl", storageId)
add("--port", "2")
add("--medium", "none")
}
// set up shared folders:
// writeable access to micromon
vbox("sharedfolder", ignoreResult=true) {
add("remove")
add(vmid)
add("--name", micromonId)
}
vbox("sharedfolder") {
add("add")
add(vmid)
add("--name", micromonId)
add("--hostpath", projectDir.absolutePath)
add("--automount")
add("--auto-mount-point", "/media/$micromonId")
}
// read-only access to pyp, required
if (!pypDir.exists()) {
throw Error("pyp folder not found at \"$pypDir\". Make sure /local.properties `pypDir` has the correct path")
}
vbox("sharedfolder", ignoreResult=true) {
add("remove")
add(vmid)
add("--name", pypId)
}
vbox("sharedfolder") {
add("add")
add(vmid)
add("--name", pypId)
add("--hostpath", pypDir.toString())
add("--readonly")
add("--automount")
add("--auto-mount-point", "/media/$pypId")
}
// read-only access to raw data folder, if available
vbox("sharedfolder", ignoreResult=true) {
add("remove")