【发布时间】:2020-04-25 16:30:53
【问题描述】:
let result = await vscode.commands.executeCommand('workbench.action.tasks.build');
立即解决。
如何使用 VS Code API 等待构建任务?
【问题讨论】:
标签: visual-studio-code vscode-extensions
let result = await vscode.commands.executeCommand('workbench.action.tasks.build');
立即解决。
如何使用 VS Code API 等待构建任务?
【问题讨论】:
标签: visual-studio-code vscode-extensions
我想通了!任务不能从vscode.tasks.executeTask 等待,但我们可以等待vscode.tasks.onDidEndTask 并检查结束的任务是否是我们的任务。
async function executeBuildTask(task: vscode.Task) {
const execution = await vscode.tasks.executeTask(task);
return new Promise<void>(resolve => {
let disposable = vscode.tasks.onDidEndTask(e => {
if (e.execution.task.group === vscode.TaskGroup.Build) {
disposable.dispose();
resolve();
}
});
});
}
async function getBuildTasks() {
return new Promise<vscode.Task[]>(resolve => {
vscode.tasks.fetchTasks().then((tasks) => {
resolve(tasks.filter((task) => task.group === vscode.TaskGroup.Build));
});
});
}
export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.commands.registerCommand('extension.helloWorld', async () => {
const buildTasks = await getBuildTasks();
await executeBuildTask(buildTasks[0]);
}));
}
请注意,目前有一个bug #96643,它阻止我们对 vscode.Task 对象进行比较:if (e.execution.task === execution.task) { ... }
【讨论】:
if (e.execution === execution) 现在应该可以工作了。
我认为这取决于在 extension.ts 中主命令是如何执行的
作为 JS/TS 的新手,我在这里可能错了,但只是想提供帮助:
确保 vscode.command.registerCommand 不是异步的,如下所示:
context.subscriptions.push(vscode.commands.registerCommand('extension.openSettings', () => {
return vscode.commands.executeCommand("workbench.action.openSettings", "settingsName");
}));
这将与异步进行比较,如下所示:
context.subscriptions.push(vscode.commands.registerCommand('extension.removeHost', async (hostID) => {
const bigipHosts: Array<string> | undefined = vscode.workspace.getConfiguration().get('extension.hosts');
const newHosts = Hosts?.filter( item => item != hostID.label)
await vscode.workspace.getConfiguration().update('f5-fast.hosts', newBigipHosts, vscode.ConfigurationTarget.Global);
hostsTreeProvider.refresh();
}));
【讨论】: