1 # Copyright (c) 2014 Jason Jarrett
3 # Tab completion for the `gulp`
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
10 # Invoke-Expression ((gulp --completion=powershell) -join [System.Environment]::NewLine)
14 $gulp_completion_Process = {
15 param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
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
24 # setup a global (in-memory) cache
25 if(-not $global:GulpfileShaCache) {
26 $global:GulpfileShaCache = @{};
29 $cache = $global:GulpfileShaCache;
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") }))
38 # lookup the sha1 for previously cached task lists.
39 if($cache.ContainsKey($sha1gulpFile)){
40 $tasks = $cache[$sha1gulpFile];
42 $tasks = (gulp --tasks-simple).split("`n");
43 $cache[$sha1gulpFile] = $tasks;
48 where { $_.startswith($commandName) }
50 foreach { New-Object System.Management.Automation.CompletionResult $_, $_, 'ParameterValue', ('{0}' -f $_) }
53 if (-not $global:options) {
55 CustomArgumentCompleters = @{};
56 NativeArgumentCompleters = @{}
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}'