Skip to content

Instantly share code, notes, and snippets.

@fearthecowboy
Last active August 25, 2017 10:35
Show Gist options
  • Save fearthecowboy/b832488906fe7cd99c422d8dea197618 to your computer and use it in GitHub Desktop.
Save fearthecowboy/b832488906fe7cd99c422d8dea197618 to your computer and use it in GitHub Desktop.
Better gulp tab expansion script
# Copyright (c) 2014 Jason Jarrett
#
# Tab completion for the `gulp`
#
# Usage:
#
# To enable powershell <tab> completion for gulp you need to be running
# at least PowerShell v3 or greater and add the below to your $PROFILE
#
# Invoke-Expression ((gulp --completion=powershell) -join [System.Environment]::NewLine)
# iex (iwr https://gist.githubusercontent.com/fearthecowboy/b832488906fe7cd99c422d8dea197618/raw/08bc0cdea834bc341f2986982aa74762c35e534f/gulp-tab.ps1)
# walks up to find the gulpfile (and supports extensions other than .js)
function Get-GulpFilePath($startPath) {
$r = resolve-path "$startPath/gulpfile.*"
if ($r) {
return $r[0]
}
$newPath = resolve-path -ea 0 "$startPath/../" -ErrorVariable x
if( $newPath.Path -eq $startPath ) {
return;
}
return Get-GulpFilePath($newPath)
}
$gulp_completion_Process = {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
# Load up an assembly to read the gulpfile's sha1
if(-not $global:GulpSHA1Managed) {
[Reflection.Assembly]::LoadWithPartialName("System.Security") | out-null
$global:GulpSHA1Managed = new-Object System.Security.Cryptography.SHA1Managed
}
# setup a global (in-memory) cache
if(-not $global:GulpfileShaCache) {
$global:GulpfileShaCache = @{};
}
$cache = $global:GulpfileShaCache;
# Get the gulpfile's sha1
$sha1gulpFile = ((Get-GulpFilePath .) | %{
$file = [System.IO.File]::Open($_.Path, "open", "read")
[string]::join('', ($global:GulpSHA1Managed.ComputeHash($file) | %{ $_.ToString("x2") }))
$file.Dispose()
})
if ($sha1gulpFile) {
# lookup the sha1 for previously cached task lists.
if($cache.ContainsKey($sha1gulpFile)){
$tasks = $cache[$sha1gulpFile];
} else {
$tasks = (gulp --tasks-simple).split("`n");
$cache[$sha1gulpFile] = $tasks;
}
$tasks |
where { $_.startswith($commandName) }
Sort-Object |
foreach { New-Object System.Management.Automation.CompletionResult $_, $_, 'ParameterValue', ('{0}' -f $_) }
}
}
if (-not $global:options) {
$global:options = @{
CustomArgumentCompleters = @{};
NativeArgumentCompleters = @{}
}
}
$global:options['NativeArgumentCompleters']['gulp'] = $gulp_completion_Process
$function:tabexpansion2 = $function:tabexpansion2 -replace 'End\r\n{','End { if ($null -ne $options) { $options += $global:options} else {$options = $global:options}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment