mirror of
https://github.com/tiennm99/ccs.git
synced 2026-07-16 12:15:57 +00:00
fix(ccs): improve environment variable handling and error resilience
- use proper .NET enum for environment variable targets - add try-catch blocks for environment variable operations - provide user-friendly warnings when operations fail - fixes PATH operations in install and uninstall scripts
This commit is contained in:
@@ -144,11 +144,16 @@ if ($EnvVars.PSObject.Properties.Count -gt 0) {
|
||||
$VarName = $Property.Name
|
||||
$VarValue = $Property.Value
|
||||
|
||||
# Save original value
|
||||
$OriginalEnvVars[$VarName] = [System.Environment]::GetEnvironmentVariable($VarName, 'Process')
|
||||
try {
|
||||
# Save original value
|
||||
$OriginalEnvVars[$VarName] = [System.Environment]::GetEnvironmentVariable($VarName, [System.EnvironmentVariableTarget]::Process)
|
||||
|
||||
# Set new value for this process only
|
||||
[System.Environment]::SetEnvironmentVariable($VarName, $VarValue, 'Process')
|
||||
# Set new value for this process only
|
||||
[System.Environment]::SetEnvironmentVariable($VarName, $VarValue, [System.EnvironmentVariableTarget]::Process)
|
||||
} catch {
|
||||
Write-Host "Warning: Could not set environment variable '$VarName'" -ForegroundColor Yellow
|
||||
Write-Host " Error: $($_.Exception.Message)" -ForegroundColor Yellow
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,12 +169,16 @@ try {
|
||||
# Restore original environment variables
|
||||
foreach ($VarName in $OriginalEnvVars.Keys) {
|
||||
$OriginalValue = $OriginalEnvVars[$VarName]
|
||||
if ($null -eq $OriginalValue) {
|
||||
# Variable didn't exist before, remove it
|
||||
[System.Environment]::SetEnvironmentVariable($VarName, $null, 'Process')
|
||||
} else {
|
||||
# Restore original value
|
||||
[System.Environment]::SetEnvironmentVariable($VarName, $OriginalValue, 'Process')
|
||||
try {
|
||||
if ($null -eq $OriginalValue) {
|
||||
# Variable didn't exist before, remove it
|
||||
[System.Environment]::SetEnvironmentVariable($VarName, $null, [System.EnvironmentVariableTarget]::Process)
|
||||
} else {
|
||||
# Restore original value
|
||||
[System.Environment]::SetEnvironmentVariable($VarName, $OriginalValue, [System.EnvironmentVariableTarget]::Process)
|
||||
}
|
||||
} catch {
|
||||
Write-Host "Warning: Could not restore environment variable '$VarName'" -ForegroundColor Yellow
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -235,7 +235,7 @@ Write-Host "└─"
|
||||
Write-Host ""
|
||||
|
||||
# Check and update PATH
|
||||
$UserPath = [Environment]::GetEnvironmentVariable("Path", "User")
|
||||
$UserPath = [Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User)
|
||||
if ($UserPath -notlike "*$CcsDir*") {
|
||||
Write-Host "[!] PATH Configuration Required"
|
||||
Write-Host ""
|
||||
@@ -243,7 +243,7 @@ if ($UserPath -notlike "*$CcsDir*") {
|
||||
|
||||
try {
|
||||
$NewPath = if ($UserPath) { "$UserPath;$CcsDir" } else { $CcsDir }
|
||||
[Environment]::SetEnvironmentVariable("Path", $NewPath, "User")
|
||||
[Environment]::SetEnvironmentVariable("Path", $NewPath, [System.EnvironmentVariableTarget]::User)
|
||||
|
||||
Write-Host " [OK] PATH updated. Restart your terminal for changes to take effect."
|
||||
Write-Host ""
|
||||
|
||||
+2
-2
@@ -20,11 +20,11 @@ if (Test-Path "$CcsDir\ccs.ps1") {
|
||||
$UninstallScript = "$CcsDir\uninstall.ps1"
|
||||
|
||||
# Remove from PATH
|
||||
$UserPath = [Environment]::GetEnvironmentVariable("Path", "User")
|
||||
$UserPath = [Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User)
|
||||
if ($UserPath -like "*$CcsDir*") {
|
||||
try {
|
||||
$NewPath = ($UserPath -split ';' | Where-Object { $_ -ne $CcsDir }) -join ';'
|
||||
[Environment]::SetEnvironmentVariable("Path", $NewPath, "User")
|
||||
[Environment]::SetEnvironmentVariable("Path", $NewPath, [System.EnvironmentVariableTarget]::User)
|
||||
Write-Host "[OK] Removed from PATH: $CcsDir"
|
||||
Write-Host " Restart your terminal for changes to take effect."
|
||||
} catch {
|
||||
|
||||
Reference in New Issue
Block a user