forked from PowerShell/PSScriptAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.psm1
833 lines (764 loc) · 29.3 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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# Build module for PowerShell ScriptAnalyzer
$projectRoot = $PSScriptRoot
$analyzerName = "PSScriptAnalyzer"
function Get-AnalyzerVersion
{
$csprojPath = [io.path]::Combine($projectRoot,"Engine","Engine.csproj")
$xml = [xml](Get-Content "${csprojPath}")
$xml.SelectSingleNode(".//VersionPrefix")."#text"
}
$analyzerVersion = Get-AnalyzerVersion
# location where analyzer goes
$script:destinationDir = [io.path]::Combine($projectRoot,"out","${analyzerName}", $analyzerVersion)
function Publish-File
{
param ([string[]]$itemsToCopy, [string]$destination)
if (-not (Test-Path $destination))
{
$null = New-Item -ItemType Directory $destination -Force
}
foreach ($file in $itemsToCopy)
{
Copy-Item -Path $file -Destination (Join-Path $destination (Split-Path $file -Leaf)) -Force
}
}
# attempt to get the users module directory
function Get-UserModulePath
{
if ( $IsCoreCLR -and -not $IsWindows )
{
$platformType = "System.Management.Automation.Platform" -as [Type]
if ( $platformType ) {
${platformType}::SelectProductNameForDirectory("USER_MODULES")
}
else {
throw "Could not determine users module path"
}
}
else {
"${HOME}/Documents/WindowsPowerShell/Modules"
}
}
function Uninstall-ScriptAnalyzer
{
[CmdletBinding(SupportsShouldProcess)]
param ( $ModulePath = $(Join-Path -Path (Get-UserModulePath) -ChildPath ${analyzerName}) )
END {
if ( $PSCmdlet.ShouldProcess("$modulePath") ) {
Remove-Item -Recurse -Path "$ModulePath" -Force
}
}
}
# install script analyzer, by default into the users module path
function Install-ScriptAnalyzer
{
[CmdletBinding(SupportsShouldProcess)]
param ( $ModulePath = $(Join-Path -Path (Get-UserModulePath) -ChildPath ${analyzerName}) )
END {
if ( $PSCmdlet.ShouldProcess("$modulePath") ) {
Copy-Item -Recurse -Path "$script:destinationDir" -Destination "$ModulePath\." -Force
}
}
}
# if script analyzer is installed, remove it
function Uninstall-ScriptAnalyzer
{
[CmdletBinding(SupportsShouldProcess)]
param ( $ModulePath = $(Join-Path -Path (Get-UserModulePath) -ChildPath ${analyzerName}) )
END {
if ((Test-Path $ModulePath) -and (Get-Item $ModulePath).PSIsContainer )
{
Remove-Item -Force -Recurse $ModulePath
}
}
}
# Clean up the build location
function Remove-Build
{
[CmdletBinding(SupportsShouldProcess=$true)]
param ()
END {
if ( $PSCmdlet.ShouldProcess("${script:destinationDir}")) {
if ( Test-Path ${script:destinationDir} ) {
Remove-Item -Force -Recurse ${script:destinationDir}
}
}
}
}
# Build documentation using platyPS
function Start-DocumentationBuild
{
$docsPath = Join-Path $projectRoot docs
$markdownDocsPath = Join-Path $docsPath Cmdlets
$outputDocsPath = Join-Path $script:destinationDir en-US
$platyPS = Get-Module -ListAvailable platyPS
if ($null -eq $platyPS -or ($platyPS | Sort-Object Version -Descending | Select-Object -First 1).Version -lt [version]0.12)
{
Write-Verbose -verbose "platyPS module not found or below required version of 0.12, installing the latest version."
Install-Module -Force -Name platyPS -Scope CurrentUser -Repository PSGallery
}
if (-not (Test-Path $markdownDocsPath))
{
throw "Cannot find markdown documentation folder."
}
Import-Module platyPS -Verbose:$false
if ( -not (Test-Path $outputDocsPath)) {
$null = New-Item -Type Directory -Path $outputDocsPath -Force
}
$null = New-ExternalHelp -Path $markdownDocsPath -OutputPath $outputDocsPath -Force
}
function Copy-CompatibilityProfiles
{
if ($PSVersionTable.PSVersion.Major -le 5)
{
Add-Type -AssemblyName 'System.IO.Compression.FileSystem'
}
$profileDir = [System.IO.Path]::Combine($PSScriptRoot, 'PSCompatibilityCollector', 'profiles')
$targetProfileDir = [io.path]::Combine($script:destinationDir,"compatibility_profiles")
if ( -not (Test-Path $targetProfileDir) ) {
$null = New-Item -Type Directory $targetProfileDir
}
Copy-Item -Force $profileDir/* $targetProfileDir
}
# build script analyzer (and optionally build everything with -All)
function Start-ScriptAnalyzerBuild
{
[CmdletBinding(DefaultParameterSetName="BuildOne")]
param (
[switch]$All,
[ValidateSet(3, 4, 5, 7)]
[int]$PSVersion = $PSVersionTable.PSVersion.Major,
[ValidateSet("Debug", "Release")]
[string]$Configuration = "Debug",
[switch]$Documentation,
[switch]$Catalog
)
BEGIN {
# don't allow the build to be started unless we have the proper Cli version
# this will not actually install dotnet if it's already present, but it will
# install the proper version
Install-Dotnet
if ( -not (Test-SuitableDotnet) ) {
$requiredVersion = $script:wantedVersion
$foundVersion = Get-InstalledCLIVersion
Write-Warning "No suitable dotnet CLI found, requires version '$requiredVersion' found only '$foundVersion'"
}
$verboseWanted = $false
if ( $PSBoundParameters['Verbose'] ) {
$verboseWanted = $PSBoundParameters['Verbose'].ToBool()
}
}
END {
# Build docs either when -Documentation switch is being specified or the first time in a clean repo
$documentationFileExists = Test-Path (Join-Path $PSScriptRoot 'out\PSScriptAnalyzer\en-us\Microsoft.Windows.PowerShell.ScriptAnalyzer.dll-Help.xml')
if ( $Documentation -or -not $documentationFileExists )
{
Write-Verbose -Verbose:$verboseWanted -Message "Start-DocumentationBuild"
Start-DocumentationBuild -Verbose:$verboseWanted
}
if ( $All )
{
# Build all the versions of the analyzer
foreach ($psVersion in 3, 4, 5, 7) {
Write-Verbose -Verbose -Message "Configuration: $Configuration PSVersion: $psVersion"
Start-ScriptAnalyzerBuild -Configuration $Configuration -PSVersion $psVersion -Verbose:$verboseWanted
}
if ( $Catalog ) {
New-Catalog -Location $script:destinationDir
}
return
}
if (-not $profilesCopied)
{
Write-Verbose -Verbose:$verboseWanted -Message "Copy-CompatibilityProfiles"
Copy-CompatibilityProfiles
# Set the variable in the caller's scope, so this will only happen once
Set-Variable -Name profilesCopied -Value $true -Scope 1
}
$framework = 'net462'
if ($PSVersion -eq 7) {
$framework = 'net6'
}
# build the appropriate assembly
if ($PSVersion -match "[34]" -and $Framework -ne "net462")
{
throw ("ScriptAnalyzer for PS version '{0}' is not applicable to {1} framework" -f $PSVersion,$Framework)
}
Push-Location -Path $projectRoot
if (-not (Test-Path "$projectRoot/global.json"))
{
throw "Not in solution root"
}
$itemsToCopyCommon = @(
"$projectRoot\Engine\PSScriptAnalyzer.psd1", "$projectRoot\Engine\PSScriptAnalyzer.psm1",
"$projectRoot\Engine\ScriptAnalyzer.format.ps1xml", "$projectRoot\Engine\ScriptAnalyzer.types.ps1xml"
)
switch ($PSVersion)
{
3
{
$destinationDirBinaries = "$script:destinationDir\PSv3"
}
4
{
$destinationDirBinaries = "$script:destinationDir\PSv4"
}
5
{
$destinationDirBinaries = "$script:destinationDir"
}
7
{
$destinationDirBinaries = "$script:destinationDir\PSv7"
}
default
{
throw "Unsupported PSVersion: '$PSVersion'"
}
}
$buildConfiguration = $Configuration
if ((3, 4, 7) -contains $PSVersion) {
$buildConfiguration = "PSV${PSVersion}${Configuration}"
}
# Build ScriptAnalyzer
# The Rules project has a dependency on the Engine therefore just building the Rules project is enough
try {
Push-Location $projectRoot/Rules
$message = "Building ScriptAnalyzer for PSVersion '$PSVersion' using framework '$framework' and configuration '$Configuration'"
Write-Verbose -Verbose:$verboseWanted -Message "$message"
Write-Progress "$message"
if ( -not $script:DotnetExe ) {
$script:DotnetExe = Get-DotnetExe
}
$dotnetArgs = "build",
"--framework",
$framework,
"--configuration",
"$buildConfiguration"
if ( $env:TF_BUILD ) {
$dotnetArgs += "--output"
$dotnetArgs += "${PSScriptRoot}\bin\${buildConfiguration}\${framework}"
}
$buildOutput = & $script:DotnetExe $dotnetArgs 2>&1
if ( $LASTEXITCODE -ne 0 ) {
Write-Verbose -Verbose -Message "dotnet is $(${script:DotnetExe}.Source)"
$dotnetArgs | Foreach-Object {"dotnetArg: $_"} | Write-Verbose -Verbose
Get-PSCallStack | Write-Verbose -Verbose
throw $buildOutput
}
Write-Verbose -Verbose:$verboseWanted -message "$buildOutput"
}
catch {
$_.TargetObject | Write-Warning
Write-Error "Failure to build for PSVersion '$PSVersion' using framework '$framework' and configuration '$buildConfiguration'"
throw
}
finally {
Pop-Location
}
Publish-File $itemsToCopyCommon $script:destinationDir
if ( $env:TF_BUILD ) {
$itemsToCopyBinaries = @(
"$projectRoot\bin\${buildConfiguration}\${Framework}\Microsoft.Windows.PowerShell.ScriptAnalyzer.dll",
"$projectRoot\bin\${buildConfiguration}\${Framework}\Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.dll"
"$projectRoot\bin\${buildConfiguration}\${framework}\Microsoft.PowerShell.CrossCompatibility.dll"
)
}
else {
$itemsToCopyBinaries = @(
"$projectRoot\Engine\bin\${buildConfiguration}\${Framework}\Microsoft.Windows.PowerShell.ScriptAnalyzer.dll",
"$projectRoot\Rules\bin\${buildConfiguration}\${Framework}\Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.dll"
"$projectRoot\Rules\bin\${buildConfiguration}\${framework}\Microsoft.PowerShell.CrossCompatibility.dll"
)
}
if ($Configuration -eq 'Debug') {
$itemsToCopyBinaries += @(
"$projectRoot\Engine\bin\${buildConfiguration}\${Framework}\Microsoft.Windows.PowerShell.ScriptAnalyzer.pdb",
"$projectRoot\Rules\bin\${buildConfiguration}\${Framework}\Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.pdb"
"$projectRoot\Rules\bin\${buildConfiguration}\${framework}\Microsoft.PowerShell.CrossCompatibility.pdb"
)
}
Publish-File $itemsToCopyBinaries $destinationDirBinaries
$settingsFiles = Get-Childitem "$projectRoot\Engine\Settings" | ForEach-Object -MemberName FullName
Publish-File $settingsFiles (Join-Path -Path $script:destinationDir -ChildPath Settings)
$rulesProjectOutputDir = if ($env:TF_BUILD) {
"$projectRoot\bin\${buildConfiguration}\${framework}" }
else {
"$projectRoot\Rules\bin\${buildConfiguration}\${framework}"
}
if ($framework -eq 'net462') {
$nsoft = Join-Path $rulesProjectOutputDir 'Newtonsoft.Json.dll'
Copy-Item -path $nsoft -Destination $destinationDirBinaries
}
else {
Copy-Item -Path (Join-Path $rulesProjectOutputDir 'Pluralize.NET.dll') -Destination $destinationDirBinaries
}
Pop-Location
}
}
function New-Catalog
{
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseCompatibleCommands', '')]
param ( [Parameter()]$Location )
$newFileCatalog = Get-Command -ErrorAction SilentlyContinue New-FileCatalog
if ($null -eq $newFileCatalog) {
Write-Warning "New-FileCatalog not found, not creating catalog"
return
}
try {
Push-Location $Location
New-FileCatalog -CatalogFilePath PSScriptAnalyzer.cat -Path .
}
finally {
Pop-Location
}
}
# TEST HELPERS
# Run our tests
function Test-ScriptAnalyzer
{
[CmdletBinding()]
param ( [switch] $InProcess )
END {
# versions 3 and 4 don't understand versioned module paths, so we need to rename the directory of the version to
# the module name, and then set the ModulePath to that
#
# the layout of the build location is
# .../out
# /PSScriptAnalyzer
# /1.18.0
# /<modulefiles live here>
# and ".../out" is added to env:PSModulePath
# on v3 and v4, it will be
# .../out
# /PSScriptAnalyzer
# /PSScriptAnalyzer
# /<modulefiles live here>
# and ".../out/PSScriptAnalyzer" is added to env:PSModulePath
#
#
$major = $PSVersionTable.PSVersion.Major
if ( $major -lt 5 ) {
# get the directory name of the destination, we need to change it
$versionDirectoryRoot = Split-Path $script:destinationDir
$testModulePath = Join-Path $versionDirectoryRoot $analyzerName
}
else {
$testModulePath = Join-Path "${projectRoot}" -ChildPath out
}
$testResultsFile = "'$(Join-Path ${projectRoot} -childPath TestResults.xml)'"
$testScripts = "'${projectRoot}\Tests\Build','${projectRoot}\Tests\Engine','${projectRoot}\Tests\Rules','${projectRoot}\Tests\Documentation'"
try {
if ( $major -lt 5 ) {
Rename-Item $script:destinationDir ${testModulePath}
}
$savedModulePath = $env:PSModulePath
$env:PSModulePath = "${testModulePath}{0}${env:PSModulePath}" -f [System.IO.Path]::PathSeparator
$analyzerPsd1Path = Join-Path -Path $script:destinationDir -ChildPath "$analyzerName.psd1"
$scriptBlock = [scriptblock]::Create("Import-Module '$analyzerPsd1Path'; Invoke-Pester -Path $testScripts")
if ( $InProcess ) {
& $scriptBlock
}
else {
$powershell = (Get-Process -id $PID).MainModule.FileName
& ${powershell} -Command $scriptBlock
}
}
finally {
$env:PSModulePath = $savedModulePath
if ( $major -lt 5 ) {
Rename-Item ${testModulePath} ${script:destinationDir}
}
}
}
}
# a simple function to make it easier to retrieve the test results
function Get-TestResults
{
param ( $logfile = (Join-Path -Path ${projectRoot} -ChildPath TestResults.xml) )
$logPath = (Resolve-Path $logfile).Path
$results = [xml](Get-Content $logPath)
$results.SelectNodes(".//test-case")
}
# a simple function to make it easier to retrieve the failures
# it's not a filter of the results of Get-TestResults because this is faster
function Get-TestFailures
{
param ( $logfile = (Join-Path -Path ${projectRoot} -ChildPath testResults.xml) )
$logPath = (Resolve-Path $logfile).Path
$results = [xml](Get-Content $logPath)
$results.SelectNodes(".//test-case[@result='Failure']")
}
# BOOTSTRAPPING CODE FOR INSTALLING DOTNET
# install dotnet cli tools based on the version mentioned in global.json
function Install-Dotnet
{
[CmdletBinding(SupportsShouldProcess=$true)]
param (
[Parameter()][Switch]$Force,
[Parameter()]$version = $( Get-GlobalJsonSdkVersion -Raw )
)
if ( Test-DotnetInstallation -requestedversion $version ) {
if ( $Force ) {
Write-Verbose -Verbose "Installing again"
}
else {
return
}
}
try {
Push-Location $PSScriptRoot
$installScriptPath = Receive-DotnetInstallScript
$installScriptName = [System.IO.Path]::GetFileName($installScriptPath)
If ( $PSCmdlet.ShouldProcess("$installScriptName for $version")) {
& "${installScriptPath}" -c release -version $version -SkipNonVersionedFiles
}
# this may be the first time that dotnet has been installed,
# set up the executable variable
if ( -not $script:DotnetExe ) {
$script:DotnetExe = Get-DotnetExe
}
}
catch {
throw $_
}
finally {
if ( Test-Path $installScriptPath ) {
Remove-Item $installScriptPath
}
Pop-Location
}
}
function Get-GlobalJsonSdkVersion {
param ( [switch]$Raw )
$json = Get-Content -raw (Join-Path $PSScriptRoot global.json) | ConvertFrom-Json
$version = $json.sdk.Version
if ( $Raw ) {
return $version
}
else {
ConvertTo-PortableVersion $version
}
}
# we don't have semantic version in earlier versions of PowerShell, so we need to
# create something that we can use
function ConvertTo-PortableVersion {
param ( [string[]]$strVersion )
if ( -not $strVersion ) {
return (ConvertTo-PortableVersion "0.0.0-0")
}
foreach ( $v in $strVersion ) {
$ver, $pre = $v.split("-",2)
try {
[int]$major, [int]$minor, [int]$patch, $unused = $ver.Split(".",4)
if ( -not $pre ) {
$pre = $unused
}
}
catch {
Write-Warning "Cannot convert '$v' to portable version"
continue
}
$h = @{
Major = $major
Minor = $minor
Patch = $patch
}
if ( $pre ) {
$h['PrereleaseLabel'] = $pre
}
else {
$h['PrereleaseLabel'] = [String]::Empty
}
$customObject = [pscustomobject]$h
# we do this so we can get an approximate sort, since this implements a pseudo-version
# type in script, we need a way to find the highest version of dotnet, it's not a great solution
# but it will work in most cases.
Add-Member -inputobject $customObject -Type ScriptMethod -Name ToString -Force -Value {
$str = "{0:0000}.{1:0000}.{2:0000}" -f $this.Major,$this.Minor,$this.Patch
if ( $this.PrereleaseLabel ) {
$str += "-{0}" -f $this.PrereleaseLabel
}
return $str
}
Add-Member -inputobject $customObject -Type ScriptMethod -Name IsContainedIn -Value {
param ( [object[]]$collection )
foreach ( $object in $collection ) {
if (
$this.Major -eq $object.Major -and
$this.Minor -eq $object.Minor -and
$this.Patch -eq $object.Patch -and
$this.PrereleaseLabel -eq $object.PrereleaseLabel
) {
return $true
}
}
return $false
}
$customObject
}
}
# see https://learn.microsoft.com/dotnet/core/tools/global-json for rules
# on how version checks are done
function Test-SuitableDotnet {
param (
$availableVersions = $( Get-InstalledCliVersion),
$requiredVersion = $script:wantedVersion
)
if ( $requiredVersion -is [String] -or $requiredVersion -is [Version] ) {
$requiredVersion = ConvertTo-PortableVersion "$requiredVersion"
}
$availableVersionList = $availableVersions | ForEach-Object { if ( $_ -is [string] -or $_ -is [version] ) { ConvertTo-PortableVersion $_ } else { $_ } }
$availableVersions = $availableVersionList
# if we have what was requested, we can use it
if ( $RequiredVersion.IsContainedIn($availableVersions)) {
return $true
}
# if we had found a match, we would have returned $true above
# exact match required for 2.1.100 through 2.1.201
if ( $RequiredVersion.Major -eq 2 -and $RequiredVersion.Minor -eq 1 -and $RequiredVersion.Patch -ge 100 -and $RequiredVersion.Patch -le 201 ) {
return $false
}
# we need to check each available version for something that's useable
foreach ( $version in $availableVersions ) {
# major/minor numbers don't match - keep looking
if ( $version.Major -ne $requiredVersion.Major -or $version.Minor -ne $requiredVersion.Minor ) {
continue
}
$requiredPatch = $requiredVersion.Patch
$possiblePatch = $version.Patch
if ( $requiredPatch -gt $possiblePatch ) {
continue
}
if ( [math]::Abs(($requiredPatch - $possiblePatch)) -lt 100 ) {
return $true
}
}
return $false
}
# these are mockable functions for testing
function Get-InstalledCLIVersion {
# dotnet might not have been installed _ever_, so just return 0.0.0.0
if ( -not $script:DotnetExe ) {
Write-Warning "Dotnet executable not found"
return (ConvertTo-PortableVersion 0.0.0)
}
try {
# earlier versions of dotnet do not support --list-sdks, so we'll check the output
# and use dotnet --version as a fallback
$sdkList = & $script:DotnetExe --list-sdks 2>&1
if ( $sdkList -match "Unknown option" ) {
$installedVersions = & $script:DotnetExe --version 2>$null
}
else {
$installedVersions = $sdkList | Foreach-Object { $_.Split()[0] }
}
}
catch {
Write-Verbose -Verbose "$_"
$installedVersions = & $script:DotnetExe --version 2>$null
}
return (ConvertTo-PortableVersion $installedVersions)
}
function Test-DotnetInstallation
{
param (
$requestedVersion = $script:wantedVersion,
$installedVersions = $( Get-InstalledCLIVersion )
)
return (Test-SuitableDotnet -availableVersions $installedVersions -requiredVersion $requestedVersion )
}
function Receive-File {
param ( [Parameter(Mandatory,Position=0)]$uri )
# enable Tls12 for the request
# -SslProtocol parameter for Invoke-WebRequest wasn't in PSv3
$securityProtocol = [System.Net.ServicePointManager]::SecurityProtocol
$tls12 = [System.Net.SecurityProtocolType]::Tls12
try {
if ( ([System.Net.ServicePointManager]::SecurityProtocol -band $tls12) -eq 0 ) {
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor $tls12
}
$null = Invoke-WebRequest -Uri ${uri} -OutFile "${installScriptName}"
}
finally {
[System.Net.ServicePointManager]::SecurityProtocol = $securityProtocol
}
if ( (Test-Path Variable:IsWindows) -and -not $IsWindows ) {
chmod +x $installScriptName
}
$installScript = Get-Item $installScriptName -ErrorAction Stop
if ( -not $installScript ) {
throw "Download failure of ${uri}"
}
return $installScript
}
function Receive-DotnetInstallScript
{
# param '$platform' is a hook to enable forcing download of a specific
# install script, generally it should not be used except in testing.
param ( $platform = "" )
# if $platform has been set, it has priority
# if it's not set to Windows or NonWindows, it will be ignored
if ( $platform -eq "Windows" ) {
$installScriptName = "dotnet-install.ps1"
}
elseif ( $platform -eq "NonWindows" ) {
$installScriptName = "dotnet-install.sh"
}
elseif ( ((Test-Path Variable:IsWindows) -and -not $IsWindows) ) {
# if the variable IsWindows exists and it is set to false
$installScriptName = "dotnet-install.sh"
}
else { # the default case - we're running on a Windows system
$installScriptName = "dotnet-install.ps1"
}
$uri = "https://dot.net/v1/${installScriptName}"
$installScript = Receive-File -Uri $uri
return $installScript.FullName
}
function Get-DotnetExe
{
param ( $version = $script:wantedVersion )
$discoveredDotnet = Get-Command -CommandType Application dotnet -ErrorAction SilentlyContinue -All
if ( $discoveredDotnet ) {
# it's possible that there are multiples. Take the highest version we find
# the problem is that invoking dotnet on a version which is lower than the specified
# version in global.json will produce an error, so we can only take the dotnet which executes
#
# dotnet --version has changed its output, so we have to work much harder to determine what's installed.
# dotnet --version can now emit a list of installed sdks as output *and* an error if the global.json
# file points to a version of the sdk which is *not* installed. However, the format of the new list
# with --version has a couple of spaces at the beginning of the line, so we need to be resilient
# against that.
$properDotnet = $discoveredDotNet |
Where-Object {
& $_ --list-sdks |
Where-Object {
$_ -match $version
}
} |
Select-Object -Last 1
if ( $properDotnet ) {
$script:DotnetExe = $properDotnet
return $properDotnet
}
}
# it's not in the path, try harder to find it by checking some usual places
if ( ! (test-path variable:IsWindows) -or $IsWindows ) {
$dotnetHuntPath = "$HOME\AppData\Local\Microsoft\dotnet\dotnet.exe"
Write-Verbose -Verbose "checking Windows $dotnetHuntPath"
if ( test-path $dotnetHuntPath ) {
$script:DotnetExe = $dotnetHuntPath
return $dotnetHuntPath
}
}
else {
$dotnetHuntPath = "$HOME/.dotnet/dotnet"
Write-Verbose -Verbose "checking non-Windows $dotnetHuntPath"
if ( test-path $dotnetHuntPath ) {
$script:DotnetExe = $dotnetHuntPath
return $dotnetHuntPath
}
}
Write-Warning "Could not find dotnet executable"
return [String]::Empty
}
try {
# The version we want based on the global.JSON file
# suck this before getting the dotnet exe
$script:wantedVersion = Get-GlobalJsonSdkVersion -Raw
$script:DotnetExe = Get-DotnetExe
}
catch {
Write-Warning "Could not find dotnet executable"
}
# Copies the built PSCompatibilityCollector module to the output destination for PSSA
function Copy-CrossCompatibilityModule
{
param(
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]
$Destination
)
$destInfo = Get-Item -Path $Destination -ErrorAction SilentlyContinue
# Can't copy to a file
if ($destInfo -and -not $destInfo.PSIsContainer)
{
throw "Destination exists but is not a directory"
}
# Create the destination if it does not exist
if (-not $destInfo)
{
New-Item -Path $Destination -ItemType Directory
}
$compatCollectorModuleName = 'PSCompatibilityCollector'
$outputAssets = @(
"$PSScriptRoot/$compatCollectorModuleName/$compatCollectorModuleName.psd1"
"$PSScriptRoot/$compatCollectorModuleName/$compatCollectorModuleName.psm1"
"$PSScriptRoot/$compatCollectorModuleName/CrossCompatibilityBinary"
"$PSScriptRoot/$compatCollectorModuleName/profiles"
)
foreach ($assetPath in $outputAssets)
{
try
{
Copy-Item -Path $assetPath -Destination $Destination -Recurse -Force -ErrorAction Stop
}
catch
{
# Display the problem as a warning, but continue
Write-Warning $_
}
}
}
# copy the manifest into the module if is present
function Copy-Manifest
{
param ( [switch]$signed )
if ( $signed ) {
$buildRoot = "signed"
}
else {
$buildRoot = "out"
}
$analyzerVersion = Get-AnalyzerVersion
# location where analyzer goes
# debugging
(Get-ChildItem -File -Recurse)|ForEach-Object {Write-Verbose -Verbose -Message $_}
$modBaseDir = [io.path]::Combine($projectRoot,${buildRoot},"${analyzerName}", $analyzerVersion)
# copy the manifest files
Push-Location $buildRoot
if ( Test-Path _manifest ) {
Copy-Item -Recurse -Path _manifest -Destination $modBaseDir -Verbose
}
else {
Write-Warning -Message "_manifest not found in $PWD"
}
Pop-Location
}
# creates the nuget package which can be used for publishing to the gallery
function Start-CreatePackage
{
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseCompatibleCommands', '')]
param ( [switch]$signed )
try {
if ( $signed ) {
$buildRoot = "signed"
}
else {
$buildRoot = "out"
}
$repoName = [guid]::NewGuid().ToString()
$nupkgDir = Join-Path $PSScriptRoot $buildRoot
$null = Register-PSRepository -Name $repoName -InstallationPolicy Trusted -SourceLocation $nupkgDir
Push-Location $nupkgDir
Publish-Module -Path $PWD/PSScriptAnalyzer -Repository $repoName
}
finally {
Pop-Location
Unregister-PSRepository -Name $repoName
}
}