Skip to content

Commit

Permalink
fix(shim): Add 'Get-CommandPath()' to find git (ScoopInstaller#4913)
Browse files Browse the repository at this point in the history
Co-authored-by: Hsiao-nan Cheung <niheaven@gmail.com>
  • Loading branch information
rashil2000 and niheaven authored May 27, 2022
1 parent 896ea6c commit 0f6d012
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 49 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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')
} 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

0 comments on commit 0f6d012

Please sign in to comment.