From 4e43409d5543d1023bf1e61134df0a97c6e6892b Mon Sep 17 00:00:00 2001 From: deadlydog Date: Tue, 5 Mar 2024 09:29:50 -0600 Subject: [PATCH] ci: Use native cmdlet to simplify updating the module manifests --- .../build-and-test-powershell-module.yml | 46 ++++--------------- 1 file changed, 10 insertions(+), 36 deletions(-) diff --git a/.github/workflows/build-and-test-powershell-module.yml b/.github/workflows/build-and-test-powershell-module.yml index ed80c05..1de3c7f 100644 --- a/.github/workflows/build-and-test-powershell-module.yml +++ b/.github/workflows/build-and-test-powershell-module.yml @@ -168,17 +168,6 @@ jobs: - name: Create Stable and Prerelease module artifacts shell: pwsh run: | - function Replace-TextInFile( - [ValidateScript({Test-Path $_ -PathType Leaf})] - [string]$filePath, - [string]$textToReplace, - [string]$replacementText) - { - $fileContents = Get-Content -Path $filePath -Raw - $newFileContents = $fileContents.Replace($textToReplace, $replacementText) - Set-Content -Path $filePath -Value $newFileContents - } - Write-Output "Reading in environment variables." [string] $moduleName = $Env:powerShellModuleName [string] $moduleDirectoryPath = $Env:powerShellModuleDirectoryPath @@ -189,43 +178,28 @@ jobs: Write-Output "Reading in dynamic environment variables." [string] $newVersionNumber = $Env:NewVersionNumber - Write-Output "Determining what the module manifest file paths should be." - [string] $manifestFilePath = Join-Path -Path $moduleDirectoryPath -ChildPath $moduleManifestFileName - [string] $prereleaseManifestFilePath = Join-Path -Path $prereleaseArtifactModuleDirectoryPath -ChildPath $moduleManifestFileName - [string] $stableManifestFilePath = Join-Path -Path $stableArtifactModuleDirectoryPath -ChildPath $moduleManifestFileName - - Write-Output "Retrieving the module manifest's current version number line from '$manifestFilePath' so it can be updated." - $manifestVersionNumberRegexPattern = "(?i)ModuleVersion = '(?.*?)'" - $manifestVersionNumberMatches = - Select-String -Path $manifestFilePath -Pattern $manifestVersionNumberRegexPattern | - Select-Object -First 1 - if ($manifestVersionNumberMatches.Matches.Count -le 0 -or - !$manifestVersionNumberMatches.Matches[0].Success) - { - throw "Could not find the manifest's current version number." - } - - $manifestVersionNumberMatch = $manifestVersionNumberMatches.Matches[0] - $currentManifestVersionNumber = $manifestVersionNumberMatch.Groups['Version'].Value - $currentManifestVersionNumberLine = $manifestVersionNumberMatch.Value - Write-Output "Copying the module files to the Prerelease artifact directory '$prereleaseArtifactModuleDirectoryPath'." Copy-Item -Path $moduleDirectoryPath -Destination $prereleaseArtifactModuleDirectoryPath -Exclude '*.Tests.ps1' -Recurse -Force Write-Output "Copying the module files to the Stable artifact directory '$stableArtifactModuleDirectoryPath'." Copy-Item -Path $moduleDirectoryPath -Destination $stableArtifactModuleDirectoryPath -Exclude '*.Tests.ps1' -Recurse -Force + Write-Output "Determining what the module manifest file paths are." + [string] $manifestFilePath = Join-Path -Path $moduleDirectoryPath -ChildPath $moduleManifestFileName + [string] $prereleaseManifestFilePath = Join-Path -Path $prereleaseArtifactModuleDirectoryPath -ChildPath $moduleManifestFileName + [string] $stableManifestFilePath = Join-Path -Path $stableArtifactModuleDirectoryPath -ChildPath $moduleManifestFileName + + Write-Output "Determining prerelease version number." # Prerelease version numbers can only contain 'a-zA-Z0-9' characters. [string] $dateTime = (Get-Date -Format 'yyyyMMddTHHmmss') [string] $truncatedCommitSha = $Env:GITHUB_SHA.Substring(0, 7) [string] $prereleaseVersionNumberPostfix = 'ci' + $dateTime + 'SHA' + $truncatedCommitSha - Write-Output "Updating the prerelease manifest's version number from '$currentManifestVersionNumber' to '$newVersionNumber$prereleaseVersionNumberPostfix'." - Replace-TextInFile -filePath $prereleaseManifestFilePath -textToReplace $currentManifestVersionNumberLine -replacementText "ModuleVersion = '$newVersionNumber'" - Replace-TextInFile -filePath $prereleaseManifestFilePath -textToReplace "# Prerelease = ''" -replacementText "Prerelease = '$prereleaseVersionNumberPostfix'" + Write-Output "Updating the prerelease manifest's version number to '$newVersionNumber$prereleaseVersionNumberPostfix'." + Update-ModuleManifest -Path $prereleaseManifestFilePath -ModuleVersion $newVersionNumber -Prerelease $prereleaseVersionNumberPostfix - Write-Output "Updating the stable manifest's version number from '$currentManifestVersionNumber' to '$newVersionNumber'." - Replace-TextInFile -filePath $stableManifestFilePath -textToReplace $currentManifestVersionNumberLine -replacementText "ModuleVersion = '$newVersionNumber'" + Write-Output "Updating the stable manifest's version number to '$newVersionNumber'." + Update-ModuleManifest -Path $stableManifestFilePath -ModuleVersion $newVersionNumber Write-Output "Testing the Prerelease manifest file '$prereleaseManifestFilePath' to ensure it is valid." Test-ModuleManifest -Path $prereleaseManifestFilePath