diff --git a/src/pwsh-install.ps1 b/src/pwsh-install.ps1 index 1553e83..981c83d 100644 --- a/src/pwsh-install.ps1 +++ b/src/pwsh-install.ps1 @@ -86,8 +86,14 @@ function Update-PowerShellProfile([string]$Path, [bool] $Install, [bool] $UseBom return } + $writePath = Get-ProfileWritePath $Path + $isSymlink = $writePath -ne $Path + if ($Install -and $isSymlink) { + [void](New-Item -Path (Split-Path -LiteralPath $writePath) -ItemType Directory -Force) + } + # Get-Content uses .NET's StreamReader, so it auto-detects UTF-8/UTF-16 with BOM. - $text = Get-Content -LiteralPath $Path -Raw -ErrorAction Ignore + $text = Get-Content -LiteralPath $writePath -Raw -ErrorAction Ignore if (!$text) { $text = '' } @@ -117,7 +123,12 @@ function Update-PowerShellProfile([string]$Path, [bool] $Install, [bool] $UseBom } if (!$text) { - Remove-FileIfExists $Path + if ($isSymlink) { + [System.IO.File]::WriteAllText($writePath, '', [System.Text.UTF8Encoding]::new($UseBom)) + } + else { + Remove-FileIfExists $Path + } return } @@ -125,10 +136,10 @@ function Update-PowerShellProfile([string]$Path, [bool] $Install, [bool] $UseBom $encoding = [System.Text.UTF8Encoding]::new($UseBom) # Atomic write: stage as .new, then replace. - $newPath = "$Path.new" + $newPath = "$writePath.new" try { [System.IO.File]::WriteAllText($newPath, $text, $encoding) - [System.IO.File]::Move($newPath, $Path, $true) + [System.IO.File]::Move($newPath, $writePath, $true) } catch { Remove-Item -LiteralPath $newPath -Force -ErrorAction Ignore