Skip to content

Commit 755d39f

Browse files
authored
add allow automatic tasks setting (microsoft#154171)
1 parent eab7ba2 commit 755d39f

File tree

4 files changed

+41
-24
lines changed

4 files changed

+41
-24
lines changed

src/vs/workbench/contrib/tasks/browser/abstractTaskService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ export abstract class AbstractTaskService extends Disposable implements ITaskSer
10841084
}).then((value) => {
10851085
if (runSource === TaskRunSource.User) {
10861086
this.getWorkspaceTasks().then(workspaceTasks => {
1087-
RunAutomaticTasks.promptForPermission(this, this._storageService, this._notificationService, this._workspaceTrustManagementService, this._openerService, workspaceTasks);
1087+
RunAutomaticTasks.promptForPermission(this, this._storageService, this._notificationService, this._workspaceTrustManagementService, this._openerService, this._configurationService, workspaceTasks);
10881088
});
10891089
}
10901090
return value;

src/vs/workbench/contrib/tasks/browser/runAutomaticTasks.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@ import { IQuickPickItem, IQuickInputService } from 'vs/platform/quickinput/commo
1515
import { Action2 } from 'vs/platform/actions/common/actions';
1616
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
1717
import { IWorkspaceTrustManagementService } from 'vs/platform/workspace/common/workspaceTrust';
18-
import { ConfigurationTarget } from 'vs/platform/configuration/common/configuration';
18+
import { ConfigurationTarget, IConfigurationService } from 'vs/platform/configuration/common/configuration';
1919
import { IOpenerService } from 'vs/platform/opener/common/opener';
2020
import { URI } from 'vs/base/common/uri';
2121
import { Event } from 'vs/base/common/event';
2222
import { ILogService } from 'vs/platform/log/common/log';
2323

24-
const ARE_AUTOMATIC_TASKS_ALLOWED_IN_WORKSPACE = 'tasks.run.allowAutomatic';
24+
const HAS_PROMPTED_FOR_AUTOMATIC_TASKS = 'task.hasPromptedForAutomaticTasks';
25+
const ALLOW_AUTOMATIC_TASKS = 'task.allowAutomaticTasks';
2526

2627
export class RunAutomaticTasks extends Disposable implements IWorkbenchContribution {
2728
constructor(
2829
@ITaskService private readonly _taskService: ITaskService,
29-
@IStorageService private readonly _storageService: IStorageService,
30+
@IConfigurationService private readonly _configurationService: IConfigurationService,
3031
@IWorkspaceTrustManagementService private readonly _workspaceTrustManagementService: IWorkspaceTrustManagementService,
3132
@ILogService private readonly _logService: ILogService) {
3233
super();
@@ -42,7 +43,7 @@ export class RunAutomaticTasks extends Disposable implements IWorkbenchContribut
4243
}
4344

4445
this._logService.trace('RunAutomaticTasks: Checking if automatic tasks should run.');
45-
const isFolderAutomaticAllowed = this._storageService.getBoolean(ARE_AUTOMATIC_TASKS_ALLOWED_IN_WORKSPACE, StorageScope.WORKSPACE, undefined);
46+
const isFolderAutomaticAllowed = this._configurationService.getValue(ALLOW_AUTOMATIC_TASKS) !== 'off';
4647
await this._workspaceTrustManagementService.workspaceTrustInitialized;
4748
const isWorkspaceTrusted = this._workspaceTrustManagementService.isWorkspaceTrusted();
4849
// Only run if allowed. Prompting for permission occurs when a user first tries to run a task.
@@ -128,30 +129,33 @@ export class RunAutomaticTasks extends Disposable implements IWorkbenchContribut
128129
}
129130

130131
public static async promptForPermission(taskService: ITaskService, storageService: IStorageService, notificationService: INotificationService, workspaceTrustManagementService: IWorkspaceTrustManagementService,
131-
openerService: IOpenerService, workspaceTaskResult: Map<string, IWorkspaceFolderTaskResult>) {
132+
openerService: IOpenerService, configurationService: IConfigurationService, workspaceTaskResult: Map<string, IWorkspaceFolderTaskResult>) {
132133
const isWorkspaceTrusted = workspaceTrustManagementService.isWorkspaceTrusted;
133134
if (!isWorkspaceTrusted) {
134135
return;
135136
}
136-
137-
const isFolderAutomaticAllowed = storageService.getBoolean(ARE_AUTOMATIC_TASKS_ALLOWED_IN_WORKSPACE, StorageScope.WORKSPACE, undefined);
138-
if (isFolderAutomaticAllowed !== undefined) {
137+
if (configurationService.getValue(ALLOW_AUTOMATIC_TASKS) === 'off') {
139138
return;
140139
}
141140

141+
const hasShownPromptForAutomaticTasks = storageService.getBoolean(HAS_PROMPTED_FOR_AUTOMATIC_TASKS, StorageScope.WORKSPACE, undefined);
142142
const { tasks, taskNames, locations } = RunAutomaticTasks._findAutoTasks(taskService, workspaceTaskResult);
143143
if (taskNames.length > 0) {
144-
// We have automatic tasks, prompt to allow.
145-
this._showPrompt(notificationService, storageService, taskService, openerService, taskNames, locations).then(allow => {
146-
if (allow) {
147-
RunAutomaticTasks._runTasks(taskService, tasks);
148-
}
149-
});
144+
if (configurationService.getValue(ALLOW_AUTOMATIC_TASKS) === 'on') {
145+
RunAutomaticTasks._runTasks(taskService, tasks);
146+
} else if (!hasShownPromptForAutomaticTasks) {
147+
// We have automatic tasks, prompt to allow.
148+
this._showPrompt(notificationService, storageService, openerService, configurationService, taskNames, locations).then(allow => {
149+
if (allow) {
150+
RunAutomaticTasks._runTasks(taskService, tasks);
151+
}
152+
});
153+
}
150154
}
151155
}
152156

153-
private static _showPrompt(notificationService: INotificationService, storageService: IStorageService, taskService: ITaskService,
154-
openerService: IOpenerService, taskNames: Array<string>, locations: Map<string, URI>): Promise<boolean> {
157+
private static _showPrompt(notificationService: INotificationService, storageService: IStorageService,
158+
openerService: IOpenerService, configurationService: IConfigurationService, taskNames: Array<string>, locations: Map<string, URI>): Promise<boolean> {
155159
return new Promise<boolean>(resolve => {
156160
notificationService.prompt(Severity.Info, nls.localize('tasks.run.allowAutomatic',
157161
"This workspace has tasks ({0}) defined ({1}) that run automatically when you open this workspace. Do you allow automatic tasks to run when you open this workspace?",
@@ -162,14 +166,15 @@ export class RunAutomaticTasks extends Disposable implements IWorkbenchContribut
162166
label: nls.localize('allow', "Allow and run"),
163167
run: () => {
164168
resolve(true);
165-
storageService.store(ARE_AUTOMATIC_TASKS_ALLOWED_IN_WORKSPACE, true, StorageScope.WORKSPACE, StorageTarget.MACHINE);
169+
configurationService.updateValue(ALLOW_AUTOMATIC_TASKS, true, ConfigurationTarget.WORKSPACE);
166170
}
167171
},
168172
{
169173
label: nls.localize('disallow', "Disallow"),
170174
run: () => {
171175
resolve(false);
172-
storageService.store(ARE_AUTOMATIC_TASKS_ALLOWED_IN_WORKSPACE, false, StorageScope.WORKSPACE, StorageTarget.MACHINE);
176+
configurationService.updateValue(ALLOW_AUTOMATIC_TASKS, false, ConfigurationTarget.WORKSPACE);
177+
173178
}
174179
},
175180
{
@@ -182,9 +187,9 @@ export class RunAutomaticTasks extends Disposable implements IWorkbenchContribut
182187
}
183188
}]
184189
);
190+
storageService.store(HAS_PROMPTED_FOR_AUTOMATIC_TASKS, true, StorageScope.WORKSPACE, StorageTarget.MACHINE);
185191
});
186192
}
187-
188193
}
189194

190195
export class ManageAutomaticTaskRunning extends Action2 {
@@ -202,14 +207,13 @@ export class ManageAutomaticTaskRunning extends Action2 {
202207

203208
public async run(accessor: ServicesAccessor): Promise<any> {
204209
const quickInputService = accessor.get(IQuickInputService);
205-
const storageService = accessor.get(IStorageService);
210+
const configurationService = accessor.get(IConfigurationService);
206211
const allowItem: IQuickPickItem = { label: nls.localize('workbench.action.tasks.allowAutomaticTasks', "Allow Automatic Tasks in Folder") };
207212
const disallowItem: IQuickPickItem = { label: nls.localize('workbench.action.tasks.disallowAutomaticTasks', "Disallow Automatic Tasks in Folder") };
208213
const value = await quickInputService.pick([allowItem, disallowItem], { canPickMany: false });
209214
if (!value) {
210215
return;
211216
}
212-
213-
storageService.store(ARE_AUTOMATIC_TASKS_ALLOWED_IN_WORKSPACE, value === allowItem, StorageScope.WORKSPACE, StorageTarget.MACHINE);
217+
configurationService.updateValue(ALLOW_AUTOMATIC_TASKS, value === allowItem, ConfigurationTarget.WORKSPACE);
214218
}
215219
}

src/vs/workbench/contrib/tasks/browser/task.contribution.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,18 @@ configurationRegistry.registerConfiguration({
496496
description: nls.localize('task.quickOpen.showAll', "Causes the Tasks: Run Task command to use the slower \"show all\" behavior instead of the faster two level picker where tasks are grouped by provider."),
497497
default: false
498498
},
499+
[TaskSettingId.AllowAutomaticTasks]: {
500+
type: 'string',
501+
enum: ['on', 'auto', 'off'],
502+
enumDescriptions: [
503+
nls.localize('ttask.allowAutomaticTasks.on', "Always"),
504+
nls.localize('task.allowAutomaticTasks.auto', "Prompt for permission for each folder"),
505+
nls.localize('task.allowAutomaticTasks.off', "Never"),
506+
],
507+
description: nls.localize('task.allowAutomaticTasks', "Enable automatic tasks in the folder."),
508+
default: 'auto',
509+
restricted: true
510+
},
499511
[TaskSettingId.ShowDecorations]: {
500512
type: 'boolean',
501513
description: nls.localize('task.showDecorations', "Shows decorations at points of interest in the terminal buffer such as the first problem found via a watch task. Note that this will only take effect for future tasks."),

src/vs/workbench/contrib/tasks/common/tasks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1199,7 +1199,8 @@ export const enum TaskSettingId {
11991199
QuickOpenHistory = 'task.quickOpen.history',
12001200
QuickOpenDetail = 'task.quickOpen.detail',
12011201
QuickOpenSkip = 'task.quickOpen.skip',
1202-
QuickOpenShowAll = 'task.quickOpen.showAll'
1202+
QuickOpenShowAll = 'task.quickOpen.showAll',
1203+
AllowAutomaticTasks = 'task.allowAutomaticTasks'
12031204
}
12041205

12051206
export const enum TasksSchemaProperties {

0 commit comments

Comments
 (0)