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
Move primary function to core.ps1
  • Loading branch information
rashil2000 committed May 21, 2022
commit 50bee437866398a6c8f57d1a9a50058a5e5f5847
44 changes: 43 additions & 1 deletion lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,48 @@ function Get-HelperPath {
}
}

function Get-CommandPath {
[CmdletBinding()]
[OutputType([String])]
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
}

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
} else {
$null
}

return $CommandPath
}
}

function Test-HelperInstalled {
[CmdletBinding()]
param(
Expand Down Expand Up @@ -726,7 +768,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 (& "$PSScriptRoot\..\libexec\scoop-which.ps1" git) -ErrorAction:Stop).Directory.Parent
$gitdir = (Get-Item (Get-CommandPath git) -ErrorAction:Stop).Directory.Parent
if ($gitdir.FullName -imatch 'mingw') {
$gitdir = $gitdir.Parent
}
Expand Down
28 changes: 3 additions & 25 deletions libexec/scoop-which.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,11 @@ try {
abort "'$command' not found" 3
}

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

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') {
scoop which $gcm.ResolvedCommandName
} else {
if ($path -eq $null) {
Write-Host 'Not a scoop shim.'
$path
exit 2
}

Expand Down