Skip to content

Commit bb6e125

Browse files
author
Uwe Janke
committed
feat: Add Fallback-Chain Update System (PSGallery -> GitHub -> UNC with warnings)
1 parent 4fd89a3 commit bb6e125

1 file changed

Lines changed: 189 additions & 38 deletions

File tree

sqmSQLTool.psm1

Lines changed: 189 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -190,41 +190,118 @@ foreach ($func in $allFunctions)
190190
}
191191

192192
# Update-Funktionen explizit exportieren (definiert im PSM1, nicht in Public\)
193-
Export-ModuleMember -Function 'Test-sqmModuleUpdate', 'Update-sqmModule'
193+
Export-ModuleMember -Function 'Test-sqmModuleUpdate', 'Test-sqmModuleUpdate-PSGallery', 'Test-sqmModuleUpdate-GitHub', 'Test-sqmModuleUpdate-UNC', 'Update-sqmModule'
194194

195195
# =============================================================================
196-
# Update-Mechanismus
196+
# Update-Mechanismus mit Fallback-Chain
197197
# =============================================================================
198+
# Priorität:
199+
# 1. PowerShell Gallery (PSGallery)
200+
# 2. GitHub Releases (fallback)
201+
# 3. UNC-Pfad (fallback)
202+
# 4. Warnung ausgeben wenn alle fehlschlagen
198203

199204
<#
200205
.SYNOPSIS
201-
Prueft ob eine neuere Version des MSSQLTools-Moduls im konfigurierten Repository verfuegbar ist.
202-
.PARAMETER RepositoryPath
203-
Pfad zum Update-Repository (ueberschreibt die Konfiguration).
204-
.PARAMETER Credential
205-
Credentials fuer UNC-Freigaben.
206+
Prueft ob eine neuere Version des Moduls in PowerShell Gallery verfuegbar ist.
207+
.PARAMETER ModuleName
208+
Name des Moduls (Standard: sqmSQLTool).
206209
#>
207-
function Test-sqmModuleUpdate
210+
function Test-sqmModuleUpdate-PSGallery
208211
{
209212
[CmdletBinding()]
210-
param (
211-
[string]$RepositoryPath = $script:sqmModuleConfig['UpdateRepository'],
212-
[System.Management.Automation.PSCredential]$Credential
213-
)
214-
if (-not $RepositoryPath)
213+
param ([string]$ModuleName = 'sqmSQLTool')
214+
215+
try
215216
{
216-
Write-Verbose "Kein Update-Repository konfiguriert."
217-
return $false
217+
$galleryModule = Find-Module -Name $ModuleName -Repository PSGallery -ErrorAction Stop
218+
$currentVersion = [version]$script:sqmModuleConfig['ModuleVersion']
219+
$galleryVersion = [version]$galleryModule.Version
220+
221+
if ($galleryVersion -gt $currentVersion)
222+
{
223+
Write-Verbose "PSGallery: Neuere Version verfuegbar ($($galleryModule.Version))"
224+
return @{
225+
Source = 'PSGallery'
226+
Version = $galleryVersion
227+
UpdateCommand = "Update-Module -Name $ModuleName -Repository PSGallery -Force"
228+
}
229+
}
230+
return $null
218231
}
219-
# Guard: check if the repository root is reachable before any path operations.
220-
# This prevents Join-Path from throwing when a drive letter (e.g. W:) does not exist.
221-
if (-not (Test-Path -Path $RepositoryPath -ErrorAction SilentlyContinue))
232+
catch
222233
{
223-
Write-Verbose "Update-Repository nicht erreichbar: $RepositoryPath"
224-
return $false
234+
Write-Verbose "PSGallery-Check fehlgeschlagen: $($_.Exception.Message)"
235+
return $null
225236
}
237+
}
238+
239+
<#
240+
.SYNOPSIS
241+
Prueft ob eine neuere Version auf GitHub Releases verfuegbar ist.
242+
.PARAMETER GitHubRepo
243+
GitHub Repository (Format: owner/repo, z.B. JankeUwe/sqmSQLTool).
244+
#>
245+
function Test-sqmModuleUpdate-GitHub
246+
{
247+
[CmdletBinding()]
248+
param ([string]$GitHubRepo = 'JankeUwe/sqmSQLTool')
249+
250+
try
251+
{
252+
$latestRelease = Invoke-RestMethod -Uri "https://api.github.com/repos/$GitHubRepo/releases/latest" `
253+
-Headers @{'Accept' = 'application/vnd.github+json'} `
254+
-ErrorAction Stop
255+
256+
# Version aus Tag extrahieren (v1.4.0.0 -> 1.4.0.0)
257+
$tagVersion = $latestRelease.tag_name -replace '^v', ''
258+
$currentVersion = [version]$script:sqmModuleConfig['ModuleVersion']
259+
$releaseVersion = [version]$tagVersion
260+
261+
if ($releaseVersion -gt $currentVersion)
262+
{
263+
Write-Verbose "GitHub: Neuere Version verfuegbar ($tagVersion)"
264+
return @{
265+
Source = 'GitHub'
266+
Version = $releaseVersion
267+
URL = $latestRelease.html_url
268+
UpdateCommand = "# ZIP von $($latestRelease.html_url) herunterladen und manuell entpacken"
269+
}
270+
}
271+
return $null
272+
}
273+
catch
274+
{
275+
Write-Verbose "GitHub-Check fehlgeschlagen: $($_.Exception.Message)"
276+
return $null
277+
}
278+
}
279+
280+
<#
281+
.SYNOPSIS
282+
Prueft ob eine neuere Version im UNC-Pfad verfuegbar ist.
283+
.PARAMETER RepositoryPath
284+
UNC-Pfad zum Repository.
285+
#>
286+
function Test-sqmModuleUpdate-UNC
287+
{
288+
[CmdletBinding()]
289+
param ([string]$RepositoryPath = $script:sqmModuleConfig['UpdateRepository'])
290+
226291
try
227292
{
293+
if (-not $RepositoryPath)
294+
{
295+
Write-Verbose "Kein UNC-Repository konfiguriert."
296+
return $null
297+
}
298+
299+
if (-not (Test-Path -Path $RepositoryPath -ErrorAction SilentlyContinue))
300+
{
301+
Write-Verbose "UNC-Repository nicht erreichbar: $RepositoryPath"
302+
return $null
303+
}
304+
228305
$remoteVersionFile = Join-Path $RepositoryPath 'ModuleVersion.txt'
229306
if (Test-Path -Path $remoteVersionFile -ErrorAction SilentlyContinue)
230307
{
@@ -240,26 +317,65 @@ function Test-sqmModuleUpdate
240317
}
241318
else
242319
{
243-
Write-Verbose "Keine Versionsinformation im Repository gefunden."
244-
return $false
320+
Write-Verbose "Keine Versionsinformation im UNC-Repository gefunden."
321+
return $null
245322
}
246323
}
324+
247325
$currentVersion = [version]$script:sqmModuleConfig['ModuleVersion']
248326
$newVersion = [version]$remoteVersion
249-
return $newVersion -gt $currentVersion
327+
328+
if ($newVersion -gt $currentVersion)
329+
{
330+
Write-Verbose "UNC: Neuere Version verfuegbar ($newVersion)"
331+
return @{
332+
Source = 'UNC'
333+
Version = $newVersion
334+
Path = $RepositoryPath
335+
UpdateCommand = "Update-sqmModule -RepositoryPath '$RepositoryPath'"
336+
}
337+
}
338+
return $null
250339
}
251340
catch
252341
{
253-
Write-Warning "Update-Pruefung fehlgeschlagen: $($_.Exception.Message)"
254-
return $false
342+
Write-Verbose "UNC-Check fehlgeschlagen: $($_.Exception.Message)"
343+
return $null
255344
}
256345
}
257346

258347
<#
259348
.SYNOPSIS
260-
Aktualisiert das MSSQLTools-Modul aus dem konfigurierten Repository.
349+
Prueft ob eine neuere Version des Moduls verfuegbar ist (Fallback-Chain: PSGallery -> GitHub -> UNC).
350+
.PARAMETER Credential
351+
Credentials fuer UNC-Freigaben.
352+
#>
353+
function Test-sqmModuleUpdate
354+
{
355+
[CmdletBinding()]
356+
param (
357+
[System.Management.Automation.PSCredential]$Credential
358+
)
359+
360+
# Fallback-Chain: PSGallery -> GitHub -> UNC
361+
$updateResult = Test-sqmModuleUpdate-PSGallery -ModuleName 'sqmSQLTool'
362+
if ($updateResult) { return $updateResult }
363+
364+
$updateResult = Test-sqmModuleUpdate-GitHub -GitHubRepo 'JankeUwe/sqmSQLTool'
365+
if ($updateResult) { return $updateResult }
366+
367+
$updateResult = Test-sqmModuleUpdate-UNC -RepositoryPath $script:sqmModuleConfig['UpdateRepository']
368+
if ($updateResult) { return $updateResult }
369+
370+
# Keine neuere Version in keinem Repository gefunden
371+
return $null
372+
}
373+
374+
<#
375+
.SYNOPSIS
376+
Aktualisiert das sqmSQLTool-Modul (nur UNC-Repositories unterstuezt; PSGallery/GitHub sind manuell).
261377
.PARAMETER RepositoryPath
262-
Pfad zum Update-Repository.
378+
Pfad zum Update-Repository (UNC-Pfad).
263379
.PARAMETER Credential
264380
Credentials fuer UNC-Freigaben.
265381
.PARAMETER Backup
@@ -287,18 +403,21 @@ function Update-sqmModule
287403
Write-Error "Repository-Pfad '$RepositoryPath' nicht erreichbar."
288404
return
289405
}
290-
if (-not $Force -and -not (Test-sqmModuleUpdate -RepositoryPath $RepositoryPath))
406+
407+
$updateInfo = Test-sqmModuleUpdate-UNC -RepositoryPath $RepositoryPath
408+
if (-not $Force -and -not $updateInfo)
291409
{
292410
Write-Host "Keine neuere Version verfuegbar." -ForegroundColor Green
293411
return
294412
}
413+
295414
if ($PSCmdlet.ShouldProcess("Modul aus '$RepositoryPath' aktualisieren", "Update"))
296415
{
297416
try
298417
{
299418
if ($Backup)
300419
{
301-
$backupDir = Join-Path $env:TEMP "MSSQLTools_Backup_$(Get-Date -Format 'yyyyMMdd_HHmsqm')"
420+
$backupDir = Join-Path $env:TEMP "sqmSQLTool_Backup_$(Get-Date -Format 'yyyyMMdd_HHmmss')"
302421
Copy-Item -Path $currentModulePath -Destination $backupDir -Recurse -Force -ErrorAction Stop
303422
Write-Host "Backup erstellt: $backupDir" -ForegroundColor Gray
304423
}
@@ -323,20 +442,52 @@ function Update-sqmModule
323442
}
324443
}
325444

326-
# Auto-update on module import (only when AutoUpdate = $true, repository is configured and reachable)
327-
if ($script:sqmModuleConfig['AutoUpdate'] -and $script:sqmModuleConfig['UpdateRepository'])
445+
# Auto-update on module import (only when AutoUpdate = $true)
446+
if ($script:sqmModuleConfig['AutoUpdate'])
328447
{
329-
$noUpdate = $env:MSSQLTOOLS_SKIP_AUTO_UPDATE -eq '1'
330-
$repoPath = $script:sqmModuleConfig['UpdateRepository']
331-
$repoReachable = Test-Path -Path $repoPath -ErrorAction SilentlyContinue
332-
if (-not $noUpdate -and $repoReachable)
448+
$noUpdate = $env:SQMSQLTOOL_SKIP_AUTO_UPDATE -eq '1'
449+
if (-not $noUpdate)
333450
{
334451
try
335452
{
336-
if (Test-sqmModuleUpdate)
453+
$updateInfo = Test-sqmModuleUpdate
454+
if ($updateInfo)
337455
{
338-
Write-Host "New module version available. Running update..." -ForegroundColor Cyan
339-
Update-sqmModule -Force -Backup
456+
Write-Host "`n[sqmSQLTool] Neuere Version verfuegbar: $($updateInfo.Version) (via $($updateInfo.Source))" -ForegroundColor Cyan
457+
458+
if ($updateInfo.Source -eq 'UNC')
459+
{
460+
# UNC: Automatisches Update
461+
Write-Host "[sqmSQLTool] Fuehre Update durch..." -ForegroundColor Cyan
462+
Update-sqmModule -Force -Backup
463+
}
464+
elseif ($updateInfo.Source -eq 'PSGallery')
465+
{
466+
# PSGallery: Hinweis auf Update-Module
467+
Write-Warning @"
468+
[sqmSQLTool] Update verfuegbar in PowerShell Gallery!
469+
470+
Zum Aktualisieren bitte ausfuehren:
471+
Update-Module -Name sqmSQLTool -Repository PSGallery -Force
472+
473+
Oder via Install-Module:
474+
Install-Module -Name sqmSQLTool -Repository PSGallery -Force -AllowClobber
475+
"@
476+
}
477+
elseif ($updateInfo.Source -eq 'GitHub')
478+
{
479+
# GitHub: Hinweis auf GitHub Release
480+
Write-Warning @"
481+
[sqmSQLTool] Update verfuegbar auf GitHub!
482+
483+
Release: $($updateInfo.URL)
484+
485+
Zum Aktualisieren:
486+
1. ZIP herunterladen von: $($updateInfo.URL)/releases/download/v$($updateInfo.Version)/sqmSQLTool-v$($updateInfo.Version).zip
487+
2. Entpacken nach: `$PROFILE\..\Modules\sqmSQLTool\
488+
3. PowerShell neu starten oder: Remove-Module sqmSQLTool; Import-Module sqmSQLTool
489+
"@
490+
}
340491
}
341492
}
342493
catch

0 commit comments

Comments
 (0)