diff --git a/src/vs/workbench/parts/tasks/electron-browser/runAutomaticTasks.ts b/src/vs/workbench/parts/tasks/electron-browser/runAutomaticTasks.ts index b5c38bfa6d767c77efe8e1ab168b3fcd730e1a9c..bd6c3b7a113dfe386c16a229cdb29fdc40759357 100644 --- a/src/vs/workbench/parts/tasks/electron-browser/runAutomaticTasks.ts +++ b/src/vs/workbench/parts/tasks/electron-browser/runAutomaticTasks.ts @@ -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>(); - const taskNames = new Array(); - 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(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>) { + private static runTasks(taskService: ITaskService, tasks: Array>) { 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): Promise { + private static findAutoTasks(taskService: ITaskService, workspaceTaskResult: Map): { tasks: Array>, taskNames: Array } { + const tasks = new Array>(); + const taskNames = new Array(); + 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(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) { + 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): Promise { return new Promise(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); } }] diff --git a/src/vs/workbench/parts/tasks/electron-browser/task.contribution.ts b/src/vs/workbench/parts/tasks/electron-browser/task.contribution.ts index 86134698d7d591165c3aa9f9f0214280c07efd39..f905a6fd77e2479397b6735203eb62bc6601aca0 100644 --- a/src/vs/workbench/parts/tasks/electron-browser/task.contribution.ts +++ b/src/vs/workbench/parts/tasks/electron-browser/task.contribution.ts @@ -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; }