From 3577f91d82082f2a6e5467539c319ab4e11fde0c Mon Sep 17 00:00:00 2001 From: HUMORCE Date: Fri, 20 Sep 2024 14:18:13 +0800 Subject: [PATCH] fix(commands): Handling broken aliases (#6141) --- CHANGELOG.md | 1 + lib/commands.ps1 | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 76c04c961d..356486b519 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - **scoop-download|install|update:** Fallback to default downloader when aria2 fails ([#4292](https://github.com/ScoopInstaller/Scoop/issues/4292)) - **decompress**: `Expand-7zipArchive` only delete temp dir / `$extractDir` if it is empty ([#6092](https://github.com/ScoopInstaller/Scoop/issues/6092)) +- **commands**: Handling broken aliases ([#6141](https://github.com/ScoopInstaller/Scoop/issues/6141)) ### Code Refactoring diff --git a/lib/commands.ps1 b/lib/commands.ps1 index 04775de357..6812aebd52 100644 --- a/lib/commands.ps1 +++ b/lib/commands.ps1 @@ -4,7 +4,7 @@ function command_files { (Get-ChildItem "$PSScriptRoot\..\libexec") + (Get-ChildItem "$scoopdir\shims") | - Where-Object 'scoop-.*?\.ps1$' -Property Name -Match + Where-Object 'scoop-.*?\.ps1$' -Property Name -Match } function commands { @@ -86,7 +86,9 @@ function rm_alias { } info "Removing alias '$name'..." - Remove-Item "$(shimdir $false)\scoop-$name.ps1" + if (Test-Path "$(shimdir $false)\scoop-$name.ps1") { + Remove-Item "$(shimdir $false)\scoop-$name.ps1" + } $aliases.PSObject.Properties.Remove($name) set_config ALIAS $aliases | Out-Null } @@ -98,11 +100,19 @@ function list_aliases { $aliases = get_config ALIAS ([PSCustomObject]@{}) $alias_info = $aliases.PSObject.Properties.Name | Where-Object { $_ } | ForEach-Object { + # Mark the alias as , if the alias script file does NOT exist. + if (!(Test-Path "$(shimdir $false)\scoop-$_.ps1")) { + [PSCustomObject]@{ + Name = $_ + Command = '' + } + return + } $content = Get-Content (command_path $_) [PSCustomObject]@{ Name = $_ - Summary = (summary $content).Trim() Command = ($content | Select-Object -Skip 1).Trim() + Summary = (summary $content).Trim() } } if (!$alias_info) {