|
| 1 | +function Update-Toolkit { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Self-update the PowerShell Dev Toolkit from its git remote. |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + Pulls the latest changes from the toolkit's git repository, shows a |
| 8 | + summary of what changed, and re-imports the module so new commands and |
| 9 | + aliases take effect immediately. |
| 10 | +
|
| 11 | + .PARAMETER CheckOnly |
| 12 | + Only check whether updates are available without applying them. |
| 13 | +
|
| 14 | + .PARAMETER Force |
| 15 | + Skip the confirmation prompt and apply updates immediately. |
| 16 | +
|
| 17 | + .EXAMPLE |
| 18 | + Update-Toolkit |
| 19 | + .EXAMPLE |
| 20 | + Update-Toolkit -CheckOnly |
| 21 | + .EXAMPLE |
| 22 | + Update-Toolkit -Force |
| 23 | + #> |
| 24 | + [CmdletBinding()] |
| 25 | + param( |
| 26 | + [switch]$CheckOnly, |
| 27 | + [switch]$Force |
| 28 | + ) |
| 29 | + |
| 30 | + $toolkitDir = $script:ToolkitRoot |
| 31 | + |
| 32 | + if (-not (Test-Path (Join-Path $toolkitDir ".git"))) { |
| 33 | + Write-Error "Toolkit directory is not a git repository: $toolkitDir" |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + $git = Get-Command git -ErrorAction SilentlyContinue |
| 38 | + if (-not $git) { |
| 39 | + Write-Error "Git is not installed or not in PATH." |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + Push-Location $toolkitDir |
| 44 | + try { |
| 45 | + $currentBranch = git rev-parse --abbrev-ref HEAD 2>$null |
| 46 | + if (-not $currentBranch) { |
| 47 | + Write-Error "Failed to determine current git branch." |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + Write-Host "Checking for updates..." -ForegroundColor Cyan |
| 52 | + git fetch origin $currentBranch --quiet 2>$null |
| 53 | + |
| 54 | + $localHead = git rev-parse HEAD 2>$null |
| 55 | + $remoteHead = git rev-parse "origin/$currentBranch" 2>$null |
| 56 | + |
| 57 | + if ($localHead -eq $remoteHead) { |
| 58 | + Write-Host "Already up to date." -ForegroundColor Green |
| 59 | + $manifest = Import-PowerShellDataFile (Join-Path $toolkitDir "PowerShellDevToolkit\PowerShellDevToolkit.psd1") |
| 60 | + Write-Host " Version: $($manifest.ModuleVersion)" -ForegroundColor Gray |
| 61 | + Write-Host " Branch: $currentBranch" -ForegroundColor Gray |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + $behind = git rev-list --count "HEAD..origin/$currentBranch" 2>$null |
| 66 | + Write-Host "$behind new commit(s) available on $currentBranch" -ForegroundColor Yellow |
| 67 | + Write-Host "" |
| 68 | + |
| 69 | + $log = git log --oneline "HEAD..origin/$currentBranch" 2>$null |
| 70 | + if ($log) { |
| 71 | + Write-Host "Changes:" -ForegroundColor Cyan |
| 72 | + $log | ForEach-Object { Write-Host " $_" -ForegroundColor Gray } |
| 73 | + Write-Host "" |
| 74 | + } |
| 75 | + |
| 76 | + $diffStat = git diff --stat "HEAD..origin/$currentBranch" 2>$null |
| 77 | + if ($diffStat) { |
| 78 | + Write-Host "Files changed:" -ForegroundColor Cyan |
| 79 | + $diffStat | ForEach-Object { Write-Host " $_" -ForegroundColor Gray } |
| 80 | + Write-Host "" |
| 81 | + } |
| 82 | + |
| 83 | + if ($CheckOnly) { return } |
| 84 | + |
| 85 | + if (-not $Force) { |
| 86 | + Write-Host "Apply update? (Y/N): " -NoNewline -ForegroundColor Yellow |
| 87 | + $response = Read-Host |
| 88 | + if ($response -ne 'Y' -and $response -ne 'y') { |
| 89 | + Write-Host "Update cancelled." -ForegroundColor Gray |
| 90 | + return |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + Write-Host "Pulling changes..." -ForegroundColor Cyan |
| 95 | + $pullOutput = git pull origin $currentBranch 2>&1 |
| 96 | + $pullExitCode = $LASTEXITCODE |
| 97 | + |
| 98 | + if ($pullExitCode -ne 0) { |
| 99 | + Write-Host "Git pull failed:" -ForegroundColor Red |
| 100 | + $pullOutput | ForEach-Object { Write-Host " $_" -ForegroundColor Red } |
| 101 | + Write-Host "" |
| 102 | + Write-Host "You may need to resolve conflicts manually." -ForegroundColor Yellow |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + $pullOutput | ForEach-Object { Write-Host " $_" -ForegroundColor Gray } |
| 107 | + |
| 108 | + Write-Host "" |
| 109 | + Write-Host "Re-importing module..." -ForegroundColor Cyan |
| 110 | + Import-Module (Join-Path $toolkitDir "PowerShellDevToolkit") -Force -Global -DisableNameChecking |
| 111 | + |
| 112 | + $manifest = Import-PowerShellDataFile (Join-Path $toolkitDir "PowerShellDevToolkit\PowerShellDevToolkit.psd1") |
| 113 | + Write-Host "" |
| 114 | + Write-Host "Updated to version $($manifest.ModuleVersion)" -ForegroundColor Green |
| 115 | + Write-Host "All commands and aliases are now current." -ForegroundColor Green |
| 116 | + |
| 117 | + Set-ToolkitUpdateTimestamp |
| 118 | + } finally { |
| 119 | + Pop-Location |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +function Test-ToolkitUpdate { |
| 124 | + <# |
| 125 | + .SYNOPSIS |
| 126 | + Silently check if toolkit updates are available (used on shell startup). |
| 127 | +
|
| 128 | + .DESCRIPTION |
| 129 | + Compares the local HEAD against the remote. Returns $true if there are |
| 130 | + commits to pull. Respects the updateCheckDays setting in config.json |
| 131 | + so it only hits the network at the configured frequency. |
| 132 | + #> |
| 133 | + [CmdletBinding()] |
| 134 | + param() |
| 135 | + |
| 136 | + $toolkitDir = $script:ToolkitRoot |
| 137 | + |
| 138 | + if (-not (Test-Path (Join-Path $toolkitDir ".git"))) { return } |
| 139 | + if (-not (Get-Command git -ErrorAction SilentlyContinue)) { return } |
| 140 | + |
| 141 | + $stampFile = Join-Path $toolkitDir ".last-update-check" |
| 142 | + $config = Get-ScriptConfig -ErrorAction SilentlyContinue |
| 143 | + $intervalDays = 1 |
| 144 | + if ($config -and $config.toolkit -and $null -ne $config.toolkit.updateCheckDays) { |
| 145 | + $intervalDays = [int]$config.toolkit.updateCheckDays |
| 146 | + } |
| 147 | + |
| 148 | + if ($intervalDays -le 0) { return } |
| 149 | + |
| 150 | + if (Test-Path $stampFile) { |
| 151 | + $lastCheck = (Get-Item $stampFile).LastWriteTime |
| 152 | + if (([datetime]::Now - $lastCheck).TotalDays -lt $intervalDays) { return } |
| 153 | + } |
| 154 | + |
| 155 | + Push-Location $toolkitDir |
| 156 | + try { |
| 157 | + $branch = git rev-parse --abbrev-ref HEAD 2>$null |
| 158 | + if (-not $branch) { return } |
| 159 | + |
| 160 | + git fetch origin $branch --quiet 2>$null |
| 161 | + $local = git rev-parse HEAD 2>$null |
| 162 | + $remote = git rev-parse "origin/$branch" 2>$null |
| 163 | + |
| 164 | + Set-ToolkitUpdateTimestamp |
| 165 | + |
| 166 | + if ($local -ne $remote) { |
| 167 | + $behind = git rev-list --count "HEAD..origin/$branch" 2>$null |
| 168 | + Write-Host "" |
| 169 | + Write-Host "PowerShell Dev Toolkit: $behind update(s) available. Run " -NoNewline -ForegroundColor Yellow |
| 170 | + Write-Host "Update-Toolkit" -NoNewline -ForegroundColor Cyan |
| 171 | + Write-Host " to update." -ForegroundColor Yellow |
| 172 | + } |
| 173 | + } finally { |
| 174 | + Pop-Location |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +function Set-ToolkitUpdateTimestamp { |
| 179 | + <# Touches the .last-update-check stamp file. #> |
| 180 | + [CmdletBinding()] |
| 181 | + param() |
| 182 | + $stampFile = Join-Path $script:ToolkitRoot ".last-update-check" |
| 183 | + [IO.File]::WriteAllText($stampFile, (Get-Date -Format 'o')) |
| 184 | +} |
0 commit comments