forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.psm1
2046 lines (1735 loc) · 67.1 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' -and $LinuxInfo.VERSION_ID -match '14.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.Mangement.Automation.dll
# it's useful for development, to do a quick changes in the engine
[switch]$SMAOnly,
[Parameter(ParameterSetName='CoreCLR')]
[switch]$Publish,
# These runtimes must match those in project.json
# We do not use ValidateScript since we want tab completion
[ValidateSet("ubuntu.14.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
)
function Stop-DevPowerShell
{
Get-Process powershell* |
Where-Object {
$_.Modules |
Where-Object {
$_.FileName -eq (Resolve-Path $script:Options.Output).Path
}
} |
Stop-Process -Verbose
}
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
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"
}
# 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 ($FullCLR) {
# 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.portable')
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
}
# 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 }
}
# 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 ($FullCLR -and (-not $SMAOnly)) {
log "Start building native powershell.exe"
try {
Push-Location "$PSScriptRoot\src\powershell-native"
# Compile native resources
@("nativemsh/pwrshplugin") | % {
$nativeResourcesFolder = $_
Get-ChildItem $nativeResourcesFolder -Filter "*.mc" | % {
$command = @"
cmd.exe /C cd /d "(Get-Location)" "&" "$($vcVarsPath)\vcvarsall.bat" "$NativeHostArch" "&" "$mcexe" -o -d -c -U $($_.FullName) -h "$nativeResourcesFolder" -r "$nativeResourcesFolder"
"@
log " Executing mc.exe Command: $command"
Start-NativeExecution { Invoke-Expression -Command:$command }
}
}
# 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
#
# BUILD_ONECORE
#
$BuildOneCoreValues = @("ON","OFF")
foreach ($oneCoreValue in $BuildOneCoreValues)
{
$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 }
}
# Copy the executable binary from the local build directory to the expected destination to enable Start-DevPowerShell to work
#
# TODO: This should be updated to handle per-architecture builds gracefully.
$dstPath = ($script:Options).Top
@(
'powershell.exe',
'powershell.pdb',
'pwrshplugin.dll',
'pwrshplugin.pdb'
) | % {
$srcPath = Join-Path (Join-Path (Join-Path (Get-Location) "bin") $msbuildConfiguration) "FullCLR/$_"
log " Copying $srcPath to $dstPath"
Copy-Item $srcPath $dstPath
}
} 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 }
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",
"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",
[switch]$DisableExit,
[string[]]$ExcludeTag = "Slow",
[string[]]$Tag = "CI",
[string]$Path = "$PSScriptRoot/test/powershell"
)
$tagString = "-outputFormat ${OutputFormat} -outputFile ${outputFile} "
if ( ! $DisableExit ) { $tagString += " -EnableExit" }
if ( $ExcludeTag -and ($ExcludeTag -ne "")) { $tagString += " -ExcludeTag @('" + (${ExcludeTag} -join "','") + "')" }
if ( $Tag ) { $tagString += " -Tag @('" + (${Tag} -join "','") + "')" }
$powershell = get-psoutput
$psdir = [io.path]::GetDirectoryName($powershell)
$moduleDir = [io.path]::Combine($psdir,"Modules","Pester")
Write-Verbose "Import-Module '$moduleDir'; Invoke-Pester $tagString $Path"
$powershellexe = get-psoutput
& $powershell -noprofile -c "Import-Module '$moduleDir'; Invoke-Pester $tagString $Path"
if ($LASTEXITCODE -ne 0) {
throw "$LASTEXITCODE Pester tests 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 Start-PSBootstrap {
[CmdletBinding(
SupportsShouldProcess=$true,
ConfirmImpact="High")]
param(
[ValidateSet("dev", "beta", "preview")]
[string]$Channel = "rel-1.0.0",
[string]$Version = "latest",
[switch]$Package,
[switch]$Force
)
log "Installing Open PowerShell build dependencies"
Push-Location $PSScriptRoot/tools
try {
# Install ours and .NET's dependencies
$Deps = @()
if ($IsUbuntu) {
# Build tools
$Deps += "curl", "g++", "cmake", "make"
# .NET Core required runtime libraries
$Deps += "libicu52", "libunwind8"
# Packaging tools
if ($Package) { $Deps += "ruby-dev" }
# Install dependencies
sudo apt-get install -y -qq $Deps
} elseif ($IsCentos) {
# Build tools
$Deps += "curl", "gcc-c++", "cmake", "make"
# .NET Core required runtime libraries
$Deps += "libicu", "libunwind"
# Packaging tools
if ($Package) { $Deps += "ruby-devel", "rpmbuild" }
# Install dependencies
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"
# Packaging tools
if ($Package) { $Deps += "ruby" }
# Install dependencies
brew install $Deps
# OpenSSL libraries must be updated
brew link --force openssl
}
# Install [fpm](https://github.com/jordansissel/fpm)
if ($Package) {
gem install fpm
}
$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) {
curl -s $obtainUrl/uninstall/$uninstallScript -o $uninstallScript
chmod +x $uninstallScript
sudo ./$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"
curl -s $obtainUrl/$installScript -o $installScript
chmod +x $installScript
bash ./$installScript -c $Channel -v $Version
}
# Install for Windows
if ($IsWindows -and -not $IsCoreCLR) {
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
$machinePath = [Environment]::GetEnvironmentVariable('Path', 'MACHINE')
$newMachineEnvironmentPath = $machinePath
$cmakePresent = precheck 'cmake' $null
$sdkPath = "${env:ProgramFiles(x86)}\Microsoft SDKs\Windows\v10.0A"
$sdkPresent = Test-Path -Path $sdkPath -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(x86)}\CMake\bin"
if($cmakePresent)
{
log "Cmake is already installed. Skipping installation."
}
else
{
log "Cmake not present. Installing cmake."
choco install cmake.portable -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')
}
}
} elseif ($IsWindows) {
Write-Warning "Start-PSBootstrap cannot be run in Core PowerShell on Windows (need Invoke-WebRequest!)"
}
} finally {
Pop-Location
}
}
function Start-PSPackage {
[CmdletBinding()]param(
# PowerShell packages use Semantic Versioning http://semver.org/
[string]$Version,
# Package iteration version (rarely changed)
[int]$Iteration = 1,
# Ubuntu, CentOS, and OS X packages are supported
[ValidateSet("deb", "osxpkg", "rpm")]
[string]$Type
)
$Description = @"
Open PowerShell on .NET Core
PowerShell is an open-source, cross-platform, scripting language and rich object shell.
Built upon .NET Core, it is also a C# REPL.
"@
# Use Git tag if not given a version
if (-not $Version) {
$Version = (git --git-dir="$PSScriptRoot/.git" describe) -Replace '^v'
}
$Source = Split-Path -Parent (Get-PSOutput -Options (New-PSOptions -Publish))
Write-Verbose "Packaging $Source"
if ($IsWindows) {
# Product Guid needs to be unique for every PowerShell version to allow SxS install
$productGuid = [guid]::NewGuid()
$msiPackagePath = New-MSIPackage -ProductSourcePath $Source -ProductVersion $Version -AssetsPath "$PSScriptRoot\Assets" -ProductGuid $productGuid
$appxPackagePath = New-AppxPackage -PackageVersion $Version -SourcePath $Source -AssetsPath "$PSScriptRoot\Assets"
$packages = @($msiPackagePath, $appxPackagePath)
return $packages
}
if (-not (Get-Command "fpm" -ErrorAction SilentlyContinue)) {
throw "Build dependency 'fpm' not found in PATH! See: https://github.com/jordansissel/fpm"
}
# Decide package output type
if (-not $Type) {
$Type = 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'
}
Write-Warning "-Type was not specified, continuing with $Type"
}
# Follow the Filesystem Hierarchy Standard for Linux and OS X
$Destination = if ($IsLinux) {
"/opt/microsoft/powershell"
} elseif ($IsOSX) {
"/usr/local/microsoft/powershell"
}
# Destination for symlink to powershell executable
$Link = if ($IsLinux) {
"/usr/bin"
} elseif ($IsOSX) {
"/usr/local/bin"
}
New-Item -Force -ItemType SymbolicLink -Path /tmp/powershell -Target $Destination/powershell >$null
# there is a weired bug in fpm
# if the target of the powershell symlink exists, `fpm` aborts
# with a `utime` error on OS X.
# so we move it to make symlink broken
$symlink_dest = "$Destination/powershell"
$hack_dest = "./_fpm_symlink_hack_powershell"
if ($IsOSX)
{
if (Test-Path $symlink_dest)
{
Write-Warning "Move $symlink_dest to $hack_dest (fpm utime bug)"
Move-Item $symlink_dest $hack_dest
}
}
# Change permissions for packaging
chmod -R go=u $Source /tmp/powershell
$libunwind = switch ($Type) {
"deb" { "libunwind8" }
"rpm" { "libunwind" }
}
$libicu = switch ($Type) {
"deb" { "libicu52" }
"rpm" { "libicu" }
}
$Arguments = @(
"--force", "--verbose",
"--name", "powershell",
"--version", $Version,
"--iteration", $Iteration,
"--maintainer", "Andrew Schwartzmeyer <[email protected]>",
"--vendor", "Microsoft <[email protected]>",
"--url", "https://github.com/PowerShell/PowerShell",
"--license", "Unlicensed",
"--description", $Description,
"--category", "shells",
"--rpm-os", "linux",
"--depends", $libunwind,
"--depends", $libicu,
"--deb-build-depends", "dotnet",
"--deb-build-depends", "cmake",
"--deb-build-depends", "g++",
"-t", $Type,
"-s", "dir",
"$Source/=$Destination/",
"/tmp/powershell=$Link"
)
# Build package
fpm $Arguments
if ($IsOSX)
{
# this is continuation of a fpm hack for a weired bug
if (Test-Path $hack_dest)
{
Write-Warning "Move $hack_dest to $symlink_dest (fpm utime bug)"
Move-Item $hack_dest $symlink_dest
}
}
}
function Publish-NuGetFeed
{
param(
[string]$OutputPath = "$PSScriptRoot/nuget-artifacts",
[Parameter(Mandatory=$true)]
[string]$VersionSuffix
)
# Add .NET CLI tools to PATH
Find-Dotnet
@(
'Microsoft.PowerShell.Commands.Management',
'Microsoft.PowerShell.Commands.Utility',
'Microsoft.PowerShell.ConsoleHost',
'Microsoft.PowerShell.Security',
'System.Management.Automation',
'Microsoft.PowerShell.CoreCLR.AssemblyLoadContext',
'Microsoft.PowerShell.CoreCLR.Eventing',
'Microsoft.PowerShell.SDK'
) | % {
if ($VersionSuffix)
{
dotnet pack "src/$_" --output $OutputPath --version-suffix $VersionSuffix
}
else
{
dotnet pack "src/$_" --output $OutputPath
}
}
}
function Start-DevPowerShell {
param(
[switch]$FullCLR,
[switch]$ZapDisable,
[string[]]$ArgumentList = '',
[switch]$LoadProfile,
[string]$binDir = (Split-Path (New-PSOptions -FullCLR:$FullCLR).Output),
[switch]$NoNewWindow,
[string]$Command,
[switch]$KeepPSModulePath
)
try {
if ((-not $NoNewWindow) -and ($IsCoreCLR))
{
Write-Warning "Start-DevPowerShell -NoNewWindow is currently implied in PowerShellCore edition https://github.com/PowerShell/PowerShell/issues/1543"
$NoNewWindow = $true
}
if (-not $LoadProfile) {
$ArgumentList = @('-noprofile') + $ArgumentList
}
if (-not $KeepPSModulePath)
{
if (-not $Command)
{
$ArgumentList = @('-NoExit') + $ArgumentList
}
$Command = '$env:PSMODULEPATH = Join-Path $env:DEVPATH Modules; ' + $Command
}
if ($Command)
{
$ArgumentList = $ArgumentList + @("-command $Command")
}
$env:DEVPATH = $binDir
if ($ZapDisable) {
$env:COMPLUS_ZapDisable = 1
}
if ($FullCLR -and (-not (Test-Path $binDir\powershell.exe.config))) {
$configContents = @"
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<developmentMode developerInstallation="true"/>
</runtime>
</configuration>
"@
$configContents | Out-File -Encoding Ascii $binDir\powershell.exe.config
}