forked from ScoopInstaller/Scoop
-
Notifications
You must be signed in to change notification settings - Fork 11
/
scoop-which.ps1
61 lines (50 loc) · 2.01 KB
/
scoop-which.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Usage: scoop which [<OPTIONS>] <COMMAND>
# Summary: Locate the path to a shim/executable of application that was installed with scoop (similar to 'which' on Linux).
#
# Options:
# -h, --help Show help for this command.
'core', 'commands', 'getopt', 'help', 'Helpers' | ForEach-Object {
. (Join-Path $PSScriptRoot "..\lib\$_.ps1")
}
Reset-Alias
$ExitCode = 0
$Options, $Command, $_err = getopt $args
if ($_err) { Stop-ScoopExecution -Message "scoop which: $_err" -ExitCode 2 }
if (!$Command) { Stop-ScoopExecution -Message 'Parameter <COMMAND> missing' -Usage (my_usage) }
$gcm = $null
try {
$gcm = Get-Command -Name $Command -ErrorAction 'Stop'
} catch {
Stop-ScoopExecution -Message "'$Command' not found"
}
$userShims = shimdir $false | Resolve-Path
$globalShims = shimdir $true # do not resolve, may not exists
$FinalPath = $null
if ($gcm.Path -and $gcm.Path.EndsWith('.ps1') -and (($gcm.Path -like "$userShims*") -or ($gcm.Path -like "$globalShims*"))) {
# This will show path to the real exe instead of the original ps1 file. Is it right?
$shimText = Get-Content -LiteralPath $gcm.Path -Encoding 'UTF8'
# TODO: Drop Invoke-Expression
$exePath = ($shimText | Where-Object { $_.StartsWith('$path') }) -split ' ' | Select-Object -Last 1 | Invoke-Expression
# Expand relative path
if ($exePath -and ![System.IO.Path]::IsPathRooted($exePath)) {
$exePath = Split-Path $gcm.Path | Join-Path -ChildPath $exePath | Resolve-Path
} else {
$exePath = $gcm.Path
}
$FinalPath = friendly_path $exePath
} else {
switch ($gcm.CommandType) {
'Application' { $FinalPath = $gcm.Source }
'Alias' {
$FinalPath = Invoke-ScoopCommand 'which' @($gcm.ResolvedCommandName)
$ExitCode = $LASTEXITCODE
}
default {
Write-UserMessage -Message 'Not a scoop shim' -Output
$FinalPath = $gcm.Path
$ExitCode = 3
}
}
}
if ($FinalPath) { Write-UserMessage -Message $FinalPath -Output }
exit $ExitCode