Skip to content

Simplify build scripts even more and upgrade platyPS in Appveyor #1088

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

Merged
merged 17 commits into from
Jan 16, 2019
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,31 +123,31 @@ Note: the PSScriptAnalyzer Chocolatey package is provided and supported by the c
* Building

You can either build using the `Visual Studio` solution `PSScriptAnalyzer.sln` or build using `PowerShell` specifically for your platform as follows:
* The default build is for PowerShell Core
* The default build is for the currently used version of PowerShell
```powershell
.\build.ps1
```
* Windows PowerShell version 5.0
```powershell
.\build.ps1 -Framework full -PSVersion 5 -Configuration Release
.\build.ps1 -PSVersion 5
```
* Windows PowerShell version 4.0
```powershell
.\build.ps1 -Framework full -PSVersion 4 -Configuration Release
.\build.ps1 -PSVersion 4
```
* Windows PowerShell version 3.0
```powershell
.\build.ps1 -Framework full -PSVersion 3 -Configuration Release
.\build.ps1 -PSVersion 3
```
* PowerShell Core
```powershell
.\buildCoreClr.ps1 -Framework core -Configuration Release -Build
.\build.ps1 -PSVersion 6
```
* Build documentation
* Build documentation (required for using the module)
```powershell
.\build.ps1 -Documentation
```
* Build all versions (PowerShell v3, v4, v5, and Core) and documentation
* Build all versions (PowerShell v3, v4, v5, and v6) and documentation
```powershell
.\build.ps1 -All
```
Expand Down
6 changes: 1 addition & 5 deletions Utils/ReleaseMaker.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,7 @@ function New-ReleaseBuild
try
{
if ( test-path out ) { remove-item out/ -recurse -force }
.\buildCoreClr.ps1 -Framework net451 -Configuration PSV3Release -Build
.\buildCoreClr.ps1 -Framework net451 -Configuration PSV4Release -Build
.\buildCoreClr.ps1 -Framework net451 -Configuration Release -Build
.\buildCoreClr.ps1 -Framework netstandard2.0 -Configuration Release -Build
.\build.ps1 -BuildDocs
.\build.ps1 -All -Configuration Release
}
finally
{
Expand Down
6 changes: 3 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ build_script:
Set-Location $env:APPVEYOR_BUILD_FOLDER
./build.ps1 -Documentation
if ( $env:PSVersion -eq "4" ) { # On WMF4: Also build for v3 to check it builds at least since we do not have a WMF3 image
./build.ps1 -Configuration "$env:BuildConfiguration" -PSVersion 3 -Framework full
./build.ps1 -Configuration "$env:BuildConfiguration" -PSVersion 3
}
./build.ps1 -Configuration "$env:BuildConfiguration" -PSVersion "$env:PSVersion" -Framework full
./build.ps1 -Configuration "$env:BuildConfiguration" -PSVersion "$env:PSVersion"
}
- pwsh: |
if ($env:PowerShellEdition -eq 'PowerShellCore') {
Set-Location $env:APPVEYOR_BUILD_FOLDER
./build.ps1 -Documentation
./build.ps1 -Configuration "$env:BuildConfiguration" -PSVersion 5 -Framework core
./build.ps1 -Configuration "$env:BuildConfiguration" -PSVersion 6
}

test_script:
Expand Down
9 changes: 2 additions & 7 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ param(
[switch]$All,

[Parameter(ParameterSetName="BuildOne")]
[ValidateSet("full", "core")]
[string]$Framework = "core",

[Parameter(ParameterSetName="BuildOne")]
[ValidateSet("3","4","5")]
[string]$PSVersion = "5",
[ValidateRange(3, 6)]
[int]$PSVersion = $PSVersionTable.PSVersion.Major,

[Parameter(ParameterSetName="BuildOne")]
[Parameter(ParameterSetName="BuildAll")]
Expand Down Expand Up @@ -55,7 +51,6 @@ END {
}
"BuildOne" {
$buildArgs = @{
Framework = $Framework
PSVersion = $PSVersion
Configuration = $Configuration
}
Expand Down
75 changes: 24 additions & 51 deletions build.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,10 @@ function Start-DocumentationBuild
$docsPath = Join-Path $projectRoot docs
$markdownDocsPath = Join-Path $docsPath markdown
$outputDocsPath = Join-Path $destinationDir en-US
$requiredVersionOfplatyPS = 0.9
#$modInfo = new-object Microsoft.PowerShell.Commands.ModuleSpecification -ArgumentList @{ ModuleName = "platyps"; ModuleVersion = $requiredVersionOfplatyPS}
#if ( $null -eq (Get-Module -ListAvailable -FullyQualifiedName $modInfo))
if ( $null -eq (Get-Module -ListAvailable platyPS))
if ( $null -eq (Get-Module -ListAvailable platyPS) -or ((Get-Module -ListAvailable platyPS | Select-Object -First 1).Version -lt [version]0.12))
Copy link
Contributor

@rjmholt rjmholt Nov 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: Got a space on the left of the $null but no space on the right of the 0.12.

gmo -List is being called twice here and only looks at the first module (gmo -List returns module in essentially random order, so that might not be the check we want).

Maybe something like:

$platyPS = Get-Module -ListAvailable @{ ModuleName = 'platyPS'; ModuleVersion = '0.12' } `
    | Sort-Object Version `
    | Select-Object -First 1

if (-not $platyPS)
{
    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
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored that slightly to use a FullyQualifiedName

Copy link
Collaborator Author

@bergmeister bergmeister Dec 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, but Get-Module -ListAvailable @{ ModuleName = 'platyPS'; ModuleVersion = '0.12.0' } does not work on my machine using either WPS or PSCore, I will refactor it to call gmo only once though and sort by version. This shows that gmo could benefit from -MinimumVersion parameter. P.S. Sort-Object Version also needs the Descending switch to get the latest version, otherwise it'd be the earliest.

{
Write-Verbose -verbose "platyPS not found, installing"
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
# throw "Cannot find required minimum version $requiredVersionOfplatyPS of platyPS. Install via 'Install-Module platyPS'"
}
if (-not (Test-Path $markdownDocsPath))
{
Expand All @@ -122,12 +118,8 @@ function Start-ScriptAnalyzerBuild
[switch]$All,

[Parameter(ParameterSetName="BuildOne")]
[ValidateSet("full", "core")]
[string]$Framework = "core",

[Parameter(ParameterSetName="BuildOne")]
[ValidateSet("3","4","5")]
[string]$PSVersion = "5",
[ValidateRange(3, 6)]
[int]$PSVersion = $PSVersionTable.PSVersion.Major,

[Parameter(ParameterSetName="BuildOne")]
[Parameter(ParameterSetName="BuildAll")]
Expand All @@ -138,19 +130,13 @@ function Start-ScriptAnalyzerBuild
[switch]$Documentation
)

BEGIN {
if ( $PSVersion -match "[34]" -and $Framework -eq "core" ) {
throw "Script Analyzer for PowerShell 3/4 cannot be built for framework 'core'"
}
}
END {
if ( $All )
{
# Build all the versions of the analyzer
Start-ScriptAnalyzerBuild -Framework full -Configuration $Configuration -PSVersion "3"
Start-ScriptAnalyzerBuild -Framework full -Configuration $Configuration -PSVersion "4"
Start-ScriptAnalyzerBuild -Framework full -Configuration $Configuration -PSVersion "5"
Start-ScriptAnalyzerBuild -Framework core -Configuration $Configuration -PSVersion "5"
foreach($psVersion in 3..6) {
Start-ScriptAnalyzerBuild -Configuration $Configuration -PSVersion $psVersion
}
Start-ScriptAnalyzerBuild -Documentation
return
}
Expand All @@ -161,22 +147,12 @@ function Start-ScriptAnalyzerBuild
return
}

Push-Location -Path $projectRoot

if ( $framework -eq "core" ) {
$frameworkName = "netstandard2.0"
}
else {
$frameworkName = "net451"
$framework = 'net451'
if ($PSVersion -ge 6) {
$framework = 'netstandard2.0'
}

# build the appropriate assembly
if ($PSVersion -match "[34]" -and $Framework -eq "core")
{
throw ("ScriptAnalyzer for PS version '{0}' is not applicable to {1} framework" -f $PSVersion,$Framework)
}

#Write-Progress "Building ScriptAnalyzer"
Push-Location -Path $projectRoot
if (-not (Test-Path "$projectRoot/global.json"))
{
throw "Not in solution root"
Expand All @@ -191,51 +167,48 @@ function Start-ScriptAnalyzerBuild

$destinationDir = "$projectRoot\out\PSScriptAnalyzer"
# this is normalizing case as well as selecting the proper location
if ( $Framework -eq "core" ) {
$destinationDirBinaries = "$destinationDir\coreclr"
}
elseif ($PSVersion -eq '3') {
if ($PSVersion -eq '3') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$PSVersion above is compared to an [int] rather than a [string] -- I know they work out the same though :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if/elseif/else might be a good candidate for a switch maybe:

switch ($PSVersion)
{
    3
    {
    }

    4
    {
    }

    5
    {
    }

    default
    {
    }
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, point taken. I'll change it to a switch for 3,4,5 and 6 and throw otherwise

$destinationDirBinaries = "$destinationDir\PSv3"
}
elseif ($PSVersion -eq '4') {
$destinationDirBinaries = "$destinationDir\PSv4"
}
else {
elseif ($PSVersion -eq '5') {
$destinationDirBinaries = $destinationDir
}
else {
$destinationDirBinaries = "$destinationDir\coreclr"
}

# build the analyzer
#Write-Progress "Building for framework $Framework, configuration $Configuration"
# The Rules project has a dependency on the Engine therefore just building the Rules project is enough
$config = "PSV${PSVersion}${Configuration}"
try {
Push-Location $projectRoot/Rules
Write-Progress "Building ScriptAnalyzer '$framework' version '${PSVersion}' configuration '${Configuration}'"
$buildOutput = dotnet build Rules.csproj --framework $frameworkName --configuration "${config}"
Write-Progress "Building ScriptAnalyzer for PSVersion '$PSVersion' using framework '$framework' and configuration '$config'"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a change in variables $config or $Configuration in the script -- just want to check you don't mean $Configuration here.

Copy link
Collaborator Author

@bergmeister bergmeister Dec 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$Configuration is the high level Debug\Release variable that the end user uses when calling the build script, internally, the actual configuration passed to msbuild is $config, which can be different and is e.g. PSV3Release for WMF3. I thought logging the actual low level variable was better, but looking at it now, I am probably wrong and will change it back to $Configuration

$buildOutput = dotnet build --framework $framework --configuration "${config}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth taking the ${config} out of brackets here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this was a James-ism ;-)

if ( $LASTEXITCODE -ne 0 ) { throw "$buildOutput" }
}
catch {
Write-Error "Failure to build $framework ${config}"
Write-Error "Failure to build for PSVersion '$PSVersion' using framework '$framework' and configuration '$config'"
return
}
finally {
Pop-Location
}

#Write-Progress "Copying files to $destinationDir"
Publish-File $itemsToCopyCommon $destinationDir

$itemsToCopyBinaries = @(
"$projectRoot\Engine\bin\${config}\${frameworkName}\Microsoft.Windows.PowerShell.ScriptAnalyzer.dll",
"$projectRoot\Rules\bin\${config}\${frameworkName}\Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.dll"
"$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

Publish-File $settingsFiles (Join-Path -Path $destinationDir -ChildPath Settings)

# copy newtonsoft dll if net451 framework
if ($Framework -eq "full") {
Copy-Item -path "$projectRoot\Rules\bin\${config}\${frameworkName}\Newtonsoft.Json.dll" -Destination $destinationDirBinaries
if ($framework -eq 'net451') {
Copy-Item -path "$projectRoot\Rules\bin\${config}\${framework}\Newtonsoft.Json.dll" -Destination $destinationDirBinaries
}

Pop-Location
Expand All @@ -256,7 +229,7 @@ function Test-ScriptAnalyzer
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")
$scriptBlock = [scriptblock]::Create("Invoke-Pester -Path $testScripts -OutputFormat NUnitXml -OutputFile $testResultsFile -Show Describe,Summary")
if ( $InProcess ) {
& $scriptBlock
}
Expand Down
5 changes: 3 additions & 2 deletions tools/appveyor.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ function Invoke-AppVeyorInstall {
}
}

$platyPSVersion = '0.12.0'
if ($null -eq (Get-Module -ListAvailable PowershellGet)) {
# WMF 4 image build
Write-Verbose -Verbose "Installing platyPS via nuget"
nuget install platyPS -Version 0.9.0 -source https://www.powershellgallery.com/api/v2 -outputDirectory "$Env:ProgramFiles\WindowsPowerShell\Modules\." -ExcludeVersion
nuget install platyPS -Version $platyPSVersion -source https://www.powershellgallery.com/api/v2 -outputDirectory "$Env:ProgramFiles\WindowsPowerShell\Modules\." -ExcludeVersion
}
else {
Write-Verbose -Verbose "Installing platyPS via Install-Module"
Install-Module -Name platyPS -Force -Scope CurrentUser -RequiredVersion '0.9.0'
Install-Module -Name platyPS -Force -Scope CurrentUser -RequiredVersion $platyPSVersion
}

# the legacy WMF4 image only has the old preview SDKs of dotnet
Expand Down