Skip to content

Commit bad45dc

Browse files
committed
Two simple tools added, plus minor corrections in tests
1 parent 8b4138d commit bad45dc

4 files changed

Lines changed: 43 additions & 21 deletions

File tree

src/ContinuousDelphi.Tools/Public/Add-ToSystemPath.ps1

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function Add-ToSystemPath {
3434
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
3535
}
3636

37-
function Normalize-DirectoryPath {
37+
function Format-DirectoryPath {
3838
param(
3939
[Parameter(Mandatory = $true)]
4040
[string]$Path
@@ -59,7 +59,7 @@ function Add-ToSystemPath {
5959
return $Path.Trim().TrimEnd('\').ToUpperInvariant()
6060
}
6161

62-
function Split-PathEntries {
62+
function Split-PathEntry {
6363
param(
6464
[AllowNull()]
6565
[string]$PathValue
@@ -76,30 +76,35 @@ function Add-ToSystemPath {
7676
}
7777

7878
function Update-CurrentSessionPath {
79+
[CmdletBinding(SupportsShouldProcess)]
80+
param()
81+
7982
$machinePath = [Environment]::GetEnvironmentVariable('Path', 'Machine')
8083
$userPath = [Environment]::GetEnvironmentVariable('Path', 'User')
8184

8285
$combined = @()
8386

8487
if (-not [string]::IsNullOrWhiteSpace($machinePath)) {
85-
$combined += Split-PathEntries -PathValue $machinePath
88+
$combined += Split-PathEntry -PathValue $machinePath
8689
}
8790

8891
if (-not [string]::IsNullOrWhiteSpace($userPath)) {
89-
$combined += Split-PathEntries -PathValue $userPath
92+
$combined += Split-PathEntry -PathValue $userPath
9093
}
9194

92-
$env:Path = ($combined -join ';')
95+
if ($PSCmdlet.ShouldProcess('env:Path', 'Update')) {
96+
$env:Path = ($combined -join ';')
97+
}
9398
}
9499

95100
if (-not (Test-IsAdministrator)) {
96101
throw 'Administrator privileges are required to modify the system PATH. Re-run PowerShell as Administrator.'
97102
}
98103

99-
$normalizedPathToAdd = Normalize-DirectoryPath -Path $PathToAdd
104+
$normalizedPathToAdd = Format-DirectoryPath -Path $PathToAdd
100105

101106
$currentMachinePath = [Environment]::GetEnvironmentVariable('Path', 'Machine')
102-
$currentEntries = Split-PathEntries -PathValue $currentMachinePath
107+
$currentEntries = Split-PathEntry -PathValue $currentMachinePath
103108

104109
$existingKeys = @{}
105110
foreach ($entry in $currentEntries) {
@@ -109,10 +114,10 @@ function Add-ToSystemPath {
109114
$newKey = Get-NormalizedPathKey -Path $normalizedPathToAdd
110115

111116
if ($existingKeys.ContainsKey($newKey)) {
112-
Write-Host ''
113-
Write-Host 'Path already exists in the system PATH:' -ForegroundColor Yellow
114-
Write-Host " $normalizedPathToAdd"
115-
Write-Host ''
117+
Write-Information ''
118+
Write-Information 'Path already exists in the system PATH:'
119+
Write-Information " $normalizedPathToAdd"
120+
Write-Information ''
116121
Update-CurrentSessionPath
117122
return
118123
}
@@ -127,14 +132,14 @@ function Add-ToSystemPath {
127132
[Environment]::SetEnvironmentVariable('Path', $newMachinePath, 'Machine')
128133
Update-CurrentSessionPath
129134

130-
Write-Host ''
131-
Write-Host 'Added to system PATH:' -ForegroundColor Green
132-
Write-Host " $normalizedPathToAdd"
133-
Write-Host ''
134-
Write-Host 'The machine PATH has been updated.' -ForegroundColor Green
135-
Write-Host 'The current PowerShell session PATH has also been refreshed.' -ForegroundColor Green
136-
Write-Host ''
137-
Write-Host 'New processes will see the updated PATH immediately.' -ForegroundColor White
138-
Write-Host 'Some already-running applications may need to be restarted.' -ForegroundColor White
139-
Write-Host ''
135+
Write-Information ''
136+
Write-Information 'Added to system PATH:'
137+
Write-Information " $normalizedPathToAdd"
138+
Write-Information ''
139+
Write-Information 'The machine PATH has been updated.'
140+
Write-Information 'The current PowerShell session PATH has also been refreshed.'
141+
Write-Information ''
142+
Write-Information 'New processes will see the updated PATH immediately.'
143+
Write-Information 'Some already-running applications may need to be restarted.'
144+
Write-Information ''
140145
}

tests/run-tests.bat

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@echo off
2+
setlocal
3+
4+
set "SCRIPT_DIR=%~dp0"
5+
6+
pwsh -NoProfile -ExecutionPolicy Bypass -File "%SCRIPT_DIR%run-tests.ps1"
7+
8+
set "EXITCODE=%ERRORLEVEL%"
9+
pause
10+
11+
endlocal & exit /b %EXITCODE%

tests/run-tests.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
Set-StrictMode -Version Latest
55
$ErrorActionPreference = 'Stop'
66

7+
# PesterConfig.psd1 uses paths relative to the repo root, so ensure CWD is
8+
# the repo root regardless of where this script was invoked from.
9+
Set-Location (Split-Path $PSScriptRoot -Parent)
10+
711
$configPath = Join-Path $PSScriptRoot 'PesterConfig.psd1'
812

913
Write-Host 'Running Pester tests...'

tools/open-repo.bat

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@echo off
2+
start "" "https://github.com/continuous-delphi/cd-tool-pwsh"

0 commit comments

Comments
 (0)