Skip to content

Refactor module functions with begin/process/end and harden module maintenance flow#46

Merged
rwidmark merged 7 commits into
devfrom
copilot/add-begin-process-end-blocks
Apr 16, 2026
Merged

Refactor module functions with begin/process/end and harden module maintenance flow#46
rwidmark merged 7 commits into
devfrom
copilot/add-begin-process-end-blocks

Conversation

Copilot AI commented Apr 16, 2026

Copy link
Copy Markdown

This updates the module functions to use PowerShell begin/process/end execution blocks and tightens error handling around repository and module operations. It also simplifies the maintenance flow so update, install-missing, and old-version cleanup behave more consistently.

  • Execution flow

    • Refactored exported functions to use begin/process/end where pipeline-aware behavior makes sense.
    • Added a small internal helper to normalize installed module metadata before update/uninstall decisions.
  • Error handling

    • Added targeted try/catch around Get-InstalledModule, Find-Module, Update-Module, Install-Module, Uninstall-Module, and PSGallery trust configuration.
    • Switched failure points to explicit -ErrorAction Stop where recovery or reporting is required.
  • Control-flow cleanup

    • Separated installed-module discovery from missing-module tracking so Update-rsModule can reliably handle both.
    • Fixed old-version cleanup so uninstall decisions are based on a single collected version set instead of repeated ad hoc calls.
    • Improved ShouldProcess usage for update, install, uninstall, and PSGallery trust changes.
  • Optimization

    • Reduced repeated module/version processing by centralizing version selection and module detail shaping.
    • Normalized version sorting/comparison through parsed version values rather than repeated inline conversions.
  • Operational messaging

    • Tightened status/warning output to better distinguish:
      • no installed modules found
      • requested modules not installed
      • repository versions unavailable for a module

Example of the refactored pattern used across functions:

begin {
    $requestedModules = [System.Collections.Generic.List[string]]::new()
}

process {
    foreach ($moduleName in @($Module)) {
        if (-not [string]::IsNullOrWhiteSpace($moduleName)) {
            [void]$requestedModules.Add($moduleName)
        }
    }
}

end {
    $modulesToProcess = if ($requestedModules.Count -gt 0) { $requestedModules.ToArray() } else { $null }
    $getModuleInfo = Get-rsInstalledModule -Module $modulesToProcess
}

Copilot AI and others added 7 commits April 16, 2026 17:42
Agent-Logs-Url: https://github.com/rwidmark/MaintainModule/sessions/110e489a-bc1a-4e49-9063-b756ca88703f

Co-authored-by: rwidmark <76907327+rwidmark@users.noreply.github.com>
Agent-Logs-Url: https://github.com/rwidmark/MaintainModule/sessions/110e489a-bc1a-4e49-9063-b756ca88703f

Co-authored-by: rwidmark <76907327+rwidmark@users.noreply.github.com>
Agent-Logs-Url: https://github.com/rwidmark/MaintainModule/sessions/110e489a-bc1a-4e49-9063-b756ca88703f

Co-authored-by: rwidmark <76907327+rwidmark@users.noreply.github.com>
Agent-Logs-Url: https://github.com/rwidmark/MaintainModule/sessions/110e489a-bc1a-4e49-9063-b756ca88703f

Co-authored-by: rwidmark <76907327+rwidmark@users.noreply.github.com>
Agent-Logs-Url: https://github.com/rwidmark/MaintainModule/sessions/110e489a-bc1a-4e49-9063-b756ca88703f

Co-authored-by: rwidmark <76907327+rwidmark@users.noreply.github.com>
Agent-Logs-Url: https://github.com/rwidmark/MaintainModule/sessions/110e489a-bc1a-4e49-9063-b756ca88703f

Co-authored-by: rwidmark <76907327+rwidmark@users.noreply.github.com>
Agent-Logs-Url: https://github.com/rwidmark/MaintainModule/sessions/110e489a-bc1a-4e49-9063-b756ca88703f

Co-authored-by: rwidmark <76907327+rwidmark@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the MaintainModule PowerShell module to be more pipeline-aware (via begin/process/end) while hardening repository/module maintenance operations with more explicit error handling and consolidated module/version shaping.

Changes:

  • Introduces an internal helper (Get-rsModuleDetail) to normalize installed module metadata and version selection.
  • Refactors exported functions to use begin/process/end blocks and improves ShouldProcess usage around update/install/uninstall and PSGallery trust changes.
  • Centralizes version parsing/sorting to reduce repeated module/version processing during maintenance.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +102 to +110
foreach ($currentModule in @($Module)) {
if ([string]::IsNullOrWhiteSpace($currentModule)) {
continue
}

Write-Output "START - Uninstall older versions of $currentModule"
Write-Output "Please wait, this can take some time..."

foreach ($_version in $versionsToRemove) {

Copilot AI Apr 16, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uninstall-rsModule currently becomes a no-op when -Module is omitted (the process loop only iterates over provided module names and there is no fallback path). This conflicts with the help text/examples that imply omitting -Module uninstalls old versions for all installed modules. Either implement the all-modules behavior (e.g., discover installed modules + old versions and loop them) or make -Module mandatory and update the comment-based help accordingly.

Suggested change
foreach ($currentModule in @($Module)) {
if ([string]::IsNullOrWhiteSpace($currentModule)) {
continue
}
Write-Output "START - Uninstall older versions of $currentModule"
Write-Output "Please wait, this can take some time..."
foreach ($_version in $versionsToRemove) {
$modulesToProcess = @($Module | Where-Object { -not [string]::IsNullOrWhiteSpace($_) })
if ($modulesToProcess.Count -eq 0) {
try {
$modulesToProcess = @(Get-InstalledModule -AllVersions -ErrorAction Stop | Select-Object -ExpandProperty Name -Unique)
}
catch {
Write-Error "Failed to discover installed modules. $($PSItem.Exception.Message)"
return
}
}
foreach ($currentModule in $modulesToProcess) {
Write-Output "START - Uninstall older versions of $currentModule"
Write-Output "Please wait, this can take some time..."
try {
$installedVersions = @(Get-InstalledModule -Name $currentModule -AllVersions -ErrorAction Stop | Sort-Object Version -Descending)
}
catch {
Write-Error "Failed to get installed versions for $($currentModule). $($PSItem.Exception.Message)"
continue
}
if ($versionsToRemove.Count -gt 0) {
$moduleVersionsToRemove = @(
$installedVersions |
Where-Object { $versionsToRemove -contains $_.Version } |
ForEach-Object { $_.Version }
)
}
else {
$moduleVersionsToRemove = @(
$installedVersions |
Select-Object -Skip 1 |
ForEach-Object { $_.Version }
)
}
if ($moduleVersionsToRemove.Count -eq 0) {
Write-Output "FINISHED - No older versions of $currentModule were found to uninstall."
continue
}
foreach ($_version in $moduleVersionsToRemove) {

Copilot uses AI. Check for mistakes.
ErrorAction = 'Stop'
}
Write-Output "$($_module.Name) has now been updated to version $($CollectLatestVersion)!"

Copilot AI Apr 16, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update-rsModule uses Find-Module results to decide whether an update is required, but it does not pass -AllowPrerelease when $AllowPrerelease is $true. This can cause prerelease updates to be skipped because the version comparison is based only on stable versions. Consider adding AllowPrerelease = $AllowPrerelease to $findModuleParameters when enabled so discovery and update behavior stay consistent.

Suggested change
if ($AllowPrerelease) {
$findModuleParameters.AllowPrerelease = $AllowPrerelease
}

Copilot uses AI. Check for mistakes.
@rwidmark rwidmark merged commit a300375 into dev Apr 16, 2026
4 checks passed
@rwidmark rwidmark deleted the copilot/add-begin-process-end-blocks branch April 16, 2026 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants