-
Notifications
You must be signed in to change notification settings - Fork 6
/
Invoke-OKCommand.ps1
395 lines (344 loc) · 15.2 KB
/
Invoke-OKCommand.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
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
. (Join-Path $PSScriptRoot "Get-OKToken.ps1")
. (Join-Path $PSScriptRoot "Get-OKCommandLength.ps1")
. (Join-Path $PSScriptRoot "Show-OKCode.ps1")
Enum OKCommandType {
Comment = 1
Numbered = 2
Named = 3
}
#$ReservedWords = @("reset","prompt","prompt_default","auto_show","comment_align","verbose","quiet","list","list-once","list-prompt","h","help","h");
Class OKCommandInfo {
[int]$physicalLineNum # 1-based
[OKCommandType]$type # what type of line is this? A comment, numbered or named
[string]$commandText # everything other than the command name (if there is one)
[int]$num # number of the command (if it is a name or number command only)
[string]$key # either ("" + $number) or $commandName, i.e. "5" or "build"
[System.Management.Automation.Language.Token[]]$tokens
#[System.Management.Automation.PSToken[]]$tokens
[int]$commentOffset # how many chars from the start of the command to the first comment token (useful to know this for aligning comments)
}
Class OKFileInfo {
[string]$fileName; # fileName
[System.Collections.Specialized.OrderedDictionary]$commands; # order dictionary of OKCommandInfo
[OKCommandInfo[]]$lines; # Commands, in the order they are found in the file
[int]$maxKeyWidth; # what is the widest command name
[int]$commentOffset;
}
function Get-OKCommand($file) {
# TODO: parameter validation
$commands = [ordered]@{ };
$lines = New-Object System.Collections.ArrayList
[regex]$rx = "^[ `t]*(?<commandName>[A-Za-z_][A-Za-z0-9-_.]*)[ `t]*\:(?<commandText>.*)$";
$num = 0;
$physicalLineNum = 0;
Get-Content $file | ForEach-Object {
$line = $_.trim();
$physicalLineNum = $physicalLineNum + 1;
if ($null -eq $line -or $line -eq "") {
# skip blank line
}
else {
$commandInfo = New-Object OKCommandInfo
$commandInfo.physicalLineNum = $physicalLineNum;
if ($line.indexOf('#') -eq 0) {
$commandInfo.type = [OKCommandType]::Comment
$commandInfo.commandText = $line;
}
else {
$groups = $rx.Matches($line).Groups;
if ($null -ne $groups) {
$commandInfo.type = [OKCommandType]::Named
$commandInfo.commandText = $groups[0].Groups["commandText"].Value.Trim();
$key = $groups[0].Groups["commandName"].Value.Trim();
$num = $num + 1;
if ($null -ne $commands[$key]) {
# Name has already been used.
# (verbose: show warning)
<#
write-host "ok: duplicate commandname '" -f Red -no;
write-host "$key" -f white -no;
write-host "' mapped to " -f Red -no;
write-host "$num" -f white;
#>
# suggest
$commandInfo.type = [OKCommandType]::Numbered
$key = ("" + $num);
}
$commandInfo.num = $num;
$commandInfo.key = $key;
}
else {
$num = $num + 1;
$commandInfo.commandText = $line
$commandInfo.type = [OKCommandType]::Numbered
$commandInfo.key = ("" + $num);
$commandInfo.num = $num;
}
$maxKeyWidth = [math]::max( $maxKeyWidth , $commandInfo.key.Length);
$commandInfo.Tokens = (Get-OKToken $commandInfo.commandText);
$commands.Add($commandInfo.key, $commandInfo) | Out-Null;
}
$lines.Add($commandInfo) | Out-Null;
}
}
# Suggestion: this could be much more configurable
$alignComments = $true;
if ($alignComments) {
$maxCommandLength = ($commands.Values | ForEach-Object {
[OKCommandInfo]$c = $_;
# Only consider commands where the total width including 'maxkey: command # comment' < console width
# Also -- only interested in commands where there *is* a comment
$commandLength = (Get-OKCommandLength ($c.tokens));
$allegedCommentLength = ($c.CommandText.Length) - $commandLength;
if ($allegedCommentLength -gt 0) {
#Write-Host "True Comment: '$($c.CommandText.SubString($commandLength).Trim())'" -f cyan;
$commentLength = $c.CommandText.SubString($commandLength).Trim().Length;
}
else {
$commentLength = $allegedCommentLength;
}
#Write-Host "commandLength $commandLength $($c.CommandText)" -f darkblue;
#Write-Host "allegedCommentLength $allegedCommentLength $($c.CommandText)" -f darkmagenta;
#Write-Host "commentLength $commentLength $($c.CommandText)" -f darkgreen;
#if (($maxKeyLength + 2 + $c.CommandText.Length) -lt $Host.UI.RawUI.WindowSize.Width) {
if (($maxKeyLength + 2 + $commandLength + $commentLength) -lt $Host.UI.RawUI.WindowSize.Width) {
if ($commentLength -gt 0) {
#Write-Host "commandLength $commandLength $($c.CommandText)" -f blue;
#Write-Host "commentLength $commentLength $($c.CommandText)" -f green;
$commandLength;
}
else {
#Write-Host "commandLength $commandLength $($c.CommandText)" -f magenta;
0;
}
}
else {
0;
}
} | Measure-Object -Maximum | ForEach-Object Maximum);
#Write-Host "maxCommandLength $maxCommandLength " -f red;
$maxCommentLength = ($commands.Values | ForEach-Object {
[OKCommandInfo]$c = $_;
# Only consider commands where the total width of command and comment < console width
$commandLength = (Get-OKCommandLength ($c.tokens));
if ($commandLength -gt 0) {
# return the length of the comment.. (only counts if there *is* a command)
#write-host "commandLength $commandLength $($c.CommandText)" -f red;
($c.key.length + 2) + ($c.CommandText.Length) - $commandLength;
}
else {
#write-host "No command... $($c.CommandText)" -f green;
0;
}
} | Measure-Object -Maximum | ForEach-Object Maximum);
# the "- 2" is the width of the ": " after each command.
$commentOffset = [Math]::Min(
$Host.UI.RawUI.WindowSize.Width - 2 - $maxCommentLength - $maxKeyLength,
$maxCommandLength + 2 + $maxKeyLength)
#Write-Host "commentOffset:$commentOffset" -f Magenta;
}
else {
$commentOffset = 0;
}
$fileInfo = New-Object OKFileInfo;
$fileInfo.fileName = $file;
$fileInfo.commands = $commands;
$fileInfo.lines = $lines;
$fileInfo.maxKeyWidth = $maxKeyWidth;
$fileInfo.commentOffset = $commentOffset;
return $fileInfo;
}
function Show-OKFile($okFileInfo) {
$maxKeyWidth = $okFileInfo.maxKeyWidth;
$okFileInfo.lines | ForEach-Object {
[OKCommandInfo]$c = $_;
if ($c.Type -eq [OKCommandType]::Comment) {
Write-Host ("#" * ($maxKeyWidth)) -NoNewline -f DarkGray;
Write-Host ": " -NoNewline -f Cyan;
Write-Host $c.commandText.TrimStart().TrimStart('#').TrimStart() -f Green
}
else {
Write-Host (" " * ($maxKeyWidth - $c.key.length)) -NoNewline
if ($c.Type -eq [OKCommandType]::Numbered) {
Write-Host $c.key -f DarkCyan -NoNewline
}
else {
# writing a command.
Write-Host $c.key -f Cyan -NoNewline
<#
$commandColor = [ConsoleColor]::Cyan;
if ($c.key -eq "now") {
$commandColor = [ConsoleColor]::White;
}
elseif ($c.key -eq "today" -or $c.key -eq "todo" -or $c.key -eq "ttd") {
$commandColor = [ConsoleColor]::Yellow;
}
elseif ($c.key -eq "due" -or $c.key -eq "bug" -or $c.key -eq "error" -or $c.key -eq "bugs" -or $c.key -eq "errors") {
$commandColor = [ConsoleColor]::Red;
}
elseif ($c.key -eq "search" -or $c.key -eq "find") {
$commandColor = [ConsoleColor]::Magenta;
}
elseif ($c.key -eq "build") {
$commandColor = [ConsoleColor]::DarkBlue;
}
elseif ($c.key -eq "publish" -or $c.key -eq "pub") {
$commandColor = [ConsoleColor]::Green;
}
elseif ($c.key -eq "plan" -or $c.key -eq "idea") {
$commandColor = [ConsoleColor]::Blue;
}
write-host $c.key -f $commandColor -NoNewline
#>
}
Write-Host ": " -f Cyan -NoNewline
Show-OKCode -code $c.commandText -CommentOffset $okFileInfo.commentOffset -MaxKeyLength $okFileInfo.MaxKeyWidth;
Write-Host "";
}
}
}
function Invoke-OKCommand {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingInvokeExpression", "")]
param (
[parameter(mandatory = $false, position = 0)][OKFileInfo]$okFileInfo,
[parameter(mandatory = $false, position = 1)][string]$commandName,
[parameter(
mandatory = $false,
position = 1,
ValueFromRemainingArguments = $true
)]$arg
)
#TODO: what if it's not a valid command?
# see if it's "close" to valid... get candidates. If exactly 1 -- run it.
# If more than 1 -- say "did you mean" and show those.
# If it's less than 1 -- error... show file.
if ($commandName -match "^[0-9]+$") {
# it is a number.
$commandIndex = ($commandName -as "int") - 1;
$numCommands = (($okFileInfo.commands).Keys).Count;
if ($commandIndex -ge 0 -and $commandIndex -lt ($numCommands)) {
$command = $okFileInfo.commands[$commandIndex];
}
else {
$command = $null;
}
}
else {
$command = $okFileInfo.commands[("" + $commandName)];
}
if ($null -eq $command) {
$candidates = New-Object System.Collections.ArrayList
$okFileInfo.commands.keys |
Where-Object { $_ -like ($commandName + "*") } |
ForEach-Object {
$candidates.Add($_) | Out-Null;
}
if ($null -eq $candidates -or $candidates.Count -eq 0) {
Write-Host "ok: unknown command " -f Red -no
Write-Host "'" -no;
Write-Host "$commandName" -f yellow -no;
Write-Host "'";
Write-Host "(use 'ok' for a list of local commands, or 'ok help' for general commands)"
return;
}
if ($candidates.Count -gt 1) {
Write-Host "ok: command '$commandName' is ambiguous, did you mean:`n`t" -no
$candidates | ForEach-Object {
Write-Host "$_ " -no -f yellow
}
return;
}
#TODO: check verbose
Write-Host "ok: No such command! " -f Yellow -NoNewline
Write-Host "Assume you meant: " -f gray -NoNewline
Write-Host "'$($candidates[0])'" -f White -NoNewline
Write-Host "..." -f gray
$command = $okFileInfo.commands[("" + $candidates[0])];
}
#TODO: check verbose
Write-Host "> " -f Magenta -NoNewline;
Show-OKCode -code $command.commandText -CommentOffset -5 -MaxKeyLength 0;
Write-Host "";
# Write command to history (but in comment form), so you can scroll up and see it there/edit it.
[Microsoft.PowerShell.PSConsoleReadLine]::AddToHistory("# " + $command.commandText)
# note arg is a list of object, and can be used in the commandText
Invoke-Expression $command.commandText;
}
<#
.SYNOPSIS
Inspect or run commands from your ok-file
.DESCRIPTION
"Invoke-OK" (and the entire OK module) rely on first finding a file in the current folder called ".ok" (or ".ok-ps"), full of useful powershell one-liners you like to use in that folder.
("Invoke-OK" is usually called by its suggested alias, `ok`. That alias is used in the examples below.)
Call "ok" with **no parameters** and the ok-file will be `pretty printed`, with a number before each powershell one-liner.
Call "ok {number}" to run the line of code that corresponds to that number.
You can also have "named" commands. Just prefix the one-liner with a name and a colon, e.g. your ok file could contain:
deploy: robocopy *.ps1 c:\launchplace /MIR
...then you would use `ok deploy` to run that robocopy command.
These one liners can accept parameters, for example, if your one-liner said:
push: git add *; git commit . -m "$arg"; git push;
Then you could call: "ok push minor changes" to make a git commit with the message "minor changes"
.PARAMETER commandName
This optional command can specify the name or number of a user-command.
If specified, Invoke-OK will call Invoke-OKCommand and pass it the name of the command file and the commandName.
.PARAMETER arg
.NOTES
.EXAMPLE
ok
(Assuming you have the ALIAS "ok" for "Invoke-OK" ... because it's super useful!)
#>
function Invoke-OK {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingInvokeExpression", "")]
param (
[parameter(mandatory = $false, position = 0)][string]$commandName,
[parameter(
mandatory = $false,
position = 1,
ValueFromRemainingArguments = $true
)]$arg
)
if ($commandName -match "^(/|--|-|\\|)(h|\?|help)$") {
Get-Help invoke-ok;
return;
}
$file = (Get-OKFileLocation);
if ($null -ne $file) {
$okFileInfo = (Get-OKCommand $file);
if ($null -eq $commandName -or $commandName -eq "") {
Show-OKFile $okFileInfo;
}
else {
Invoke-OKCommand -okFileInfo $okFileInfo -commandName $commandName -arg $arg;
}
}
else {
if ($null -ne $commandName -and '' -ne $commandName) {
Write-Host "ok: No .ok or .ok-ps file in which to find this " -ForegroundColor red -NoNewline ;
Write-Host "'" -ForegroundColor white -NoNewline ;
Write-Host $commandName -ForegroundColor yellow -NoNewline ;
Write-Host "'" -ForegroundColor white;
}
}
}
# all knowledge about how to probe for and determine the location of the ok-file is encapsulated in this function.
function Get-OKFileLocation () {
if (Test-Path ".\.ok-ps") {
return ".\.ok-ps"
}
elseif (Test-Path ".\.ok") {
return ".\.ok"
}
return $null;
}
# TODO: export alias from module;
Set-Alias ok Invoke-OK;
# TODO: export from module:
# Invoke-OK
# Get-OKCommand
# Show-OKFile
# Invoke-OK
# Don't export
# Get-OKToken
# Get-OKCommandLength -- nah way too specific to deserve sharing
# Show-OKCode -code $c.commandText -CommentOffset $commandInfo.commentOffset; -- too specific?
# Show-OKToken