-
Notifications
You must be signed in to change notification settings - Fork 382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update, simplify, and consolidate build scripts #1082
Changes from 8 commits
cab2708
8977733
920a8ab
7a3f1d2
90e7957
3064367
f46abf3
1b8386a
2978f78
5c01978
9ef4a9d
e9c8175
118e0b2
9a20e22
b3018ea
cb95da1
a6a0070
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,183 +1,72 @@ | ||
# This function might be a partially out of date and only the -BuildDoc switch is guaranteed to work since it is used and needed for the build process. | ||
[CmdletBinding()] | ||
param( | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
[Parameter(ParameterSetName='Build')] | ||
[ValidateSet('PSV3 Debug','PSV3 Release','Debug','Release')] | ||
[string] $Configuration = 'Debug', | ||
[CmdletBinding(DefaultParameterSetName="BuildOne")] | ||
param( | ||
[Parameter(ParameterSetName="BuildAll")] | ||
[switch]$All, | ||
|
||
[Parameter(ParameterSetName='Build')] | ||
[switch] $BuildDocs = $false, | ||
[Parameter(ParameterSetName="BuildOne")] | ||
[ValidateSet("full", "core")] | ||
[string]$Framework = "core", | ||
|
||
[Parameter(ParameterSetName='Build')] | ||
[switch] $CleanOutput = $false, | ||
[Parameter(ParameterSetName="BuildOne")] | ||
[ValidateSet("3","4","5")] | ||
[string]$PSVersion = "5", | ||
|
||
[Parameter(ParameterSetName='Build')] | ||
[switch] $Install = $false, | ||
[Parameter(ParameterSetName="BuildOne")] | ||
[Parameter(ParameterSetName="BuildAll")] | ||
[ValidateSet("Debug", "Release")] | ||
[string]$Configuration = "Debug", | ||
|
||
[Parameter(ParameterSetName='Build')] | ||
[switch] $Uninstall = $false, | ||
[Parameter(ParameterSetName="BuildDoc")] | ||
[switch]$Documentation, | ||
|
||
[Parameter(ParameterSetName='Test')] | ||
[switch] $Test = $false, | ||
[Parameter(ParameterSetName='BuildAll')] | ||
[Parameter(ParameterSetName='BuildOne')] | ||
[switch]$Clobber, | ||
|
||
[Parameter(ParameterSetName='Test')] | ||
[switch] $Engine = $false, | ||
[Parameter(Mandatory=$true,ParameterSetName='Clean')] | ||
[switch] $Clean, | ||
|
||
[Parameter(ParameterSetName='Test')] | ||
[switch] $Rules = $false, | ||
[Parameter(Mandatory=$true,ParameterSetName='Test')] | ||
[switch] $Test, | ||
|
||
[Parameter(ParameterSetName='Test')] | ||
[switch] $RunInDifferentProcess = $false | ||
[switch] $InProcess | ||
) | ||
|
||
# Some cmdlets like copy-item do not respond to the $verbosepreference variable | ||
# hence we set it explicitly | ||
$verbosity = $false | ||
if ($VerbosePreference.Equals([System.Management.Automation.ActionPreference]'Continue')) | ||
{ | ||
$verbosity = $true | ||
} | ||
|
||
Function CreateIfNotExists([string] $folderPath) | ||
{ | ||
if (-not (Test-Path $folderPath)) | ||
{ | ||
New-Item -Path $folderPath -ItemType Directory -Verbose:$verbosity | ||
} | ||
} | ||
|
||
$projectRoot = Resolve-path (Split-Path $MyInvocation.InvocationName) | ||
$solutionPath = Join-Path $projectRoot 'PSScriptAnalyzer.sln' | ||
$outPath = Join-Path $projectRoot 'out' | ||
$destinationPath = Join-Path $outPath PSScriptAnalyzer | ||
|
||
if (-not (Test-Path $solutionPath)) | ||
{ | ||
$errMsg = "{0} not the right directory" -f $solutionPath | ||
throw $errMsg | ||
} | ||
|
||
if ($CleanOutput) | ||
{ | ||
Remove-Item -Recurse $outPath\* -Force -Verbose:$verbosity | ||
} | ||
|
||
if ($BuildDocs) | ||
{ | ||
$docsPath = Join-Path $projectRoot 'docs' | ||
$markdownDocsPath = Join-Path $docsPath 'markdown' | ||
$outputDocsPath = Join-Path $destinationPath en-US | ||
|
||
CreateIfNotExists($outputDocsPath) | ||
|
||
# Build documentation using platyPS | ||
$requiredVersionOfplatyPS = 0.9 | ||
if ($null -eq (Get-Module platyPS -ListAvailable -Verbose:$verbosity | Where-Object { $_.Version -ge $requiredVersionOfplatyPS })) | ||
{ | ||
"Cannot find required minimum version $requiredVersionOfplatyPS of platyPS. Please install it from https://www.powershellgallery.com/packages/platyPS/ using e.g. the following command: Install-Module platyPS" | ||
} | ||
if ($null -eq (Get-Module platyPS -Verbose:$verbosity)) | ||
{ | ||
Import-Module platyPS -Verbose:$verbosity | ||
} | ||
if (-not (Test-Path $markdownDocsPath -Verbose:$verbosity)) | ||
{ | ||
throw "Cannot find markdown documentation folder." | ||
} | ||
New-ExternalHelp -Path $markdownDocsPath -OutputPath $outputDocsPath -Force -Verbose:$verbosity | ||
} | ||
|
||
# Appveyor errors out due to $profile being null. Hence... | ||
$moduleRootPath = "$HOME/Documents/WindowsPowerShell/Modules" | ||
if ($null -ne $profile) | ||
{ | ||
$moduleRootPath = Join-Path (Split-Path $profile) 'Modules' | ||
} | ||
$modulePSSAPath = Join-Path $moduleRootPath 'PSScriptAnalyzer' | ||
if ($Install) | ||
{ | ||
if (-not (Test-Path $moduleRootPath)) | ||
{ | ||
New-Item -Path $moduleRootPath -ItemType Directory -Force -Verbose:$verbosity | ||
} | ||
if (-not (Test-Path -Path $destinationPath)) | ||
{ | ||
throw "Please build the module first." | ||
} | ||
Copy-Item -Path $destinationPath -Destination $modulePSSAPath -Recurse -Verbose:$verbosity | ||
} | ||
|
||
if ($Test) | ||
{ | ||
Import-Module -Name Pester -MinimumVersion 4.3.1 -ErrorAction Stop | ||
Function GetTestRunnerScriptContent($testPath) | ||
{ | ||
$x = @" | ||
cd $testPath | ||
Invoke-Pester | ||
"@ | ||
return $x | ||
} | ||
|
||
Function CreateTestRunnerScript($testPath) | ||
{ | ||
$tmptmpFilePath = [System.IO.Path]::GetTempFileName() | ||
$tmpFilePath = $tmptmpFilePath + '.ps1' | ||
Move-Item $tmptmpFilePath $tmpFilePath -Verbose:$verbosity | ||
$content = GetTestRunnerScriptContent $testPath | ||
Set-Content -Path $tmpFilePath -Value $content -Verbose:$verbosity | ||
return $tmpFilePath | ||
} | ||
|
||
Function GetTestPath($TestType) | ||
{ | ||
if ($TestType -eq "engine") | ||
{ | ||
$testPath = Join-Path $projectRoot "Tests/Engine" | ||
} | ||
else | ||
{ | ||
$testPath = Join-Path $projectRoot "Tests/Rules" | ||
END { | ||
Import-Module -Force (Join-Path $PSScriptRoot build.psm1) | ||
if ( $Clean -or $Clobber ) { | ||
Remove-Build | ||
if ( $PSCmdlet.ParameterSetName -eq "Clean" ) { | ||
return | ||
} | ||
return $testPath | ||
} | ||
|
||
Function RunTest($TestType, [Boolean] $DifferentProcess) | ||
{ | ||
$testPath = GetTestPath($TestType) | ||
if ($DifferentProcess) | ||
{ | ||
$testScriptFilePath = CreateTestRunnerScript $testPath | ||
Start-Process powershell -ArgumentList "-NoExit","-File $testScriptFilePath" -Verb runas | ||
# clean up the test file | ||
$setName = $PSCmdlet.ParameterSetName | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is used above, maybe it's worth assigning above, below the |
||
switch ( $setName ) { | ||
"BuildAll" { | ||
Build-ScriptAnalyzer -All -Configuration $Configuration | ||
} | ||
else | ||
{ | ||
try | ||
{ | ||
Push-Location . | ||
([scriptblock]::Create((GetTestRunnerScriptContent $testPath))).Invoke() | ||
} | ||
finally | ||
{ | ||
Pop-Location | ||
|
||
"BuildDoc" { | ||
Build-ScriptAnalyzer -Documentation | ||
} | ||
"BuildOne" { | ||
$buildArgs = @{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Side note: I find I do this a lot in PowerShell as well (pass arguments down through commands). Perhaps it's an antipattern (Law of Demeter violation type of thing), but it would be nice to introduce a simpler way for commands to "inherit" all the common parameters between it and its caller (which haven't been overridden). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i often create a hash earlier in the script and then modify it based on conditions. |
||
Framework = $Framework | ||
PSVersion = $PSVersion | ||
Configuration = $Configuration | ||
} | ||
Build-ScriptAnalyzer @buildArgs | ||
} | ||
"Test" { | ||
Test-ScriptAnalyzer -InProcess:$InProcess | ||
return | ||
} | ||
default { | ||
throw "Unexpected parameter set '$setName'" | ||
} | ||
} | ||
|
||
if ($Engine -or (-not ($Engine -or $Rules))) | ||
{ | ||
RunTest 'engine' $RunInDifferentProcess | ||
} | ||
if ($Rules -or (-not ($Engine -or $Rules))) | ||
{ | ||
RunTest 'rules' $RunInDifferentProcess | ||
} | ||
} | ||
|
||
if ($Uninstall) | ||
{ | ||
Remove-Item -Path $modulePSSAPath -Force -Verbose:$verbosity -Recurse | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for the sake of argument, should the default build be for the current platform the script is being run on? That's possibly an order of magnitude harder to accomplish, but thought I would bring it up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for that but I see it as an optional nice to have that could come after this PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:) why would anyone be using anything other than core? :)