mirror of
https://github.com/tiennm99/ccs.git
synced 2026-10-18 18:15:15 +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:
1 parent
84020edbde
commit
eac9e39d79
3 files changed
+23
-14
No files matched your search
@@ -144,11 +144,16 @@ if ($EnvVars.PSObject.Properties.Count -gt 0) {
|
|||||||
$VarName = $Property.Name
|
$VarName = $Property.Name
|
||||||
$VarValue = $Property.Value
|
$VarValue = $Property.Value
|
||||||
|
|
||||||
# Save original value
|
try {
|
||||||
$OriginalEnvVars[$VarName] = [System.Environment]::GetEnvironmentVariable($VarName, 'Process')
|
# Save original value
|
||||||
|
$OriginalEnvVars[$VarName] = [System.Environment]::GetEnvironmentVariable($VarName, [System.EnvironmentVariableTarget]::Process)
|
||||||
|
|
||||||
# Set new value for this process only
|
# Set new value for this process only
|
||||||
[System.Environment]::SetEnvironmentVariable($VarName, $VarValue, 'Process')
|
[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
|
# Restore original environment variables
|
||||||
foreach ($VarName in $OriginalEnvVars.Keys) {
|
foreach ($VarName in $OriginalEnvVars.Keys) {
|
||||||
$OriginalValue = $OriginalEnvVars[$VarName]
|
$OriginalValue = $OriginalEnvVars[$VarName]
|
||||||
if ($null -eq $OriginalValue) {
|
try {
|
||||||
# Variable didn't exist before, remove it
|
if ($null -eq $OriginalValue) {
|
||||||
[System.Environment]::SetEnvironmentVariable($VarName, $null, 'Process')
|
# Variable didn't exist before, remove it
|
||||||
} else {
|
[System.Environment]::SetEnvironmentVariable($VarName, $null, [System.EnvironmentVariableTarget]::Process)
|
||||||
# Restore original value
|
} else {
|
||||||
[System.Environment]::SetEnvironmentVariable($VarName, $OriginalValue, 'Process')
|
# 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 ""
|
Write-Host ""
|
||||||
|
|
||||||
# Check and update PATH
|
# Check and update PATH
|
||||||
$UserPath = [Environment]::GetEnvironmentVariable("Path", "User")
|
$UserPath = [Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User)
|
||||||
if ($UserPath -notlike "*$CcsDir*") {
|
if ($UserPath -notlike "*$CcsDir*") {
|
||||||
Write-Host "[!] PATH Configuration Required"
|
Write-Host "[!] PATH Configuration Required"
|
||||||
Write-Host ""
|
Write-Host ""
|
||||||
@@ -243,7 +243,7 @@ if ($UserPath -notlike "*$CcsDir*") {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
$NewPath = if ($UserPath) { "$UserPath;$CcsDir" } else { $CcsDir }
|
$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 " [OK] PATH updated. Restart your terminal for changes to take effect."
|
||||||
Write-Host ""
|
Write-Host ""
|
||||||
|
|||||||
+2
-2
@@ -20,11 +20,11 @@ if (Test-Path "$CcsDir\ccs.ps1") {
|
|||||||
$UninstallScript = "$CcsDir\uninstall.ps1"
|
$UninstallScript = "$CcsDir\uninstall.ps1"
|
||||||
|
|
||||||
# Remove from PATH
|
# Remove from PATH
|
||||||
$UserPath = [Environment]::GetEnvironmentVariable("Path", "User")
|
$UserPath = [Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User)
|
||||||
if ($UserPath -like "*$CcsDir*") {
|
if ($UserPath -like "*$CcsDir*") {
|
||||||
try {
|
try {
|
||||||
$NewPath = ($UserPath -split ';' | Where-Object { $_ -ne $CcsDir }) -join ';'
|
$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 "[OK] Removed from PATH: $CcsDir"
|
||||||
Write-Host " Restart your terminal for changes to take effect."
|
Write-Host " Restart your terminal for changes to take effect."
|
||||||
} catch {
|
} catch {
|
||||||
|
|||||||
Reference in new issue
Block a user