From efb0b3e724ef751050daf0149675c37c5fef08dd Mon Sep 17 00:00:00 2001 From: Andy Jordan <2226434+andyleejordan@users.noreply.github.com> Date: Thu, 26 Sep 2024 12:51:55 -0700 Subject: [PATCH] Centralize the project's version too --- .pipelines/PSScriptAnalyzer-Official.yml | 4 +- Directory.Build.props | 2 + Engine/Engine.csproj | 4 +- Engine/PSScriptAnalyzer.psd1 | 2 +- ...osoft.PowerShell.CrossCompatibility.csproj | 4 +- Rules/Rules.csproj | 4 +- Utils/ReleaseMaker.psm1 | 198 ------------------ build.psm1 | 14 +- tools/updateVersion.ps1 | 24 +++ 9 files changed, 45 insertions(+), 211 deletions(-) delete mode 100644 Utils/ReleaseMaker.psm1 create mode 100644 tools/updateVersion.ps1 diff --git a/.pipelines/PSScriptAnalyzer-Official.yml b/.pipelines/PSScriptAnalyzer-Official.yml index 212aeaede..15bccd7f0 100644 --- a/.pipelines/PSScriptAnalyzer-Official.yml +++ b/.pipelines/PSScriptAnalyzer-Official.yml @@ -63,8 +63,8 @@ extends: ob_outputDirectory: $(Build.SourcesDirectory)/out steps: - pwsh: | - [xml]$xml = Get-Content Engine/Engine.csproj - $version = $xml.SelectSingleNode(".//VersionPrefix")."#text" + [xml]$xml = Get-Content ./Directory.Build.props + $version = $xml.Project.PropertyGroup.ModuleVersion Write-Output "##vso[task.setvariable variable=version;isOutput=true]$version" name: package displayName: Get version from project properties diff --git a/Directory.Build.props b/Directory.Build.props index 193280892..e05d0ecbe 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,5 +1,7 @@ + + 1.22.0 true diff --git a/Engine/Engine.csproj b/Engine/Engine.csproj index ad301705a..3025c9a08 100644 --- a/Engine/Engine.csproj +++ b/Engine/Engine.csproj @@ -1,10 +1,10 @@  - 1.22.0 + $(ModuleVersion) net6;net462 Microsoft.Windows.PowerShell.ScriptAnalyzer - 1.22.0 + $(ModuleVersion) Engine Microsoft.Windows.PowerShell.ScriptAnalyzer 9.0 diff --git a/Engine/PSScriptAnalyzer.psd1 b/Engine/PSScriptAnalyzer.psd1 index feae6fba4..5e933ca4a 100644 --- a/Engine/PSScriptAnalyzer.psd1 +++ b/Engine/PSScriptAnalyzer.psd1 @@ -11,7 +11,7 @@ Author = 'Microsoft Corporation' RootModule = 'PSScriptAnalyzer.psm1' # Version number of this module. -ModuleVersion = '1.22.0' +ModuleVersion = '{{ModuleVersion}}' # ID used to uniquely identify this module GUID = 'd6245802-193d-4068-a631-8863a4342a18' diff --git a/PSCompatibilityCollector/Microsoft.PowerShell.CrossCompatibility/Microsoft.PowerShell.CrossCompatibility.csproj b/PSCompatibilityCollector/Microsoft.PowerShell.CrossCompatibility/Microsoft.PowerShell.CrossCompatibility.csproj index a1eeae704..da987fb69 100644 --- a/PSCompatibilityCollector/Microsoft.PowerShell.CrossCompatibility/Microsoft.PowerShell.CrossCompatibility.csproj +++ b/PSCompatibilityCollector/Microsoft.PowerShell.CrossCompatibility/Microsoft.PowerShell.CrossCompatibility.csproj @@ -1,9 +1,9 @@  - 1.22.0 + $(ModuleVersion) netstandard2.0;net462 - 1.22.0 + $(ModuleVersion) diff --git a/Rules/Rules.csproj b/Rules/Rules.csproj index 2afc1a20b..8fef9e969 100644 --- a/Rules/Rules.csproj +++ b/Rules/Rules.csproj @@ -1,10 +1,10 @@ - 1.22.0 + $(ModuleVersion) net6;net462 Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules - 1.22.0 + $(ModuleVersion) Rules Microsoft.Windows.PowerShell.ScriptAnalyzer true diff --git a/Utils/ReleaseMaker.psm1 b/Utils/ReleaseMaker.psm1 deleted file mode 100644 index ac59dbb32..000000000 --- a/Utils/ReleaseMaker.psm1 +++ /dev/null @@ -1,198 +0,0 @@ -Function Get-SolutionPath -{ - Split-Path $PSScriptRoot -Parent -} - -Function Get-ChangeLogPath -{ - Join-Path (Get-SolutionPath) 'CHANGELOG.MD' -} - -Function Get-EngineProjectPath -{ - Join-Path (Get-SolutionPath) 'Engine' -} - -Function Get-ModuleManifestPath -{ - Join-Path (Get-EngineProjectPath) 'PSScriptAnalyzer.psd1' -} - -Function New-Release -{ - [CmdletBinding()] - param($newVer, $oldVer) - - $isVersionGiven = $true - if ($null -eq $newVer -or $null -eq $oldVer) - { - Write-Warning "Parameters are null. Checking changelog for version..." - $isVersionGiven = $false - } - - $solutionRoot = Get-SolutionPath - $enginePath = Get-EngineProjectPath - $versions = Get-VersionsFromChangeLog - if ($versions.Count -le 2) - { - throw "This edge condition for the number versions less that 2 is not implemented." - } - - if ($isVersionGiven) - { - Function Test-IfNotPresentInChangelog - { - param($extractedVersion, $inputVersion) - if ($extractedVersion -ne $inputVersion) - { - throw ("Version {0} does not exist in changelog. Please update changelog." -f $inputVersion) - } - } - - Test-IfNotPresentInChangelog $versions[0] $newVer - Test-IfNotPresentInChangelog $versions[1] $oldVer - } - else - { - $newVer = $versions[0] - $oldVer = $versions[1] - $caption = "Version Check" - $query = "Is version {0} the next release and version {1} the previous release ?" -f $newVer,$oldVer - [bool] $yesToAll = $false - [bool] $noToAll = $false - - if (!$PSCmdlet.ShouldContinue($query, $caption, $false, [ref] $yesToAll, [ref] $noToAll)) - { - return "Aborting..." - } - } - - # update version - Update-Version $newVer $oldVer $solutionRoot - - # copy release notes from changelog to module manifest - Update-ReleaseNotesInModuleManifest $newVer $oldVer - - # build the module - New-ReleaseBuild -} - -function Get-VersionsFromChangeLog -{ - $moduleManifestPath = Get-ModuleManifestPath - $changelogPath = Get-ChangeLogPath - $matches = [regex]::new("\[(\d+\.\d+\.\d+)\]").Matches((get-content $changelogPath -raw)) - $versions = $matches | ForEach-Object {$_.Groups[1].Value} - $versions -} - -function New-ReleaseBuild -{ - $solutionPath = Get-SolutionPath - Push-Location $solutionPath - try - { - if ( test-path out ) { remove-item out/ -recurse -force } - .\build.ps1 -All -Configuration Release - .\PSCompatibilityCollector\build.ps1 -Clean -Configuration Release - Copy-Item -Recurse .\PSCompatibilityCollector\out\* .\out\ - } - finally - { - Pop-Location - } -} - -function Update-ReleaseNotesInModuleManifest -{ - [CmdletBinding()] - param($newVer, $oldVer) - - $moduleManifestPath = Get-ModuleManifestPath - Write-Verbose ("Module manifest: {0}" -f $moduleManifestPath) - $changelogPath = Get-ChangeLogPath - Write-Verbose ("CHANGELOG: {0}" -f $changelogPath) - $changelogRegexPattern = "##\s\[{0}\].*\n((?:.*\n)+)##\s\[{1}\].*" ` - -f [regex]::Escape($newVer),[regex]::Escape($oldVer) - $changelogRegex = [regex]::new($changelogRegexPattern) - $matches = $changelogRegex.Match((get-content $changelogPath -raw)) - $changelogWithHyperlinks = $matches.Groups[1].Value.Trim() - - Write-Verbose 'CHANGELOG:' - Write-Verbose $changelogWithHyperlinks - - # Remove hyperlinks from changelog to make is suitable for publishing on powershellgallery.com - Write-Verbose "Removing hyperlinks from changelog" - $changelog = Remove-MarkdownHyperlink $changelogWithHyperlinks - Write-Verbose "CHANGELOG without hyperlinks:" - Write-Verbose $changelog - - $releaseNotesPattern = ` - "(?ReleaseNotes\s*=\s*@')(?(?:.*\n)*)(?'@)" - $replacement = "`${releaseNotesBegin}" ` - + [environment]::NewLine ` - + $changelog ` - + [environment]::NewLine ` - + "`${releaseNotesEnd}" - $r = [regex]::new($releaseNotesPattern) - $updatedManifestContent = $r.Replace([System.IO.File]::ReadAllText($moduleManifestPath), $replacement) - Set-ContentUtf8NoBom $moduleManifestPath $updatedManifestContent -} - -function Remove-MarkdownHyperlink -{ - param($markdownContent) - $markdownContent -replace "\[(.*?)\]\(.*?\)",'$1' -} - -function Combine-Path -{ - if ($args.Count -lt 2) - { - throw "give more 1 argument" - } - - $path = Join-Path $args[0] $args[1] - for ($k = 2; $k -lt $args.Count; $k++) - { - $path = Join-Path $path $args[$k] - } - - $path -} - -function Update-Version -{ - param( - [string] $newVer, - [string] $oldVer, - [string] $solutionPath - ) - - $ruleJson = Combine-Path $solutionPath 'Rules' 'Rules.csproj' - $engineJson = Combine-Path $solutionPath 'Engine' 'Engine.csproj' - $pssaManifest = Combine-Path $solutionPath 'Engine' 'PSScriptAnalyzer.psd1' - - Update-PatternInFile $ruleJson '"version": "{0}"' $oldVer $newVer - Update-PatternInFile $ruleJson '"Engine": "{0}"' $oldVer $newVer - Update-PatternInFile $engineJson '"version": "{0}"' $oldVer $newVer - Update-PatternInFile $pssaManifest "ModuleVersion = '{0}'" $oldVer $newVer -} - -function Update-PatternInFile -{ - param ($path, $unformattedPattern, $oldVal, $newVal) - - $content = Get-Content $path - $newcontent = $content -replace ($unformattedPattern -f $oldVal),($unformattedPattern -f $newVal) - Set-ContentUtf8NoBom $path $newcontent -} - -function Set-ContentUtf8NoBom { - param($path, $content) - $utfNoBom = [System.Text.UTF8Encoding]::new($false) - [System.IO.File]::WriteAllLines($path, $content, $utfNoBom) -} - -Export-ModuleMember -Function New-Release -Export-ModuleMember -Function New-ReleaseBuild diff --git a/build.psm1 b/build.psm1 index 6889f1b89..604c5d17e 100644 --- a/build.psm1 +++ b/build.psm1 @@ -7,8 +7,8 @@ $analyzerName = "PSScriptAnalyzer" function Get-AnalyzerVersion { - [xml]$xml = Get-Content $([io.path]::Combine($projectRoot,"Engine","Engine.csproj")) - $xml.SelectSingleNode(".//VersionPrefix")."#text" + [xml]$xml = Get-Content $([io.path]::Combine($projectRoot, "Directory.Build.props")) + $xml.Project.PropertyGroup.ModuleVersion } $analyzerVersion = Get-AnalyzerVersion @@ -159,9 +159,15 @@ function Start-ScriptAnalyzerBuild throw "Not in solution root" } + # "Copy" the module file with the version placeholder replaced + $manifestContent = Get-Content -LiteralPath "$projectRoot\Engine\PSScriptAnalyzer.psd1" -Raw + $newManifestContent = $manifestContent -replace '{{ModuleVersion}}', $analyzerVersion + Set-Content -LiteralPath "$script:destinationDir\PSScriptAnalyzer.psd1" -Encoding utf8 -Value $newManifestContent + $itemsToCopyCommon = @( - "$projectRoot\Engine\PSScriptAnalyzer.psd1", "$projectRoot\Engine\PSScriptAnalyzer.psm1", - "$projectRoot\Engine\ScriptAnalyzer.format.ps1xml", "$projectRoot\Engine\ScriptAnalyzer.types.ps1xml" + "$projectRoot\Engine\PSScriptAnalyzer.psm1", + "$projectRoot\Engine\ScriptAnalyzer.format.ps1xml", + "$projectRoot\Engine\ScriptAnalyzer.types.ps1xml" ) switch ($PSVersion) diff --git a/tools/updateVersion.ps1 b/tools/updateVersion.ps1 new file mode 100644 index 000000000..be75435dd --- /dev/null +++ b/tools/updateVersion.ps1 @@ -0,0 +1,24 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +param( + [Parameter(Mandatory)] + [semver]$Version, + + [Parameter(Mandatory)] + [string]$Changes +) + +git diff --staged --quiet --exit-code +if ($LASTEXITCODE -ne 0) { + throw "There are staged changes in the repository. Please commit or reset them before running this script." +} + +$Path = "Directory.Build.props" +$f = Get-Content -Path $Path +$f = $f -replace '^(?\s+)(.+)(?)$', "`${prefix}${Version}`${suffix}" +$f | Set-Content -Path $Path +git add $Path + +git commit --edit --message v${Version}: $Changes" +"