Skip to content

Commit

Permalink
ci: Use native cmdlet to simplify updating the module manifests
Browse files Browse the repository at this point in the history
  • Loading branch information
deadlydog committed Mar 5, 2024
1 parent 12da21e commit 4e43409
Showing 1 changed file with 10 additions and 36 deletions.
46 changes: 10 additions & 36 deletions .github/workflows/build-and-test-powershell-module.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = '(?<Version>.*?)'"
$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
Expand Down

0 comments on commit 4e43409

Please sign in to comment.