Skip to content

Commit a6aae82

Browse files
committed
PowerShell completion caching strategy
Adding a basic strategy which improves the performance of powershell completion.
1 parent a22c5f7 commit a6aae82

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

completion/powershell

+31-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,37 @@
1111
$gulp_completion_Process = {
1212
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
1313

14-
(node -e "try { var gulp = require('gulp'); require('./gulpfile'); console.log(Object.keys(gulp.tasks).join(' ')); } catch (e) {}").split(' ') |
14+
15+
# Load up an assembly to read the gulpfile's sha1
16+
if(-not $global:GulpSHA1Managed) {
17+
[Reflection.Assembly]::LoadWithPartialName("System.Security") | out-null
18+
$global:GulpSHA1Managed = new-Object System.Security.Cryptography.SHA1Managed
19+
}
20+
21+
# setup a global (in-memory) cache
22+
if(-not $global:GulpfileShaCache) {
23+
$global:GulpfileShaCache = @{};
24+
}
25+
26+
$cache = $global:GulpfileShaCache;
27+
28+
# Get the gulpfile's sha1
29+
$sha1gulpFile = (resolve-path gulpfile.js -ErrorAction Ignore | %{
30+
$file = [System.IO.File]::Open($_.Path, "open", "read")
31+
[string]::join('', ($global:GulpSHA1Managed.ComputeHash($file) | %{ $_.ToString("x2") }))
32+
$file.Dispose()
33+
})
34+
35+
# lookup the sha1 for previously cached task lists.
36+
if($cache.ContainsKey($sha1gulpFile)){
37+
$tasks = $cache[$sha1gulpFile];
38+
} else {
39+
$tasks = (node -e "try { var gulp = require('gulp'); require('./gulpfile'); console.log(Object.keys(gulp.tasks).join(' ')); } catch (e) {}").split(' ');
40+
$cache[$sha1gulpFile] = $tasks;
41+
}
42+
43+
44+
$tasks |
1545
where { $_.startswith($commandName) }
1646
Sort-Object |
1747
foreach { New-Object System.Management.Automation.CompletionResult $_, $_, 'ParameterValue', ('{0}' -f $_) }

0 commit comments

Comments
 (0)