forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.psm1
2366 lines (2007 loc) · 79.8 KB
/
build.psm1
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
# Use the .NET Core APIs to determine the current platform; if a runtime
# exception is thrown, we are on FullCLR, not .NET Core.
try {
$Runtime = [System.Runtime.InteropServices.RuntimeInformation]
$OSPlatform = [System.Runtime.InteropServices.OSPlatform]
$IsCoreCLR = $true
$IsLinux = $Runtime::IsOSPlatform($OSPlatform::Linux)
$IsOSX = $Runtime::IsOSPlatform($OSPlatform::OSX)
$IsWindows = $Runtime::IsOSPlatform($OSPlatform::Windows)
} catch {
# If these are already set, then they're read-only and we're done
try {
$IsCoreCLR = $false
$IsLinux = $false
$IsOSX = $false
$IsWindows = $true
}
catch { }
}
if ($IsLinux) {
$LinuxInfo = Get-Content /etc/os-release | ConvertFrom-StringData
$IsUbuntu = $LinuxInfo.ID -match 'ubuntu'
$IsUbuntu14 = $IsUbuntu -and $LinuxInfo.VERSION_ID -match '14.04'
$IsUbuntu16 = $IsUbuntu -and $LinuxInfo.VERSION_ID -match '16.04'
$IsCentOS = $LinuxInfo.ID -match 'centos' -and $LinuxInfo.VERSION_ID -match '7'
}
function Start-PSBuild {
[CmdletBinding(DefaultParameterSetName='CoreCLR')]
param(
# When specified this switch will stops running dev powershell
# to help avoid compilation error, because file are in use.
[switch]$StopDevPowerShell,
[switch]$NoPath,
[switch]$Restore,
[string]$Output,
[switch]$ResGen,
[switch]$TypeGen,
[switch]$Clean,
# this switch will re-build only System.Management.Automation.dll
# it's useful for development, to do a quick changes in the engine
[switch]$SMAOnly,
# These runtimes must match those in project.json
# We do not use ValidateScript since we want tab completion
[ValidateSet("ubuntu.14.04-x64",
"ubuntu.16.04-x64",
"debian.8-x64",
"centos.7-x64",
"win7-x64",
"win81-x64",
"win10-x64",
"osx.10.11-x64")]
[Parameter(ParameterSetName='CoreCLR')]
[string]$Runtime,
[Parameter(ParameterSetName='FullCLR', Mandatory=$true)]
[switch]$FullCLR,
[Parameter(ParameterSetName='FullCLR')]
[switch]$XamlGen,
[Parameter(ParameterSetName='FullCLR')]
[ValidateSet('x86', 'x64')] # TODO: At some point, we need to add ARM support to match CoreCLR
[string]$NativeHostArch = "x64",
[ValidateSet('Linux', 'Debug', 'Release', '')] # We might need "Checked" as well
[string]$Configuration,
[Parameter(ParameterSetName='CoreCLR')]
[switch]$Publish,
[Parameter(ParameterSetName='CoreCLR')]
[switch]$CrossGen
)
function Stop-DevPowerShell {
Get-Process powershell* |
Where-Object {
$_.Modules |
Where-Object {
$_.FileName -eq (Resolve-Path $script:Options.Output).Path
}
} |
Stop-Process -Verbose
}
if ($CrossGen -and !$Publish) {
# By specifying -CrossGen, we implicitly set -Publish to $true, if not already specified.
$Publish = $true
}
if ($Clean) {
log "Cleaning your working directory. You can also do it with 'git clean -fdX'"
Push-Location $PSScriptRoot
try {
git clean -fdX
} finally {
Pop-Location
}
}
# save Git description to file for PowerShell to include in PSVersionTable
git --git-dir="$PSScriptRoot/.git" describe --dirty --abbrev=60 > "$psscriptroot/powershell.version"
# simplify ParameterSetNames
if ($PSCmdlet.ParameterSetName -eq 'FullCLR') {
$FullCLR = $true
}
# Add .NET CLI tools to PATH
Find-Dotnet
# verify we have all tools in place to do the build
$precheck = precheck 'dotnet' "Build dependency 'dotnet' not found in PATH. Run Start-PSBootstrap. Also see: https://dotnet.github.io/getting-started/"
if ($IsWindows) {
# use custom package store - this value is also defined in nuget.config under config/repositoryPath
# dotnet restore uses this value as the target for installing the assemblies for referenced nuget packages.
# dotnet build does not currently consume the config value but will consume env:NUGET_PACKAGES to resolve these dependencies
$env:NUGET_PACKAGES="$PSScriptRoot\Packages"
# cmake is needed to build powershell.exe
$precheck = $precheck -and (precheck 'cmake' 'cmake not found. Run Start-PSBootstrap. You can also install it from https://chocolatey.org/packages/cmake')
Use-MSBuild
#mc.exe is Message Compiler for native resources
$mcexe = Get-ChildItem "${env:ProgramFiles(x86)}\Windows Kits\10\" -Recurse -Filter 'mc.exe' | ? {$_.FullName -match 'x64'} | select -First 1 | % {$_.FullName}
if (-not $mcexe) {
throw 'mc.exe not found. Run Start-PSBootstrap or install Microsoft Windows 10 SDK from https://developer.microsoft.com/en-US/windows/downloads/windows-10-sdk'
}
$vcVarsPath = (Get-Item(Join-Path -Path "$env:VS140COMNTOOLS" -ChildPath '../../vc')).FullName
if ((Test-Path -Path $vcVarsPath\vcvarsall.bat) -eq $false) {
throw "Could not find Visual Studio vcvarsall.bat at $vcVarsPath. Please ensure the optional feature 'Common Tools for Visual C++' is installed."
}
# setup msbuild configuration
if ($Configuration -eq 'Debug' -or $Configuration -eq 'Release') {
$msbuildConfiguration = $Configuration
} else {
$msbuildConfiguration = 'Release'
}
# setup cmakeGenerator
if ($NativeHostArch -eq 'x86') {
$cmakeGenerator = 'Visual Studio 14 2015'
} else {
$cmakeGenerator = 'Visual Studio 14 2015 Win64'
}
} elseif ($IsLinux -or $IsOSX) {
foreach ($Dependency in 'cmake', 'make', 'g++') {
$precheck = $precheck -and (precheck $Dependency "Build dependency '$Dependency' not found. Run Start-PSBootstrap.")
}
}
# Abort if any precheck failed
if (-not $precheck) {
return
}
# set output options
$OptionsArguments = @{
Publish=$Publish
Output=$Output
FullCLR=$FullCLR
Runtime=$Runtime
Configuration=$Configuration
Verbose=$true
SMAOnly=[bool]$SMAOnly
}
$script:Options = New-PSOptions @OptionsArguments
if ($StopDevPowerShell) {
Stop-DevPowerShell
}
# setup arguments
$Arguments = @()
if ($Publish -or $FullCLR) {
$Arguments += "publish"
} else {
$Arguments += "build"
}
if ($Output) {
$Arguments += "--output", (Join-Path $PSScriptRoot $Output)
}
elseif ($SMAOnly) {
$Arguments += "--output", (Split-Path $script:Options.Output)
}
$Arguments += "--configuration", $Options.Configuration
$Arguments += "--framework", $Options.Framework
$Arguments += "--runtime", $Options.Runtime
# handle Restore
if ($Restore -or -not (Test-Path "$($Options.Top)/project.lock.json")) {
log "Run dotnet restore"
$RestoreArguments = @("--verbosity")
if ($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent) {
$RestoreArguments += "Info"
} else {
$RestoreArguments += "Warning"
}
$RestoreArguments += "$PSScriptRoot"
Start-NativeExecution { dotnet restore $RestoreArguments }
# .NET Core's crypto library needs brew's OpenSSL libraries added to its rpath
if ($IsOSX) {
# This is the restored library used to build
# This is allowed to fail since the user may have already restored
Write-Warning ".NET Core links the incorrect OpenSSL, correcting NuGet package libraries..."
find $env:HOME/.nuget -name System.Security.Cryptography.Native.dylib | xargs sudo install_name_tool -add_rpath /usr/local/opt/openssl/lib
}
}
# handle ResGen
# Heuristic to run ResGen on the fresh machine
if ($ResGen -or -not (Test-Path "$PSScriptRoot/src/Microsoft.PowerShell.ConsoleHost/gen")) {
log "Run ResGen (generating C# bindings for resx files)"
Start-ResGen
}
# handle xaml files
# Heuristic to resolve xaml on the fresh machine
if ($FullCLR -and ($XamlGen -or -not (Test-Path "$PSScriptRoot/src/Microsoft.PowerShell.Activities/gen/*.g.cs"))) {
log "Run XamlGen (generating .g.cs and .resources for .xaml files)"
Start-XamlGen -MSBuildConfiguration $msbuildConfiguration
}
# Build native components
if (($IsLinux -or $IsOSX) -and -not $SMAOnly) {
$Ext = if ($IsLinux) {
"so"
} elseif ($IsOSX) {
"dylib"
}
$Native = "$PSScriptRoot/src/libpsl-native"
$Lib = "$($Options.Top)/libpsl-native.$Ext"
log "Start building $Lib"
try {
Push-Location $Native
Start-NativeExecution { cmake -DCMAKE_BUILD_TYPE=Debug . }
Start-NativeExecution { make -j }
Start-NativeExecution { ctest --verbose }
} finally {
Pop-Location
}
if (-not (Test-Path $Lib)) {
throw "Compilation of $Lib failed"
}
} elseif ($IsWindows -and (-not $SMAOnly)) {
log "Start building native Windows binaries"
try {
Push-Location "$PSScriptRoot\src\powershell-native"
# Compile native resources
$currentLocation = Get-Location
@("nativemsh/pwrshplugin") | % {
$nativeResourcesFolder = $_
Get-ChildItem $nativeResourcesFolder -Filter "*.mc" | % {
$command = @"
cmd.exe /C cd /d "$currentLocation" "&" "$($vcVarsPath)\vcvarsall.bat" "$NativeHostArch" "&" mc.exe -o -d -c -U "$($_.FullName)" -h "$nativeResourcesFolder" -r "$nativeResourcesFolder"
"@
log " Executing mc.exe Command: $command"
Start-NativeExecution { Invoke-Expression -Command:$command 2>&1 }
}
}
function Build-NativeWindowsBinaries {
param(
# Describes wither it should build the CoreCLR or FullCLR version
[ValidateSet("ON", "OFF")]
[string]$OneCoreValue,
# Array of file names to copy from the local build directory to the packaging directory
[string[]]$FilesToCopy
)
# Disabling until I figure out if it is necessary
# $overrideFlags = "-DCMAKE_USER_MAKE_RULES_OVERRIDE=$PSScriptRoot\src\powershell-native\windows-compiler-override.txt"
$overrideFlags = ""
$location = Get-Location
$command = @"
cmd.exe /C cd /d "$location" "&" "$($vcVarsPath)\vcvarsall.bat" "$NativeHostArch" "&" cmake "$overrideFlags" -DBUILD_ONECORE=$OneCoreValue -G "$cmakeGenerator" . "&" msbuild ALL_BUILD.vcxproj "/p:Configuration=$msbuildConfiguration"
"@
log " Executing Build Command: $command"
Start-NativeExecution { Invoke-Expression -Command:$command }
$clrTarget = "FullClr"
if ($OneCoreValue -eq "ON")
{
$clrTarget = "CoreClr"
}
# Copy the binaries from the local build directory to the packaging directory
$dstPath = ($script:Options).Top
$FilesToCopy | % {
$srcPath = Join-Path (Join-Path (Join-Path (Get-Location) "bin") $msbuildConfiguration) "$clrTarget/$_"
log " Copying $srcPath to $dstPath"
Copy-Item $srcPath $dstPath
}
}
if ($FullCLR) {
$fullBinaries = @(
'powershell.exe',
'powershell.pdb',
'pwrshplugin.dll',
'pwrshplugin.pdb'
)
Build-NativeWindowsBinaries "OFF" $fullBinaries
}
else
{
$coreClrBinaries = @(
'pwrshplugin.dll',
'pwrshplugin.pdb'
)
Build-NativeWindowsBinaries "ON" $coreClrBinaries
# Place the remoting configuration script in the same directory
# as the binary so it will get published.
Copy-Item .\Install-PowerShellRemoting.ps1 ($script:Options).Top
}
} finally {
Pop-Location
}
}
# handle TypeGen
if ($TypeGen -or -not (Test-Path "$PSScriptRoot/src/Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/CorePsTypeCatalog.cs")) {
log "Run TypeGen (generating CorePsTypeCatalog.cs)"
Start-TypeGen
}
try {
# Relative paths do not work well if cwd is not changed to project
Push-Location $Options.Top
log "Run dotnet $Arguments from $pwd"
Start-NativeExecution { dotnet $Arguments }
if ($CrossGen) {
$publishPath = Split-Path $Options.Output
Start-CrossGen -PublishPath $publishPath
log "PowerShell.exe with ngen binaries is available at: $($Options.Output)"
} else {
log "PowerShell output: $($Options.Output)"
}
} finally {
Pop-Location
}
}
function New-PSOptions {
[CmdletBinding()]
param(
[ValidateSet("Linux", "Debug", "Release", "")]
[string]$Configuration,
[ValidateSet("netcoreapp1.0", "net451")]
[string]$Framework,
# These are duplicated from Start-PSBuild
# We do not use ValidateScript since we want tab completion
[ValidateSet("",
"ubuntu.14.04-x64",
"ubuntu.16.04-x64",
"debian.8-x64",
"centos.7-x64",
"win7-x64",
"win81-x64",
"win10-x64",
"osx.10.11-x64")]
[string]$Runtime,
[switch]$Publish,
[string]$Output,
[switch]$FullCLR,
[switch]$SMAOnly
)
# Add .NET CLI tools to PATH
Find-Dotnet
if (-not $Configuration) {
$Configuration = if ($IsLinux -or $IsOSX) {
"Linux"
} elseif ($IsWindows) {
"Debug"
}
Write-Verbose "Using configuration '$Configuration'"
}
if ($FullCLR) {
$Top = "$PSScriptRoot/src/powershell-win-full"
} else {
if ($Configuration -eq 'Linux')
{
$Top = "$PSScriptRoot/src/powershell-unix"
}
else
{
$Top = "$PSScriptRoot/src/powershell-win-core"
}
}
Write-Verbose "Top project directory is $Top"
if (-not $Framework) {
$Framework = if ($FullCLR) {
"net451"
} else {
"netcoreapp1.0"
}
Write-Verbose "Using framework '$Framework'"
}
if (-not $Runtime) {
$Runtime = dotnet --info | % {
if ($_ -match "RID") {
$_ -split "\s+" | Select-Object -Last 1
}
}
if (-not $Runtime) {
Throw "Could not determine Runtime Identifier, please update dotnet"
} else {
Write-Verbose "Using runtime '$Runtime'"
}
}
$Executable = if ($IsLinux -or $IsOSX) {
"powershell"
} elseif ($IsWindows) {
"powershell.exe"
}
# Build the Output path
if ($Output) {
$Output = Join-Path $PSScriptRoot $Output
} else {
$Output = [IO.Path]::Combine($Top, "bin", $Configuration, $Framework, $Runtime)
# Publish injects the publish directory
if ($Publish -or $FullCLR) {
$Output = [IO.Path]::Combine($Output, "publish")
}
$Output = [IO.Path]::Combine($Output, $Executable)
}
$RealFramework = $Framework
if ($SMAOnly)
{
$Top = "$PSScriptRoot/src/System.Management.Automation"
if ($Framework -match 'netcoreapp')
{
$RealFramework = 'netstandard1.6'
}
}
return @{ Top = $Top;
Configuration = $Configuration;
Framework = $RealFramework;
Runtime = $Runtime;
Output = $Output }
}
function Get-PSOutput {
[CmdletBinding()]param(
[hashtable]$Options
)
if ($Options) {
return $Options.Output
} elseif ($script:Options) {
return $script:Options.Output
} else {
return (New-PSOptions).Output
}
}
function Get-PesterTag {
param ( [Parameter(Position=0)][string]$testbase = "$PSScriptRoot/test/powershell" )
$alltags = @{}
$warnings = @()
get-childitem -Recurse $testbase -File |?{$_.name -match "tests.ps1"}| %{
$fullname = $_.fullname
$tok = $err = $null
$ast = [System.Management.Automation.Language.Parser]::ParseFile($FullName, [ref]$tok,[ref]$err)
$des = $ast.FindAll({$args[0] -is "System.Management.Automation.Language.CommandAst" -and $args[0].CommandElements[0].Value -eq "Describe"},$true)
foreach( $describe in $des) {
$elements = $describe.CommandElements
$lineno = $elements[0].Extent.StartLineNumber
$foundTag = $false
for ( $i = 0; $i -lt $elements.Count; $i++) {
if ( $elements[$i].extent.text -match "^-t" ) {
$vAst = $elements[$i+1]
if ( $vAst.FindAll({$args[0] -is "System.Management.Automation.Language.VariableExpressionAst"},$true) ) {
$warnings += "TAGS must be static strings, error in ${fullname}, line $lineno"
}
$values = $vAst.FindAll({$args[0] -is "System.Management.Automation.Language.StringConstantExpressionAst"},$true).Value
$values | %{
if ( $_ -notmatch "CI|FEATURE|SCENARIO|SLOW" ) {
$warnings += "${fullname} includes improper tag '$_', line '$lineno'"
}
$alltags[$_]++
}
$foundTag = $true
}
}
if ( ! $foundTag ) {
$warnings += "${fullname} does not include -Tag in Describe, line '$lineno'"
}
}
}
if ( $Warnings.Count -gt 0 ) {
$alltags['Result'] = "Fail"
}
else {
$alltags['Result'] = "Pass"
}
$alltags['Warnings'] = $warnings
$o = [pscustomobject]$alltags
$o.psobject.TypeNames.Add("DescribeTagsInUse")
$o
}
function Start-PSPester {
[CmdletBinding()]
param(
[string]$OutputFormat = "NUnitXml",
[string]$OutputFile = "pester-tests.xml",
[string[]]$ExcludeTag = "Slow",
[string[]]$Tag = "CI",
[string]$Path = "$PSScriptRoot/test/powershell",
[switch]$ThrowOnFailure,
[switch]$FullCLR,
[string]$binDir = (Split-Path (New-PSOptions -FullCLR:$FullCLR).Output),
[string]$powershell = (Join-Path $binDir 'powershell'),
[string]$Pester = ([IO.Path]::Combine($binDir, "Modules", "Pester"))
)
Write-Verbose "Running pester tests at '$path' with tag '$($Tag -join ''', ''')' and ExcludeTag '$($ExcludeTag -join ''', ''')'" -Verbose
# All concatenated commands/arguments are suffixed with the delimiter (space)
$Command = ""
# Windows needs the execution policy adjusted
if ($IsWindows) {
$Command += "Set-ExecutionPolicy -Scope Process Unrestricted; "
}
$startParams = @{binDir=$binDir}
if(!$FullCLR)
{
$Command += "Import-Module '$Pester'; "
}
$Command += "Invoke-Pester "
$Command += "-OutputFormat ${OutputFormat} -OutputFile ${OutputFile} "
if ($ExcludeTag -and ($ExcludeTag -ne "")) {
$Command += "-ExcludeTag @('" + (${ExcludeTag} -join "','") + "') "
}
if ($Tag) {
$Command += "-Tag @('" + (${Tag} -join "','") + "') "
}
$Command += "'" + $Path + "'"
Write-Verbose $Command
# To ensure proper testing, the module path must not be inherited by the spawned process
if($FullCLR)
{
Start-DevPowerShell -binDir $binDir -FullCLR -NoNewWindow -ArgumentList '-noprofile', '-noninteractive' -Command $command
}
else {
try {
$originalModulePath = $env:PSMODULEPATH
& $powershell -noprofile -c $Command
} finally {
$env:PSMODULEPATH = $originalModulePath
}
}
if($ThrowOnFailure)
{
Test-PSPesterResults -TestResultsFile $OutputFile
}
}
#
# Read the test result file and
# Throw if a test failed
function Test-PSPesterResults
{
param(
[string]$TestResultsFile = "pester-tests.xml",
[string] $TestArea = 'test/powershell'
)
if(!(Test-Path $TestResultsFile))
{
throw "Test result file '$testResultsFile' not found for $TestArea."
}
$x = [xml](Get-Content -raw $testResultsFile)
if ([int]$x.'test-results'.failures -gt 0)
{
throw "$($x.'test-results'.failures) tests in $TestArea failed"
}
}
function Start-PSxUnit {
[CmdletBinding()]param()
log "xUnit tests are currently disabled pending fixes due to API and AssemblyLoadContext changes - @andschwa"
return
if ($IsWindows) {
throw "xUnit tests are only currently supported on Linux / OS X"
}
if ($IsOSX) {
log "Not yet supported on OS X, pretending they passed..."
return
}
# Add .NET CLI tools to PATH
Find-Dotnet
$Arguments = "--configuration", "Linux", "-parallel", "none"
if ($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent) {
$Arguments += "-verbose"
}
$Content = Split-Path -Parent (Get-PSOutput)
if (-not (Test-Path $Content)) {
throw "PowerShell must be built before running tests!"
}
try {
Push-Location $PSScriptRoot/test/csharp
# Path manipulation to obtain test project output directory
$Output = Join-Path $pwd ((Split-Path -Parent (Get-PSOutput)) -replace (New-PSOptions).Top)
Write-Verbose "Output is $Output"
Copy-Item -ErrorAction SilentlyContinue -Recurse -Path $Content/* -Include Modules,libpsl-native* -Destination $Output
Start-NativeExecution { dotnet test $Arguments }
if ($LASTEXITCODE -ne 0) {
throw "$LASTEXITCODE xUnit tests failed"
}
} finally {
Pop-Location
}
}
function Install-Dotnet {
[CmdletBinding()]
param(
[string]$Channel = "rel-1.0.0",
[string]$Version = "latest",
[switch]$NoSudo
)
# This allows sudo install to be optional; needed when running in containers / as root
# Note that when it is null, Invoke-Expression (but not &) must be used to interpolate properly
$sudo = if (!$NoSudo) { "sudo" }
$obtainUrl = "https://raw.githubusercontent.com/dotnet/cli/rel/1.0.0/scripts/obtain"
# Install for Linux and OS X
if ($IsLinux -or $IsOSX) {
# Uninstall all previous dotnet packages
$uninstallScript = if ($IsUbuntu) {
"dotnet-uninstall-debian-packages.sh"
} elseif ($IsOSX) {
"dotnet-uninstall-pkgs.sh"
}
if ($uninstallScript) {
Start-NativeExecution {
curl -sO $obtainUrl/uninstall/$uninstallScript
Invoke-Expression "$sudo bash ./$uninstallScript"
}
} else {
Write-Warning "This script only removes prior versions of dotnet for Ubuntu 14.04 and OS X"
}
# Install new dotnet 1.0.0 preview packages
$installScript = "dotnet-install.sh"
Start-NativeExecution {
curl -sO $obtainUrl/$installScript
bash ./$installScript -c $Channel -v $Version
}
# .NET Core's crypto library needs brew's OpenSSL libraries added to its rpath
if ($IsOSX) {
# This is the library shipped with .NET Core
# This is allowed to fail as the user may have installed other versions of dotnet
Write-Warning ".NET Core links the incorrect OpenSSL, correcting .NET CLI libraries..."
find $env:HOME/.dotnet -name System.Security.Cryptography.Native.dylib | xargs sudo install_name_tool -add_rpath /usr/local/opt/openssl/lib
}
} elseif ($IsWindows) {
Remove-Item -ErrorAction SilentlyContinue -Recurse -Force ~\AppData\Local\Microsoft\dotnet
$installScript = "dotnet-install.ps1"
Invoke-WebRequest -Uri $obtainUrl/$installScript -OutFile $installScript
& ./$installScript -c $Channel -v $Version
}
}
function Start-PSBootstrap {
[CmdletBinding(
SupportsShouldProcess=$true,
ConfirmImpact="High")]
param(
[string]$Channel = "rel-1.0.0",
[string]$Version = "latest",
[switch]$Package,
[switch]$NoSudo,
[switch]$Force
)
log "Installing PowerShell build dependencies"
Push-Location $PSScriptRoot/tools
# This allows sudo install to be optional; needed when running in containers / as root
# Note that when it is null, Invoke-Expression (but not &) must be used to interpolate properly
$sudo = if (!$NoSudo) { "sudo" }
try {
# Update googletest submodule for linux native cmake
if ($IsLinux -or $IsOSX) {
try {
Push-Location $PSScriptRoot
$Submodule = "$PSScriptRoot/src/libpsl-native/test/googletest"
Remove-Item -Path $Submodule -Recurse -Force -ErrorAction SilentlyContinue
git submodule update --init -- $submodule
} finally {
Pop-Location
}
}
# Install ours and .NET's dependencies
$Deps = @()
if ($IsUbuntu) {
# Build tools
$Deps += "curl", "g++", "cmake", "make"
# .NET Core required runtime libraries
$Deps += "libunwind8"
if ($IsUbuntu14) { $Deps += "libicu52" }
elseif ($IsUbuntu16) { $Deps += "libicu55" }
# Packaging tools
if ($Package) { $Deps += "ruby-dev", "groff" }
# Install dependencies
Start-NativeExecution {
Invoke-Expression "$sudo apt-get update"
Invoke-Expression "$sudo apt-get install -y -qq $Deps"
}
} elseif ($IsCentOS) {
# Build tools
$Deps += "which", "curl", "gcc-c++", "cmake", "make"
# .NET Core required runtime libraries
$Deps += "libicu", "libunwind"
# Packaging tools
if ($Package) { $Deps += "ruby-devel", "rpm-build", "groff" }
# Install dependencies
Start-NativeExecution {
Invoke-Expression "$sudo yum install -y -q $Deps"
}
} elseif ($IsOSX) {
precheck 'brew' "Bootstrap dependency 'brew' not found, must install Homebrew! See http://brew.sh/"
# Build tools
$Deps += "curl", "cmake"
# .NET Core required runtime libraries
$Deps += "openssl"
# Install dependencies
Start-NativeExecution { brew install $Deps }
}
# Install [fpm](https://github.com/jordansissel/fpm) and [ronn](https://github.com/rtomayko/ronn)
if ($Package) {
try {
# We cannot guess if the user wants to run gem install as root
Start-NativeExecution { gem install fpm ronn }
} catch {
Write-Warning "Installation of fpm and ronn gems failed! Must resolve manually."
}
}
$DotnetArguments = @{ Channel=$Channel; Version=$Version; NoSudo=$NoSudo }
Install-Dotnet @DotnetArguments
# Install for Windows
if ($IsWindows) {
$machinePath = [Environment]::GetEnvironmentVariable('Path', 'MACHINE')
$newMachineEnvironmentPath = $machinePath
$cmakePresent = precheck 'cmake' $null
$win10sdkBinPath = "${env:ProgramFiles(x86)}\Windows Kits\10\bin\x64"
$sdkPresent = Test-Path -Path $win10sdkBinPath -PathType Container
# Install chocolatey
$chocolateyPath = "$env:AllUsersProfile\chocolatey\bin"
if(precheck 'choco' $null) {
log "Chocolatey is already installed. Skipping installation."
}
elseif(($cmakePresent -eq $false) -or ($sdkPresent -eq $false)) {
log "Chocolatey not present. Installing chocolatey."
if ($Force -or $PSCmdlet.ShouldProcess("Install chocolatey via https://chocolatey.org/install.ps1")) {
Invoke-Expression ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))
if (-not ($machinePath.ToLower().Contains($chocolateyPath.ToLower()))) {
log "Adding $chocolateyPath to Path environment variable"
$env:Path += ";$chocolateyPath"
$newMachineEnvironmentPath += ";$chocolateyPath"
} else {
log "$chocolateyPath already present in Path environment variable"
}
} else {
Write-Error "Chocolatey is required to install missing dependencies. Please install it from https://chocolatey.org/ manually. Alternatively, install cmake and Windows 10 SDK."
return $null
}
} else {
log "Skipping installation of chocolatey, cause both cmake and Win 10 SDK are present."
}
# Install cmake
$cmakePath = "${env:ProgramFiles}\CMake\bin"
if($cmakePresent) {
log "Cmake is already installed. Skipping installation."
} else {
log "Cmake not present. Installing cmake."
choco install cmake -y --version 3.6.0
if (-not ($machinePath.ToLower().Contains($cmakePath.ToLower()))) {
log "Adding $cmakePath to Path environment variable"
$env:Path += ";$cmakePath"
$newMachineEnvironmentPath = "$cmakePath;$newMachineEnvironmentPath"
} else {
log "$cmakePath already present in Path environment variable"
}
}
# Install Windows 10 SDK
$packageName = "windows-sdk-10.0"
if (-not $sdkPresent) {
log "Windows 10 SDK not present. Installing $packageName."
choco install windows-sdk-10.0 -y
} else {
log "Windows 10 SDK present. Skipping installation."
}
# Update path machine environment variable
if ($newMachineEnvironmentPath -ne $machinePath) {
log "Updating Path machine environment variable"
if ($Force -or $PSCmdlet.ShouldProcess("Update Path machine environment variable to $newMachineEnvironmentPath")) {
[Environment]::SetEnvironmentVariable('Path', $newMachineEnvironmentPath, 'MACHINE')
}
}
}
} finally {
Pop-Location
}
}
function Start-PSPackage {
[CmdletBinding()]param(
# PowerShell packages use Semantic Versioning http://semver.org/
[string]$Version,
# Package name
[ValidatePattern("^powershell")]
[string]$Name = "powershell",
# Ubuntu, CentOS, and OS X, and Windows packages are supported
[ValidateSet("deb", "osxpkg", "rpm", "msi", "appx")]
[string[]]$Types
)
# Use Git tag if not given a version
if (-not $Version) {
$Version = (git --git-dir="$PSScriptRoot/.git" describe) -Replace '^v'
}
if ($IsWindows) {
Write-Warning "Please ensure you have previously run Start-PSBuild -Clean -CrossGen -Configuration Release!"
$Source = Split-Path -Parent (Get-PSOutput -Options (New-PSOptions -Publish -Configuration Release))
} else {
Write-Warning "Please ensure you have previously run Start-PSBuild -Clean -CrossGen!"
$Source = Split-Path -Parent (Get-PSOutput -Options (New-PSOptions -Publish))
}
Write-Verbose "Packaging $Source"
# Decide package output type
if (-not $Types) {
$Types = if ($IsLinux) {
if ($LinuxInfo.ID -match "ubuntu") {
"deb"
} elseif ($LinuxInfo.ID -match "centos") {
"rpm"
} else {
throw "Building packages for $($LinuxInfo.PRETTY_NAME) is unsupported!"
}
} elseif ($IsOSX) {
"osxpkg"
} elseif ($IsWindows) {
"msi", "appx"
}
Write-Warning "-Types was not specified, continuing with $Types!"
}
switch ($Types) {
"msi" {
$Arguments = @{
ProductSourcePath = $Source;
ProductVersion = $Version;
AssetsPath = "$PSScriptRoot\assets";
LicenseFilePath = "$PSScriptRoot\assets\license.rtf";
# Product Guid needs to be unique for every PowerShell version to allow SxS install
ProductGuid = [Guid]::NewGuid()
}
New-MSIPackage @Arguments
}
"appx" {
$Arguments = @{
PackageVersion = $Version;
SourcePath = $Source;
AssetsPath = "$PSScriptRoot\assets"
}
New-AppxPackage @Arguments
}
default {
$Arguments = @{
Type = $_;
Source = $Source;
Name = $Name;
Version = $Version
}
New-UnixPackage @Arguments
}
}
}
function New-UnixPackage {
[CmdletBinding()]param(
[Parameter(Mandatory)]
[ValidateSet("deb", "osxpkg", "rpm")]
[string]$Type,
[Parameter(Mandatory)]
[string]$Source,
# Must start with 'powershell' but may have any suffix
[Parameter(Mandatory)]
[ValidatePattern("^powershell")]
[string]$Name,
[Parameter(Mandatory)]
[string]$Version,