forked from PowerShell/PSScriptAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.psm1
More file actions
316 lines (285 loc) · 10.6 KB
/
build.psm1
File metadata and controls
316 lines (285 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# Build module for PowerShell ScriptAnalyzer
$projectRoot = $PSScriptRoot
$destinationDir = Join-Path -Path $projectRoot -ChildPath (Join-Path -Path "out" -ChildPath "PSScriptAnalyzer")
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 ! $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 PSScriptAnalyzer) )
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 PSScriptAnalyzer) )
END {
if ( $PSCmdlet.ShouldProcess("$modulePath") ) {
Copy-Item -Recurse -Path "$destinationDir" -Destination "$ModulePath\." -Force
}
}
}
# if script analyzer is installed, remove it
function Uninstall-ScriptAnalyzer
{
[CmdletBinding(SupportsShouldProcess)]
param ( $ModulePath = $(Join-Path -Path (Get-UserModulePath) -ChildPath PSScriptAnalyzer) )
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("${destinationDir}")) {
if ( Test-Path ${destinationDir} ) {
Remove-Item -Force -Recurse ${destinationDir}
}
}
}
}
# Build documentation using platyPS
function Start-DocumentationBuild
{
$docsPath = Join-Path $projectRoot docs
$markdownDocsPath = Join-Path $docsPath markdown
$outputDocsPath = Join-Path $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
}
if (-not (Test-Path $markdownDocsPath))
{
throw "Cannot find markdown documentation folder."
}
Import-Module platyPS
if ( ! (Test-Path $outputDocsPath)) {
$null = New-Item -Type Directory -Path $outputDocsPath -Force
}
$null = New-ExternalHelp -Path $markdownDocsPath -OutputPath $outputDocsPath -Force
}
# this function checks to be sure that the version of dotnet is useable to build analyzer
function Assert-UsableDotNet
{
if ( ! (Get-Command dotnet)) {
throw "dotnet not found"
}
$globalJson = Get-Content (Join-Path $PSScriptRoot global.json)
[System.Version]$neededVersion = ($globalJson | ConvertFrom-Json).sdk.version
[System.Version]$dotnetVersion = try {
push-location $PSHOME
dotnet --version
}
catch {
throw "dotnet execution error"
}
finally {
pop-location
}
# we can't just do a simple check for the version
# see https://docs.microsoft.com/en-us/dotnet/core/tools/global-json
if ( $neededVersion.Major -ne $dotnetVersion.Major ) {
throw "Incorrect dotnet version: have '$dotnetVersion' need '$neededVersion'"
}
if ( $neededVersion.Minor -ne $dotnetVersion.Minor ) {
throw "Incorrect dotnet version: have '$dotnetVersion' need '$neededVersion'"
}
# Special logic for the build number
$neededBuildVersion = $neededVersion.Build / 100 -as [int]
$dotnetBuildVersion = $dotnetVersion.Build / 100 -as [int]
if ( $neededBuildVersion -ne $dotnetBuildVersion ) {
throw "Incorrect dotnet version: have '$dotnetVersion' need '$neededVersion'"
}
if ( $neededVersion.Build -gt $dotnetVersion.Build ) {
throw "Incorrect dotnet version: have '$dotnetVersion' need '$neededVersion'"
}
# if we haven't thrown yet, we have a dotnet we can use
return
}
# build script analyzer (and optionally build everything with -All)
function Start-ScriptAnalyzerBuild
{
[CmdletBinding(DefaultParameterSetName="BuildOne")]
param (
[switch]$All,
[ValidateRange(3, 6)]
[int]$PSVersion = $PSVersionTable.PSVersion.Major,
[ValidateSet("Debug", "Release")]
[string]$Configuration = "Debug",
[switch]$Documentation
)
BEGIN {
if ( ! $IsWindows ) {
Assert-UsableDotNet
}
}
END {
if ( $All )
{
# Build all the versions of the analyzer
foreach($psVersion in 3..6) {
Start-ScriptAnalyzerBuild -Configuration $Configuration -PSVersion $psVersion
}
return
}
$documentationFileExists = Test-Path (Join-Path $PSScriptRoot 'out\PSScriptAnalyzer\en-us\Microsoft.Windows.PowerShell.ScriptAnalyzer.dll-Help.xml')
# Build docs either when -Documentation switch is being specified or the first time in a clean repo
if ( $Documentation -or -not $documentationFileExists )
{
Start-DocumentationBuild
}
if ($PSVersion -ge 6) {
$framework = 'netstandard2.0'
}
else {
$framework = "net452"
}
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"
)
$destinationDir = "$projectRoot\out\PSScriptAnalyzer"
switch ($PSVersion)
{
3
{
$destinationDirBinaries = "$destinationDir\PSv3"
}
4
{
$destinationDirBinaries = "$destinationDir\PSv4"
}
5
{
$destinationDirBinaries = "$destinationDir"
}
6
{
$destinationDirBinaries = "$destinationDir\coreclr"
}
default
{
throw "Unsupported PSVersion: '$PSVersion'"
}
}
$config = "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
Write-Progress "Building ScriptAnalyzer for PSVersion '$PSVersion' using framework '$framework' and configuration '$Configuration'"
$buildOutput = dotnet build --framework $framework --configuration "$config"
if ( $LASTEXITCODE -ne 0 ) { throw "$buildOutput" }
}
catch {
Write-Error "Failure to build for PSVersion '$PSVersion' using framework '$framework' and configuration '$config'"
return
}
finally {
Pop-Location
}
Publish-File $itemsToCopyCommon $destinationDir
$itemsToCopyBinaries = @(
"$projectRoot\Engine\bin\${config}\${framework}\Microsoft.Windows.PowerShell.ScriptAnalyzer.dll",
"$projectRoot\Rules\bin\${config}\${framework}\Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.dll"
)
Publish-File $itemsToCopyBinaries $destinationDirBinaries
$settingsFiles = Get-Childitem "$projectRoot\Engine\Settings" | ForEach-Object -MemberName FullName
Publish-File $settingsFiles (Join-Path -Path $destinationDir -ChildPath Settings)
if ($framework -eq 'net452') {
Copy-Item -path "$projectRoot\Rules\bin\${config}\${framework}\Newtonsoft.Json.dll" -Destination $destinationDirBinaries
}
Pop-Location
}
}
# TEST HELPERS
# Run our tests
function Test-ScriptAnalyzer
{
[CmdletBinding()]
param ( [Parameter()][switch]$InProcess )
END {
$testModulePath = Join-Path "${projectRoot}" -ChildPath out
$testResultsFile = Join-Path ${projectRoot} -childPath TestResults.xml
$testScripts = "${projectRoot}\Tests\Engine,${projectRoot}\Tests\Rules,${projectRoot}\Tests\Documentation"
try {
$savedModulePath = $env:PSModulePath
$env:PSModulePath = "${testModulePath}{0}${env:PSModulePath}" -f [System.IO.Path]::PathSeparator
$scriptBlock = [scriptblock]::Create("Invoke-Pester -Path $testScripts -OutputFormat NUnitXml -OutputFile $testResultsFile -Show Describe,Summary")
if ( $InProcess ) {
& $scriptBlock
}
else {
$powershell = (Get-Process -id $PID).MainModule.FileName
& ${powershell} -Command $scriptBlock
}
}
finally {
$env:PSModulePath = $savedModulePath
}
}
}
# 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']")
}