|
| 1 | +<# |
| 2 | + SPDX-License-Identifier: MIT |
| 3 | + Copyright (c) 2026 Leon McClatchey, Linktech Engineering LLC |
| 4 | +
|
| 5 | + Package: VSCode-Updater |
| 6 | + Author: Leon McClatchey |
| 7 | + Company: Linktech Engineering LLC |
| 8 | + Created: 2026-07-11 |
| 9 | + Modified: 2026-07-11 |
| 10 | + File: Private/UnifiedPreCleanup.ps1 |
| 11 | + Version: 1.0.0 |
| 12 | + Description: Description goes here |
| 13 | +#> |
| 14 | + |
| 15 | + |
| 16 | +function Invoke-VSUPreCleanup { |
| 17 | + |
| 18 | + param( |
| 19 | + [string]$InstallRoot |
| 20 | + ) |
| 21 | + |
| 22 | + Write-Log "[PRE] Starting unified pre‑installation cleanup" |
| 23 | + |
| 24 | + # ===================================================================== |
| 25 | + # 1. Kill ALL VS Code processes |
| 26 | + # ===================================================================== |
| 27 | + |
| 28 | + $vsProcesses = @( |
| 29 | + "Code", |
| 30 | + "Code - Insiders", |
| 31 | + "CodeHelper", |
| 32 | + "CodeHelper.exe", |
| 33 | + "CodeHelper64", |
| 34 | + "CodeHelper32" |
| 35 | + ) |
| 36 | + |
| 37 | + foreach ($p in $vsProcesses) { |
| 38 | + Get-Process -Name $p -ErrorAction SilentlyContinue | |
| 39 | + ForEach-Object { |
| 40 | + Write-Log "[PRE] Killing VS Code process: $($_.Name) (PID=$($_.Id))" |
| 41 | + Stop-Process -Id $_.Id -Force -ErrorAction SilentlyContinue |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + # ===================================================================== |
| 46 | + # 2. Kill ALL installer/bootstrapper/worker processes |
| 47 | + # ===================================================================== |
| 48 | + |
| 49 | + $installerProcesses = @( |
| 50 | + "VSCodeSetup", |
| 51 | + "CodeSetup", |
| 52 | + "setup", |
| 53 | + "is-", |
| 54 | + "innosetup" |
| 55 | + ) |
| 56 | + |
| 57 | + foreach ($p in $installerProcesses) { |
| 58 | + Get-Process -Name $p -ErrorAction SilentlyContinue | |
| 59 | + ForEach-Object { |
| 60 | + Write-Log "[PRE] Killing installer/bootstrapper: $($_.Name) (PID=$($_.Id))" |
| 61 | + Stop-Process -Id $_.Id -Force -ErrorAction SilentlyContinue |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + # ===================================================================== |
| 66 | + # 3. Remove symlink if it exists |
| 67 | + # ===================================================================== |
| 68 | + |
| 69 | + $symlinkPath = Join-Path $InstallRoot "VSCode" |
| 70 | + |
| 71 | + if (Test-Path $symlinkPath) { |
| 72 | + $item = Get-Item $symlinkPath -Force |
| 73 | + |
| 74 | + if ($item.Attributes -match "ReparsePoint") { |
| 75 | + Write-Log "[PRE] Removing VSCode junction: $symlinkPath" |
| 76 | + Remove-Item $symlinkPath -Force -ErrorAction SilentlyContinue |
| 77 | + } |
| 78 | + else { |
| 79 | + Write-Log "[PRE] VSCode path exists but is not a junction — removing" |
| 80 | + Remove-Item $symlinkPath -Recurse -Force -ErrorAction SilentlyContinue |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + # ===================================================================== |
| 85 | + # 4. Prune old installation folders (keep newest two) |
| 86 | + # ===================================================================== |
| 87 | + |
| 88 | + Write-Log "[PRE] Pruning old installation folders" |
| 89 | + |
| 90 | + $patternOld = '^VSCode-\d{8}-\d{6}$' |
| 91 | + $patternNew = '^VSCode-\d{14}$' |
| 92 | + |
| 93 | + $folders = Get-ChildItem $InstallRoot -Directory | |
| 94 | + Where-Object { |
| 95 | + $_.Name -match $patternOld -or |
| 96 | + $_.Name -match $patternNew |
| 97 | + } | |
| 98 | + Sort-Object Name -Descending |
| 99 | + |
| 100 | + if ($folders.Count -gt 2) { |
| 101 | + $toDelete = $folders | Select-Object -Skip 2 |
| 102 | + foreach ($f in $toDelete) { |
| 103 | + Write-Log "[PRE] Removing old version folder: $($f.FullName)" |
| 104 | + Remove-Item $f.FullName -Recurse -Force -ErrorAction SilentlyContinue |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + # ===================================================================== |
| 109 | + # 5. Remove debris (staging, partial installs, temp folders) |
| 110 | + # ===================================================================== |
| 111 | + |
| 112 | + Write-Log "[PRE] Removing debris" |
| 113 | + |
| 114 | + $debrisPatterns = @( |
| 115 | + "new_*", |
| 116 | + "tmp_*", |
| 117 | + "partial_*", |
| 118 | + "is-*.tmp", |
| 119 | + "is-*.bin", |
| 120 | + "innosetup.tmp" |
| 121 | + ) |
| 122 | + |
| 123 | + foreach ($pattern in $debrisPatterns) { |
| 124 | + Get-ChildItem $InstallRoot -Filter $pattern -ErrorAction SilentlyContinue | |
| 125 | + ForEach-Object { |
| 126 | + Write-Log "[PRE] Removing debris: $($_.FullName)" |
| 127 | + Remove-Item $_.FullName -Recurse -Force -ErrorAction SilentlyContinue |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + # ===================================================================== |
| 132 | + # 6. Clean installation path (your existing function) |
| 133 | + # ===================================================================== |
| 134 | + |
| 135 | + Write-Log "[PRE] Cleaning installation path" |
| 136 | + CleanCodePath | Out-Null |
| 137 | + |
| 138 | + # ===================================================================== |
| 139 | + # 7. Validate environment |
| 140 | + # ===================================================================== |
| 141 | + |
| 142 | + Write-Log "[PRE] Validating installer environment" |
| 143 | + Test-InstallerEnvironment | Out-Null |
| 144 | + |
| 145 | + # ===================================================================== |
| 146 | + # 8. Freeze remediation (optional) |
| 147 | + # ===================================================================== |
| 148 | + |
| 149 | + if ($script:VSU_SafeInstallerMode) { |
| 150 | + Write-Log "[PRE] Safe installer mode enabled — applying freeze remediation" |
| 151 | + Invoke-InstallerFreezeRemediation | Out-Null |
| 152 | + } |
| 153 | + else { |
| 154 | + Write-Log "[PRE] Safe installer mode disabled — skipping freeze remediation" |
| 155 | + } |
| 156 | + |
| 157 | + Write-Log "[PRE] Unified pre‑installation cleanup complete" |
| 158 | +} |
0 commit comments