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
819function Publish-File
920{
@@ -40,7 +51,7 @@ function Get-UserModulePath
4051function 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
5263function 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
6475function 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}
0 commit comments