forked from PowerShell/vscode-powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindModule.ts
99 lines (82 loc) · 3.72 KB
/
FindModule.ts
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
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import vscode = require("vscode");
import { RequestType } from "vscode-languageclient";
import QuickPickItem = vscode.QuickPickItem;
import { LanguageClientConsumer } from "../languageClientConsumer";
export const FindModuleRequestType =
new RequestType<any, any, void, void>("powerShell/findModule");
export const InstallModuleRequestType =
new RequestType<string, void, void, void>("powerShell/installModule");
export class FindModuleFeature extends LanguageClientConsumer {
private command: vscode.Disposable;
private cancelFindToken: vscode.CancellationTokenSource;
constructor() {
super();
this.command = vscode.commands.registerCommand("PowerShell.PowerShellFindModule", () => {
// It takes a while to get the list of PowerShell modules, display some UI to let user know
this.cancelFindToken = new vscode.CancellationTokenSource();
vscode.window
.showQuickPick(
["Cancel"],
{ placeHolder: "Please wait, retrieving list of PowerShell modules. This can take some time..." },
this.cancelFindToken.token)
.then((response) => {
if (response === "Cancel") { this.clearCancelFindToken(); }
});
// Cancel the loading prompt after 60 seconds
setTimeout(() => {
if (this.cancelFindToken) {
this.clearCancelFindToken();
vscode.window.showErrorMessage(
"The online source for PowerShell modules is not responding. " +
"Cancelling Find/Install PowerShell command.");
}
}, 60000);
this.pickPowerShellModule().then((moduleName) => {
if (moduleName) {
// vscode.window.setStatusBarMessage("Installing PowerShell Module " + moduleName, 1500);
this.languageClient.sendRequest(InstallModuleRequestType, moduleName);
}
});
});
}
public dispose() {
this.command.dispose();
}
private pickPowerShellModule(): Thenable<string> {
return this.languageClient.sendRequest(FindModuleRequestType, null).then((modules) => {
const items: QuickPickItem[] = [];
// We've got the modules info, let's cancel the timeout unless it's already been cancelled
if (this.cancelFindToken) {
this.clearCancelFindToken();
} else {
// Already timed out, would be weird to dislay modules after we said it timed out.
return Promise.resolve("");
}
for (const item in modules) {
if (modules.hasOwnProperty(item)) {
items.push({ label: modules[item].name, description: modules[item].description });
}
}
if (items.length === 0) {
return Promise.reject("No PowerShell modules were found.");
}
const options: vscode.QuickPickOptions = {
placeHolder: "Select a PowerShell module to install",
matchOnDescription: true,
matchOnDetail: true,
};
return vscode.window.showQuickPick(items, options).then((item) => {
return item ? item.label : "";
});
});
}
private clearCancelFindToken() {
if (this.cancelFindToken) {
this.cancelFindToken.dispose();
this.cancelFindToken = undefined;
}
}
}