-
Notifications
You must be signed in to change notification settings - Fork 0
65 update module #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
65 update module #69
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8ac8ae7
fixes refresh of update
visschersm 8146dcb
Merge pull request #68 from MatrTech/main
visschersm f905a07
fixes refresh logic
visschersm 3e35384
Merge branch '65-update-module' of https://github.com/matrtech/quickp…
visschersm cfc5a13
fixes update task
visschersm 9123b8d
fixes quickpath update
visschersm 0f33b8e
fixes module name usage
visschersm ea4f8de
adds tests for Get-MyModuleVersion function
visschersm 3f64dd9
fixes name for update
visschersm de16fa4
adds exception tests
visschersm 2a344a5
adds tests for update script
visschersm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,52 @@ | ||
| function Get-MyModuleVersion { | ||
| # Get all installed versions of the module | ||
| $moduleName = $MyInvocation.MyCommand.Module.Name | ||
| $modules = Get-Module -Name $moduleName -ListAvailable | ||
| try { | ||
| $moduleName = "quickpath" | ||
| $commandName = "qp" | ||
|
|
||
| if ($modules) { | ||
| # Filter to only include modules from the current module path, to avoid listing multiple versions in different paths | ||
| $currentPath = (Get-Module -Name $moduleName).ModuleBase | ||
| $module = Get-Module -Name $moduleName -ErrorAction SilentlyContinue | ||
| if ($module) { | ||
| return $module.Version | ||
| } | ||
|
|
||
| $modules = $modules | Where-Object { $_.ModuleBase -eq $currentPath } | ||
|
|
||
| # Sort by version and select the latest one | ||
| $latestModule = $modules | Sort-Object Version -Descending | Select-Object -First 1 | ||
| return $latestModule.Version | ||
| } else { | ||
| throw "Module 'YourModuleName' is not installed." | ||
| $module = Get-ModuleFromCommand($commandName) | ||
| if ($module) { | ||
| return $module.Version | ||
| } | ||
|
|
||
| $module = Get-ModuleFromManifest($moduleName) | ||
| if ($module) { | ||
| return $module.Version | ||
| } | ||
| Write-Error "Module version could not be found." | ||
| } | ||
| catch { | ||
| Write-Error "Could not determine module version: $($_.Exception.Message)" | ||
| } | ||
| } | ||
|
|
||
| function Get-ModuleFromCommand { | ||
| param ( | ||
| [string]$commandName | ||
| ) | ||
|
|
||
| $qpCommand = Get-Command -Name $commandName -ErrorAction SilentlyContinue | ||
| if ($qpCommand -and $qpCommand.Module) { | ||
| return $qpCommand.Module | ||
| } | ||
|
|
||
| return $null | ||
| } | ||
|
|
||
| function Get-ModuleFromManifest { | ||
| param( | ||
| [string]$moduleName = "quickpath" | ||
| ) | ||
| $manifest = "$PSScriptRoot\..\output\$moduleName\$moduleName.psd1" | ||
|
|
||
| if (Test-Path $manifest) { | ||
| $data = Test-ModuleManifest -Path $manifest -ErrorAction Stop | ||
| return $data | ||
| } | ||
|
|
||
| return $null | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,99 +1,52 @@ | ||
| function Update-QuickPath { | ||
| param( | ||
| [switch]$FromGallery | ||
| ) | ||
|
|
||
| Write-Host "Updating quickpath..." -ForegroundColor Cyan | ||
|
|
||
| if ($FromGallery) { | ||
| Update-QuickPathFromGallery | ||
| return | ||
| } | ||
|
|
||
| Update-QuickPathFromBuild | ||
| } | ||
|
|
||
| function Update-QuickPathFromGallery { | ||
| try { | ||
| Update-Module -Name quickpath -Force -ErrorAction Stop | ||
| Import-Module quickpath -Force -ErrorAction Stop | ||
| Write-Host "quickpath updated from gallery and reloaded." -ForegroundColor Green | ||
| $moduleName = "quickpath" | ||
| if (Get-Module $moduleName) { | ||
| Remove-Module $moduleName -Force -ErrorAction SilentlyContinue | ||
| } | ||
| Update-Module -Name $moduleName -Force -ErrorAction Stop | ||
| Import-Module $moduleName -Force -ErrorAction Stop | ||
| Write-Host "$moduleName updated from gallery and reloaded." -ForegroundColor Green | ||
| } | ||
| catch { | ||
| Write-Error "Failed to update quickpath from gallery: $($_.Exception.Message)" | ||
| Write-Error "Failed to update '$moduleName' from gallery: $($_.Exception.Message)" | ||
| throw | ||
| } | ||
| # [CmdletBinding()] | ||
| # param( | ||
| # [ValidateSet('Patch', 'Minor', 'Major')] | ||
| # [string]$Increment = 'Patch', | ||
|
|
||
| # [switch]$SkipTests, | ||
|
|
||
| # # Force update from the PowerShell Gallery instead of local source | ||
| # [switch]$FromGallery | ||
| # ) | ||
|
|
||
| # try { | ||
| # Write-Host "Updating quickpath..." -ForegroundColor Cyan | ||
|
|
||
| # if ($FromGallery) { | ||
|
|
||
| # Update-QuickPathFromGallery | ||
| # return | ||
| # } | ||
|
|
||
| # $buildFile = Get-BuildFile | ||
|
|
||
| # if (-not $buildFile) { | ||
| # Write-Verbose "Build script not found near '$startPath'; falling back to gallery update." | ||
| # Update-QuickPathFromGallery | ||
| # return | ||
| # } | ||
|
|
||
| # ValidateOrInstallTools | ||
|
|
||
| # # Build argument list for Invoke-Build | ||
| # $tasks = @() | ||
| # if (-not $SkipTests) { $tasks += 'Test' } | ||
| # $tasks += 'Refresh' | ||
|
|
||
| # # Execute build in the current session so Import-Module in Refresh affects this session | ||
| # Invoke-Build -File $buildFile -Increment $Increment -Task $tasks | ||
|
|
||
| # # As an extra safety, try re-importing the freshly built manifest | ||
| # $outManifest = Join-Path (Split-Path $buildFile -Parent) 'output\quickpath\quickpath.psd1' | ||
| # if (Test-Path $outManifest) { | ||
| # Import-Module $outManifest -Force -ErrorAction SilentlyContinue | ||
| # } | ||
|
|
||
| # Write-Host "quickpath has been built, tested, and reloaded." -ForegroundColor Green | ||
| # } | ||
| # catch { | ||
| # Write-Error "Failed to update quickpath: $($_.Exception.Message)" | ||
| # throw | ||
| # } | ||
| } | ||
|
|
||
| # function Update-QuickPathFromGallery { | ||
| # try { | ||
| # Update-Module -Name quickpath -Force -ErrorAction Stop | ||
| # Import-Module quickpath -Force -ErrorAction Stop | ||
| # Write-Host "quickpath updated from gallery and reloaded." -ForegroundColor Green | ||
| # } | ||
| # catch { | ||
| # Write-Error "Failed to update quickpath from gallery: $($_.Exception.Message)" | ||
| # throw | ||
| # } | ||
| # } | ||
|
|
||
| # function Get-BuildFile { | ||
| # $startPath = (Get-Location).Path | ||
| # $dir = $startPath | ||
| # $buildFile = $null | ||
| # for ($i = 0; $i -lt 6; $i++) { | ||
| # $candidate = Join-Path $dir 'quickpath.build.ps1' | ||
| # if (Test-Path $candidate) { $buildFile = $candidate; break } | ||
| # $parent = Split-Path $dir -Parent | ||
| # if ([string]::IsNullOrEmpty($parent) -or $parent -eq $dir) { break } | ||
| # $dir = $parent | ||
| # } | ||
| # return $buildFile | ||
| # } | ||
|
|
||
| # function ValidateOrInstallTools { | ||
| # # Ensure required tools are available | ||
| # if (-not (Get-Module -ListAvailable -Name InvokeBuild)) { | ||
| # Write-Verbose "Installing InvokeBuild..." | ||
| # Install-Module -Name InvokeBuild -Scope CurrentUser -Force -ErrorAction Stop | ||
| # } | ||
| # if (-not (Get-Module -ListAvailable -Name Pester)) { | ||
| # Write-Verbose "Installing Pester..." | ||
| # Install-Module -Name Pester -Scope CurrentUser -Force -ErrorAction Stop | ||
| # } | ||
| # } | ||
| function Update-QuickPathFromBuild { | ||
| try { | ||
| $moduleName = "quickpath" | ||
| $quickPathManifest = Join-Path -Path $PSScriptRoot -ChildPath "..\output\quickpath\quickpath.psd1" | ||
| if ( -not (Test-Path $quickPathManifest)) { | ||
| Write-Warning "Built quickpath module not found at '$quickPathManifest'. Please run 'Invoke-Build Build' first." | ||
| return | ||
| } | ||
|
|
||
| if (Get-Module $moduleName) { | ||
| Remove-Module $moduleName -Force -ErrorAction SilentlyContinue | ||
| } | ||
|
|
||
| Import-Module $quickPathManifest -Force -ErrorAction Stop | ||
| Write-Host "$moduleName has been built, tested, and reloaded." -ForegroundColor Green | ||
| } | ||
| catch { | ||
| Write-Error "An error occurred while updating from build: $($_.Exception.Message)" | ||
| throw | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| Describe "Get-ModuleVersion" { | ||
| BeforeAll { | ||
| . $PSScriptRoot\..\private\Get-MyModuleVersion.ps1 | ||
| } | ||
| It "Get Loaded Module version" { | ||
| $expectedVersion = "1.2.3" | ||
| Mock Get-Module { | ||
| [PSCustomObject]@{ | ||
| Version = [version]$expectedVersion | ||
| } | ||
| } | ||
|
|
||
| $actualVersion = Get-MyModuleVersion | ||
|
|
||
| $actualVersion | Should -Be $expectedVersion | ||
| } | ||
| It "Get Module version from command" { | ||
| $expectedVersion = "2.3.4" | ||
| Mock Get-Module { | ||
| return $null | ||
| } | ||
| Mock Get-Command { | ||
| [PSCustomObject]@{ | ||
| Module = [PSCustomObject]@{ | ||
| Version = [version]$expectedVersion | ||
| } | ||
| } | ||
| } | ||
|
|
||
| $actualVersion = Get-MyModuleVersion | ||
|
|
||
| $actualVersion | Should -Be $expectedVersion | ||
| } | ||
| It "Get Module version from manifest" { | ||
| $expectedVersion = "3.4.5" | ||
| Mock Get-Module { | ||
| return $null | ||
| } | ||
| Mock Get-Command { | ||
| return $null | ||
| } | ||
| Mock Test-ModuleManifest { | ||
| [PSCustomObject]@{ | ||
| Version = [version]$expectedVersion | ||
| } | ||
| } | ||
|
|
||
| $actualVersion = Get-MyModuleVersion | ||
|
|
||
| $actualVersion | Should -Be $expectedVersion | ||
| } | ||
| It 'Failed to find module version, writes error' { | ||
| Mock Get-Module { | ||
| return $null | ||
| } | ||
| Mock Get-Command { | ||
| return $null | ||
| } | ||
| Mock Test-Path { | ||
| return $false | ||
| } | ||
| Mock Write-Error {} | ||
|
|
||
| Get-MyModuleVersion | ||
|
|
||
| Assert-MockCalled Write-Error -Exactly 1 | ||
| } | ||
| It 'Catches exception and writes error' { | ||
| Mock Get-Module { | ||
| throw "Some error" | ||
| } | ||
| Mock Write-Error {} | ||
|
|
||
| Get-MyModuleVersion | ||
|
|
||
| Assert-MockCalled Write-Error -Exactly 1 | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.