-
Notifications
You must be signed in to change notification settings - Fork 30.4k
/
Copy pathrunAutomaticTasks.ts
158 lines (143 loc) · 6.37 KB
/
runAutomaticTasks.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as nls from 'vs/nls';
import { Disposable } from 'vs/base/common/lifecycle';
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
import { ITaskService, WorkspaceFolderTaskResult } from 'vs/workbench/contrib/tasks/common/taskService';
import { forEach } from 'vs/base/common/collections';
import { RunOnOptions, Task, TaskRunSource } from 'vs/workbench/contrib/tasks/common/tasks';
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
import { INotificationService, Severity } from 'vs/platform/notification/common/notification';
import { Action } from 'vs/base/common/actions';
import { IQuickPickItem, IQuickInputService } from 'vs/platform/quickinput/common/quickInput';
const ARE_AUTOMATIC_TASKS_ALLOWED_IN_WORKSPACE = 'tasks.run.allowAutomatic';
export class RunAutomaticTasks extends Disposable implements IWorkbenchContribution {
constructor(
@ITaskService private readonly taskService: ITaskService,
@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) {
// 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 => {
let { tasks } = RunAutomaticTasks.findAutoTasks(this.taskService, workspaceTaskResult);
if (tasks.length > 0) {
RunAutomaticTasks.runTasks(this.taskService, tasks);
}
});
}
}
private static runTasks(taskService: ITaskService, tasks: Array<Task | Promise<Task>>) {
tasks.forEach(task => {
if (task instanceof Promise) {
task.then(promiseResult => {
if (promiseResult) {
taskService.run(promiseResult);
}
});
} else {
taskService.run(task);
}
});
}
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 !== undefined) {
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 => {
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 and run"),
run: () => {
resolve(true);
storageService.store(ARE_AUTOMATIC_TASKS_ALLOWED_IN_WORKSPACE, true, StorageScope.WORKSPACE);
}
},
{
label: nls.localize('disallow', "Disallow"),
run: () => {
resolve(false);
storageService.store(ARE_AUTOMATIC_TASKS_ALLOWED_IN_WORKSPACE, false, StorageScope.WORKSPACE);
}
},
{
label: nls.localize('openTasks', "Open tasks.json"),
run: () => {
taskService.openConfig(undefined);
resolve(false);
}
}]
);
});
}
}
export class ManageAutomaticTaskRunning extends Action {
public static readonly ID = 'workbench.action.tasks.manageAutomaticRunning';
public static readonly LABEL = nls.localize('workbench.action.tasks.manageAutomaticRunning', "Manage Automatic Tasks in Folder");
constructor(
id: string, label: string,
@IStorageService private readonly storageService: IStorageService,
@IQuickInputService private readonly quickInputService: IQuickInputService
) {
super(id, label);
}
public async run(event?: any): Promise<any> {
const allowItem: IQuickPickItem = { label: nls.localize('workbench.action.tasks.allowAutomaticTasks', "Allow Automatic Tasks in Folder") };
const disallowItem: IQuickPickItem = { label: nls.localize('workbench.action.tasks.disallowAutomaticTasks', "Disallow Automatic Tasks in Folder") };
const value = await this.quickInputService.pick([allowItem, disallowItem], { canPickMany: false });
if (!value) {
return;
}
this.storageService.store(ARE_AUTOMATIC_TASKS_ALLOWED_IN_WORKSPACE, value === allowItem, StorageScope.WORKSPACE);
}
}