Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- **depends:** Avoid digits in archive file extension (except for .7z and .001) ([#4915](https://github.com/ScoopInstaller/Scoop/issues/4915))
- **bucket:** Don't check remote URL of non-git buckets ([#4923](https://github.com/ScoopInstaller/Scoop/issues/4923))
- **bucket:** Don't write message OK before bucket is cloned ([#4925](https://github.com/ScoopInstaller/Scoop/issues/4925))
- **shim:** Add 'Get-CommandPath()' to find git ([#4913](https://github.com/ScoopInstaller/Scoop/issues/4913))
- **shim:** Remove character replacement in .cmd -> .ps1 shims ([#4914](https://github.com/ScoopInstaller/Scoop/issues/4914))
- **scoop:** Pass CLI arguments as string objects ([#4931](https://github.com/ScoopInstaller/Scoop/issues/4931))
- **scoop-info:** Fix error message when manifest is not found ([#4935](https://github.com/ScoopInstaller/Scoop/issues/4935))
Expand Down
49 changes: 48 additions & 1 deletion lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,39 @@ function Get-HelperPath {
}
}

function Get-CommandPath {
[CmdletBinding()]
[OutputType([String])]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[String]
$Command
)

begin {
$userShims = Convert-Path (shimdir $false)
$globalShims = fullpath (shimdir $true) # don't resolve: may not exist
}

process {
try {
$comm = Get-Command $Command -ErrorAction Stop
} catch {
return $null
}
$commandPath = if ($comm.Path -like "$userShims*" -or $comm.Path -like "$globalShims*") {
Get-ShimTarget ($comm.Path -replace '\.exe$', '.shim')
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If a bin includes both *.ps1 and *.exe shims (like aria2c):

> gcm aria2c -all
...\shims\aria2c.ps1
...\shims\aria2c.exe

The command scoop which aria2c show 'aria2c' not found / not a scoop shim..

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.

The new shim system should use only .exe shims, and .ps1 shims are only used for .cmd or bash script.

Try remove all shims except scoop.* and scoop reset *, and this should be fixed.

} elseif ($comm.CommandType -eq 'Application') {
$comm.Source
} elseif ($comm.CommandType -eq 'Alias') {
Get-CommandPath $comm.ResolvedCommandName
} else {
$null
}
return $commandPath
}
}

function Test-HelperInstalled {
[CmdletBinding()]
param(
Expand Down Expand Up @@ -599,6 +632,20 @@ function get_app_name_from_shim($shim) {
return get_app_name $content
}

function Get-ShimTarget($ShimPath) {
if ($ShimPath) {
$shimTarget = if ($ShimPath.EndsWith('.shim')) {
(Get-Content -Path $ShimPath | Select-Object -First 1).Replace('path = ', '').Replace('"', '')
} else {
((Select-String -Path $ShimPath -Pattern '^(?:@rem|#)\s*(.*)$').Matches.Groups | Select-Object -Index 1).Value
}
if (!$shimTarget) {
$shimTarget = ((Select-String -Path $ShimPath -Pattern '[''"]([^@&]*?)[''"]' -AllMatches).Matches.Groups | Select-Object -Last 1).Value
}
$shimTarget | Convert-Path
}
}

function warn_on_overwrite($shim, $path) {
if (!(Test-Path $shim)) {
return
Expand Down Expand Up @@ -726,7 +773,7 @@ function shim($path, $global, $name, $arg) {
} else {
warn_on_overwrite "$shim.cmd" $path
# find path to Git's bash so that batch scripts can run bash scripts
$gitdir = (Get-Item (Get-Command git -CommandType:Application -ErrorAction:Stop).Source -ErrorAction:Stop).Directory.Parent
$gitdir = (Get-Item (Get-CommandPath git) -ErrorAction:Stop).Directory.Parent
if ($gitdir.FullName -imatch 'mingw') {
$gitdir = $gitdir.Parent
}
Expand Down
14 changes: 0 additions & 14 deletions libexec/scoop-shim.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,6 @@ function Get-ShimPath($ShimName, $Global) {
}
}

function Get-ShimTarget($ShimPath) {
if ($ShimPath) {
$shimTarget = if ($ShimPath.EndsWith('.shim')) {
(Get-Content -Path $ShimPath | Select-Object -First 1).Replace('path = ', '').Replace('"', '')
} else {
((Select-String -Path $ShimPath -Pattern '^(?:@rem|#)\s*(.*)$').Matches.Groups | Select-Object -Index 1).Value
}
if (!$shimTarget) {
$shimTarget = ((Select-String -Path $ShimPath -Pattern '[''"]([^@&]*?)[''"]' -AllMatches).Matches.Groups | Select-Object -Last 1).Value
}
$shimTarget | Convert-Path
}
}

switch ($SubCommand) {
'add' {
if ($commandPath -notmatch '[\\/]') {
Expand Down
40 changes: 6 additions & 34 deletions libexec/scoop-which.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,12 @@ if (!$command) {
exit 1
}

try {
$gcm = Get-Command "$command" -ErrorAction Stop
} catch {
abort "'$command' not found" 3
}

$path = $gcm.Path
$usershims = Convert-Path (shimdir $false)
$globalshims = fullpath (shimdir $true) # don't resolve: may not exist

if ($path -like "$usershims*" -or $path -like "$globalshims*") {
$exepath = if ($path.EndsWith('.exe') -or $path.EndsWith('.shim')) {
(Get-Content ($path -replace '\.exe$', '.shim') | Select-Object -First 1).Replace('path = ', '').Replace('"', '')
} else {
((Select-String -Path $path -Pattern '^(?:@rem|#)\s*(.*)$').Matches.Groups | Select-Object -Index 1).Value
}
if (!$exepath) {
$exepath = ((Select-String -Path $path -Pattern '[''"]([^@&]*?)[''"]' -AllMatches).Matches.Groups | Select-Object -Last 1).Value
}

if (![System.IO.Path]::IsPathRooted($exepath)) {
# Expand relative path
$exepath = Convert-Path $exepath
}
$path = Get-CommandPath $command

friendly_path $exepath
} elseif ($gcm.CommandType -eq 'Application') {
$gcm.Source
} elseif ($gcm.CommandType -eq 'Alias') {
scoop which $gcm.ResolvedCommandName
} else {
Write-Host 'Not a scoop shim.'
$path
if ($null -eq $path) {
Write-Host "'$command' not found / not a scoop shim."
exit 2
} else {
friendly_path $path
exit 0
}

exit 0