1 | # Copyright (c) 2014 Jason Jarrett
|
2 | #
|
3 | # Tab completion for the `gulp`
|
4 | #
|
5 | # Usage:
|
6 | #
|
7 | # To enable powershell <tab> completion for gulp you need to be running
|
8 | # at least PowerShell v3 or greater and add the below to your $PROFILE
|
9 | #
|
10 | # Invoke-Expression ((gulp --completion=powershell) -join [System.Environment]::NewLine)
|
11 | #
|
12 | #
|
13 |
|
14 | $gulp_completion_Process = {
|
15 | param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
|
16 |
|
17 |
|
18 | # Load up an assembly to read the gulpfile's sha1
|
19 | if(-not $global:GulpSHA1Managed) {
|
20 | [Reflection.Assembly]::LoadWithPartialName("System.Security") | out-null
|
21 | $global:GulpSHA1Managed = new-Object System.Security.Cryptography.SHA1Managed
|
22 | }
|
23 |
|
24 | # setup a global (in-memory) cache
|
25 | if(-not $global:GulpfileShaCache) {
|
26 | $global:GulpfileShaCache = @{};
|
27 | }
|
28 |
|
29 | $cache = $global:GulpfileShaCache;
|
30 |
|
31 | # Get the gulpfile's sha1
|
32 | $sha1gulpFile = (resolve-path gulpfile.js -ErrorAction Ignore | %{
|
33 | $file = [System.IO.File]::Open($_.Path, "open", "read")
|
34 | [string]::join('', ($global:GulpSHA1Managed.ComputeHash($file) | %{ $_.ToString("x2") }))
|
35 | $file.Dispose()
|
36 | })
|
37 |
|
38 | # lookup the sha1 for previously cached task lists.
|
39 | if($cache.ContainsKey($sha1gulpFile)){
|
40 | $tasks = $cache[$sha1gulpFile];
|
41 | } else {
|
42 | $tasks = (gulp --tasks-simple).split("`n");
|
43 | $cache[$sha1gulpFile] = $tasks;
|
44 | }
|
45 |
|
46 |
|
47 | $tasks |
|
48 | where { $_.startswith($commandName) }
|
49 | Sort-Object |
|
50 | foreach { New-Object System.Management.Automation.CompletionResult $_, $_, 'ParameterValue', ('{0}' -f $_) }
|
51 | }
|
52 |
|
53 | if (-not $global:options) {
|
54 | $global:options = @{
|
55 | CustomArgumentCompleters = @{};
|
56 | NativeArgumentCompleters = @{}
|
57 | }
|
58 | }
|
59 |
|
60 | $global:options['NativeArgumentCompleters']['gulp'] = $gulp_completion_Process
|
61 | $function:tabexpansion2 = $function:tabexpansion2 -replace 'End\r\n{','End { if ($null -ne $options) { $options += $global:options} else {$options = $global:options}'
|