-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind-ProfileFunction.ps1
More file actions
576 lines (478 loc) · 19.1 KB
/
Find-ProfileFunction.ps1
File metadata and controls
576 lines (478 loc) · 19.1 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
function Find-ProfileFunction
{
<#
.SYNOPSIS
Searches profile functions to help find the right command quickly.
.DESCRIPTION
Scans the profile `Functions` directory, extracts metadata from each function file,
and returns ranked matches based on keyword relevance.
Search matching considers:
- Function name
- Function synopsis (from comment-based help)
- Category/folder name
- Aliases defined with Set-Alias in the function file
Compatible with PowerShell Desktop 5.1+ on Windows, macOS, and Linux.
.PARAMETER Query
One or more search terms. Terms are split on whitespace.
By default, all terms must match each result. Use -MatchAny to match any term.
.PARAMETER Category
Optional category filter. Accepts folder names (e.g., 'NetworkAndDns'),
display names (e.g., 'Network And Dns'), or short aliases (e.g., 'dns', 'dev').
Case-insensitive. Supports tab completion.
Short aliases include: ad, dev, media, module, modules, network, net, dns, profile,
sysadmin, sys, admin, utils, util.
.PARAMETER Top
Maximum number of ranked results to return.
.PARAMETER MatchAny
Match results containing any query term. By default, all query terms are required.
.EXAMPLE
PS > Find-ProfileFunction dns
Finds functions related to DNS and returns ranked matches.
.EXAMPLE
PS > Find-ProfileFunction 'markdown convert'
Name : ConvertTo-Markdown
Category : Utilities
Synopsis : Converts a URL or local file path to Markdown using Pandoc.
Aliases : url2markdown
Score : 285
Path : /Users/jon/.config/powershell/Functions/Utilities/ConvertTo-Markdown.ps1
Name : ConvertTo-MarkdownObject
Category : Utilities
Synopsis : Converts arbitrary PowerShell objects into Markdown text.
Aliases :
Score : 235
Path : /Users/jon/.config/powershell/Functions/Utilities/ConvertTo-MarkdownObject.ps1
Finds functions that match both "markdown" and "convert".
.EXAMPLE
PS > Find-ProfileFunction docker -Category dev -Top 5
Returns the top 5 Docker-related functions in the Developer category.
.EXAMPLE
PS > Find-ProfileFunction reboot | Format-Table Name, Category, Synopsis -AutoSize
Pipes ranked results into a custom table layout.
.OUTPUTS
PSCustomObject
Returns objects with properties:
Name, Category, Synopsis, Aliases, Score, Path
.NOTES
Author: Jon LaBelle
License: MIT
Source: https://github.com/jonlabelle/pwsh-profile/blob/main/Functions/ProfileManagement/Find-ProfileFunction.ps1
.LINK
https://github.com/jonlabelle/pwsh-profile/blob/main/Functions/ProfileManagement/Find-ProfileFunction.ps1
#>
[CmdletBinding()]
[OutputType([PSCustomObject])]
param(
[Parameter(Mandatory, Position = 0)]
[ValidateNotNullOrEmpty()]
[string[]]$Query,
[Parameter()]
[ArgumentCompleter({
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
$profileDir = if ($PROFILE) { Split-Path $PROFILE -Parent } else { $PSScriptRoot }
$functionsDir = Join-Path -Path $profileDir -ChildPath 'Functions'
if (-not (Test-Path $functionsDir)) { return }
$folders = Get-ChildItem -Path $functionsDir -Directory | Select-Object -ExpandProperty Name
$shortcuts = @{
'ActiveDirectory' = @('ad')
'Developer' = @('dev')
'MediaProcessing' = @('media')
'ModuleManagement' = @('module', 'modules')
'NetworkAndDns' = @('network', 'dns', 'net')
'ProfileManagement' = @('profile')
'SystemAdministration' = @('sysadmin', 'sys', 'admin')
'Utilities' = @('utils', 'util')
}
$completions = [System.Collections.Generic.List[string]]::new()
foreach ($folder in $folders)
{
$completions.Add($folder)
$spaced = ($folder -creplace '([A-Z])', ' $1').Trim()
if ($spaced -ne $folder) { $completions.Add($spaced) }
if ($shortcuts.ContainsKey($folder))
{
foreach ($shortcut in $shortcuts[$folder])
{
$completions.Add($shortcut)
}
}
}
$completions |
Where-Object { $_ -like "$wordToComplete*" } |
Sort-Object -Unique |
ForEach-Object {
if ($_ -match '\s')
{
[System.Management.Automation.CompletionResult]::new("'$_'", $_, 'ParameterValue', $_)
}
else
{
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
})]
[string[]]$Category,
[Parameter()]
[ValidateRange(1, 500)]
[int]$Top = 15,
[Parameter()]
[switch]$MatchAny
)
begin
{
Write-Verbose 'Starting Find-ProfileFunction'
$skipProcessing = $false
$resolvedCategories = @()
$searchTerms = @()
$queryPhrase = ''
$categoryShortcuts = @{
'ActiveDirectory' = @('ad')
'Developer' = @('dev')
'MediaProcessing' = @('media')
'ModuleManagement' = @('module', 'modules')
'NetworkAndDns' = @('network', 'dns', 'net')
'ProfileManagement' = @('profile')
'SystemAdministration' = @('sysadmin', 'sys', 'admin')
'Utilities' = @('utils', 'util')
}
$profilePath = $PROFILE
if (-not $profilePath)
{
$profilePath = $PSCommandPath
}
$functionsPath = Join-Path -Path (Split-Path -Path $profilePath -Parent) -ChildPath 'Functions'
if (-not (Test-Path -LiteralPath $functionsPath -PathType Container))
{
Write-Warning "Functions directory not found at: $functionsPath"
$skipProcessing = $true
return
}
foreach ($item in $Query)
{
if ([string]::IsNullOrWhiteSpace($item))
{
continue
}
foreach ($term in ($item -split '\s+'))
{
if (-not [string]::IsNullOrWhiteSpace($term))
{
$searchTerms += $term.ToLowerInvariant()
}
}
}
if ($searchTerms.Count -eq 0)
{
Write-Warning 'At least one non-empty search term is required.'
$skipProcessing = $true
return
}
$queryPhrase = (($Query -join ' ').Trim()).ToLowerInvariant()
if ($Category)
{
$allFolders = Get-ChildItem -Path $functionsPath -Directory | Select-Object -ExpandProperty Name
foreach ($cat in $Category)
{
$catLower = $cat.ToLowerInvariant()
$matched = $null
foreach ($folder in $categoryShortcuts.Keys)
{
if ($categoryShortcuts[$folder] -contains $catLower)
{
$matched = $folder
break
}
}
if (-not $matched)
{
$matched = $allFolders | Where-Object { $_.ToLowerInvariant() -eq $catLower } | Select-Object -First 1
}
if (-not $matched)
{
$matched = $allFolders |
Where-Object {
(($_ -creplace '([A-Z])', ' $1').Trim()).ToLowerInvariant() -eq $catLower
} |
Select-Object -First 1
}
if ($matched)
{
$resolvedCategories += $matched
Write-Verbose "Resolved category '$cat' to '$matched'"
}
else
{
Write-Warning "Unknown category: '$cat'. Use tab completion to see available categories."
}
}
$resolvedCategories = $resolvedCategories | Sort-Object -Unique
if ($resolvedCategories.Count -eq 0)
{
Write-Warning 'No valid categories specified. Run without -Category to search all.'
$skipProcessing = $true
return
}
}
}
process
{
if ($skipProcessing) { return }
function Get-FunctionSynopsis
{
param(
[Parameter(Mandatory)]
[string]$Path,
[Parameter(Mandatory)]
[System.Management.Automation.Language.FunctionDefinitionAst]$FunctionAst
)
$helpContent = $FunctionAst.GetHelpContent()
if ($helpContent -and -not [string]::IsNullOrWhiteSpace($helpContent.Synopsis))
{
return (($helpContent.Synopsis -split '\r?\n' | ForEach-Object { $_.Trim() }) -join ' ')
}
$raw = Get-Content -LiteralPath $Path -Raw
if ($raw -match '(?s)\.SYNOPSIS\s+(.+?)(?=\r?\n\s*\.)')
{
return (($matches[1].Trim() -split '\r?\n' | ForEach-Object { $_.Trim() }) -join ' ')
}
return 'No description available'
}
function Get-FunctionAliases
{
param(
[Parameter(Mandatory)]
[System.Management.Automation.Language.ScriptBlockAst]$Ast
)
$foundAliases = [System.Collections.Generic.List[string]]::new()
$setAliasCalls = $Ast.FindAll({
$args[0] -is [System.Management.Automation.Language.CommandAst] -and
$args[0].GetCommandName() -eq 'Set-Alias'
}, $true)
foreach ($aliasCall in $setAliasCalls)
{
$aliasName = $null
$nameParam = $aliasCall.CommandElements | Where-Object {
$_ -is [System.Management.Automation.Language.CommandParameterAst] -and
$_.ParameterName -eq 'Name'
} | Select-Object -First 1
if ($nameParam)
{
$nameIndex = $aliasCall.CommandElements.IndexOf($nameParam)
if ($nameIndex -ge 0 -and ($nameIndex + 1) -lt $aliasCall.CommandElements.Count)
{
$nameElement = $aliasCall.CommandElements[$nameIndex + 1]
if ($nameElement -is [System.Management.Automation.Language.StringConstantExpressionAst])
{
$aliasName = $nameElement.Value
}
}
}
if (-not $aliasName -and $aliasCall.CommandElements.Count -ge 2)
{
$possibleAlias = $aliasCall.CommandElements[1]
if ($possibleAlias -is [System.Management.Automation.Language.StringConstantExpressionAst])
{
$aliasName = $possibleAlias.Value
}
}
if (-not [string]::IsNullOrWhiteSpace($aliasName) -and -not $foundAliases.Contains($aliasName))
{
[void]$foundAliases.Add($aliasName)
}
}
return @($foundAliases)
}
function Get-Score
{
param(
[Parameter(Mandatory)]
[PSCustomObject]$Metadata,
[Parameter(Mandatory)]
[string[]]$Terms,
[Parameter(Mandatory)]
[bool]$UseMatchAny,
[Parameter(Mandatory)]
[string]$QueryText
)
$score = 0
$matchedTerms = 0
foreach ($term in $Terms)
{
$termMatched = $false
if ($Metadata.NameLower -eq $term)
{
$score += 120
$termMatched = $true
}
elseif ($Metadata.NameLower.StartsWith($term))
{
$score += 95
$termMatched = $true
}
elseif ($Metadata.NameLower.Contains($term))
{
$score += 70
$termMatched = $true
}
if ($Metadata.AliasesLower -contains $term)
{
$score += 80
$termMatched = $true
}
elseif ($Metadata.AliasesLower | Where-Object { $_ -like "*$term*" })
{
$score += 50
$termMatched = $true
}
if ($Metadata.SynopsisLower.Contains($term))
{
$score += 35
$termMatched = $true
}
if ($Metadata.CategoryLower -eq $term -or $Metadata.CategoryDisplayLower -eq $term)
{
$score += 40
$termMatched = $true
}
elseif ($Metadata.CategoryLower.Contains($term) -or $Metadata.CategoryDisplayLower.Contains($term))
{
$score += 20
$termMatched = $true
}
if ($termMatched)
{
$matchedTerms++
}
}
if ($matchedTerms -eq 0)
{
return -1
}
if (-not $UseMatchAny -and $matchedTerms -lt $Terms.Count)
{
return -1
}
if (-not [string]::IsNullOrWhiteSpace($QueryText))
{
if ($Metadata.NameLower.Contains($QueryText))
{
$score += 40
}
if ($Metadata.SynopsisLower.Contains($QueryText))
{
$score += 20
}
}
return $score
}
try
{
$functionFiles = Get-ChildItem -Path $functionsPath -Filter '*.ps1' -File -Recurse
if ($resolvedCategories.Count -gt 0)
{
$functionFiles = $functionFiles | Where-Object { $resolvedCategories -contains $_.Directory.Name }
}
if (-not $functionFiles)
{
Write-Warning 'No function files found for the specified filter.'
return
}
$results = [System.Collections.Generic.List[object]]::new()
foreach ($file in $functionFiles)
{
try
{
$tokens = $null
$errors = $null
$ast = [System.Management.Automation.Language.Parser]::ParseFile($file.FullName, [ref]$tokens, [ref]$errors)
$functionAst = $ast.Find({
$args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst]
}, $true)
$functionName = [System.IO.Path]::GetFileNameWithoutExtension($file.Name)
if ($functionAst)
{
$functionName = $functionAst.Name
}
$synopsis = if ($functionAst)
{
Get-FunctionSynopsis -Path $file.FullName -FunctionAst $functionAst
}
else
{
'Could not parse function definition'
}
$aliases = if ($ast)
{
Get-FunctionAliases -Ast $ast
}
else
{
@()
}
$category = $file.Directory.Name
$categoryDisplay = ($category -creplace '([A-Z])', ' $1').Trim()
$metadata = [PSCustomObject]@{
Name = $functionName
NameLower = $functionName.ToLowerInvariant()
Category = $category
CategoryLower = $category.ToLowerInvariant()
CategoryDisplay = $categoryDisplay
CategoryDisplayLower = $categoryDisplay.ToLowerInvariant()
Synopsis = $synopsis
SynopsisLower = $synopsis.ToLowerInvariant()
Aliases = @($aliases)
AliasesLower = @($aliases | ForEach-Object { $_.ToLowerInvariant() })
Path = $file.FullName
}
$score = Get-Score -Metadata $metadata -Terms $searchTerms -UseMatchAny $MatchAny.IsPresent -QueryText $queryPhrase
if ($score -ge 0)
{
$results.Add([PSCustomObject]@{
Name = $metadata.Name
Category = $metadata.CategoryDisplay
Synopsis = $metadata.Synopsis
Aliases = ($metadata.Aliases -join ', ')
Score = $score
Path = $metadata.Path
})
}
}
catch
{
Write-Verbose "Skipping $($file.FullName): $($_.Exception.Message)"
}
}
if ($results.Count -eq 0)
{
Write-Warning "No functions matched query: '$($Query -join ' ')'"
return
}
$rankedResults = $results |
Sort-Object -Property @{ Expression = 'Score'; Descending = $true }, @{ Expression = 'Name'; Descending = $false } |
Select-Object -First $Top
$rankedResults
}
catch
{
Write-Error "Error searching profile functions: $($_.Exception.Message)"
throw
}
}
end
{
Write-Verbose 'Find-ProfileFunction completed'
}
}
# Create alias 'Search-ProfileFunction' if it doesn't conflict
if (-not (Get-Command -Name 'Search-ProfileFunction' -ErrorAction SilentlyContinue))
{
try
{
Write-Verbose "Creating 'Search-ProfileFunction' alias for Find-ProfileFunction"
Set-Alias -Name 'Search-ProfileFunction' -Value 'Find-ProfileFunction' -Force -ErrorAction Stop
}
catch
{
Write-Warning "Find-ProfileFunction: Could not create 'Search-ProfileFunction' alias: $($_.Exception.Message)"
}
}