Skip to content

Commit 7c4f588

Browse files
committed
ci: Simplify code to update module manifests
1 parent ae05f11 commit 7c4f588

File tree

1 file changed

+10
-37
lines changed

1 file changed

+10
-37
lines changed

.github/workflows/build-and-test-powershell-module.yml

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,6 @@ jobs:
139139
- name: Create Stable and Prerelease module artifacts
140140
shell: pwsh
141141
run: |
142-
function Replace-TextInFile(
143-
[ValidateScript({Test-Path $_ -PathType Leaf})]
144-
[string]$filePath,
145-
[string]$textToReplace,
146-
[string]$replacementText)
147-
{
148-
$fileContents = Get-Content -Path $filePath -Raw
149-
$newFileContents = $fileContents.Replace($textToReplace, $replacementText)
150-
Set-Content -Path $filePath -Value $newFileContents
151-
}
152-
153142
Write-Output "Reading in environment variables."
154143
[string] $moduleName = $Env:powerShellModuleName
155144
[string] $moduleDirectoryPath = $Env:powerShellModuleDirectoryPath
@@ -160,43 +149,28 @@ jobs:
160149
Write-Output "Reading in dynamic environment variables."
161150
[string] $newVersionNumber = $Env:NewVersionNumber
162151
163-
Write-Output "Determining what the module manifest file paths should be."
164-
[string] $manifestFilePath = Join-Path -Path $moduleDirectoryPath -ChildPath $moduleManifestFileName
165-
[string] $prereleaseManifestFilePath = Join-Path -Path $prereleaseArtifactModuleDirectoryPath -ChildPath $moduleManifestFileName
166-
[string] $stableManifestFilePath = Join-Path -Path $stableArtifactModuleDirectoryPath -ChildPath $moduleManifestFileName
167-
168-
Write-Output "Retrieving the module manifest's current version number line from '$manifestFilePath' so it can be updated."
169-
$manifestVersionNumberRegexPattern = "(?i)ModuleVersion = '(?<Version>.*?)'"
170-
$manifestVersionNumberMatches =
171-
Select-String -Path $manifestFilePath -Pattern $manifestVersionNumberRegexPattern |
172-
Select-Object -First 1
173-
if ($manifestVersionNumberMatches.Matches.Count -le 0 -or
174-
!$manifestVersionNumberMatches.Matches[0].Success)
175-
{
176-
throw "Could not find the manifest's current version number."
177-
}
178-
179-
$manifestVersionNumberMatch = $manifestVersionNumberMatches.Matches[0]
180-
$currentManifestVersionNumber = $manifestVersionNumberMatch.Groups['Version'].Value
181-
$currentManifestVersionNumberLine = $manifestVersionNumberMatch.Value
182-
183152
Write-Output "Copying the module files to the Prerelease artifact directory '$prereleaseArtifactModuleDirectoryPath'."
184153
Copy-Item -Path $moduleDirectoryPath -Destination $prereleaseArtifactModuleDirectoryPath -Exclude '*.Tests.ps1' -Recurse -Force
185154
186155
Write-Output "Copying the module files to the Stable artifact directory '$stableArtifactModuleDirectoryPath'."
187156
Copy-Item -Path $moduleDirectoryPath -Destination $stableArtifactModuleDirectoryPath -Exclude '*.Tests.ps1' -Recurse -Force
188157
158+
Write-Output "Determining what the module manifest file paths are."
159+
[string] $manifestFilePath = Join-Path -Path $moduleDirectoryPath -ChildPath $moduleManifestFileName
160+
[string] $prereleaseManifestFilePath = Join-Path -Path $prereleaseArtifactModuleDirectoryPath -ChildPath $moduleManifestFileName
161+
[string] $stableManifestFilePath = Join-Path -Path $stableArtifactModuleDirectoryPath -ChildPath $moduleManifestFileName
162+
163+
Write-Output "Determining prerelease version number."
189164
# Prerelease version numbers can only contain 'a-zA-Z0-9' characters.
190165
[string] $dateTime = (Get-Date -Format 'yyyyMMddTHHmmss')
191166
[string] $truncatedCommitSha = $Env:GITHUB_SHA.Substring(0, 7)
192167
[string] $prereleaseVersionNumberPostfix = 'ci' + $dateTime + 'SHA' + $truncatedCommitSha
193168
194-
Write-Output "Updating the prerelease manifest's version number from '$currentManifestVersionNumber' to '$newVersionNumber$prereleaseVersionNumberPostfix'."
195-
Replace-TextInFile -filePath $prereleaseManifestFilePath -textToReplace $currentManifestVersionNumberLine -replacementText "ModuleVersion = '$newVersionNumber'"
196-
Replace-TextInFile -filePath $prereleaseManifestFilePath -textToReplace "# Prerelease = ''" -replacementText "Prerelease = '$prereleaseVersionNumberPostfix'"
169+
Write-Output "Updating the prerelease manifest's version number to '$newVersionNumber$prereleaseVersionNumberPostfix'."
170+
Update-ModuleManifest -Path $prereleaseManifestFilePath -ModuleVersion $newVersionNumber -Prerelease $prereleaseVersionNumberPostfix
197171
198-
Write-Output "Updating the stable manifest's version number from '$currentManifestVersionNumber' to '$newVersionNumber'."
199-
Replace-TextInFile -filePath $stableManifestFilePath -textToReplace $currentManifestVersionNumberLine -replacementText "ModuleVersion = '$newVersionNumber'"
172+
Write-Output "Updating the stable manifest's version number to '$newVersionNumber'."
173+
Update-ModuleManifest -Path $stableManifestFilePath -ModuleVersion $newVersionNumber
200174
201175
Write-Output "Testing the Prerelease manifest file '$prereleaseManifestFilePath' to ensure it is valid."
202176
Test-ModuleManifest -Path $prereleaseManifestFilePath
@@ -213,7 +187,6 @@ jobs:
213187
Write-Output "Copying the deployment files '$deployFilesDirectoryPath' to the deployment artifact directory '$deployFilesArtifactDirectoryPath'."
214188
Copy-Item -Path $deployFilesDirectoryPath -Destination $deployFilesArtifactDirectoryPath -Recurse -Force
215189
216-
217190
- name: Update CI version tag and set new version tag
218191
# Only run this step if we are doing a push (not a PR) to the default branch (e.g. main).
219192
if: github.event_name != 'pull_request' && github.ref == format('refs/heads/{0}', github.event.repository.default_branch)

0 commit comments

Comments
 (0)