Skip to content

Commit 6c946c0

Browse files
authored
Merge pull request #1247 from JamesWTruher/VersionBuild
Change build script to install into a versioned directory
2 parents 64b77fb + 41a2ea1 commit 6c946c0

6 files changed

+103
-47
lines changed

Tests/Engine/ModuleDependencyHandler.tests.ps1

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Describe "Resolve DSC Resource Dependency" {
88
$skipTest = $true
99
return
1010
}
11-
$SavedPSModulePath = $env:PSModulePath
11+
$savedPSModulePath = $env:PSModulePath
1212
$violationFileName = 'MissingDSCResource.ps1'
1313
$violationFilePath = Join-Path $directory $violationFileName
1414
$testRootDirectory = Split-Path -Parent $directory
@@ -27,15 +27,19 @@ Describe "Resolve DSC Resource Dependency" {
2727
}
2828
AfterAll {
2929
if ( $skipTest ) { return }
30-
$env:PSModulePath = $SavedPSModulePath
30+
$env:PSModulePath = $savedPSModulePath
3131
}
3232

3333
Context "Module handler class" {
3434
BeforeAll {
3535
if ( $skipTest ) { return }
3636
$moduleHandlerType = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.ModuleDependencyHandler]
3737
$oldEnvVars = Get-Item Env:\* | Sort-Object -Property Key
38-
$oldPSModulePath = $env:PSModulePath
38+
$savedPSModulePath = $env:PSModulePath
39+
}
40+
AfterAll {
41+
if ( $skipTest ) { return }
42+
$env:PSModulePath = $savedPSModulePath
3943
}
4044
It "Sets defaults correctly" -skip:$skipTest {
4145
$rsp = [runspacefactory]::CreateRunspace()
@@ -54,7 +58,7 @@ Describe "Resolve DSC Resource Dependency" {
5458
$expectedPssaAppDataPath = Join-Path $depHandler.LocalAppDataPath "PSScriptAnalyzer"
5559
$depHandler.PSSAAppDataPath | Should -Be $expectedPssaAppDataPath
5660

57-
$expectedPSModulePath = $oldPSModulePath + [System.IO.Path]::PathSeparator + $depHandler.TempModulePath
61+
$expectedPSModulePath = $savedPSModulePath + [System.IO.Path]::PathSeparator + $depHandler.TempModulePath
5862
$env:PSModulePath | Should -Be $expectedPSModulePath
5963

6064
$depHandler.Dispose()
@@ -174,7 +178,7 @@ Describe "Resolve DSC Resource Dependency" {
174178
# Save the current environment variables
175179
$oldLocalAppDataPath = $env:LOCALAPPDATA
176180
$oldTempPath = $env:TEMP
177-
$oldPSModulePath = $env:PSModulePath
181+
$savedPSModulePath = $env:PSModulePath
178182

179183
# set the environment variables
180184
$tempPath = Join-Path $oldTempPath ([guid]::NewGUID()).ToString()
@@ -184,8 +188,8 @@ Describe "Resolve DSC Resource Dependency" {
184188
$env:TEMP = $newTempPath
185189

186190
# create the temporary directories
187-
New-Item -Type Directory -Path $newLocalAppDataPath
188-
New-Item -Type Directory -Path $newTempPath
191+
New-Item -Type Directory -Path $newLocalAppDataPath -force
192+
New-Item -Type Directory -Path $newTempPath -force
189193

190194
# create and dispose module dependency handler object
191195
# to setup the temporary module
@@ -203,7 +207,8 @@ Describe "Resolve DSC Resource Dependency" {
203207
}
204208

205209
AfterAll {
206-
$env:PSModulePath = $oldPSModulePath
210+
if ( $skipTest ) { return }
211+
$env:PSModulePath = $savedPSModulePath
207212
}
208213

209214
It "has a single parse error" -skip:$skipTest {
@@ -217,7 +222,7 @@ Describe "Resolve DSC Resource Dependency" {
217222

218223
It "Keeps PSModulePath unchanged before and after invocation" -skip:$skipTest {
219224
$dr = Invoke-ScriptAnalyzer -Path $violationFilePath -ErrorVariable parseErrors -ErrorAction SilentlyContinue
220-
$env:PSModulePath | Should -Be $oldPSModulePath
225+
$env:PSModulePath | Should -Be $savedPSModulePath
221226
}
222227

223228
if (!$skipTest)

Tests/Rules/DscExamplesPresent.tests.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ if ($PSVersionTable.PSVersion -ge [Version]'5.0.0') {
2323
}
2424

2525
Context "When examples present" {
26-
New-Item -Path $examplesPath -ItemType Directory
27-
New-Item -Path "$examplesPath\FileResource_Example.psm1" -ItemType File
26+
New-Item -Path $examplesPath -ItemType Directory -force
27+
New-Item -Path "$examplesPath\FileResource_Example.psm1" -ItemType File -force
2828

2929
$noViolations = Invoke-ScriptAnalyzer -ErrorAction SilentlyContinue $classResourcePath | Where-Object {$_.RuleName -eq $ruleName}
3030

@@ -57,8 +57,8 @@ Describe "DscExamplesPresent rule in regular (non-class) based resource" {
5757
}
5858

5959
Context "When examples present" {
60-
New-Item -Path $examplesPath -ItemType Directory
61-
New-Item -Path "$examplesPath\MSFT_WaitForAll_Example.psm1" -ItemType File
60+
New-Item -Path $examplesPath -ItemType Directory -force
61+
New-Item -Path "$examplesPath\MSFT_WaitForAll_Example.psm1" -ItemType File -force
6262

6363
$noViolations = Invoke-ScriptAnalyzer -ErrorAction SilentlyContinue $resourcePath | Where-Object {$_.RuleName -eq $ruleName}
6464

Tests/Rules/DscTestsPresent.tests.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ if ($PSVersionTable.PSVersion -ge [Version]'5.0.0') {
2323
}
2424

2525
Context "When tests present" {
26-
New-Item -Path $testsPath -ItemType Directory
27-
New-Item -Path "$testsPath\FileResource_Test.psm1" -ItemType File
26+
New-Item -Path $testsPath -ItemType Directory -force
27+
New-Item -Path "$testsPath\FileResource_Test.psm1" -ItemType File -force
2828

2929
$noViolations = Invoke-ScriptAnalyzer -ErrorAction SilentlyContinue $classResourcePath | Where-Object {$_.RuleName -eq $ruleName}
3030

@@ -57,8 +57,8 @@ Describe "DscTestsPresent rule in regular (non-class) based resource" {
5757
}
5858

5959
Context "When tests present" {
60-
New-Item -Path $testsPath -ItemType Directory
61-
New-Item -Path "$testsPath\MSFT_WaitForAll_Test.psm1" -ItemType File
60+
New-Item -Path $testsPath -ItemType Directory -force
61+
New-Item -Path "$testsPath\MSFT_WaitForAll_Test.psm1" -ItemType File -force
6262

6363
$noViolations = Invoke-ScriptAnalyzer -ErrorAction SilentlyContinue $resourcePath | Where-Object {$_.RuleName -eq $ruleName}
6464

Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Describe "UseIdenticalMandatoryParametersForDSC" {
6363
}
6464
"@
6565
# and under it a directory called dscresources\something
66-
New-Item -ItemType Directory -Path $noParentClassDir
66+
New-Item -ItemType Directory -Path $noParentClassDir -force
6767
$noparentClassFilepath = Join-Path -Path $noParentClassDir -ChildPath 'ClassWithNoParent.psm1'
6868
$noparentClassMofFilepath = Join-Path -Path $noParentClassDir -ChildPath 'ClassWithNoParent.schema.mof'
6969

@@ -102,7 +102,7 @@ class ClassWithNoParent
102102
}
103103
"@
104104
# and under it a directory called dscresources\something
105-
New-Item -ItemType Directory -Path $noParentClassDir
105+
New-Item -ItemType Directory -Path $noParentClassDir -force
106106
$noparentClassFilepath = Join-Path -Path $noParentClassDir -ChildPath 'MSFT_ClassWithNoParent.psm1'
107107
$noparentClassMofFilepath = Join-Path -Path $noParentClassDir -ChildPath 'MSFT_ClassWithNoParent.schema.mof'
108108

build.psm1

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,18 @@
33

44
# Build module for PowerShell ScriptAnalyzer
55
$projectRoot = $PSScriptRoot
6-
$destinationDir = Join-Path -Path $projectRoot -ChildPath (Join-Path -Path "out" -ChildPath "PSScriptAnalyzer")
6+
$analyzerName = "PSScriptAnalyzer"
7+
8+
function Get-AnalyzerVersion
9+
{
10+
$csprojPath = [io.path]::Combine($projectRoot,"Engine","Engine.csproj")
11+
$xml = [xml](Get-Content "${csprojPath}")
12+
$xml.SelectSingleNode(".//VersionPrefix")."#text"
13+
}
14+
15+
$analyzerVersion = Get-AnalyzerVersion
16+
# location where analyzer goes
17+
$script:destinationDir = [io.path]::Combine($projectRoot,"out","${analyzerName}", $analyzerVersion)
718

819
function Publish-File
920
{
@@ -40,7 +51,7 @@ function Get-UserModulePath
4051
function Uninstall-ScriptAnalyzer
4152
{
4253
[CmdletBinding(SupportsShouldProcess)]
43-
param ( $ModulePath = $(Join-Path -Path (Get-UserModulePath) -ChildPath PSScriptAnalyzer) )
54+
param ( $ModulePath = $(Join-Path -Path (Get-UserModulePath) -ChildPath ${analyzerName}) )
4455
END {
4556
if ( $PSCmdlet.ShouldProcess("$modulePath") ) {
4657
Remove-Item -Recurse -Path "$ModulePath" -Force
@@ -52,10 +63,10 @@ function Uninstall-ScriptAnalyzer
5263
function Install-ScriptAnalyzer
5364
{
5465
[CmdletBinding(SupportsShouldProcess)]
55-
param ( $ModulePath = $(Join-Path -Path (Get-UserModulePath) -ChildPath PSScriptAnalyzer) )
66+
param ( $ModulePath = $(Join-Path -Path (Get-UserModulePath) -ChildPath ${analyzerName}) )
5667
END {
5768
if ( $PSCmdlet.ShouldProcess("$modulePath") ) {
58-
Copy-Item -Recurse -Path "$destinationDir" -Destination "$ModulePath\." -Force
69+
Copy-Item -Recurse -Path "$script:destinationDir" -Destination "$ModulePath\." -Force
5970
}
6071
}
6172
}
@@ -64,7 +75,7 @@ function Install-ScriptAnalyzer
6475
function Uninstall-ScriptAnalyzer
6576
{
6677
[CmdletBinding(SupportsShouldProcess)]
67-
param ( $ModulePath = $(Join-Path -Path (Get-UserModulePath) -ChildPath PSScriptAnalyzer) )
78+
param ( $ModulePath = $(Join-Path -Path (Get-UserModulePath) -ChildPath ${analyzerName}) )
6879
END {
6980
if ((Test-Path $ModulePath) -and (Get-Item $ModulePath).PSIsContainer )
7081
{
@@ -79,9 +90,9 @@ function Remove-Build
7990
[CmdletBinding(SupportsShouldProcess=$true)]
8091
param ()
8192
END {
82-
if ( $PSCmdlet.ShouldProcess("${destinationDir}")) {
83-
if ( Test-Path ${destinationDir} ) {
84-
Remove-Item -Force -Recurse ${destinationDir}
93+
if ( $PSCmdlet.ShouldProcess("${script:destinationDir}")) {
94+
if ( Test-Path ${script:destinationDir} ) {
95+
Remove-Item -Force -Recurse ${script:destinationDir}
8596
}
8697
}
8798
}
@@ -92,7 +103,7 @@ function Start-DocumentationBuild
92103
{
93104
$docsPath = Join-Path $projectRoot docs
94105
$markdownDocsPath = Join-Path $docsPath markdown
95-
$outputDocsPath = Join-Path $destinationDir en-US
106+
$outputDocsPath = Join-Path $script:destinationDir en-US
96107
$platyPS = Get-Module -ListAvailable platyPS
97108
if ($null -eq $platyPS -or ($platyPS | Sort-Object Version -Descending | Select-Object -First 1).Version -lt [version]0.12)
98109
{
@@ -118,12 +129,12 @@ function Copy-CompatibilityProfiles
118129
}
119130

120131
$profileDir = [System.IO.Path]::Combine($PSScriptRoot, 'PSCompatibilityCollector', 'profiles')
121-
$destinationDir = [System.IO.Path]::Combine($PSScriptRoot, 'out', 'PSScriptAnalyzer', "compatibility_profiles")
122-
if ( -not (Test-Path $destinationDir) ) {
123-
$null = New-Item -Type Directory $destinationDir
132+
$targetProfileDir = [io.path]::Combine($script:destinationDir,"compatibility_profiles")
133+
if ( -not (Test-Path $targetProfileDir) ) {
134+
$null = New-Item -Type Directory $targetProfileDir
124135
}
125136

126-
Copy-Item -Force $profileDir/* $destinationDir
137+
Copy-Item -Force $profileDir/* $targetProfileDir
127138
}
128139

129140
# build script analyzer (and optionally build everything with -All)
@@ -162,9 +173,6 @@ function Start-ScriptAnalyzerBuild
162173
Start-DocumentationBuild
163174
}
164175

165-
# Destination for the composed module when built
166-
$destinationDir = "$projectRoot\out\PSScriptAnalyzer"
167-
168176
if ( $All )
169177
{
170178
# Build all the versions of the analyzer
@@ -205,24 +213,23 @@ function Start-ScriptAnalyzerBuild
205213
"$projectRoot\Engine\ScriptAnalyzer.format.ps1xml", "$projectRoot\Engine\ScriptAnalyzer.types.ps1xml"
206214
)
207215

208-
$destinationDir = "$projectRoot\out\PSScriptAnalyzer"
209216
switch ($PSVersion)
210217
{
211218
3
212219
{
213-
$destinationDirBinaries = "$destinationDir\PSv3"
220+
$destinationDirBinaries = "$script:destinationDir\PSv3"
214221
}
215222
4
216223
{
217-
$destinationDirBinaries = "$destinationDir\PSv4"
224+
$destinationDirBinaries = "$script:destinationDir\PSv4"
218225
}
219226
5
220227
{
221-
$destinationDirBinaries = "$destinationDir"
228+
$destinationDirBinaries = "$script:destinationDir"
222229
}
223230
6
224231
{
225-
$destinationDirBinaries = "$destinationDir\coreclr"
232+
$destinationDirBinaries = "$script:destinationDir\coreclr"
226233
}
227234
default
228235
{
@@ -252,7 +259,7 @@ function Start-ScriptAnalyzerBuild
252259
Pop-Location
253260
}
254261

255-
Publish-File $itemsToCopyCommon $destinationDir
262+
Publish-File $itemsToCopyCommon $script:destinationDir
256263

257264
$itemsToCopyBinaries = @(
258265
"$projectRoot\Engine\bin\${config}\${Framework}\Microsoft.Windows.PowerShell.ScriptAnalyzer.dll",
@@ -262,7 +269,7 @@ function Start-ScriptAnalyzerBuild
262269
Publish-File $itemsToCopyBinaries $destinationDirBinaries
263270

264271
$settingsFiles = Get-Childitem "$projectRoot\Engine\Settings" | ForEach-Object -MemberName FullName
265-
Publish-File $settingsFiles (Join-Path -Path $destinationDir -ChildPath Settings)
272+
Publish-File $settingsFiles (Join-Path -Path $script:destinationDir -ChildPath Settings)
266273

267274
if ($framework -eq 'net452') {
268275
Copy-Item -path "$projectRoot\Rules\bin\${config}\${framework}\Newtonsoft.Json.dll" -Destination $destinationDirBinaries
@@ -280,10 +287,38 @@ function Test-ScriptAnalyzer
280287
param ( [Parameter()][switch]$InProcess, [switch]$ShowAll )
281288

282289
END {
283-
$testModulePath = Join-Path "${projectRoot}" -ChildPath out
290+
# versions 3 and 4 don't understand versioned module paths, so we need to rename the directory of the version to
291+
# the module name, and then set the ModulePath to that
292+
#
293+
# the layout of the build location is
294+
# .../out
295+
# /PSScriptAnalyzer
296+
# /1.18.0
297+
# /<modulefiles live here>
298+
# and ".../out" is added to env:PSModulePath
299+
# on v3 and v4, it will be
300+
# .../out
301+
# /PSScriptAnalyzer
302+
# /PSScriptAnalyzer
303+
# /<modulefiles live here>
304+
# and ".../out/PSScriptAnalyzer" is added to env:PSModulePath
305+
#
306+
#
307+
$major = $PSVersionTable.PSVersion.Major
308+
if ( $major -lt 5 ) {
309+
# get the directory name of the destination, we need to change it
310+
$versionDirectoryRoot = Split-Path $script:destinationDir
311+
$testModulePath = Join-Path $versionDirectoryRoot $analyzerName
312+
}
313+
else {
314+
$testModulePath = Join-Path "${projectRoot}" -ChildPath out
315+
}
284316
$testResultsFile = "'$(Join-Path ${projectRoot} -childPath TestResults.xml)'"
285317
$testScripts = "'${projectRoot}\Tests\Engine','${projectRoot}\Tests\Rules','${projectRoot}\Tests\Documentation'"
286318
try {
319+
if ( $major -lt 5 ) {
320+
Rename-Item $script:destinationDir ${testModulePath}
321+
}
287322
$savedModulePath = $env:PSModulePath
288323
$env:PSModulePath = "${testModulePath}{0}${env:PSModulePath}" -f [System.IO.Path]::PathSeparator
289324
if ($ShowAll)
@@ -304,6 +339,9 @@ function Test-ScriptAnalyzer
304339
}
305340
finally {
306341
$env:PSModulePath = $savedModulePath
342+
if ( $major -lt 5 ) {
343+
Rename-Item ${testModulePath} ${script:destinationDir}
344+
}
307345
}
308346
}
309347
}

tools/appveyor.psm1

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,23 @@ function Invoke-AppveyorTest {
5151
Write-Verbose -Verbose ("Running tests on PowerShell version " + $PSVersionTable.PSVersion)
5252
Write-Verbose -Verbose "Language set to '${env:LANG}'"
5353

54-
# Copy the generated modules into the out directory
55-
$modulePath = $env:PSModulePath.Split([System.IO.Path]::PathSeparator) | Where-Object { Test-Path $_} | Select-Object -First 1
56-
Copy-Item "${CheckoutPath}\out\PSScriptAnalyzer" "$modulePath\" -Recurse -Force
57-
Copy-Item "${CheckoutPath}\PSCompatibilityCollector\out\PSCompatibilityCollector" "$modulePath\" -Recurse -Force
54+
# set up env:PSModulePath to the build location, don't copy it to the "normal place"
55+
$analyzerVersion = ([xml](Get-Content "${CheckoutPath}\Engine\Engine.csproj")).SelectSingleNode(".//VersionPrefix")."#text".Trim()
56+
$majorVersion = ([System.Version]$analyzerVersion).Major
57+
$psMajorVersion = $PSVersionTable.PSVersion.Major
58+
59+
if ( $psMajorVersion -lt 5 ) {
60+
$versionModuleDir = "${CheckoutPath}\out\PSScriptAnalyzer\${analyzerVersion}"
61+
$renameTarget = "${CheckoutPath}\out\PSScriptAnalyzer\PSScriptAnalyzer"
62+
Rename-Item "${versionModuleDir}" "${renameTarget}"
63+
$moduleDir = "${CheckoutPath}\out\PSScriptAnalyzer"
64+
}
65+
else{
66+
$moduleDir = "${CheckoutPath}\out"
67+
}
68+
69+
$env:PSModulePath = "${moduleDir}","${env:PSModulePath}" -join [System.IO.Path]::PathSeparator
70+
Write-Verbose -Verbose "module path: ${env:PSModulePath}"
5871

5972
# Set up testing assets
6073
$testResultsPath = Join-Path ${CheckoutPath} TestResults.xml

0 commit comments

Comments
 (0)