提交 2cfc11d5 编写于 作者: A Alex Ross

Move automatic task prompt to happen when the user tries to run a task

Fixes #64355
上级 e5acb920
......@@ -6,7 +6,7 @@
import * as nls from 'vs/nls';
import { Disposable } from 'vs/base/common/lifecycle';
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
import { ITaskService } from 'vs/workbench/parts/tasks/common/taskService';
import { ITaskService, WorkspaceFolderTaskResult } from 'vs/workbench/parts/tasks/common/taskService';
import { forEach } from 'vs/base/common/collections';
import { RunOnOptions, Task, TaskRunSource } from 'vs/workbench/parts/tasks/common/tasks';
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
......@@ -18,97 +18,110 @@ const ARE_AUTOMATIC_TASKS_ALLOWED_IN_WORKSPACE = 'tasks.run.allowAutomatic';
export class RunAutomaticTasks extends Disposable implements IWorkbenchContribution {
constructor(
@ITaskService private readonly taskService: ITaskService,
@IStorageService private readonly storageService: IStorageService,
@INotificationService private readonly notificationService: INotificationService) {
@IStorageService storageService: IStorageService) {
super();
const isFolderAutomaticAllowed = storageService.getBoolean(ARE_AUTOMATIC_TASKS_ALLOWED_IN_WORKSPACE, StorageScope.WORKSPACE, undefined);
this.tryRunTasks(isFolderAutomaticAllowed);
}
private tryRunTasks(isAllowed: boolean | undefined) {
// Not necessarily allowed to run the tasks, but we can see if there are any.
if (isAllowed !== false) {
// Only run if allowed. Prompting for permission occurs when a user first tries to run a task.
if (isAllowed === true) {
this.taskService.getWorkspaceTasks(TaskRunSource.FolderOpen).then(workspaceTaskResult => {
if (workspaceTaskResult) {
const tasks = new Array<Task | Promise<Task>>();
const taskNames = new Array<string>();
workspaceTaskResult.forEach(resultElement => {
if (resultElement.set) {
resultElement.set.tasks.forEach(task => {
if (task.runOptions.runOn === RunOnOptions.folderOpen) {
tasks.push(task);
taskNames.push(task._label);
}
});
}
if (resultElement.configurations) {
forEach(resultElement.configurations.byIdentifier, (configedTask) => {
if (configedTask.value.runOptions.runOn === RunOnOptions.folderOpen) {
tasks.push(new Promise<Task>(resolve => {
this.taskService.getTask(resultElement.workspaceFolder, configedTask.value._id, true).then(task => resolve(task));
}));
if (configedTask.value._label) {
taskNames.push(configedTask.value._label);
} else {
taskNames.push(configedTask.value.configures.task);
}
}
});
}
});
if (tasks.length > 0) {
// We have automatic tasks, prompt to run it if we don't already have permission.
if (isAllowed === undefined) {
this.showPrompt(taskNames).then(postPromptAllowed => {
if (postPromptAllowed) {
this.runTasks(tasks);
}
});
} else { // isAllowed must be true
this.runTasks(tasks);
}
}
let { tasks } = RunAutomaticTasks.findAutoTasks(this.taskService, workspaceTaskResult);
if (tasks.length > 0) {
RunAutomaticTasks.runTasks(this.taskService, tasks);
}
});
}
}
private runTasks(tasks: Array<Task | Promise<Task>>) {
private static runTasks(taskService: ITaskService, tasks: Array<Task | Promise<Task>>) {
tasks.forEach(task => {
if (task instanceof Promise) {
task.then(promiseResult => {
if (promiseResult) {
this.taskService.run(promiseResult);
taskService.run(promiseResult);
}
});
} else {
this.taskService.run(task);
taskService.run(task);
}
});
}
private showPrompt(taskNames: Array<string>): Promise<boolean> {
private static findAutoTasks(taskService: ITaskService, workspaceTaskResult: Map<string, WorkspaceFolderTaskResult>): { tasks: Array<Task | Promise<Task>>, taskNames: Array<string> } {
const tasks = new Array<Task | Promise<Task>>();
const taskNames = new Array<string>();
if (workspaceTaskResult) {
workspaceTaskResult.forEach(resultElement => {
if (resultElement.set) {
resultElement.set.tasks.forEach(task => {
if (task.runOptions.runOn === RunOnOptions.folderOpen) {
tasks.push(task);
taskNames.push(task._label);
}
});
}
if (resultElement.configurations) {
forEach(resultElement.configurations.byIdentifier, (configedTask) => {
if (configedTask.value.runOptions.runOn === RunOnOptions.folderOpen) {
tasks.push(new Promise<Task>(resolve => {
taskService.getTask(resultElement.workspaceFolder, configedTask.value._id, true).then(task => resolve(task));
}));
if (configedTask.value._label) {
taskNames.push(configedTask.value._label);
} else {
taskNames.push(configedTask.value.configures.task);
}
}
});
}
});
}
return { tasks, taskNames };
}
public static promptForPermission(taskService: ITaskService, storageService: IStorageService, notificationService: INotificationService,
workspaceTaskResult: Map<string, WorkspaceFolderTaskResult>) {
const isFolderAutomaticAllowed = storageService.getBoolean(ARE_AUTOMATIC_TASKS_ALLOWED_IN_WORKSPACE, StorageScope.WORKSPACE, undefined);
if (isFolderAutomaticAllowed !== void 0) {
return;
}
let { tasks, taskNames } = RunAutomaticTasks.findAutoTasks(taskService, workspaceTaskResult);
if (taskNames.length > 0) {
// We have automatic tasks, prompt to allow.
this.showPrompt(notificationService, storageService, taskService, taskNames).then(allow => {
if (allow) {
RunAutomaticTasks.runTasks(taskService, tasks);
}
});
}
}
private static showPrompt(notificationService: INotificationService, storageService: IStorageService, taskService: ITaskService,
taskNames: Array<string>): Promise<boolean> {
return new Promise<boolean>(resolve => {
this.notificationService.prompt(Severity.Info, nls.localize('tasks.run.allowAutomatic', "This folder has tasks ({0}) defined in \'tasks.json\' that run automatically when you open this folder. Do you allow automatic tasks to run when you open this folder?", taskNames.join(', ')),
notificationService.prompt(Severity.Info, nls.localize('tasks.run.allowAutomatic', "This folder has tasks ({0}) defined in \'tasks.json\' that run automatically when you open this folder. Do you allow automatic tasks to run when you open this folder?", taskNames.join(', ')),
[{
label: nls.localize('allow', "Allow"),
label: nls.localize('allow', "Allow and run"),
run: () => {
resolve(true);
this.storageService.store(ARE_AUTOMATIC_TASKS_ALLOWED_IN_WORKSPACE, true, StorageScope.WORKSPACE);
storageService.store(ARE_AUTOMATIC_TASKS_ALLOWED_IN_WORKSPACE, true, StorageScope.WORKSPACE);
}
},
{
label: nls.localize('disallow', "Disallow"),
run: () => {
resolve(false);
this.storageService.store(ARE_AUTOMATIC_TASKS_ALLOWED_IN_WORKSPACE, false, StorageScope.WORKSPACE);
storageService.store(ARE_AUTOMATIC_TASKS_ALLOWED_IN_WORKSPACE, false, StorageScope.WORKSPACE);
}
},
{
label: nls.localize('openTasks', "Open tasks.json"),
run: () => {
this.taskService.openConfig(undefined);
taskService.openConfig(undefined);
resolve(false);
}
}]
......
......@@ -1505,6 +1505,11 @@ class TaskService extends Disposable implements ITaskService {
return this._workspaceTasksPromise;
}
this.updateWorkspaceTasks(runSource);
if (runSource === TaskRunSource.User) {
this._workspaceTasksPromise.then(workspaceFolderTasks => {
RunAutomaticTasks.promptForPermission(this, this.storageService, this.notificationService, workspaceFolderTasks);
});
}
return this._workspaceTasksPromise;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册