Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(shim): Add 'Get-CommandPath()' to find git #4913

Merged
merged 8 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
tweak: Use 'Get-ShimTarget()' that from 'scoop-shim'
  • Loading branch information
niheaven committed May 24, 2022
commit 5ba63e847bb34b7d3122ae6f32018bc57306969b
61 changes: 33 additions & 28 deletions lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -321,42 +321,33 @@ function Get-HelperPath {
function Get-CommandPath {
[CmdletBinding()]
[OutputType([String])]
param([Parameter(Mandatory = $true, ValueFromPipeline = $true)][String]$Command)
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[String]
$Command
)

begin {
$CommandPath = $null
$gcm = Get-Command $Command -ErrorAction Ignore
$path = $gcm.Path
$usershims = Convert-Path (shimdir $false)
$globalshims = fullpath (shimdir $true) # don't resolve: may not exist
$userShims = Convert-Path (shimdir $false)
$globalShims = fullpath (shimdir $true) # don't resolve: may not exist
}

process {
$CommandPath = 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
}

friendly_path $exepath
} elseif ($gcm.CommandType -eq 'Application') {
$gcm.Source
} elseif ($gcm.CommandType -eq 'Alias') {
Get-CommandPath $gcm.ResolvedCommandName
try {
$comm = Get-Command $Command -ErrorAction Stop
} catch {
abort "'$Command' not found" 3
niheaven marked this conversation as resolved.
Show resolved Hide resolved
}
$commandPath = if ($comm.Path -like "$userShims*" -or $comm.Path -like "$globalShims*") {
Get-ShimTarget ($comm.Path -replace '\.exe$', '.shim')
Copy link

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
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
return $commandPath
}
}

Expand Down Expand Up @@ -641,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
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
8 changes: 4 additions & 4 deletions libexec/scoop-which.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ try {
}

$path = Get-CommandPath $command
$path

if ($path -eq $null) {
if ($null -eq $path) {
Write-Host 'Not a scoop shim.'
exit 2
} else {
friendly_path $path
exit 0
}

exit 0