From 52b1ea5289932247abd7950ede0d2c4d5183a94b Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Sat, 17 Oct 2026 09:56:46 +0200 Subject: [PATCH] Resolve freshly installed bootstrap tools --- bootstrap.ps1 | 52 +++++++++++++++++++++++++++++++++------ tests/Bootstrap.Tests.ps1 | 28 ++++++++++++++++++++- 2 files changed, 71 insertions(+), 9 deletions(-) diff --git a/bootstrap.ps1 b/bootstrap.ps1 index 1a88615..beb986c 100644 --- a/bootstrap.ps1 +++ b/bootstrap.ps1 @@ -30,12 +30,45 @@ function Invoke-BootstrapWingetInstall { if ($LASTEXITCODE -ne 0) { throw "WinGet failed to install '$Id' (exit $LASTEXITCODE)." } } -function Get-PowerShell7Path { +function Find-PowerShell7Path { $command = Get-Command pwsh.exe -ErrorAction SilentlyContinue if ($command) { return $command.Source } - $installedPath = Join-Path $env:ProgramFiles 'PowerShell\7\pwsh.exe' - if (Test-Path -LiteralPath $installedPath -PathType Leaf) { return $installedPath } - throw 'PowerShell 7 was installed but pwsh.exe could not be located. Open a new terminal and rerun bootstrap.ps1.' + if (-not [string]::IsNullOrWhiteSpace($env:ProgramFiles)) { + $installedPath = Join-Path $env:ProgramFiles 'PowerShell\7\pwsh.exe' + if (Test-Path -LiteralPath $installedPath -PathType Leaf) { return $installedPath } + } + return $null +} + +function Find-GitPath { + $command = Get-Command git.exe -ErrorAction SilentlyContinue + if ($command) { return $command.Source } + + $candidates = @() + if (-not [string]::IsNullOrWhiteSpace($env:ProgramFiles)) { + $candidates += (Join-Path $env:ProgramFiles 'Git\cmd\git.exe') + $candidates += (Join-Path $env:ProgramFiles 'Git\bin\git.exe') + } + $programFilesX86 = [Environment]::GetEnvironmentVariable('ProgramFiles(x86)') + if (-not [string]::IsNullOrWhiteSpace($programFilesX86)) { + $candidates += (Join-Path $programFilesX86 'Git\cmd\git.exe') + } + if (-not [string]::IsNullOrWhiteSpace($env:LOCALAPPDATA)) { + $candidates += (Join-Path $env:LOCALAPPDATA 'Programs\Git\cmd\git.exe') + } + foreach ($candidate in $candidates) { + if (Test-Path -LiteralPath $candidate -PathType Leaf) { return $candidate } + } + return $null +} + +function Update-BootstrapProcessPath { + $pathValues = @( + [Environment]::GetEnvironmentVariable('Path', 'Machine'), + [Environment]::GetEnvironmentVariable('Path', 'User'), + $env:Path + ) | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } + $env:Path = $pathValues -join ';' } function Ensure-WinGet { @@ -77,11 +110,14 @@ if ([string]::IsNullOrWhiteSpace($Repository) -or [string]::IsNullOrWhiteSpace($ Write-Host 'Checking WinGet...' Ensure-WinGet -if (-not (Get-Command git.exe -ErrorAction SilentlyContinue)) { Invoke-BootstrapWingetInstall -Id 'Git.Git' } -if (-not (Get-Command pwsh.exe -ErrorAction SilentlyContinue)) { Invoke-BootstrapWingetInstall -Id 'Microsoft.PowerShell' } -$powerShell7 = Get-PowerShell7Path +if (-not (Find-GitPath)) { Invoke-BootstrapWingetInstall -Id 'Git.Git' } +if (-not (Find-PowerShell7Path)) { Invoke-BootstrapWingetInstall -Id 'Microsoft.PowerShell' } +Update-BootstrapProcessPath -$git = (Get-Command git.exe -ErrorAction Stop).Source +$git = Find-GitPath +if (-not $git) { throw 'Git was installed but git.exe could not be located.' } +$powerShell7 = Find-PowerShell7Path +if (-not $powerShell7) { throw 'PowerShell 7 was installed but pwsh.exe could not be located.' } if (Test-Path -LiteralPath (Join-Path $Checkout '.git')) { Push-Location $Checkout try { diff --git a/tests/Bootstrap.Tests.ps1 b/tests/Bootstrap.Tests.ps1 index 6832e8d..cdc2039 100644 --- a/tests/Bootstrap.Tests.ps1 +++ b/tests/Bootstrap.Tests.ps1 @@ -1,6 +1,20 @@ +BeforeAll { + $script:bootstrapPath = "$PSScriptRoot/../bootstrap.ps1" + $tokens = $null + $errors = $null + $script:bootstrapAst = [System.Management.Automation.Language.Parser]::ParseFile($bootstrapPath, [ref]$tokens, [ref]$errors) + if ($errors.Count -gt 0) { throw "bootstrap.ps1 does not parse: $($errors[0].Message)" } + + $functionAst = $bootstrapAst.Find({ + param($node) + $node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq 'Find-GitPath' + }, $true) + . ([scriptblock]::Create($functionAst.Extent.Text)) +} + Describe 'Bootstrap invocation' { It 'rejects in-memory execution because elevation requires a script file' { - $source = Get-Content "$PSScriptRoot/../bootstrap.ps1" -Raw + $source = Get-Content $bootstrapPath -Raw $bootstrap = [scriptblock]::Create($source) $previousLocalAppData = $env:LOCALAPPDATA try { @@ -10,4 +24,16 @@ Describe 'Bootstrap invocation' { } finally { $env:LOCALAPPDATA = $previousLocalAppData } } + + It 'finds a fresh machine-scope Git install before PATH refreshes' { + $previousProgramFiles = $env:ProgramFiles + try { + Mock Get-Command { $null } -ParameterFilter { $Name -eq 'git.exe' } + $env:ProgramFiles = $TestDrive + $expected = Join-Path $TestDrive 'Git\cmd\git.exe' + New-Item -ItemType File -Path $expected -Force | Out-Null + Find-GitPath | Should -Be $expected + } + finally { $env:ProgramFiles = $previousProgramFiles } + } }