-
Notifications
You must be signed in to change notification settings - Fork 50
/
hz.ps1
2705 lines (2257 loc) · 93.8 KB
/
hz.ps1
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
## Copyright (c) 2008-2024, Hazelcast, Inc. All Rights Reserved.
##
## Licensed 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.
## Hazelcast.NET Build Script
# constant
$defaultServerVersion="5.5.0"
# PowerShell errors can *also* be a pain
# see https://stackoverflow.com/questions/10666035
# see https://stackoverflow.com/questions/10666101
# don't die as soon as a command reports an error, we will take care of it!
# (and, GitHub actions tend to end up running with 'Stop' by default)
$ErrorActionPreference='Continue'
# prepare directories
$scriptRoot = "$PSScriptRoot"
$slnRoot = [System.IO.Path]::GetFullPath("$scriptRoot")
$srcDir = [System.IO.Path]::GetFullPath("$slnRoot/src")
$tmpDir = [System.IO.Path]::GetFullPath("$slnRoot/temp")
$buildDir = [System.IO.Path]::GetFullPath("$slnRoot/build")
# include utils
. "$buildDir/utils.ps1"
# ensure we have the right platform
Validate-Platform
# pwsh handling of args is... interesting
$clargs = [Environment]::GetCommandLineArgs()
$script = [IO.Path]::GetFileName($PSCommandPath) # this is always going to be 'script.ps1'
$clarg0 = [IO.Path]::GetFileName($clargs[0]) # this is always going to be the pwsh exe/dll
$clarg1 = $null
if ($clargs.Count -gt 1) {
$clarg1 = [IO.Path]::GetFileName($clargs[1]) # this is going to be either 'script.ps1' or the first arg
}
if ($script -eq $clarg1) {
# the pwsh exe/dll running the script (e.g. launched from bash, cmd...)
$ignore, $ignore, $argx = $clargs
}
else {
# the script running within pwsh (e.g. script launched from the pwsh prompt)
$argx = $args
# still, unquoted -- is stripped by pwsh no matter what
# and, --foo:bar is OK but -foo:bar is still processed by pwsh
# need to use pwsh --% escape
}
# PowerShell args can *also* be a pain - because 'pwsh' loves to pre-handle
# args when run directly but not when run from a scrip, etc - so we have our
# own way of dealing with args
# Tests filter.
# Can use eg "namespace==Hazelcast.Tests.Core" to only run and cover some tests.
# NUnit selection: https://docs.nunit.org/articles/nunit/running-tests/Test-Selection-Language.html
# test|name|class|namespace|method|cat ==|!=|=~|!~ 'value'|/value/|"value"
# "class == /Hazelcast.Tests.Networking.NetworkAddressTests/"
# "test == /Hazelcast.Tests.Networking.NetworkAddressTests/Parse"
# DotCover filter: https://www.jetbrains.com/help/dotcover/Running_Coverage_Analysis_from_the_Command_LIne.html#filters
# +:<select>=<value> -:<select>=<value> separated with ';'
# eg -:module=AdditionalTests;-:type=MainTests.Unit*;-:type=MainTests.IntegrationTests;function=TestFeature1;
# <select> can be: +:module=*;class=*;function=*; -:myassembly * is supported
$params = @(
@{ name = "enterprise"; type = [switch]; default = $false;
desc = "whether to run enterprise tests";
info = "Running enterprise tests require an enterprise key, which can be supplied either via the HAZELCAST_ENTERPRISE_KEY environment variable, or the build/enterprise.key file."
},
@{ name = "server"; type = [string]; default = $defaultServerVersion; alias="server-version";
parm = "<version>";
desc = "the server version when running tests, the remote controller, or a server";
note = "The server <version> must match a released Hazelcast IMDG server version, e.g. 4.0 or 4.1-SNAPSHOT. Server JARs are automatically downloaded."
},
@{ name = "framework"; type = [string]; default = $null; alias = "f"
parm = "<version>";
desc = "the framework to run tests for (default is all)";
note = "The framework <version> must match a valid .NET target framework moniker, e.g. net462 or netcoreapp3.1. Check the project files (.csproj) for supported versions."
},
@{ name = "configuration"; type = [string]; default = "Release"; alias = "c"
parm = "<config>";
desc = "the build configuration";
note = "Configuration is 'Release' by default but can be forced to be 'Debug'."
},
@{ name = "testFilter"; type = [string]; default = $null; alias = "tf,test-filter";
parm = "<filter>";
desc = "a test filter (default is all tests)";
note = "The test <filter> can be used to filter the tests to run, it must respect the NUnit test selection language, which is detailed at: https://docs.nunit.org/articles/nunit/running-tests/Test-Selection-Language.html. Example: -tf `"test == /Hazelcast.Tests.NearCache.NearCacheRecoversFromDistortionsTest/`""
},
@{ name = "test"; type = [string]; default = $null; alias = "t";
parm = "<pattern>";
desc = "a simplified test filter";
note = "The simplified test <pattern> filter is equivalent to the full `"name =~ /<pattern>/`" filter."
},
@{ name = "coverageFilter"; type = [string]; default = $null; alias = "cf,coverage-filter";
parm = "<filter>";
desc = "a test coverage filter (default is all)";
note = "The coverage <filter> can be used to filter the tests to cover, it must respect the dotCover language, which is detailed at: https://www.jetbrains.com/help/dotcover/Running_Coverage_Analysis_from_the_Command_LIne.html#filters."
},
@{ name = "sign"; type = [switch]; default = $false;
desc = "whether to sign assemblies";
note = "Signing assemblies requires the private signing key in build/hazelcast.snk file."
},
@{ name = "cover"; type = [switch]; default = $false;
desc = "whether to run test coverage during tests"
},
@{ name = "version"; type = [string]; default = $null;
parm = "<version>";
desc = "the version to build, set, tag, etc.";
note = "The <version> must be a valid SemVer version such as 3.2.1 or 6.7.8-preview.2. If no value is specified then the version is obtained from src/Directory.Build.props."
},
@{ name = "noRestore"; type = [switch]; default = $false; alias = "nr,no-restore";
desc = "do not restore global NuGet packages"
},
@{ name = "localRestore"; type = [switch]; default = $false; alias = "lr,local-restore";
desc = "restore all NuGet packages locally"
},
@{ name = "constants"; type = [string]; default = $null;
parm = "<constants>";
desc = "additional MSBuild constants"
},
@{ name = "classpath"; type = [string]; default = $null; alias = "cp";
parm = "<classpath>";
desc = "define an additional classpath";
info = "The classpath is appended to the default remote controller or server classpath." },
@{ name = "reproducible"; type = [switch]; default = $false; alias = "repro";
desc = "build reproducible assemblies" },
@{ name = "serverConfig"; type = [string]; default = $null; alias = "server-config";
parm = "<path>";
desc = "the full path to the server configuration xml file"
},
@{ name = "verbose-tests"; type = [switch]; default = $false;
desc = "verbose tests results with errors"
},
@{ name = "yolo"; type = [switch]; default = $false;
desc = "confirms excution of sensitive actions"
},
@{ name = "copy-files-source"; type = [string]; default = $null;
desc = "source folder to be copied"
},
@{ name = "publicApi"; type = [switch]; default = $false; alias = "public-api";
desc = "Whether to enforce Roslyn Public API rules"
},
@{ name = "beta"; type = [switch]; default = $false;
desc = "whether to run beta features and tests"
}
)
# first one is the default one
# order is important as they will run in the specified order
$actions = @(
# keep 'help' in the first position!
@{ name = "help";
desc = "display this help";
uniq = $true; outputs = $true
},
@{ name = "completion-initialize";
desc = "initialize command tab-completion";
internal = $true; uniq = $true
},
@{ name = "completion-commands";
desc = "list commands for tab-completion";
internal = $true; uniq = $true; outputs = $true
},
@{ name = "noop";
desc = "no operation";
internal = $true
},
@{ name = "clean";
desc = "cleans the solution"
},
@{ name = "set-version";
desc = "sets the version";
note = "Updates the version in src/Directory.Build.props with the specified version."
},
@{ name = "verify-version";
desc = "verifies the version";
note = "Ensures that the version in src/Directory.Build.prop matches the -version option."
},
@{ name = "tag-release";
desc = "tags a release";
note = "Create a vX.Y.Z tag corresponding to the version in src/Directory.Build.Props, or the version specified via the -version option."
},
@{ name = "build";
desc = "builds the solution";
need = @( "git", "dotnet-complete", "build-proj", "can-sign" )
},
@{ name = "test";
desc = "runs the tests";
need = @( "git", "dotnet-complete", "java", "server-files", "build-proj", "enterprise-key", "certs", "snapshot-repo")
},
@{ name = "build-docs";
desc = "builds the documentation";
note = "Building the documentation is not supported on non-Windows platforms as DocFX requires .NET Framework.";
need = @( "git", "build-proj", "docfx" )
},
@{ name = "git-docs";
desc = "prepares the documentation release Git commit";
note = "The commit still needs to be pushed to GitHub pages."
},
@{ name = "pack-nuget";
desc = "packs the NuGet packages";
need = @( "dotnet-minimal" )
},
@{ name = "serve-docs";
desc = "serves the documentation";
need = @( "build-proj", "docfx" )
},
@{ name = "run-remote-controller"; alias = "rc";
uniq = $true;
desc = "runs the remote controller for tests";
note = "This command downloads the required JARs and configuration file.";
need = @( "java", "server-files", "enterprise-key", "snapshot-repo" )
},
@{ name = "start-remote-controller";
uniq = $true;
desc = "starts the remote controller for tests";
note = "This command downloads the required JARs and configuration file.";
need = @( "java", "server-files", "enterprise-key", "snapshot-repo" )
},
@{ name = "stop-remote-controller";
uniq = $true;
desc = "stops the remote controller";
},
@{ name = "run-server";
uniq = $true;
desc = "runs a server for tests";
note = "This command downloads the required JARs and configuration file.";
need = @( "java", "server-files", "enterprise-key", "snapshot-repo" )
},
@{ name = "get-server";
uniq = $true;
desc = "gets a server for tests";
note = "This command downloads the required JARs and configuration file.";
need = @( "java", "server-files", "enterprise-key", "snapshot-repo" )
},
@{ name = "generate-codecs";
uniq = $true;
desc = "generates the codec source files";
need = @( "git", "python" )
},
@{ name = "run-example"; alias = "ex";
uniq = $true;
desc = "runs an example";
note = "The example name must be passed as first command arg e.g. ./hz.ps1 run-example Logging. Extra raw parameters can be passed to the example."
},
@{ name = "publish-examples";
desc = "publishes examples";
note = "Publishes examples into temp/examples";
need = @( "dotnet-complete" )
},
@{ name = "cover-to-docs";
desc = "copy test coverage to documentation";
note = "Documentation and test coverage must exist."
},
@{ name = "update-doc-version";
desc = "updates versions in doc version.md";
note = "The resulting commit still needs to be pushed."
},
@{ name = "generate-certs";
desc = "generates the test certificates"
},
@{ name = "install-root-ca";
desc = "(experimental) installs the ROOT CA test certificate";
note = "Requires priviledges. Not supported."
},
@{ name = "remove-root-ca";
desc = "(experimental) removes the ROOT CA test certificate";
note = "Requires priviledges. Not supported."
},
@{ name = "cleanup-code";
desc = "cleans the code"
},
@{ name = "getfwks-json";
desc = "get frameworks";
internal = $true; uniq = $true; outputs = $true
},
@{
name = "copy-files";
desc= "copies from given --copy-files-source to current solution folder by reflecting the folder structure.";
}
)
# include devops
$devops_actions = "$buildDir/devops/csharp-release/actions.ps1"
if (test-path $devops_actions) { . $devops_actions }
# prepare
function say-hello {
Write-Output "Hazelcast .NET Command Line"
Write-Output "PowerShell $powershellVersion on $platform"
}
# process args
$options = Parse-Args $argx $params
if ($options -is [string]) {
Die "$options - use 'help' to list valid parameters"
}
$err = Parse-Commands $options.commands $actions
if ($err -is [string]) {
Die "$err - use 'help' to list valid commands"
}
# get ready
$quiet = $false
$actions | foreach-object {
$action = $_
if (-not $action.run) { return }
if ($action.outputs) { $quiet = $true }
}
if (-not $quiet) { say-hello }
# process defined constants
# see https://github.com/dotnet/sdk/issues/9562
if (![string]::IsNullOrWhiteSpace($options.constants)) {
$options.constants = $options.constants.Replace(",", ";")
}
# clear rogue environment variable
$env:FrameworkPathOverride=""
# this will be SystemDefault by default, and on some oldish environment (Windows 8...) it
# may not enable Tls12 by default, and use Tls10, and that will prevent us from connecting
# to some SSL/TLS servers (for instance, NuGet) => explicitly add Tls12
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol `
-bor [Net.SecurityProtocolType]::Tls12
# validate the version to build
$hasVersion = $false
$versionPrefix = ""
$versionSuffix = ""
if (-not [System.String]::IsNullOrWhiteSpace($options.version)) {
if (-not ($options.version -match '^(\d+\.\d+\.\d+)(?:\-([a-z0-9\.\-]*))?$')) {
Die "Version `"$($options.version)`" is not a valid SemVer version"
}
$versionPrefix = $Matches.1
$versionSuffix = $Matches.2
$options.version = $versionPrefix.Trim()
if (-not [System.String]::IsNullOrWhiteSpace($versionSuffix)) {
$options.version += "-$($versionSuffix.Trim())"
}
$hasVersion = $true
}
# set versions and configure
$serverVersion = $options.server # use specified value by default
$isSnapshot = $options.server.Contains("SNAPSHOT") -or $options.server -eq "master"
$isBeta = $options.server.Contains("BETA")
$hzRCVersion = "0.8-SNAPSHOT" # use appropriate version
#$hzRCVersion = "0.5-SNAPSHOT" # for 3.12.x
# determine java code repositories for tests
$mvnOssPublicRepo = "https://oss.sonatype.org/content/repositories/snapshots"
$mvnOssSnapshotRepo = "https://hazelcast.jfrog.io/artifactory/snapshot-internal"
$mvnOssSnapshotRepoBasicAuth = "https://repository.hazelcast.com/snapshot-internal"
$mvnEntSnapshotRepo = "https://repository.hazelcast.com/snapshot"
$mvnOssReleaseRepo = "https://repo1.maven.org/maven2"
$mvnEntReleaseRepo = "https://repository.hazelcast.com/release"
if ($isSnapshot) {
$mvnOssRepo = $mvnOssSnapshotRepo
$mvnEntRepo = $mvnEntSnapshotRepo
} else {
$mvnOssRepo = $mvnOssReleaseRepo
$mvnEntRepo = $mvnEntReleaseRepo
}
# more directories
$outDir = [System.IO.Path]::GetFullPath("$slnRoot/temp/output")
$docDir = [System.IO.Path]::GetFullPath("$slnRoot/doc")
$libDir = [System.IO.Path]::GetFullPath("$slnRoot/temp/lib")
if ($isWindows) { $userHome = $env:USERPROFILE } else { $userHome = $env:HOME }
# nuget packages
$nugetPackages = "$userHome/.nuget"
if ($options.localRestore) {
$nugetPackages = "$slnRoot/.nuget"
if (-not (Test-Path $nugetPackages)) { mkdir $nugetPackages }
}
# get current version
$propsXml = [xml] (Get-Content "$srcDir/Directory.Build.props")
$currentVersionPrefix = $propsXml.project.propertygroup.versionprefix | Where-Object { -not [System.String]::IsNullOrWhiteSpace($_) }
$currentVersionSuffix = $propsXml.project.propertygroup.versionsuffix | Where-Object { -not [System.String]::IsNullOrWhiteSpace($_) }
$currentVersion = $currentVersionPrefix.Trim()
if (-not [System.String]::IsNullOrWhiteSpace($currentVersionSuffix)) {
$currentVersion += "-$($currentVersionSuffix.Trim())"
}
# set version
if ($hasVersion)
{
# a version was passed in arguments
$isNewVersion = ($options.version -ne $currentVersion)
}
else
{
$versionPrefix = $currentVersionPrefix
$versionSuffix = $currentVersionSuffix
$options.version = $currentVersion
$isNewVersion = $false
}
$isPreRelease = -not [System.String]::IsNullOrWhiteSpace($versionSuffix)
# get doc destination according to version
if ($isPreRelease) {
$docDstDir = "dev"
$docMessage = "Update dev documentation ($($options.version))"
}
else {
$docDstDir = $versionPrefix
$docMessage = "Version $($options.version) documentation"
}
function determine-target-frameworks {
$csproj = [xml] (get-content "$srcDir/Hazelcast.Net.Tests/Hazelcast.Net.Tests.csproj")
$csproj.Project.PropertyGroup | foreach-object {
if ($_.Condition -ne $null -and $_.Condition.Contains("Windows_NT")) {
if ($_.Condition.Contains("==")) {
$targetsOnWindows = $_.TargetFrameworks
}
if ($_.Condition.Contains("!=")) {
$targetsOnLinux = $_.TargetFrameworks
}
}
}
$targetsOnWindows = $targetsOnWindows.Split(";", [StringSplitOptions]::RemoveEmptyEntries)
$targetsOnLinux = $targetsOnLinux.Split(";", [StringSplitOptions]::RemoveEmptyEntries)
for ($i = 0; $i -lt $targetsOnWindows.Length; $i++) { $targetsOnWindows[$i] = $targetsOnWindows[$i].Trim() }
for ($i = 0; $i -lt $targetsOnLinux.Length; $i++) { $targetsOnLinux[$i] = $targetsOnLinux[$i].Trim() }
if ($isWindows) { $frameworks = $targetsOnWindows }
else { $frameworks = $targetsOnLinux }
return $frameworks, $targetsOnWindows, $targetsOnLinux
}
# determine framework(s) - for running tests
# we always need to build *all* frameworks because e.g. some projects need to be built
# for netstandard in order to run on .NET Core - so one single framework cannot do it
$frameworks, $windowsFrameworks, $linuxFrameworks = determine-target-frameworks
$testFrameworks = $frameworks
if (-not [System.String]::IsNullOrWhiteSpace($options.framework)) {
$fwks = $options.framework.ToLower().Split(",", [StringSplitOptions]::RemoveEmptyEntries)
foreach ($fwk in $fwks) {
if (-not $frameworks.Contains($fwk)) {
Die "Framework '$fwk' is not supported on platform '$platform', supported frameworks are: $([System.String]::Join(", ", $frameworks))."
}
}
$testFrameworks = $fwks
}
function ensure-snapshot-repo {
if ($isSnapshot) {
$ossRepoTokenType = ""
if (-not [System.String]::IsNullOrWhiteSpace($env:HZ_SNAPSHOT_REPO_TOKEN))
{
$token = $env:HZ_SNAPSHOT_REPO_TOKEN
$ossRepoTokenType = "Bearer"
Write-Output "Using token for OSS repository"
$strToken = "Bearer "+$token
$script:ossRepoRequestHeader =@{
Authorization = $strToken
}
}
elseif (-not [System.String]::IsNullOrWhiteSpace($env:HZ_SNAPSHOT_INTERNAL_USERNAME) -and
-not [System.String]::IsNullOrWhiteSpace($env:HZ_SNAPSHOT_INTERNAL_PASSWORD))
{
$ossRepoTokenType = "Basic"
$token = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$( $env:HZ_SNAPSHOT_INTERNAL_USERNAME ):$( $env:HZ_SNAPSHOT_INTERNAL_PASSWORD )"))
Write-Output "Using credentials for OSS repository from environment variables"
$mvnOssRepo = $mvnOssSnapshotRepoBasicAuth
$strToken = "Basic "+$token
$script:ossRepoRequestHeader =@{
Authorization = $strToken
}
}
else
{
$token = $null
$script:ossRepoRequestHeader = $null
Die "No token or credentials for OSS repository. Provide HZ_SNAPSHOT_REPO_TOKEN or
HZ_SNAPSHOT_INTERNAL_USERNAME and HZ_SNAPSHOT_INTERNAL_PASSWORD environment variables."
}
$script:ossRepoTokenType = $ossRepoTokenType
$script:ossRepoToken = $token
}
}
# ensure we have the enterprise key for testing
function ensure-enterprise-key {
$enterpriseKey = $env:HAZELCAST_ENTERPRISE_KEY
if ($options.enterprise) {
if ([System.String]::IsNullOrWhiteSpace($enterpriseKey)) {
if (test-path "$buildDir/enterprise.key") {
Write-Output ""
Write-Output "Found enterprise key in build/enterprise.key file."
$enterpriseKey = @(get-content "$buildDir/enterprise.key")[0].Trim()
$env:HAZELCAST_ENTERPRISE_KEY = $enterpriseKey
}
else {
Die "Enterprise features require an enterprise key, either in`n- HAZELCAST_ENTERPRISE_KEY environment variable, or`n- $buildDir/enterprise.key file."
}
}
else {
Write-Output ""
Write-Output "Found enterprise key in HAZELCAST_ENTERPRISE_KEY environment variable."
}
}
$script:enterpriseKey = $enterpriseKey
}
# finds latest version of a NuGet package in the NuGet cache
function findLatestVersion($path) {
if ([System.IO.Directory]::Exists($path)) {
$v = Get-ChildItem $path | `
foreach-object { [Version]::Parse($_.Name) } | `
Sort-Object -descending | `
Select-Object -first 1
}
else {
$l = [System.IO.Path]::GetDirectoryname($path).Length
$l = $path.Length - $l
$v = Get-ChildItem "$path.*" | `
foreach-object { [Version]::Parse($_.Name.SubString($l)) } | `
Sort-Object -descending | `
Select-Object -first 1
}
return $v
}
# ensures that a command exists in the path
function ensure-command($command) {
$r = get-command $command 2>&1
if ($null -eq $r.Name) {
Die "Command '$command' is missing."
}
else {
Write-Output "Detected $command at '$($r.Source)'"
}
}
function get-master-server-version ( $result ) {
Write-Output "Determine master server version from GitHub"
$url = "https://raw.githubusercontent.com/hazelcast/hazelcast/master/pom.xml"
Write-Output "GET $url"
$response = invoke-web-request $url
if ($response.StatusCode -ne 200) {
Die "Error: could not download POM file from GitHub ($($response.StatusCode))"
}
$pom = [xml] $response.Content
if ($pom.project -eq $null -or $pom.project.version -eq $null) {
Die "Error: got invalid POM file from GitHub (could not find version)"
}
$version = $pom.project.version
if ([string]::IsNullOrWhiteSpace($version)) {
Die "Error: got invalid POM file from GitHub (could not find version)"
}
if (-not $version.EndsWith("-SNAPSHOT")) {
$version += "-SNAPSHOT"
}
$result.version = $version
}
# $options.server contains the specified server version, which can be 5.0, 5.0.1,
# 5.0-SNAPSHOT, 5.0.1-SNAPSHOT, master, or anything really - and it may match an
# actual server version, but also be master, or 4.0-SNAPSHOT that would be n/a on
# Maven, because for some reason we don't keep .0-SNAPSHOT on Maven, but Maven
# would advertise the 4.0.x-SNAPSHOT instead - so here we are going to figure out
# if, from the specified $options.server, we can derive a $script:serverVersion
# that is an available, actual server version.
# or, $options.serverActual
function determine-server-version {
$version = $options.server
# set the actual server version
# this will be updated below if required
$script:serverVersion = $version
# BETA versions should be build and places under temp/lib.
if($isBeta){
Write-Output "Server: version $version is BETA, using this version"
return
}
if (-not $isSnapshot) {
Write-Output "Server: version $version is not a -SNAPSHOT, using this version"
return
}
if ($version -eq "master") {
Write-Output "Server: version is $version, determine actual version from GitHub"
$r = @{}
get-master-server-version $r
$version = $r.version
Write-Output "Server: determined version $version from GitHub"
$script:serverVersion = $version
}
$url = "$mvnOssSnapshotRepo/com/hazelcast/hazelcast/$version/maven-metadata.xml"
Write-Output "GET1 $url"
$response = invoke-web-request $url $null $script:ossRepoRequestHeader
if ($response.StatusCode -eq 200) {
Write-Output "Server: found version $version on Maven, using this version"
return
}
Write-Output "Server: could not find version $version on Maven ($($response.StatusCode))"
$url2 = "$mvnOssSnapshotRepo/com/hazelcast/hazelcast/maven-metadata.xml"
Write-Output "GET2 $url2"
$response2 = invoke-web-request $url $null $script:ossRepoRequestHeader
if ($response2.StatusCode -ne 200) {
Die "Error: could not download metadata from Maven ($($response2.StatusCode))"
}
$metadata = [xml] $response2.Content
$version0 = $version
$version = $version.SubString(0, $version.Length - "-SNAPSHOT".Length)
$nodes = $metadata.SelectNodes("//version [starts-with(., '$version')]")
if ($nodes.Count -lt 1) {
Die "Server: could not find a version starting with '$version' on Maven"
}
foreach ($node in $nodes | sort-object -descending -property innerText) {
$nodeVersion = $node.innerText
if ($nodeVersion -eq $version0) { # we 404ed on that one already (why is it listed?!)
Write-Output "Server: skip listed version $nodeVersion"
continue
}
Write-Output "Server: try listed version $nodeVersion"
$url = "$mvnOssSnapshotRepo/com/hazelcast/hazelcast/$nodeVersion/maven-metadata.xml"
Write-Output "Maven: $url"
$response = invoke-web-request $url $null $script:ossRepoRequestHeader
if ($response.StatusCode -eq 200) {
Write-Output "Server: found version $nodeVersion on Maven, using this version"
$script:serverVersion = $nodeVersion
return;
}
else {
Write-Output "Server: could not find version $nodeVersion on Maven ($($response.StatusCode))"
}
}
Die "Server: could not find a version."
}
# get a Maven artifact
function download-maven-artifact ( $repoUrl, $group, $artifact, $jversion, $classifier, $dest ) {
$headers = $null
if ($jversion.EndsWith("-SNAPSHOT")) {
$url = "$repoUrl/$group/$artifact/$jversion/maven-metadata.xml"
if($repoUrl -eq $mvnOssSnapshotRepo){
$headers = $script:ossRepoRequestHeader
}
$response = invoke-web-request $url $null $headers
if ($response.StatusCode -ne 200) {
Die "Failed to download $url ($($response.StatusCode))"
}
try {
$metadata = [xml] $response.Content
}
catch {
Die "Invalid metadata content at $url."
}
$xpath = "//snapshotVersion [extension='jar'"
if (![System.String]::IsNullOrWhiteSpace($classifier)) {
$xpath += " and classifier='$classifier'"
}
else {
$xpath += " and not(classifier)"
}
$xpath += "]"
$node = $metadata.SelectNodes($xpath)[0]
if ($node -eq $null) {
Die "Incomplete metadata at $url."
}
$jarVersion = "-" + $node.value
}
else {
$jarVersion = "-" + $jversion
}
$url = "$repoUrl/$group/$artifact/$jversion/$artifact$jarVersion"
if (![System.String]::IsNullOrWhiteSpace($classifier)) {
$url += "-$classifier"
}
$url += ".jar"
$response = invoke-web-request $url $dest $headers
if ($response.StatusCode -ne 200) {
Die "Failed to download $url ($($response.StatusCode))"
}
}
# ensure we have a specified jar, by downloading it needed
# add the jar to the $script:options.classpath
function ensure-jar ( $jar, $repo, $artifact ) {
if(Test-Path "$libDir/$jar") {
Write-Output "Detected $jar"
} else {
Write-Output "Downloading $jar ..."
$parts = $artifact.Split(':')
$group = $parts[0].Replace('.', '/')
$art = $parts[1]
$ver = $parts[2]
$cls = $null
if ($parts.Length -eq 5 -and $parts[4] -eq "tests") {
$cls = "tests"
}
download-maven-artifact $repo $group $art $ver $cls "$libDir/$jar"
}
$s = ";"
if (-not $isWindows) { $s = ":" }
$classpath = $script:options.classpath
if (-not [System.String]::IsNullOrWhiteSpace($classpath)) { $classpath += $s }
$classpath += "$libDir/$jar"
# Be sure to quote the path to escape from white space
# where you call the $script:options.classpath
# ex: $quotedClassPath = '"{0}"' -f $script:options.classpath
$script:options.classpath = $classpath
}
function verify-server-files {
if (-not (test-path "$libDir")) { return $false }
if (-not (test-path "$libDir/hazelcast-remote-controller-${hzRCVersion}.jar")) { return $false }
if (-not (test-path "$libDir/hazelcast-${serverVersion}-tests.jar")) { return $false }
if ($options.enterprise) {
# ensure we have the hazelcast enterprise server
if (${serverVersion} -lt "5.0") { # FIXME version comparison
if (-not (test-path "$libDir/hazelcast-enterprise-all-${serverVersion}.jar")) { return $false }
}
else {
if (-not (test-path "$libDir/hazelcast-enterprise-${serverVersion}.jar")) { return $false }
if (-not (test-path "$libDir/hazelcast-sql-${serverVersion}.jar")) { return $false }
}
# ensure we have the hazelcast enterprise test jar
if (-not (test-path "$libDir/hazelcast-enterprise-${serverVersion}-tests.jar")) { return $false }
}
else {
# ensure we have the hazelcast server jar
if (${serverVersion} -lt "5.0") { # FIXME version comparison
if (-not (test-path "$libDir/hazelcast-all-${serverVersion}.jar")) { return $false }
}
else {
if (-not (test-path "$libDir/hazelcast-${serverVersion}.jar")) { return $false }
if (-not (test-path "$libDir/hazelcast-sql-${serverVersion}.jar")) { return $false }
}
}
# specified file not found
if (-not [string]::IsNullOrWhiteSpace($options.serverConfig) -and -not (test-path $options.serverConfig)) { return $false }
# defaults?
if (-not (test-path "$libDir/hazelcast-$($options.server).xml") -or
-not (test-path "$libDir/hazelcast-$serverVersion.xml")) { return $false }
# all clear
return $true
}
function Get-hazelcast-default-xml($refspec) {
$url = "https://api.github.com/repos/hazelcast/hazelcast-mono/contents/hazelcast/hazelcast/src/main/resources/hazelcast-default.xml?ref=$refspec"
$dest = "$libDir/hazelcast-$serverVersion.xml"
$token = get-github-token
$headers = @{ Authorization = "Token $token"; Accept = "application/vnd.github.v4.raw" }
$response = invoke-web-request $url $dest $headers
if ($response.StatusCode -ne 200) {
Write-Output "Failed to download hazelcast-default.xml ($($response.StatusCode)) from refspec $refspec"
if (test-path $dest) { remove-item $dest }
return $false
}
else {
Write-Output "Found hazelcast-default.xml from refspec $refspec"
return $true
}
}
function Get-hazelcast-default-xml-or-die($refspec) {
if (-not (Get-hazelcast-default-xml $refspec)) {
Die "Error: failed to download hazelcast-default.xml from refspec $refspec"
}
return $true
}
# ensures we have all jars & config required for the remote controller and the server,
# by downloading them if needed, and add them to the $script:options.classpath
function ensure-server-files {
Write-Output "Prepare server/rc..."
if (-not (test-path "$libDir")) { mkdir "$libDir" >$null }
# if we don't have all server files for the specified version,
# we're going to try and download things below, but beforehand
# let's determine which server version we *really* want
if (-not (verify-server-files)) { determine-server-version }
# ensure we have the remote controller + hazelcast test jar
ensure-jar "hazelcast-remote-controller-${hzRCVersion}.jar" $mvnOssPublicRepo "com.hazelcast:hazelcast-remote-controller:${hzRCVersion}"
ensure-jar "hazelcast-${serverVersion}-tests.jar" $mvnOssRepo "com.hazelcast:hazelcast:${serverVersion}:jar:tests"
if ($options.enterprise) {
# ensure we have the hazelcast enterprise server
if (${serverVersion} -lt "5.0") { # FIXME version comparison
ensure-jar "hazelcast-enterprise-all-${serverVersion}.jar" $mvnEntRepo "com.hazelcast:hazelcast-enterprise-all:${serverVersion}"
}
else {
ensure-jar "hazelcast-enterprise-${serverVersion}.jar" $mvnEntRepo "com.hazelcast:hazelcast-enterprise:${serverVersion}"
ensure-jar "hazelcast-sql-${serverVersion}.jar" $mvnOssRepo "com.hazelcast:hazelcast-sql:${serverVersion}"
}
}
else {
# ensure we have the hazelcast server jar
if (${serverVersion} -lt "5.0") { # FIXME version comparison
ensure-jar "hazelcast-all-${serverVersion}.jar" $mvnOssRepo "com.hazelcast:hazelcast-all:${serverVersion}"
}
else {
ensure-jar "hazelcast-${serverVersion}.jar" $mvnOssRepo "com.hazelcast:hazelcast:${serverVersion}"
ensure-jar "hazelcast-sql-${serverVersion}.jar" $mvnOssRepo "com.hazelcast:hazelcast-sql:${serverVersion}"
}
}
if (-not [string]::IsNullOrWhiteSpace($options.serverConfig)) {
# config was specified, it must exist
if (test-path $options.serverConfig) {
Write-Output "Detected $($options.serverConfig)"
}
else {
Die "Configuration file $($options.serverConfig) is missing."
}
}
elseif (test-path "$libDir/hazelcast-$($options.server).xml") {
# config was not specified, try with exact specified server version
Write-Output "Detected hazelcast-$($options.server).xml"
$options.serverConfig = "$libDir/hazelcast-$($options.server).xml"
}
elseif (test-path "$libDir/hazelcast-$serverVersion.xml") {
# config was not specified, try with detected server version
Write-Output "Detected hazelcast-$serverVersion.xml"
$options.serverConfig = "$libDir/hazelcast-$serverVersion.xml"
}
else {
# no config found, try to download
Write-Output "Downloading hazelcast-default.xml -> hazelcast-$serverVersion.xml..."
$found = $false
$v = $serverVersion.TrimEnd("-SNAPSHOT")
# special master case
if ($options.server -eq "master") {
$found = Get-hazelcast-default-xml-or-die "main"
}
# special beta case
if ($isBeta) {
$found = Get-hazelcast-default-xml-or-die $v
}
if (-not $found) {
# try tag eg 'v4.2.1' or 'v4.3'
$found = Get-hazelcast-default-xml "v$v"
}
if (-not $found) {
$p0 = $v.IndexOf('.')
$p1 = $v.LastIndexOf('.')
if ($p0 -ne $p1) {
$v = $v.SubString(0, $p1) # 4.2.1 -> 4.2 but 4.3 remains 4.3
}
# try branch eg '4.2.z' or '4.3.z'
$found = Get-hazelcast-default-xml "$v.z"
}
if (-not $found) {
# try branch eg '4.3' because '5.0' exists but not '5.0.z'
$found = Get-hazelcast-default-xml $v
}
if (-not $found) {
# are we the master branch version?
$r = @{}
get-master-server-version $r
if ($r.version -eq $serverVersion) {
Write-Output "Master branch is $($r.version), matches."
$found = Get-hazelcast-default-xml-or-die "master"
}
else {
Write-Output "Master branch is $($r.version), does not match $serverVersion."
}
}
if (-not $found) {
Die "Running out of options... failed to download hazelcast-default.xml."
}
$options.serverConfig = "$libDir/hazelcast-$serverVersion.xml"
}
}
# gets a dotnet sdk for a particular version
function get-dotnet-sdk ( $sdks, $v, $preview ) {
# trust dotnet to return the sdks ordered by version, so last is the highest version
# exclude versions containing "-" ie anything pre-release (TODO: why?)
$sdk = $sdks `
| Select-String -pattern "^$v" `
| Foreach-Object { $_.ToString().Split(' ')[0] } `
| Where-Object { ($preview -and $_.Contains('-')) -or (-not $preview -and -not $_.Contains('-')) } `
| Select-Object -last 1
if ($null -eq $sdk) { return "n/a" }
else { return $sdk.ToString() }
}
function require-dotnet-version ( $result, $sdks, $search, $frameworks, $framework, $name, $required, $allowPrerelease ) {
$release = get-dotnet-sdk $sdks $search $false
$preview = get-dotnet-sdk $sdks $search $true
$result.validSdk = $true
if ("n/a" -eq $release) {
if ($allowPrerelease -and "n/a" -ne $preview) {
# only have preview, and preview is allowed = will use preview
$result.ok = $true
$result.sdkInfo = $preview
}
else {
# have nothing usable, is an issue only if required
$missing = $frameworks.Contains($framework)
if ($missing -and $required) {
Write-Output " ERR: this script requires the Microsoft .NET $name SDK."
$result.validSdks = $false
$result.validSdk = $false
}
$result.sdkInfo = "${search}:n/a"
}
}
else {
if ("n/a" -eq $preview) {
# only have release = will use release
$result.sdkInfo = $release
}
elseif ($allowPrerelease) {
# have both, and preview is allowed = will use preview
$result.sdkInfo = $preview
}