Skip to content

Commit d2a9021

Browse files
🩹 [Patch]: Require PSSemVer and improve logging (#44)
## Description This pull request includes several updates and improvements to the `Publish-PSModule` function in the `scripts/helpers/Publish-PSModule.ps1` file. The changes focus on enhancing logging, error handling, and parameter management for PowerShell Gallery operations. Enhancements to logging and error handling: * Added `Write-Output` statements to log the process of finding modules and prerelease versions in the PowerShell Gallery. [[1]](diffhunk://#diff-780715ac24c6bbe21b54f3d268778136b79f21a62275bbd494dfa857b4ba40d9R173-R177) [[2]](diffhunk://#diff-780715ac24c6bbe21b54f3d268778136b79f21a62275bbd494dfa857b4ba40d9L263-R275) * Replaced `Write-Output $_` with `throw $_` to improve error handling during retries. Parameter management improvements: * Introduced a hashtable `$params` to manage parameters for the `Find-PSResource` cmdlet, improving code readability and maintainability. Minor corrections: * Corrected the casing of `Write-GitHubNotice` from `Write-GithubNotice` to ensure consistent function naming. [[1]](diffhunk://#diff-780715ac24c6bbe21b54f3d268778136b79f21a62275bbd494dfa857b4ba40d9L335-R347) [[2]](diffhunk://#diff-780715ac24c6bbe21b54f3d268778136b79f21a62275bbd494dfa857b4ba40d9L376-R388) Module dependencies: * Added `PSSemVer` to the list of required modules in the script header. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [x] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas
1 parent 12425f8 commit d2a9021

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

scripts/helpers/Publish-PSModule.ps1

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#Requires -Modules Utilities, PowerShellGet, Microsoft.PowerShell.PSResourceGet, Retry, GitHub
1+
#Requires -Modules Utilities, PowerShellGet, Microsoft.PowerShell.PSResourceGet, Retry, GitHub, PSSemVer
22

33
function Publish-PSModule {
44
<#
@@ -170,9 +170,11 @@ function Publish-PSModule {
170170
LogGroup 'Get latest version - PSGallery' {
171171
try {
172172
Retry -Count 5 -Delay 10 {
173+
Write-Output "Finding module [$Name] in the PowerShell Gallery."
173174
$latest = Find-PSResource -Name $Name -Repository PSGallery -Verbose:$false
175+
Write-Output ($latest | Format-Table | Out-String)
174176
} -Catch {
175-
Write-Output $_
177+
throw $_
176178
}
177179
$psGalleryVersion = New-PSSemVer -Version $latest.Version
178180
} catch {
@@ -260,7 +262,17 @@ function Publish-PSModule {
260262
$newVersionString = "$($newVersion.Major).$($newVersion.Minor).$($newVersion.Patch)"
261263

262264
# PowerShell Gallery
263-
$psGalleryPrereleases = Find-PSResource -Name $Name -Repository PSGallery -Verbose:$false -Version * -Prerelease
265+
$params = @{
266+
Name = $Name
267+
Version = '*'
268+
Prerelease = $true
269+
Repository = 'PSGallery'
270+
Verbose = $false
271+
ErrorAction = 'SilentlyContinue'
272+
}
273+
Write-Output 'Finding the latest prerelease version in the PowerShell Gallery.'
274+
Write-Output ($params | Format-Table | Out-String)
275+
$psGalleryPrereleases = Find-PSResource @params
264276
$psGalleryPrereleases = $psGalleryPrereleases | Where-Object { $_.Version -like "$newVersionString" }
265277
$psGalleryPrereleases = $psGalleryPrereleases | Where-Object { $_.Prerelease -like "$prereleaseName*" }
266278
$latestPSGalleryPrerelease = $psGalleryPrereleases.Prerelease | ForEach-Object {
@@ -332,7 +344,7 @@ function Publish-PSModule {
332344
if ($whatIf) {
333345
Write-Output "gh pr comment $($pull_request.number) -b 'Published to the PowerShell Gallery [$publishPSVersion]($psGalleryReleaseLink) has been created.'"
334346
} else {
335-
Write-GithubNotice "Module [$Name - $publishPSVersion] published to the PowerShell Gallery."
347+
Write-GitHubNotice "Module [$Name - $publishPSVersion] published to the PowerShell Gallery."
336348
gh pr comment $pull_request.number -b "Module [$Name - $publishPSVersion]($psGalleryReleaseLink) published to the PowerShell Gallery."
337349
if ($LASTEXITCODE -ne 0) {
338350
Write-Error 'Failed to comment on the pull request.'
@@ -373,7 +385,7 @@ function Publish-PSModule {
373385
exit $LASTEXITCODE
374386
}
375387
}
376-
Write-GithubNotice "Release created: [$newVersion]"
388+
Write-GitHubNotice "Release created: [$newVersion]"
377389
}
378390
}
379391

0 commit comments

Comments
 (0)