Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions src/pwsh-install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ function Remove-FileIfExists([string]$Path) {
}
}

function Resolve-ProfilePath([string]$Path) {
$profileFile = Get-Item -LiteralPath $Path -Force -ErrorAction Ignore
if ($profileFile) {
return $profileFile.ResolvedTarget
}
return $Path
}

function Get-EnabledCoreutilsAliases([string]$CmdDir) {
[string[]]$disabled = @()
if ($props = Get-ItemProperty -LiteralPath $CoreutilsRegPath -Name $DisabledUtilitiesRegName -ErrorAction Ignore) {
Expand Down Expand Up @@ -78,17 +86,15 @@ function Get-InjectedSection([string]$CmdDir) {
}

function Update-PowerShellProfile([string]$Path, [bool] $Install, [bool] $UseBom, [string]$Section, [bool]$RefreshOnly) {
$parent = Split-Path -LiteralPath $Path
if ($Install -and !$RefreshOnly) {
$parent = Split-Path -LiteralPath $Path
[void](New-Item -Path $parent -ItemType Directory -Force)
}
elseif (!(Test-Path -LiteralPath $Path)) {
return
}

$profile = Get-Item -LiteralPath $Path -Force -ErrorAction Ignore
if ($profile) {
$Path = $profile.ResolvedTarget
Comment on lines -89 to -91

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR now piggy-backs a change where I do this early (down in the Add function), which allows me to clean up the Update-PowerShellProfile logic. The new Update-PowerShellProfile is much easier to read (no if/elseif).

# We cannot re-sign the profile, so skip it.
$signature = Get-AuthenticodeSignature -LiteralPath $Path -ErrorAction SilentlyContinue
if ($signature -and ($signature.Status -ne [System.Management.Automation.SignatureStatus]::NotSigned)) {
return $false
}

# Get-Content uses .NET's StreamReader, so it auto-detects UTF-8/UTF-16 with BOM.
Expand All @@ -104,7 +110,7 @@ function Update-PowerShellProfile([string]$Path, [bool] $Install, [bool] $UseBom
throw "Invalid coreutils section markers in PowerShell profile: $Path"
}
if ($RefreshOnly -and $markerCount -eq 0) {
return
return $true
}

# Strip the existing section (markers + content + any surrounding blank lines) in one shot.
Expand All @@ -123,7 +129,7 @@ function Update-PowerShellProfile([string]$Path, [bool] $Install, [bool] $UseBom

if (!$text) {
Remove-FileIfExists $Path
return
return $true
}

$text += "`r`n"
Expand All @@ -139,6 +145,8 @@ function Update-PowerShellProfile([string]$Path, [bool] $Install, [bool] $UseBom
Remove-Item -LiteralPath $newPath -Force -ErrorAction Ignore
throw
}

return $true
}

function Get-MsiPwshProfilePaths {
Expand Down Expand Up @@ -231,6 +239,8 @@ function Get-ProfilePlan([bool] $Install, [string]$Scope) {
return $null
}

$Path = Resolve-ProfilePath $Path

$existing = $plan[$Path]
if ($existing) {
return $existing
Expand Down Expand Up @@ -286,6 +296,9 @@ function Get-RefreshProfilePlan {
if (!$Path) {
return
}

$Path = Resolve-ProfilePath $Path

if ($plan[$Path]) {
return
}
Expand Down Expand Up @@ -325,7 +338,10 @@ else {
}

foreach ($entry in $plan) {
Update-PowerShellProfile -Path $entry.Path -Install $entry.Install -UseBom $false -Section $section -RefreshOnly $refresh
if (!Update-PowerShellProfile -Path $entry.Path -Install $entry.Install -UseBom $false -Section $section -RefreshOnly $refresh) {
# Update failed: Skip the record update.
$entry.RecordSid = $null
}
}

# Only adjust records once every Update succeeded. A failure mid-loop leaves
Expand Down