-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickpath.psm1
More file actions
133 lines (104 loc) · 3.71 KB
/
quickpath.psm1
File metadata and controls
133 lines (104 loc) · 3.71 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
$Public = @( Get-ChildItem -Path $PSScriptRoot\public\*.ps1 -Recurse -ErrorAction SilentlyContinue -Force )
$Private = @( Get-ChildItem -Path $PSScriptRoot\private\*.ps1 -Recurse -ErrorAction SilentlyContinue -Force )
$Classes = @( Get-ChildItem -Path $PSScriptRoot\classes\*.ps1 -Recurse -ErrorAction SilentlyContinue -Force )
#Dot source the files
Foreach ($import in @($Public + $Private + $Classes)) {
Try {
. $import.fullname
}
Catch {
Write-Error -Message "Failed to import function $($import.fullname): $_"
}
}
function Get-DynamicHelp {
param([string[]]$CommandList)
$commandText = $CommandList | ForEach-Object { "`t$_" }
$commandText = $commandText -join "`n"
@"
NAME
quickpath
SYNOPSIS
A helper script to more easily navigate your system from the commandline.
DESCRIPTION
'quickpath' is a script to help easily navigate your system using the commandline.
Using aliases the script saves relative paths to quickly navigate to folders associated with the alias
and even makes it easy to open the folders/projects in your favorite tools.
USAGE
qp [command] [arguments]
EXAMPLE
qp alias add '{"aliases": ["<myalias>"], "windowsPath": "the\\path\\to\\my\\alias" }'
COMMANDS
<path>
$commandText
"@
}
function qp {
[CmdletBinding()]
param(
[Parameter(Position = 0)]
[string]$Command,
[Parameter(Position = 1, ValueFromRemainingArguments = $true)]
[string[]]$Arguments
)
try {
Initialize-QuickPath
Write-Verbose "qp: commands available: $($script:COMMANDS.Count)"
$commandNames = ($script:COMMANDS | ForEach-Object Name | Sort-Object)
$helpText = Get-DynamicHelp $commandNames
$alias = Get-Alias @($Command)
$path = if ($alias) { $alias.WindowsPath } else { $Command }
if (Test-Path -Path $path) {
Set-Location $path
return
}
$commandObj = $script:COMMANDS | Where-Object { $_.Name -eq $Command }
if ($null -eq $commandObj) {
Write-Host $helpText
return
}
if (-not $Arguments -or $Arguments.length -eq 0) {
$commandObj.InvokeFunction()
return;
}
$commandObj.InvokeFunction($Arguments)
}
catch {
Write-Error "Error: $($_.Exception.Message)"
}
}
Register-ArgumentCompleter -CommandName qp -ScriptBlock {
param(
$wordToComplete,
$commandAst,
$cursorPosition
)
$commands = Get-Commands
$inputCount = $commandAst.CommandElements.count
if ($inputCount -eq 2) {
$command = $commands | Where-Object { $_.Name -like "$wordToComplete*" }
if ($command -ne $null) {
return $command.Name
}
}
$inputArguments = $commandAst.CommandElements | Select-Object -Skip 1 | ForEach-Object { $_.Extent.Text }
$wordToComplete = $inputArguments | Select-Object -Last 1
$subCommands = @()
$command = $null
foreach ($argument in $inputArguments[0..($inputArguments.Count - 2)]) {
$command = $commands | Where-Object { $_.Name -eq $argument }
$subCommands = $command.SubCommands
}
$command = $subCommands | Where-Object { $_.Name -like "$wordToComplete*" }
if ($null -ne $command) {
return $command.Name
}
$aliasFilePath = Get-AliasFilePath
$aliasPathMappings = Import-Aliases $aliasFilePath
foreach ($aliasMapping in $aliasPathMappings) {
$matched = $aliasMapping.Aliases | Where-Object { $_ -like "$wordToComplete*" } | Select-Object -First 1
if ($matched) {
return "$matched"
}
}
}
Export-ModuleMember -Function qp