-
Notifications
You must be signed in to change notification settings - Fork 1
/
mkx_completion.sh
executable file
·655 lines (613 loc) · 22.8 KB
/
mkx_completion.sh
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
#!/usr/bin/env bash
# vim:ts=4:sw=4:expandtab
############################################################
# NAME
# mkx_completion.sh - bash completion for mkx
#
# AUTHORS
# neiku project <[email protected]>
#
# VERSION
# 2015/12/26: 支持自动补全mk/mkd(options/targets)
# 2015/12/27: 支持自动补全mk/mkd/mkc(-f/-C选项)
# 2016/01/10: 支持自动补全mkr/mkt/mks
# 2016/01/22: 支持自动补全mkrun/mkm
# 2016/01/23: 解决无法正常输出带有#的配置问题
# 完善mkm find所有子模块的自动补全
# 完善mkm del所有子模块的自动补全
# 2016/02/16: 支持自定义默认target变量名(key => output-name)
# (默认使用OUTPUT做为默认target变量名)
# 2017/01/16: 支持跨平台
# - 通过env程序+PATH变量动态查询bash,控制权
# 、准确性都得到保障
#
############################################################
function _complete_callback_mk()
{
local cur pre opt len word
local makefiles submakedirs targets
local makeflags opt_makefile opt_makedir
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
pre="${COMP_WORDS[COMP_CWORD-1]}"
opt="-f -C"
# -f option-value
opt_makefile=""
len=${#COMP_WORDS[@]}
for ((i = 0; i < $len; i++)) ; do
word="${COMP_WORDS[$i]}"
if [[ "$word" == -f* ]] ; then
# -f<makefile> format, select the last one
if [[ "${word:2}" != "" ]] ; then
opt_makefile="${word:2}"
continue
fi
# -f <makefile> format, select the last one
if [[ $i < $len-1 ]] ; then
word="${COMP_WORDS[$((i+1))]}"
if [[ "$word" != "" ]] ; then
opt_makefile="$word"
continue
fi
fi
fi
done
# -C option-value
opt_makedir="."
len=${#COMP_WORDS[@]}
for ((i = 0; i < $len; i++)) ; do
word="${COMP_WORDS[$i]}"
if [[ "$word" == -C* ]] ; then
# -f<makedir> format, select the last one
if [[ "${word:2}" != "" && -d "${word:2}" ]] ; then
opt_makedir="${word:2}"
continue
fi
# -f <makedir> format, select the last one
if [[ $i < $len-1 ]] ; then
word="${COMP_WORDS[$((i+1))]}"
if [[ "$word" != "" && -d "$word" ]] ; then
opt_makedir="$word"
continue
fi
fi
fi
done
# make flags (-f/-C)
makeflags=""
if [[ "${opt_makedir}" != "" ]] ; then
makeflags="${makeflags} -C ${opt_makedir}"
fi
if [[ "${opt_makefile}" != "" ]] ; then
makeflags="${makeflags} -f ${opt_makefile}"
fi
# options-name
if [[ "$pre" == "-f" ]] ; then
makefiles="`ls ${opt_makedir}/[Mm]akefile* ${opt_makedir}/GNUmakefile* 2>/dev/null \
| while read file; do basename "$file"; done`"
if [[ "$makefiles" == "" ]] ; then
# makefile not found, just complete for file
makefiles="`ls 2>/dev/null`"
fi
COMPREPLY=( $(compgen -W "$makefiles" -- "$cur") )
return 0
fi
if [[ "$pre" == "-C" ]] ; then
submakedirs="`make ${makeflags} -n -p 2>/dev/null | grep '^DIRS =' | tail -n1 | cut -c8-`"
if [[ "$submakedirs" == "" ]] ; then
submakedirs="`find ${opt_makedir} -maxdepth 1 -type d 2>/dev/null`"
fi
if [[ "$submakedirs" == "" ]] ; then
COMPREPLY=( $(compgen -W "`ls`" -- "$cur") )
else
COMPREPLY=( $(compgen -W "$submakedirs" -- "$cur") )
fi
return 0
fi
if [[ "$cur" == -* ]] ; then
COMPREPLY=( $(compgen -W "$opt" -- "$cur") )
return 0
fi
# targets
submakedirs="`make ${makeflags} -n -p 2>/dev/null | grep '^DIRS =' | tail -n1 | cut -c8-`"
if [[ "$submakedirs" != "" ]] ; then
# redirect to -C
COMPREPLY=( "-C" )
return 0
fi
output="`mkm get config output-name OUTPUT`"
length="$((${#output} + 4))"
targets="`make ${makeflags} -n -p 2>/dev/null | grep "^$output =" | tail -n1 | cut -c$length-`"
if [[ "$targets" == "" ]] ; then
# target not fond, just compete for file
COMPREPLY=( $(compgen -W "`ls`" -- "$cur") )
return 0
fi
COMPREPLY=( $(compgen -W "$targets" -- "$cur") )
return 0
}
complete -F _complete_callback_mk mk
complete -F _complete_callback_mk mkc
complete -F _complete_callback_mk mkd
function _complete_callback_mkr()
{
local cur pre opt len word
local makefiles submakedirs targets
local makeflags opt_makefile opt_makedir
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
pre="${COMP_WORDS[COMP_CWORD-1]}"
opt="-f -C"
# -f option-value
opt_makefile=""
len=${#COMP_WORDS[@]}
for ((i = 0; i < $len; i++)) ; do
word="${COMP_WORDS[$i]}"
if [[ "$word" == -f* ]] ; then
# -f<makefile> format, select the last one
if [[ "${word:2}" != "" ]] ; then
opt_makefile="${word:2}"
continue
fi
# -f <makefile> format, select the last one
if [[ $i < $len-1 ]] ; then
word="${COMP_WORDS[$((i+1))]}"
if [[ "$word" != "" ]] ; then
opt_makefile="$word"
continue
fi
fi
fi
done
# -C option-value
opt_makedir="."
len=${#COMP_WORDS[@]}
for ((i = 0; i < $len; i++)) ; do
word="${COMP_WORDS[$i]}"
if [[ "$word" == -C* ]] ; then
# -f<makedir> format, select the last one
if [[ "${word:2}" != "" && -d "${word:2}" ]] ; then
opt_makedir="${word:2}"
continue
fi
# -f <makedir> format, select the last one
if [[ $i < $len-1 ]] ; then
word="${COMP_WORDS[$((i+1))]}"
if [[ "$word" != "" && -d "$word" ]] ; then
opt_makedir="$word"
continue
fi
fi
fi
done
# make flags (-f/-C)
makeflags=""
if [[ "${opt_makedir}" != "" ]] ; then
makeflags="${makeflags} -C ${opt_makedir}"
fi
if [[ "${opt_makefile}" != "" ]] ; then
makeflags="${makeflags} -f ${opt_makefile}"
fi
# options-name
if [[ "$pre" == "-f" ]] ; then
makefiles="`ls ${opt_makedir}/[Mm]akefile* ${opt_makedir}/GNUmakefile* 2>/dev/null \
| while read file; do basename "$file"; done`"
if [[ "$makefiles" == "" ]] ; then
# makefile not found, just complete for file
makefiles="`ls 2>/dev/null`"
fi
COMPREPLY=( $(compgen -W "$makefiles" -- "$cur") )
return 0
fi
if [[ "$pre" == "-C" ]] ; then
submakedirs="`make ${makeflags} -n -p 2>/dev/null | grep '^DIRS =' | tail -n1 | cut -c8-`"
if [[ "$submakedirs" == "" ]] ; then
submakedirs="`find ${opt_makedir} -maxdepth 1 -type d 2>/dev/null`"
fi
if [[ "$submakedirs" == "" ]] ; then
COMPREPLY=( $(compgen -W "`ls`" -- "$cur") )
else
COMPREPLY=( $(compgen -W "$submakedirs" -- "$cur") )
fi
return 0
fi
if [[ "$cur" == -* ]] ; then
COMPREPLY=( $(compgen -W "$opt" -- "$cur") )
return 0
fi
# targets
submakedirs="`make ${makeflags} -n -p 2>/dev/null | grep '^DIRS =' | tail -n1 | cut -c8-`"
if [[ "$submakedirs" != "" ]] ; then
# redirect to -C
COMPREPLY=( "-C" )
return 0
fi
output="`mkm get config output-name OUTPUT`"
length="$((${#output} + 4))"
targets="`make ${makeflags} -n -p 2>/dev/null | grep "^$output =" | tail -n1 | cut -c$length-`"
if [[ "$targets" == "" ]] ; then
# target not fond, try configed-targets
targets="`mkm list target 2>/dev/null
| awk '{for(i=3;i<=NF;i++) printf $i""FS; print ""}'
| tr ' ' '\n'
| sed '/^$/d'
| sort -u`"
if [[ "$targets" == "" ]] ; then
COMPREPLY=( $(compgen -W "`ls`" -- "$cur") )
return 0
fi
fi
COMPREPLY=( $(compgen -W "$targets" -- "$cur") )
return 0
}
complete -F _complete_callback_mkr mkr
complete -F _complete_callback_mkr mkt
complete -F _complete_callback_mkr mks
function _complete_callback_mkrun()
{
local cur len word
COMPREPLY=()
len=${#COMP_WORDS[@]}
cur="${COMP_WORDS[COMP_CWORD]}"
if [ $len -le 2 ] ; then
# mkrun <> or mkrun <cmdtype>
word="`mkm list command 2>/dev/null | awk '{print $2}' | sort -u`"
else
# mkrun cmdtype <target> ...
word="`mkm list command 2>/dev/null | awk '{print $1}' | sort -u`"
fi
COMPREPLY=( $(compgen -W "$word" -- "$cur") )
return 0
}
complete -F _complete_callback_mkrun mkrun
function _complete_callback_mkm()
{
local cur len word
local cmd mod opt scope target module
COMPREPLY=()
len=${#COMP_WORDS[@]}
cur="${COMP_WORDS[COMP_CWORD]}"
# mkm <> or mkm <cmd>
if [ $len -le 2 ] ; then
word="add del find get list path"
COMPREPLY=( $(compgen -W "$word" -- "$cur") )
return 0
fi
# mkm cmd <mod> ...
if [ $len -le 3 ] ; then
cmd="${COMP_WORDS[1]}"
if [ "$cmd" = "del" ] ; then
word="command commandtpl config module target"
elif [ "$cmd" = "get" ] ; then
word="config"
else
word="command commandtpl config module target targetreg"
fi
COMPREPLY=( $(compgen -W "$word" -- "$cur") )
return 0
fi
# mkm cmd mod <scope (--system|--global)> ...
opt="--system --global"
cmd="${COMP_WORDS[1]}"
mod="${COMP_WORDS[2]}"
if [ "$cmd" = "path" -o "$cmd" = "list" ] ; then
word="$opt"
COMPREPLY=( $(compgen -W "$word" -- "$cur") )
return 0
fi
if [ "$cmd" = "find" ] ; then
if [ "$mod" = "target" ] ; then
scope="${COMP_WORDS[3]}"
if [ "$scope" = "--system" -o "$scope" = "--global" ] ; then
# mkm find target scope <target>
if [ $len -le 5 ] ; then
word="`mkm list target $scope 2>/dev/null \
| awk '{for(i=3;i<=NF;i++) printf $i""FS; print ""}' \
| tr ' ' '\n' \
| sed '/^$/d' \
| sort -u`"
fi
elif [ $len -le 4 ] ; then
# mkm find target <scope> <target>
word="`mkm list target 2>/dev/null \
| awk '{for(i=3;i<=NF;i++) printf $i""FS; print ""}' \
| tr ' ' '\n' \
| sed '/^$/d' \
| sort -u`"
word="$opt $word"
fi
COMPREPLY=( $(compgen -W "$word" -- "$cur") )
return 0
fi
if [ "$mod" = "module" ] ; then
scope="${COMP_WORDS[3]}"
if [ "$scope" = "--system" -o "$scope" = "--global" ] ; then
# mkm find module scope <module>
if [ $len -le 5 ] ; then
word="`mkm list module $scope 2>/dev/null \
| awk '{print $1}' \
| sort -u`"
fi
elif [ $len -le 4 ] ; then
# mkm find module <scope> <module>
word="`mkm list module 2>/dev/null \
| awk '{print $1}' \
| sort -u`"
word="$opt $word"
fi
COMPREPLY=( $(compgen -W "$word" -- "$cur") )
return 0
fi
if [ "$mod" = "config" ] ; then
scope="${COMP_WORDS[3]}"
if [ "$scope" = "--system" -o "$scope" = "--global" ] ; then
# mkm find config scope <key>
if [ $len -le 5 ] ; then
word="`mkm list config $scope 2>/dev/null \
| awk '{print $1}' \
| sort -u`"
fi
elif [ $len -le 4 ] ; then
# mkm find config <scope> <key>
word="`mkm list config 2>/dev/null \
| awk '{print $1}' \
| sort -u`"
word="$opt $word"
fi
COMPREPLY=( $(compgen -W "$word" -- "$cur") )
return 0
fi
if [ "$mod" = "command" ] ; then
scope="${COMP_WORDS[3]}"
if [ "$scope" = "--system" -o "$scope" = "--global" ] ; then
if [ $len -le 5 ] ; then
# mkm find command scope <target>
word="`mkm list command $scope 2>/dev/null \
| awk '{print $1}' \
| sort -u`"
elif [ $len -le 6 ] ; then
# mkm find command scope target <cmdtype>
target="${COMP_WORDS[4]}"
word="`mkm list command $scope 2>/dev/null \
| grep -w -P "^$target[ \t]*" \
| awk '{print $2}' \
| sort -u`"
fi
# no need auto-complete
else
if [ $len -le 4 ] ; then
# mkm find command <scope> <target>
word="`mkm list command 2>/dev/null \
| awk '{print $1}' \
| sort -u`"
word="$opt $word"
elif [ $len -le 5 ] ; then
# mkm find command target <cmdtype>
target="${COMP_WORDS[3]}"
word="`mkm list command 2>/dev/null \
| grep -w -P "^$target[ \t]*" \
| awk '{print $2}' \
| sort -u`"
fi
# no need auto-complete
fi
COMPREPLY=( $(compgen -W "$word" -- "$cur") )
return 0
fi
if [ "$mod" = "commandtpl" ] ; then
scope="${COMP_WORDS[3]}"
if [ "$scope" = "--system" -o "$scope" = "--global" ] ; then
if [ $len -le 5 ] ; then
# mkm find commandtpl scope <module>
word="`mkm list commandtpl $scope 2>/dev/null \
| awk '{print $1}' \
| sort -u`"
elif [ $len -le 6 ] ; then
# mkm find commandtpl scope module <cmdtype>
module="${COMP_WORDS[4]}"
word="`mkm list commandtpl $scope 2>/dev/null \
| grep -w -P "^$module[ \t]*" \
| awk '{print $2}' \
| sort -u`"
fi
# no need auto-complete
else
if [ $len -le 4 ] ; then
# mkm find commandtpl <scope> <module>
word="`mkm list commandtpl 2>/dev/null \
| awk '{print $1}' \
| sort -u`"
word="$opt $word"
elif [ $len -le 5 ] ; then
# mkm find commandtpl module <cmdtype>
module="${COMP_WORDS[3]}"
word="`mkm list commandtpl 2>/dev/null \
| grep -w -P "^$module[ \t]*" \
| awk '{print $2}' \
| sort -u`"
fi
# no need auto-complete
fi
COMPREPLY=( $(compgen -W "$word" -- "$cur") )
return 0
fi
if [ "$mod" = "targetreg" ] ; then
scope="${COMP_WORDS[3]}"
if [ "$scope" = "--system" -o "$scope" = "--global" ] ; then
# mkm find targetreg scope <target>
if [ $len -le 5 ] ; then
COMPREPLY=( $(compgen -f -- "$cur") )
return 0
fi
elif [ $len -le 4 ] ; then
# mkm find targetreg <scope> <target>
COMPREPLY=( $(compgen -W "$opt" -f -- "$cur") )
return 0
fi
fi
return 0
fi
if [ "$cmd" = "del" ] ; then
if [ "$mod" = "target" ] ; then
scope="${COMP_WORDS[3]}"
if [ "$scope" = "--system" -o "$scope" = "--global" ] ; then
# mkm del target scope <target>...
word="`mkm list target $scope 2>/dev/null \
| awk '{for(i=3;i<=NF;i++) printf $i""FS; print ""}' \
| tr ' ' '\n' \
| sed '/^$/d' \
| sort -u`"
elif [ $len -le 4 ] ; then
# mkm del target <scope> <target>...
word="`mkm list target 2>/dev/null \
| awk '{for(i=3;i<=NF;i++) printf $i""FS; print ""}' \
| tr ' ' '\n' \
| sed '/^$/d' \
| sort -u`"
word="$opt $word"
else
# mkm del target <target>...
word="`mkm list target 2>/dev/null \
| awk '{for(i=3;i<=NF;i++) printf $i""FS; print ""}' \
| tr ' ' '\n' \
| sed '/^$/d' \
| sort -u`"
fi
COMPREPLY=( $(compgen -W "$word" -- "$cur") )
return 0
fi
if [ "$mod" = "module" ] ; then
scope="${COMP_WORDS[3]}"
if [ "$scope" = "--system" -o "$scope" = "--global" ] ; then
# mkm del module scope <module>...
word="`mkm list module $scope 2>/dev/null \
| awk '{print $1}' \
| sort -u`"
elif [ $len -le 4 ] ; then
# mkm del module <scope> <module>...
word="`mkm list module 2>/dev/null \
| awk '{print $1}' \
| sort -u`"
word="$opt $word"
else
# mkm del module <module>...
word="`mkm list module 2>/dev/null \
| awk '{print $1}' \
| sort -u`"
fi
COMPREPLY=( $(compgen -W "$word" -- "$cur") )
return 0
fi
if [ "$mod" = "config" ] ; then
scope="${COMP_WORDS[3]}"
if [ "$scope" = "--system" -o "$scope" = "--global" ] ; then
# mkm del config scope <key>...
word="`mkm list config $scope 2>/dev/null \
| awk '{print $1}' \
| sort -u`"
elif [ $len -le 4 ] ; then
# mkm del config <scope> <key>...
word="`mkm list config 2>/dev/null \
| awk '{print $1}' \
| sort -u`"
word="$opt $word"
else
# mkm del config <key>...
word="`mkm list config 2>/dev/null \
| awk '{print $1}' \
| sort -u`"
fi
COMPREPLY=( $(compgen -W "$word" -- "$cur") )
return 0
fi
if [ "$mod" = "command" ] ; then
scope="${COMP_WORDS[3]}"
if [ "$scope" = "--system" -o "$scope" = "--global" ] ; then
if [ $len -le 5 ] ; then
# mkm del command scope <target>
word="`mkm list command $scope 2>/dev/null \
| awk '{print $1}' \
| sort -u`"
elif [ $len -le 6 ] ; then
# mkm del command scope target <cmdtype>
target="${COMP_WORDS[4]}"
word="`mkm list command $scope 2>/dev/null \
| grep -w -P "^$target[ \t]*" \
| awk '{print $2}' \
| sort -u`"
fi
# no need auto-complete
else
if [ $len -le 4 ] ; then
# mkm del command <scope> <target>
word="`mkm list command 2>/dev/null \
| awk '{print $1}' \
| sort -u`"
word="$opt $word"
elif [ $len -le 5 ] ; then
# mkm del command target <cmdtype>
target="${COMP_WORDS[3]}"
word="`mkm list command 2>/dev/null \
| grep -w -P "^$target[ \t]*" \
| awk '{print $2}' \
| sort -u`"
fi
# no need auto-complete
fi
COMPREPLY=( $(compgen -W "$word" -- "$cur") )
return 0
fi
if [ "$mod" = "commandtpl" ] ; then
scope="${COMP_WORDS[3]}"
if [ "$scope" = "--system" -o "$scope" = "--global" ] ; then
if [ $len -le 5 ] ; then
# mkm del commandtpl scope <module>
word="`mkm list commandtpl $scope 2>/dev/null \
| awk '{print $1}' \
| sort -u`"
elif [ $len -le 6 ] ; then
# mkm del commandtpl scope module <cmdtype>
module="${COMP_WORDS[4]}"
word="`mkm list commandtpl $scope 2>/dev/null \
| grep -w -P "^$module[ \t]*" \
| awk '{print $2}' \
| sort -u`"
fi
# no need auto-complete
else
if [ $len -le 4 ] ; then
# mkm del commandtpl <scope> <module>
word="`mkm list commandtpl 2>/dev/null \
| awk '{print $1}' \
| sort -u`"
word="$opt $word"
elif [ $len -le 5 ] ; then
# mkm del commandtpl module <cmdtype>
module="${COMP_WORDS[3]}"
word="`mkm list commandtpl 2>/dev/null \
| grep -w -P "^$module[ \t]*" \
| awk '{print $2}' \
| sort -u`"
fi
# no need auto-complete
fi
COMPREPLY=( $(compgen -W "$word" -- "$cur") )
return 0
fi
return 0
fi
if [ "$cmd" = "get" ] ; then
if [ "$mod" = "config" ] ; then
scope="${COMP_WORDS[3]}"
word="`mkm list config $scope 2>/dev/null \
| awk '{print $1}' \
| sort -u`"
word="$opt $word"
fi
COMPREPLY=( $(compgen -W "$word" -- "$cur") )
return 0
fi
# TODO: add ...
return 0
}
complete -F _complete_callback_mkm mkm