-
Notifications
You must be signed in to change notification settings - Fork 282
Expand file tree
/
Copy pathbuild.ps1
More file actions
1911 lines (1617 loc) · 74.1 KB
/
build.ps1
File metadata and controls
1911 lines (1617 loc) · 74.1 KB
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
#!/usr/bin/env pwsh
# ----------------------------------------------------------------------------
# Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
#
# WSO2 LLC. licenses this file to you under the Apache License,
# Version 2.0 (the "License"); you may not use this file except
# in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# ----------------------------------------------------------------------------
[CmdletBinding()]
param(
[Parameter(Position = 0)]
[string]$Command,
[Parameter(Position = 1)]
[string]$GO_OS,
[Parameter(Position = 2)]
[string]$GO_ARCH,
[Parameter(Position = 3)]
[string]$TestRun,
[Parameter(Position = 4)]
[string]$TestPackage,
[switch]$WithoutConsent
)
# Accept --without-consent anywhere in positional arguments.
$positionalArgs = @($Command, $GO_OS, $GO_ARCH, $TestRun, $TestPackage)
$withoutConsentFromArgs = $false
for ($i = 0; $i -lt $positionalArgs.Count; $i++) {
if ($positionalArgs[$i] -ceq "--without-consent") {
$withoutConsentFromArgs = $true
$positionalArgs[$i] = $null
}
}
$Command = $positionalArgs[0]
$GO_OS = $positionalArgs[1]
$GO_ARCH = $positionalArgs[2]
$TestRun = $positionalArgs[3]
$TestPackage = $positionalArgs[4]
$skipConsent = $WithoutConsent.IsPresent -or $withoutConsentFromArgs -or ($env:WITHOUT_CONSENT -eq "true")
# Check for PowerShell Version Compatibility
if ($PSVersionTable.PSVersion.Major -lt 7) {
Write-Host ""
Write-Host "================================================================" -ForegroundColor Red
Write-Host " [ERROR] UNSUPPORTED POWERSHELL VERSION" -ForegroundColor Red
Write-Host "================================================================" -ForegroundColor Red
Write-Host ""
Write-Host " You are currently running PowerShell $($PSVersionTable.PSVersion.ToString())" -ForegroundColor Yellow
Write-Host " Thunder requires PowerShell 7 (Core) or later." -ForegroundColor Yellow
Write-Host ""
Write-Host " Please install the latest version from:"
Write-Host " https://github.com/PowerShell/PowerShell" -ForegroundColor Cyan
Write-Host ""
exit 1
}
$ErrorActionPreference = "Stop"
$SCRIPT_DIR = $PSScriptRoot
# Script-level variables for process management
$script:BACKEND_PID = $null
$script:FRONTEND_PID = $null
# --- Set Default OS and the architecture ---
# Auto-detect GO OS
if ([string]::IsNullOrEmpty($GO_OS)) {
try {
$DEFAULT_OS = & go env GOOS
if ([string]::IsNullOrEmpty($DEFAULT_OS)) {
throw "Go environment not found"
}
}
catch {
$DEFAULT_OS = "windows"
}
$GO_OS = $DEFAULT_OS
}
# Auto-detect GO ARCH
if ([string]::IsNullOrEmpty($GO_ARCH)) {
try {
$DEFAULT_ARCH = & go env GOARCH
if ([string]::IsNullOrEmpty($DEFAULT_ARCH)) {
throw "Go environment not found"
}
}
catch {
# Use PowerShell to detect architecture
if ([Environment]::Is64BitOperatingSystem) {
$DEFAULT_ARCH = "amd64"
}
else {
throw "Unsupported architecture"
}
}
$GO_ARCH = $DEFAULT_ARCH
}
Write-Host "Using GO OS: $GO_OS and ARCH: $GO_ARCH"
$SAMPLE_DIST_NODE_VERSION = "node18"
$SAMPLE_DIST_OS = $GO_OS
$SAMPLE_DIST_ARCH = $GO_ARCH
# Transform OS for node packaging executor
if ($SAMPLE_DIST_OS -eq "darwin") {
$SAMPLE_DIST_OS = "macos"
}
elseif ($SAMPLE_DIST_OS -eq "windows") {
$SAMPLE_DIST_OS = "win"
}
if ($SAMPLE_DIST_ARCH -eq "amd64") {
$SAMPLE_DIST_ARCH = "x64"
}
# --- Thunder Package Distribution details ---
$GO_PACKAGE_OS = $GO_OS
$GO_PACKAGE_ARCH = $GO_ARCH
# Normalize OS name for distribution packaging
if ($GO_OS -eq "darwin") {
$GO_PACKAGE_OS = "macos"
}
elseif ($GO_OS -eq "windows") {
$GO_PACKAGE_OS = "win"
}
if ($GO_ARCH -eq "amd64") {
$GO_PACKAGE_ARCH = "x64"
}
$VERSION_FILE = "version.txt"
$VERSION = Get-Content $VERSION_FILE -Raw
$VERSION = $VERSION.Trim()
$THUNDER_VERSION = $VERSION
if ($THUNDER_VERSION.StartsWith("v")) {
$THUNDER_VERSION = $THUNDER_VERSION.Substring(1)
}
$BINARY_NAME = "thunder"
$PRODUCT_FOLDER = "${BINARY_NAME}-${THUNDER_VERSION}-${GO_PACKAGE_OS}-${GO_PACKAGE_ARCH}"
# --- Sample App Distribution details ---
$SAMPLE_PACKAGE_OS = $SAMPLE_DIST_OS
$SAMPLE_PACKAGE_ARCH = $SAMPLE_DIST_ARCH
# React Vanilla Sample
$VANILLA_SAMPLE_APP_SERVER_BINARY_NAME = "server"
$vanillaPackageJson = Get-Content "samples/apps/react-vanilla-sample/package.json" -Raw | ConvertFrom-Json
$VANILLA_SAMPLE_APP_VERSION = $vanillaPackageJson.version
$VANILLA_SAMPLE_APP_FOLDER = "sample-app-react-vanilla-${VANILLA_SAMPLE_APP_VERSION}-${SAMPLE_PACKAGE_OS}-${SAMPLE_PACKAGE_ARCH}"
# React SDK Sample
$reactSdkPackageJson = Get-Content "samples/apps/react-sdk-sample/package.json" -Raw | ConvertFrom-Json
$REACT_SDK_SAMPLE_APP_VERSION = $reactSdkPackageJson.version
$REACT_SDK_SAMPLE_APP_FOLDER = "sample-app-react-sdk-${REACT_SDK_SAMPLE_APP_VERSION}-${SAMPLE_PACKAGE_OS}-${SAMPLE_PACKAGE_ARCH}"
# React API-based Sample
$reactApiPackageJson = Get-Content "samples/apps/react-api-based-sample/package.json" -Raw | ConvertFrom-Json
$REACT_API_SAMPLE_APP_VERSION = $reactApiPackageJson.version
$REACT_API_SAMPLE_APP_FOLDER = "sample-app-react-api-based-${REACT_API_SAMPLE_APP_VERSION}-${SAMPLE_PACKAGE_OS}-${SAMPLE_PACKAGE_ARCH}"
# Directories
$TARGET_DIR = Join-Path $SCRIPT_DIR "target"
$OUTPUT_DIR = Join-Path $TARGET_DIR "out"
$DIST_DIR = Join-Path $TARGET_DIR "dist"
$BUILD_DIR = Join-Path $OUTPUT_DIR ".build"
$LOCAL_CERT_DIR = Join-Path $OUTPUT_DIR ".cert"
$BACKEND_BASE_DIR = "backend"
$BACKEND_DIR = Join-Path $BACKEND_BASE_DIR "cmd/server"
$REPOSITORY_DIR = Join-Path $BACKEND_BASE_DIR "cmd/server/repository"
$REPOSITORY_DB_DIR = Join-Path $REPOSITORY_DIR "database"
$SERVER_SCRIPTS_DIR = Join-Path $BACKEND_BASE_DIR "scripts"
$SERVER_DB_SCRIPTS_DIR = Join-Path $BACKEND_BASE_DIR "dbscripts"
$SECURITY_DIR = "repository/resources/security"
$FRONTEND_BASE_DIR = "frontend"
$GATE_APP_DIST_DIR = "apps/gate"
$CONSOLE_APP_DIST_DIR = "apps/console"
$FRONTEND_GATE_APP_SOURCE_DIR = Join-Path $FRONTEND_BASE_DIR "apps/thunder-gate"
$FRONTEND_CONSOLE_APP_SOURCE_DIR = Join-Path $FRONTEND_BASE_DIR "apps/thunder-console"
$SAMPLE_BASE_DIR = "samples"
$VANILLA_SAMPLE_APP_DIR = Join-Path $SAMPLE_BASE_DIR "apps/react-vanilla-sample"
$VANILLA_SAMPLE_APP_SERVER_DIR = Join-Path $VANILLA_SAMPLE_APP_DIR "server"
$REACT_SDK_SAMPLE_APP_DIR = Join-Path $SAMPLE_BASE_DIR "apps/react-sdk-sample"
$REACT_API_SAMPLE_APP_DIR = Join-Path $SAMPLE_BASE_DIR "apps/react-api-based-sample"
# Default ports
$GATE_APP_DEFAULT_PORT = 5190
$CONSOLE_APP_DEFAULT_PORT = 5191
$DOCS_DEFAULT_PORT = 3000
# ============================================================================
# Read Configuration from deployment.yaml
# ============================================================================
$CONFIG_FILE = "./backend/cmd/server/repository/conf/deployment.yaml"
# Function to read config with fallback
function Read-Config {
if (-not (Test-Path $CONFIG_FILE)) {
# Use defaults if config file not found
$script:HOSTNAME = "localhost"
$script:PORT = 8090
$script:HTTP_ONLY = "false"
$script:PUBLIC_HOSTNAME = ""
$script:CONSENT_ENABLED = $true
}
else {
# Try yq first (YAML parser)
if (Get-Command yq -ErrorAction SilentlyContinue) {
$script:HOSTNAME = & yq eval '.server.hostname // "localhost"' $CONFIG_FILE 2>$null
$script:PORT = & yq eval '.server.port // 8090' $CONFIG_FILE 2>$null
$script:HTTP_ONLY = & yq eval '.server.http_only // false' $CONFIG_FILE 2>$null
$script:PUBLIC_HOSTNAME = & yq eval '.server.public_hostname // ""' $CONFIG_FILE 2>$null
$consentEnabled = & yq eval '.consent.enabled // true' $CONFIG_FILE 2>$null
$script:CONSENT_ENABLED = ($consentEnabled -eq "true")
}
else {
# Fallback: basic parsing with regex
$content = Get-Content $CONFIG_FILE -Raw
# Try to extract hostname
if ($content -match 'hostname:\s*["'']?([^"''\n]+)["'']?') {
$script:HOSTNAME = $matches[1].Trim()
}
else {
$script:HOSTNAME = "localhost"
}
# Try to extract port
if ($content -match 'port:\s*(\d+)') {
$script:PORT = [int]$matches[1]
}
else {
$script:PORT = 8090
}
# Try to extract http_only
if ($content -match 'http_only:\s*true') {
$script:HTTP_ONLY = "true"
}
else {
$script:HTTP_ONLY = "false"
}
# Try to extract public_hostname
if ($content -match 'public_hostname:\s*["'']?([^"''\n]+)["'']?') {
$script:PUBLIC_HOSTNAME = $matches[1].Trim()
}
else {
$script:PUBLIC_HOSTNAME = ""
}
# Try to extract consent.enabled
if ($content -match 'consent:[\s\S]*?enabled:\s*(true|false)') {
$script:CONSENT_ENABLED = ($matches[1] -eq "true")
}
else {
$script:CONSENT_ENABLED = $true
}
}
}
# Determine protocol
if ($script:HTTP_ONLY -eq "true") {
$script:PROTOCOL = "http"
}
else {
$script:PROTOCOL = "https"
}
}
# Read configuration
Read-Config
# Construct base URL (internal API endpoint)
$BASE_URL = "${PROTOCOL}://${HOSTNAME}:${PORT}"
# Construct public URL (external/redirect URLs)
if ($PUBLIC_HOSTNAME) {
$PUBLIC_URL = $PUBLIC_HOSTNAME
}
else {
$PUBLIC_URL = $BASE_URL
}
function Get-CoverageExclusionPattern {
# Read exclusion patterns (full package paths) from .excludecoverage file
# This function can be called from any directory
$coverage_exclude_file = $null
# Check if we're already in the backend directory or need to use relative path
if (Test-Path ".excludecoverage") {
$coverage_exclude_file = ".excludecoverage"
}
elseif (Test-Path (Join-Path $SCRIPT_DIR $BACKEND_BASE_DIR ".excludecoverage")) {
$coverage_exclude_file = Join-Path $SCRIPT_DIR $BACKEND_BASE_DIR ".excludecoverage"
}
else {
return ""
}
# Read non-comment, non-empty lines and join with '|' for regex (exact package path matching)
$patterns = Get-Content $coverage_exclude_file | Where-Object {
$_ -notmatch '^\s*#' -and $_ -notmatch '^\s*$'
}
if ($patterns) {
return ($patterns -join '|')
}
return ""
}
function Clean {
Write-Host "================================================================"
Write-Host "Cleaning build artifacts..."
if (Test-Path $TARGET_DIR) {
Remove-Item -Path $TARGET_DIR -Recurse -Force -ErrorAction SilentlyContinue
}
Write-Host "Removing certificates in $BACKEND_DIR/$SECURITY_DIR"
if (Test-Path (Join-Path $BACKEND_DIR $SECURITY_DIR)) {
Remove-Item -Path (Join-Path $BACKEND_DIR $SECURITY_DIR) -Recurse -Force -ErrorAction SilentlyContinue
}
Write-Host "Removing certificates in $VANILLA_SAMPLE_APP_DIR"
Remove-Item -Path (Join-Path $VANILLA_SAMPLE_APP_DIR "server.cert") -Force -ErrorAction SilentlyContinue
Remove-Item -Path (Join-Path $VANILLA_SAMPLE_APP_DIR "server.key") -Force -ErrorAction SilentlyContinue
Write-Host "Removing certificates in $VANILLA_SAMPLE_APP_SERVER_DIR"
Remove-Item -Path (Join-Path $VANILLA_SAMPLE_APP_SERVER_DIR "server.cert") -Force -ErrorAction SilentlyContinue
Remove-Item -Path (Join-Path $VANILLA_SAMPLE_APP_SERVER_DIR "server.key") -Force -ErrorAction SilentlyContinue
Write-Host "Removing certificates in $REACT_SDK_SAMPLE_APP_DIR"
Remove-Item -Path (Join-Path $REACT_SDK_SAMPLE_APP_DIR "server.cert") -Force -ErrorAction SilentlyContinue
Remove-Item -Path (Join-Path $REACT_SDK_SAMPLE_APP_DIR "server.key") -Force -ErrorAction SilentlyContinue
Write-Host "Removing certificates in $REACT_API_SAMPLE_APP_DIR"
Remove-Item -Path (Join-Path $REACT_API_SAMPLE_APP_DIR "server.cert") -Force -ErrorAction SilentlyContinue
Remove-Item -Path (Join-Path $REACT_API_SAMPLE_APP_DIR "server.key") -Force -ErrorAction SilentlyContinue
Write-Host "================================================================"
}
function Build-Backend {
Write-Host "================================================================"
Write-Host "Building Go backend..."
New-Item -Path $BUILD_DIR -ItemType Directory -Force | Out-Null
# Set binary name with .exe extension for Windows
$output_binary = $BINARY_NAME
if ($GO_OS -eq "windows") {
$output_binary = "${BINARY_NAME}.exe"
}
# Prepare build date without spaces to avoid ldflags splitting
$buildDate = (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")
$env:GOOS = $GO_OS
$env:GOARCH = $GO_ARCH
$env:CGO_ENABLED = "0"
# Check if coverage build is requested via ENABLE_COVERAGE environment variable
$buildArgs = @('build', '-x')
if ($env:ENABLE_COVERAGE -eq "true") {
Write-Host "Building with coverage instrumentation enabled..."
# Build coverage package list, excluding patterns from .excludecoverage
Push-Location $BACKEND_BASE_DIR
try {
$exclude_pattern = Get-CoverageExclusionPattern
$coverpkg = ""
if ($exclude_pattern) {
Write-Host "Excluding coverage for patterns: $exclude_pattern"
$packages = & go list ./...
$filtered_packages = $packages | Where-Object { $_ -notmatch $exclude_pattern }
$coverpkg = $filtered_packages -join ','
}
else {
$packages = & go list ./...
$coverpkg = $packages -join ','
}
}
finally {
Pop-Location
}
$buildArgs += @('-cover', "-coverpkg=$coverpkg")
}
# Construct ldflags safely and pass as an argument array to avoid PowerShell splitting
$ldflags = "-X main.version=$VERSION -X main.buildDate=$buildDate"
$outputPath = Join-Path $BUILD_DIR $output_binary
$buildArgs += @('-ldflags', $ldflags, '-o', $outputPath, './cmd/server')
Write-Host "Executing: go $($buildArgs -join ' ')"
Push-Location $BACKEND_BASE_DIR
try {
& go @buildArgs
if ($LASTEXITCODE -ne 0) {
throw "Go build failed with exit code $LASTEXITCODE"
}
}
finally {
Pop-Location
}
Write-Host "Initializing databases..."
Initialize-Databases -override $true
Write-Host "================================================================"
}
function Build-Frontend {
Write-Host "================================================================"
Write-Host "Building frontend apps..."
# Check if pnpm is installed, if not install it
if (-not (Get-Command pnpm -ErrorAction SilentlyContinue)) {
Write-Host "pnpm not found, installing..."
& npm install -g pnpm
}
# Navigate to frontend directory and install dependencies
Push-Location $FRONTEND_BASE_DIR
try {
Write-Host "Installing frontend dependencies..."
& pnpm install --frozen-lockfile
Write-Host "Building frontend applications & packages..."
& pnpm build
}
finally {
Pop-Location
}
Write-Host "================================================================"
}
function Build-Docs {
Write-Host "================================================================"
Write-Host "Building documentation..."
# Check if pnpm is installed, if not install it
if (-not (Get-Command pnpm -ErrorAction SilentlyContinue)) {
Write-Host "pnpm not found, installing..."
& npm install -g pnpm
}
# Navigate to frontend directory first to ensure build:docs script can run
Push-Location $FRONTEND_BASE_DIR
try {
Write-Host "Installing frontend dependencies (required for docs build)..."
& pnpm install --frozen-lockfile
Write-Host "Building documentation..."
& pnpm run build:docs
}
finally {
Pop-Location
}
Write-Host "================================================================"
}
function Initialize-Databases {
param(
[bool]$override = $false
)
Write-Host "================================================================"
Write-Host "Initializing SQLite databases..."
# Check for sqlite3 CLI availability
$sqliteCmd = Get-Command sqlite3 -ErrorAction SilentlyContinue
if (-not $sqliteCmd) {
Write-Host ""
Write-Host "ERROR: 'sqlite3' CLI not found on PATH. The build script uses the sqlite3 command to initialize local SQLite databases."
Write-Host "On Windows you can install sqlite3 using one of the following methods:"
Write-Host " 1) Chocolatey (requires admin PowerShell):"
Write-Host " choco install sqlite"
Write-Host " 2) Scoop (recommended for user installs):"
Write-Host " scoop install sqlite"
Write-Host " 3) Download prebuilt binaries from https://www.sqlite.org/download.html and add the folder to your PATH."
Write-Host ""
Write-Host "Alternatively, skip database initialization and create the DB files manually under '$REPOSITORY_DB_DIR'."
throw "sqlite3 CLI not found. Install sqlite3 and re-run the build."
}
New-Item -Path $REPOSITORY_DB_DIR -ItemType Directory -Force | Out-Null
$db_files = @("configdb.db", "runtimedb.db", "userdb.db")
$script_paths = @("configdb/sqlite.sql", "runtimedb/sqlite.sql", "userdb/sqlite.sql")
for ($i = 0; $i -lt $db_files.Length; $i++) {
$db_file = $db_files[$i]
$script_rel_path = $script_paths[$i]
$db_path = Join-Path $REPOSITORY_DB_DIR $db_file
$script_path = Join-Path $SERVER_DB_SCRIPTS_DIR $script_rel_path
if (Test-Path $script_path) {
if (Test-Path $db_path) {
if ($override) {
Write-Host " - Removing existing $db_file as override is true"
Remove-Item $db_path -Force
}
else {
Write-Host " ! Skipping $db_file : DB already exists. Delete the existing and re-run to recreate."
continue
}
}
Write-Host " - Creating $db_file using $script_path"
# Use sqlite3 command line tool
& sqlite3 $db_path ".read $script_path"
if ($LASTEXITCODE -ne 0) {
throw "SQLite operation failed with exit code $LASTEXITCODE"
}
Write-Host " - Enabling WAL mode for $db_file"
& sqlite3 $db_path "PRAGMA journal_mode=WAL;"
if ($LASTEXITCODE -ne 0) {
throw "Failed to enable WAL mode with exit code $LASTEXITCODE"
}
}
else {
Write-Host " ! Skipping $db_file : SQL script not found at $script_path"
}
}
Write-Host "SQLite database initialization complete."
Write-Host "================================================================"
}
function Prepare-Backend-For-Packaging {
Write-Host "================================================================"
Write-Host "Copying backend artifacts..."
# Use appropriate binary name based on OS
$binary_name = $BINARY_NAME
if ($GO_OS -eq "windows") {
$binary_name = "${BINARY_NAME}.exe"
}
$package_folder = Join-Path $DIST_DIR $PRODUCT_FOLDER
Copy-Item -Path (Join-Path $BUILD_DIR $binary_name) -Destination $package_folder -Force
Copy-Item -Path $REPOSITORY_DIR -Destination $package_folder -Recurse -Force
Copy-Item -Path $VERSION_FILE -Destination $package_folder -Force
Copy-Item -Path $SERVER_SCRIPTS_DIR -Destination $package_folder -Recurse -Force
Copy-Item -Path $SERVER_DB_SCRIPTS_DIR -Destination $package_folder -Recurse -Force
$security_dir = Join-Path $package_folder $SECURITY_DIR
New-Item -Path $security_dir -ItemType Directory -Force | Out-Null
# Copy bootstrap directory
Write-Host "Copying bootstrap scripts..."
Copy-Item -Path (Join-Path $BACKEND_DIR "bootstrap") -Destination $package_folder -Recurse -Force
Write-Host "=== Ensuring server certificates exist in the distribution ==="
Ensure-Certificates -cert_dir $security_dir -cert_name_prefix "server"
Ensure-Certificates -cert_dir $security_dir -cert_name_prefix "signing"
Write-Host "================================================================"
Write-Host "=== Ensuring crypto file exists in the distribution ==="
Ensure-Crypto-File -conf_dir (Join-Path $package_folder "repository/conf")
Write-Host "================================================================"
}
function Prepare-Frontend-For-Packaging {
Write-Host "================================================================"
Write-Host "Copying frontend artifacts..."
$package_folder = Join-Path $DIST_DIR $PRODUCT_FOLDER
New-Item -Path (Join-Path $package_folder $GATE_APP_DIST_DIR) -ItemType Directory -Force | Out-Null
New-Item -Path (Join-Path $package_folder $CONSOLE_APP_DIST_DIR) -ItemType Directory -Force | Out-Null
# Copy gate app build output
if (Test-Path (Join-Path $FRONTEND_GATE_APP_SOURCE_DIR "dist")) {
Write-Host "Copying Gate app build output..."
Copy-Item -Path (Join-Path $FRONTEND_GATE_APP_SOURCE_DIR "dist\*") -Destination (Join-Path $package_folder $GATE_APP_DIST_DIR) -Recurse -Force
}
else {
Write-Host "Warning: Gate app build output not found at $((Join-Path $FRONTEND_GATE_APP_SOURCE_DIR "dist"))"
}
# Copy console app build output
if (Test-Path (Join-Path $FRONTEND_CONSOLE_APP_SOURCE_DIR "dist")) {
Write-Host "Copying Console app build output..."
Copy-Item -Path (Join-Path $FRONTEND_CONSOLE_APP_SOURCE_DIR "dist\*") -Destination (Join-Path $package_folder $CONSOLE_APP_DIST_DIR) -Recurse -Force
}
else {
Write-Host "Warning: Console app build output not found at $((Join-Path $FRONTEND_CONSOLE_APP_SOURCE_DIR "dist"))"
}
Write-Host "================================================================"
}
function Package {
Write-Host "================================================================"
Write-Host "Packaging backend & frontend artifacts..."
$package_folder = Join-Path $DIST_DIR $PRODUCT_FOLDER
New-Item -Path $package_folder -ItemType Directory -Force | Out-Null
Prepare-Frontend-For-Packaging
Prepare-Backend-For-Packaging
# Copy the appropriate startup and setup scripts based on the target OS
if ($GO_OS -eq "windows") {
Write-Host "Including Windows scripts (start.ps1, setup.ps1)..."
Copy-Item -Path "start.ps1" -Destination $package_folder -Force
Copy-Item -Path "setup.ps1" -Destination $package_folder -Force
}
else {
Write-Host "Including Unix scripts (start.sh, setup.sh)..."
Copy-Item -Path "start.sh" -Destination $package_folder -Force
Copy-Item -Path "setup.sh" -Destination $package_folder -Force
}
if (-not $skipConsent) {
Write-Host "Packaging consent server..."
$packageFolderAbs = (Resolve-Path -Path $package_folder).Path
& (Join-Path $SCRIPT_DIR "scripts/package-consent-server.ps1") `
-GoOS $GO_OS -GoArch $GO_ARCH -DistOutputPath $packageFolderAbs
if ($LASTEXITCODE -ne 0) {
throw "Consent server packaging failed with exit code $LASTEXITCODE"
}
} else {
Write-Host "Skipping consent server packaging (--without-consent)..."
$targetYaml = Join-Path $package_folder "repository/conf/deployment.yaml"
$yqPatched = $false
if (Get-Command yq -ErrorAction SilentlyContinue) {
& yq eval '.consent.enabled = false' -i $targetYaml
if ($LASTEXITCODE -eq 0) {
$yqPatched = $true
}
}
if (-not $yqPatched) {
$content = Get-Content $targetYaml
$inConsent = $false
for ($i = 0; $i -lt $content.Length; $i++) {
if ($content[$i] -match '^consent:') {
$inConsent = $true
} elseif ($inConsent -and $content[$i] -match '^\s*enabled:\s*true') {
$content[$i] = $content[$i] -replace 'enabled:\s*true', 'enabled: false'
$inConsent = $false
} elseif ($inConsent -and $content[$i] -match '^\S') {
$inConsent = $false
}
}
$content | Set-Content $targetYaml
}
$consentDisabled = $false
$inConsentBlock = $false
foreach ($line in (Get-Content $targetYaml)) {
if ($line -match '^consent:') {
$inConsentBlock = $true
} elseif ($inConsentBlock -and $line -match '^\s+enabled:\s*false') {
$consentDisabled = $true
break
} elseif ($inConsentBlock -and $line -match '^\S') {
break
}
}
if (-not $consentDisabled) {
throw "Failed to disable consent in '$targetYaml' — packaging cannot continue with consent still enabled."
}
}
Write-Host "Creating zip file..."
$zipFile = Join-Path $DIST_DIR "$PRODUCT_FOLDER.zip"
if (Test-Path $zipFile) {
Remove-Item $zipFile -Force
}
Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::CreateFromDirectory($package_folder, $zipFile)
Remove-Item -Path $package_folder -Recurse -Force
if (Test-Path $BUILD_DIR) {
Remove-Item -Path $BUILD_DIR -Recurse -Force
}
Write-Host "================================================================"
}
function Build-Sample-App {
Write-Host "================================================================"
Write-Host "Building sample apps..."
# Build React Vanilla sample
Write-Host "=== Building React Vanilla sample app ==="
Write-Host "=== Ensuring React Vanilla sample app certificates exist ==="
Ensure-Certificates -cert_dir $VANILLA_SAMPLE_APP_DIR -cert_name_prefix "server"
Push-Location $VANILLA_SAMPLE_APP_DIR
try {
Write-Host "Installing React Vanilla sample dependencies..."
& npm ci
if ($LASTEXITCODE -ne 0) {
throw "npm ci failed with exit code $LASTEXITCODE"
}
Write-Host "Building React Vanilla sample app (TypeScript + Vite)..."
Write-Host " - Running TypeScript build (tsc -b)..."
& npx tsc -b
if ($LASTEXITCODE -ne 0) {
throw "tsc build failed with exit code $LASTEXITCODE"
}
Write-Host " - Running Vite build..."
& npx vite build
if ($LASTEXITCODE -ne 0) {
throw "vite build failed with exit code $LASTEXITCODE"
}
# Replicate npm script: copy dist to server/app and copy certs
$serverDir = Join-Path $VANILLA_SAMPLE_APP_DIR "server"
$serverAppDir = Join-Path $serverDir "app"
if (Test-Path $serverAppDir) {
Remove-Item -Path $serverAppDir -Recurse -Force
}
New-Item -Path $serverAppDir -ItemType Directory -Force | Out-Null
$distFull = Resolve-Path -Path "dist" | Select-Object -ExpandProperty Path
Copy-Item -Path (Join-Path $distFull "*") -Destination $serverAppDir -Recurse -Force
# Copy server certs into server directory
if (Test-Path (Join-Path $VANILLA_SAMPLE_APP_DIR "server.key")) {
Copy-Item -Path (Join-Path $VANILLA_SAMPLE_APP_DIR "server.key") -Destination $serverDir -Force
}
if (Test-Path (Join-Path $VANILLA_SAMPLE_APP_DIR "server.cert")) {
Copy-Item -Path (Join-Path $VANILLA_SAMPLE_APP_DIR "server.cert") -Destination $serverDir -Force
}
# Install server dependencies
Push-Location $serverDir
try {
Write-Host " - Installing server dependencies..."
& npm ci
if ($LASTEXITCODE -ne 0) {
throw "npm ci (server) failed with exit code $LASTEXITCODE"
}
}
finally {
Pop-Location
}
}
finally {
Pop-Location
}
Write-Host "✅ React Vanilla sample app built successfully."
# Build React SDK sample
Write-Host "=== Building React SDK sample app ==="
# Ensure certificates exist for React SDK sample
Write-Host "=== Ensuring React SDK sample app certificates exist ==="
Ensure-Certificates -cert_dir $REACT_SDK_SAMPLE_APP_DIR -cert_name_prefix "server"
Push-Location $REACT_SDK_SAMPLE_APP_DIR
try {
Write-Host "Installing React SDK sample dependencies..."
& npm ci
if ($LASTEXITCODE -ne 0) {
throw "npm ci failed with exit code $LASTEXITCODE"
}
Write-Host "Building React SDK sample app..."
& npm run build
if ($LASTEXITCODE -ne 0) {
throw "npm run build failed with exit code $LASTEXITCODE"
}
}
finally {
Pop-Location
}
Write-Host "✅ React SDK sample app built successfully."
# Build React API-based sample
Write-Host "=== Building React API-based sample app ==="
# Ensure certificates exist for React API-based sample
Write-Host "=== Ensuring React API-based sample app certificates exist ==="
Ensure-Certificates -cert_dir $REACT_API_SAMPLE_APP_DIR
Push-Location $REACT_API_SAMPLE_APP_DIR
try {
Write-Host "Installing React API-based sample dependencies..."
& npm ci
if ($LASTEXITCODE -ne 0) {
throw "npm ci failed with exit code $LASTEXITCODE"
}
Write-Host "Building React API-based sample app..."
& npm run build
if ($LASTEXITCODE -ne 0) {
throw "npm run build failed with exit code $LASTEXITCODE"
}
}
finally {
Pop-Location
}
Write-Host "✅ React API-based sample app built successfully."
Write-Host "================================================================"
}
function Package-Sample-App {
Write-Host "================================================================"
Write-Host "Packaging sample apps..."
# Package React Vanilla sample
Write-Host "=== Packaging React Vanilla sample app ==="
Package-Vanilla-Sample
# Package React SDK sample
Write-Host "=== Packaging React SDK sample app ==="
Package-React-SDK-Sample
# Package React API-based sample
Write-Host "=== Packaging React API-based sample app ==="
Package-React-API-Based-Sample
Write-Host "================================================================"
}
function Package-Vanilla-Sample {
# Use appropriate binary name based on OS
$binary_name = $VANILLA_SAMPLE_APP_SERVER_BINARY_NAME
$executable_name = "$VANILLA_SAMPLE_APP_SERVER_BINARY_NAME-$SAMPLE_DIST_OS-$SAMPLE_DIST_ARCH"
if ($SAMPLE_DIST_OS -eq "win") {
$binary_name = "${VANILLA_SAMPLE_APP_SERVER_BINARY_NAME}.exe"
$executable_name = "${VANILLA_SAMPLE_APP_SERVER_BINARY_NAME}-${SAMPLE_DIST_OS}-${SAMPLE_DIST_ARCH}.exe"
}
$vanilla_sample_app_folder = Join-Path $DIST_DIR $VANILLA_SAMPLE_APP_FOLDER
New-Item -Path $vanilla_sample_app_folder -ItemType Directory -Force | Out-Null
$vanilla_sample_app_folder = (Resolve-Path -Path $vanilla_sample_app_folder).Path
# Copy the built app files
$serverAppSource = Join-Path $VANILLA_SAMPLE_APP_SERVER_DIR "app"
if (-not (Test-Path $serverAppSource)) {
Write-Host "Server app folder '$serverAppSource' not found; falling back to copying from '$VANILLA_SAMPLE_APP_DIR/dist'..."
New-Item -Path $VANILLA_SAMPLE_APP_SERVER_DIR -ItemType Directory -Force | Out-Null
New-Item -Path $serverAppSource -ItemType Directory -Force | Out-Null
$distFull = Resolve-Path -Path (Join-Path $VANILLA_SAMPLE_APP_DIR "dist") | Select-Object -ExpandProperty Path
Copy-Item -Path (Join-Path $distFull "*") -Destination $serverAppSource -Recurse -Force
}
Copy-Item -Path $serverAppSource -Destination $vanilla_sample_app_folder -Recurse -Force
Push-Location $VANILLA_SAMPLE_APP_SERVER_DIR
try {
New-Item -Path "executables" -ItemType Directory -Force | Out-Null
# Install dependencies to ensure pkg is available
& npm ci
if ($LASTEXITCODE -ne 0) {
throw "npm ci failed with exit code $LASTEXITCODE"
}
& npx pkg . -t $SAMPLE_DIST_NODE_VERSION-$SAMPLE_DIST_OS-$SAMPLE_DIST_ARCH -o executables/$VANILLA_SAMPLE_APP_SERVER_BINARY_NAME-$SAMPLE_DIST_OS-$SAMPLE_DIST_ARCH
if ($LASTEXITCODE -ne 0) {
throw "npx pkg failed with exit code $LASTEXITCODE"
}
}
finally {
Pop-Location
}
# Copy the server binary
Copy-Item -Path (Join-Path $VANILLA_SAMPLE_APP_SERVER_DIR "executables/$executable_name") -Destination (Join-Path $vanilla_sample_app_folder $binary_name) -Force
# Copy README and other necessary files
if (Test-Path (Join-Path $VANILLA_SAMPLE_APP_DIR "README.md")) {
Copy-Item -Path (Join-Path $VANILLA_SAMPLE_APP_DIR "README.md") -Destination $vanilla_sample_app_folder -Force
}
# Ensure the certificates exist in the sample app directory
Write-Host "=== Ensuring certificates exist in the React Vanilla sample distribution ==="
Ensure-Certificates -cert_dir $vanilla_sample_app_folder -cert_name_prefix "server"
# Copy the appropriate startup script based on the target OS
if ($SAMPLE_DIST_OS -eq "win") {
Write-Host "Including Windows start script (start.ps1)..."
Copy-Item -Path (Join-Path $VANILLA_SAMPLE_APP_SERVER_DIR "start.ps1") -Destination $vanilla_sample_app_folder -Force
}
else {
Write-Host "Including Unix start script (start.sh)..."
Copy-Item -Path (Join-Path $VANILLA_SAMPLE_APP_SERVER_DIR "start.sh") -Destination $vanilla_sample_app_folder -Force
}
Write-Host "Creating React Vanilla sample zip file..."
$distAbs = (Resolve-Path -Path $DIST_DIR).Path
$zipFile = [System.IO.Path]::Combine($distAbs, "$VANILLA_SAMPLE_APP_FOLDER.zip")
if (Test-Path $zipFile) {
Remove-Item $zipFile -Force
}
Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::CreateFromDirectory($vanilla_sample_app_folder, $zipFile)
Remove-Item -Path $vanilla_sample_app_folder -Recurse -Force
Write-Host "✅ React Vanilla sample app packaged successfully as $zipFile"
}
function Package-React-SDK-Sample {
$react_sdk_sample_app_folder_t = Join-Path $DIST_DIR $REACT_SDK_SAMPLE_APP_FOLDER
New-Item -Path $react_sdk_sample_app_folder_t -ItemType Directory -Force | Out-Null
# Copy the built React app (dist folder)
if (Test-Path (Join-Path $REACT_SDK_SAMPLE_APP_DIR "dist")) {
Write-Host "Copying React SDK sample build output..."
Copy-Item -Path (Join-Path $REACT_SDK_SAMPLE_APP_DIR "dist") -Destination $react_sdk_sample_app_folder_t -Recurse -Force
}
else {
Write-Host "Warning: React SDK sample build output not found at $((Join-Path $REACT_SDK_SAMPLE_APP_DIR 'dist'))"
throw "React SDK sample build output not found"
}
# Copy README and other necessary files
if (Test-Path (Join-Path $REACT_SDK_SAMPLE_APP_DIR "README.md")) {
Copy-Item -Path (Join-Path $REACT_SDK_SAMPLE_APP_DIR "README.md") -Destination $react_sdk_sample_app_folder_t -Force
}
if (Test-Path (Join-Path $REACT_SDK_SAMPLE_APP_DIR ".env.example")) {
Copy-Item -Path (Join-Path $REACT_SDK_SAMPLE_APP_DIR ".env.example") -Destination $react_sdk_sample_app_folder_t -Force
}
# Copy the appropriate startup script based on the target OS
if ($SAMPLE_DIST_OS -eq "win") {
Write-Host "Including Windows start script (start.ps1)..."
Copy-Item -Path (Join-Path $REACT_SDK_SAMPLE_APP_DIR "start.ps1") -Destination $react_sdk_sample_app_folder_t -Force
}
else {
Write-Host "Including Unix start script (start.sh)..."
Copy-Item -Path (Join-Path $REACT_SDK_SAMPLE_APP_DIR "start.sh") -Destination $react_sdk_sample_app_folder_t -Force
}
Write-Host "Creating React SDK sample zip file..."
$distAbs = (Resolve-Path -Path $DIST_DIR).Path
$zipFile = [System.IO.Path]::Combine($distAbs, "$REACT_SDK_SAMPLE_APP_FOLDER.zip")
if (Test-Path $zipFile) {
Remove-Item $zipFile -Force
}
Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::CreateFromDirectory($react_sdk_sample_app_folder_t, $zipFile)
Remove-Item -Path $react_sdk_sample_app_folder_t -Recurse -Force
Write-Host "✅ React SDK sample app packaged successfully as $zipFile"
}
function Package-React-API-Based-Sample {
$react_api_sample_app_folder_t = Join-Path $DIST_DIR $REACT_API_SAMPLE_APP_FOLDER
New-Item -Path $react_api_sample_app_folder_t -ItemType Directory -Force | Out-Null
# Copy the built React app (dist folder)
if (Test-Path (Join-Path $REACT_API_SAMPLE_APP_DIR "dist")) {
Write-Host "Copying React API-based sample build output..."
Copy-Item -Path (Join-Path $REACT_API_SAMPLE_APP_DIR "dist") -Destination $react_api_sample_app_folder_t -Recurse -Force
}
else {
Write-Host "Warning: React API-based sample build output not found at $((Join-Path $REACT_API_SAMPLE_APP_DIR 'dist'))"
throw "React API-based sample build output not found"
}
# Copy README if it exists
if (Test-Path (Join-Path $REACT_API_SAMPLE_APP_DIR "README.md")) {
Copy-Item -Path (Join-Path $REACT_API_SAMPLE_APP_DIR "README.md") -Destination $react_api_sample_app_folder_t -Force
}