-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdef.ps1
executable file
·87 lines (75 loc) · 2.06 KB
/
def.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<#
.SYNOPSIS
Prints the definition of a file or function
.PARAMETER File
Command to create or edit. Can create parent directories
#>
param(
[Parameter(Mandatory = $true)]
[string] $File
)
$script:ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
function noHome($p) {
$homeRegex = "^" + [regex]::escape($Home)
$p -replace $homeRegex, '~'
}
function printPythonFiles($content) {
# This is a bit hacky; but better than trying to eval `(Join-Path $PSScriptRoot some_script.py)`
# TODO parse a function like `pipx run (Join-Path $PSScriptRoot gpx.py) @args
# MAYBE move this magic logic into wh.ps1 and have defe.ps1 also use it!
foreach ($match in $content | Select-String '\w+\.py' ) {
$pyFile = $match.Matches.Value
foreach ($found in Get-ChildItem -Recurse $PSScriptRoot $pyFile) {
" "
Write-Host $found.FullName -ForegroundColor Blue
Get-Content $found
}
}
}
Get-Command $File -All -ErrorAction SilentlyContinue | ForEach-Object {
$cmd = $_
switch ($cmd.CommandType) {
Alias {
$cmd.DisplayName
}
Application {
noHome $cmd.Source
$extension = [System.IO.Path]::GetExtension($cmd.Source)
if (Test-Shebang $cmd.Source) {
Get-Content $cmd.Source
}
elseif ($extension -and $extension -ne ".exe") {
Get-Content $cmd.Source
}
else {
"[binary]"
}
}
Function {
noHome $cmd.ScriptBlock.File
$cmd.Definition
}
ExternalScript {
noHome $cmd.Source
$content = Get-Content $cmd.Source
$i = $content.IndexOf("Set-StrictMode -Version Latest")
if ($i -ge 0) {
$pStart = $content.IndexOf('param(')
$pEnd = $content.IndexOf(')')
if ($pStart -ge 0 -and $pEnd -gt $pStart -and $pEnd -lt $i) {
$content | Select-Object -Skip $pStart -First ($pEnd - $pStart + 1)
}
$content | Select-Object -skip ($i + 2)
printPythonFiles $content
} else {
$content
}
}
default {
$cmd.Name
$cmd.Definition
}
}
" "
}