Skip to content

Commit 05ec316

Browse files
authored
run auto tasks if trusted & on (microsoft#165570)
1 parent 7f3a7ed commit 05ec316

File tree

2 files changed

+14
-69
lines changed

2 files changed

+14
-69
lines changed

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

Lines changed: 11 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,15 @@ import { Disposable } from 'vs/base/common/lifecycle';
99
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
1010
import { ITaskService, IWorkspaceFolderTaskResult } from 'vs/workbench/contrib/tasks/common/taskService';
1111
import { RunOnOptions, Task, TaskRunSource, TaskSource, TaskSourceKind, TASKS_CATEGORY, WorkspaceFileTaskSource, IWorkspaceTaskSource } from 'vs/workbench/contrib/tasks/common/tasks';
12-
import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage';
13-
import { INotificationService, Severity } from 'vs/platform/notification/common/notification';
1412
import { IQuickPickItem, IQuickInputService } from 'vs/platform/quickinput/common/quickInput';
1513
import { Action2 } from 'vs/platform/actions/common/actions';
1614
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
1715
import { IWorkspaceTrustManagementService } from 'vs/platform/workspace/common/workspaceTrust';
1816
import { ConfigurationTarget, IConfigurationService } from 'vs/platform/configuration/common/configuration';
19-
import { IOpenerService } from 'vs/platform/opener/common/opener';
2017
import { URI } from 'vs/base/common/uri';
2118
import { Event } from 'vs/base/common/event';
2219
import { ILogService } from 'vs/platform/log/common/log';
2320

24-
const HAS_PROMPTED_FOR_AUTOMATIC_TASKS = 'task.hasPromptedForAutomaticTasks';
2521
const ALLOW_AUTOMATIC_TASKS = 'task.allowAutomaticTasks';
2622

2723
export class RunAutomaticTasks extends Disposable implements IWorkbenchContribution {
@@ -30,10 +26,7 @@ export class RunAutomaticTasks extends Disposable implements IWorkbenchContribut
3026
@ITaskService private readonly _taskService: ITaskService,
3127
@IConfigurationService private readonly _configurationService: IConfigurationService,
3228
@IWorkspaceTrustManagementService private readonly _workspaceTrustManagementService: IWorkspaceTrustManagementService,
33-
@ILogService private readonly _logService: ILogService,
34-
@IStorageService private readonly _storageService: IStorageService,
35-
@IOpenerService private readonly _openerService: IOpenerService,
36-
@INotificationService private readonly _notificationService: INotificationService) {
29+
@ILogService private readonly _logService: ILogService) {
3730
super();
3831
if (this._workspaceTrustManagementService.isWorkspaceTrusted()) {
3932
this._tryRunTasks();
@@ -58,7 +51,7 @@ export class RunAutomaticTasks extends Disposable implements IWorkbenchContribut
5851
}
5952
const workspaceTasks = await this._taskService.getWorkspaceTasks(TaskRunSource.FolderOpen);
6053
this._logService.trace(`RunAutomaticTasks: Found ${workspaceTasks.size} automatic tasks`);
61-
await this._runWithPermission(this._taskService, this._storageService, this._notificationService, this._workspaceTrustManagementService, this._openerService, this._configurationService, workspaceTasks);
54+
await this._runWithPermission(this._taskService, this._configurationService, workspaceTasks);
6255
}
6356

6457
private _runTasks(taskService: ITaskService, tasks: Array<Task | Promise<Task | undefined>>) {
@@ -130,71 +123,24 @@ export class RunAutomaticTasks extends Disposable implements IWorkbenchContribut
130123
return { tasks, taskNames, locations };
131124
}
132125

133-
private async _runWithPermission(taskService: ITaskService, storageService: IStorageService, notificationService: INotificationService, workspaceTrustManagementService: IWorkspaceTrustManagementService,
134-
openerService: IOpenerService, configurationService: IConfigurationService, workspaceTaskResult: Map<string, IWorkspaceFolderTaskResult>) {
126+
private async _runWithPermission(taskService: ITaskService, configurationService: IConfigurationService, workspaceTaskResult: Map<string, IWorkspaceFolderTaskResult>) {
135127

136-
const hasShownPromptForAutomaticTasks = storageService.getBoolean(HAS_PROMPTED_FOR_AUTOMATIC_TASKS, StorageScope.WORKSPACE, false);
137-
const { tasks, taskNames, locations } = this._findAutoTasks(taskService, workspaceTaskResult);
128+
const { tasks, taskNames } = this._findAutoTasks(taskService, workspaceTaskResult);
138129

139130
if (taskNames.length === 0) {
140131
return;
141132
}
142-
if (configurationService.getValue(ALLOW_AUTOMATIC_TASKS) === 'on') {
143-
this._runTasks(taskService, tasks);
144-
} else if (configurationService.getValue(ALLOW_AUTOMATIC_TASKS) === 'auto' && !hasShownPromptForAutomaticTasks) {
145-
// by default, only prompt once per folder
146-
// otherwise, this can be configured via the setting
147-
this._showPrompt(notificationService, storageService, openerService, configurationService, taskNames, locations).then(allow => {
148-
if (allow) {
149-
storageService.store(HAS_PROMPTED_FOR_AUTOMATIC_TASKS, true, StorageScope.WORKSPACE, StorageTarget.USER);
150-
this._runTasks(taskService, tasks);
151-
}
152-
});
133+
if (configurationService.getValue(ALLOW_AUTOMATIC_TASKS) === 'off') {
134+
return;
153135
}
154-
}
155-
156-
private _showPrompt(notificationService: INotificationService, storageService: IStorageService,
157-
openerService: IOpenerService, configurationService: IConfigurationService, taskNames: Array<string>, locations: Map<string, URI>): Promise<boolean> {
158-
return new Promise<boolean>(resolve => {
159-
notificationService.prompt(Severity.Info, nls.localize('tasks.run.allowAutomatic',
160-
"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?",
161-
taskNames.join(', '),
162-
Array.from(locations.keys()).join(', ')
163-
),
164-
[{
165-
label: nls.localize('allow', "Allow and run"),
166-
run: () => {
167-
resolve(true);
168-
configurationService.updateValue(ALLOW_AUTOMATIC_TASKS, 'on', ConfigurationTarget.WORKSPACE);
169-
}
170-
},
171-
{
172-
label: nls.localize('disallow', "Disallow"),
173-
run: () => {
174-
resolve(false);
175-
configurationService.updateValue(ALLOW_AUTOMATIC_TASKS, 'off', ConfigurationTarget.WORKSPACE);
176-
177-
}
178-
},
179-
{
180-
label: locations.size === 1 ? nls.localize('openTask', "Open file") : nls.localize('openTasks', "Open files"),
181-
run: async () => {
182-
for (const location of locations) {
183-
await openerService.open(location[1]);
184-
}
185-
resolve(false);
186-
}
187-
}]
188-
);
189-
storageService.store(HAS_PROMPTED_FOR_AUTOMATIC_TASKS, true, StorageScope.WORKSPACE, StorageTarget.MACHINE);
190-
});
136+
this._runTasks(taskService, tasks);
191137
}
192138
}
193139

194140
export class ManageAutomaticTaskRunning extends Action2 {
195141

196142
public static readonly ID = 'workbench.action.tasks.manageAutomaticRunning';
197-
public static readonly LABEL = nls.localize('workbench.action.tasks.manageAutomaticRunning', "Manage Automatic Tasks in Folder");
143+
public static readonly LABEL = nls.localize('workbench.action.tasks.manageAutomaticRunning', "Manage Automatic Tasks");
198144

199145
constructor() {
200146
super({
@@ -207,12 +153,12 @@ export class ManageAutomaticTaskRunning extends Action2 {
207153
public async run(accessor: ServicesAccessor): Promise<any> {
208154
const quickInputService = accessor.get(IQuickInputService);
209155
const configurationService = accessor.get(IConfigurationService);
210-
const allowItem: IQuickPickItem = { label: nls.localize('workbench.action.tasks.allowAutomaticTasks', "Allow Automatic Tasks in Folder") };
211-
const disallowItem: IQuickPickItem = { label: nls.localize('workbench.action.tasks.disallowAutomaticTasks', "Disallow Automatic Tasks in Folder") };
156+
const allowItem: IQuickPickItem = { label: nls.localize('workbench.action.tasks.allowAutomaticTasks', "Allow Automatic Tasks") };
157+
const disallowItem: IQuickPickItem = { label: nls.localize('workbench.action.tasks.disallowAutomaticTasks', "Disallow Automatic Tasks") };
212158
const value = await quickInputService.pick([allowItem, disallowItem], { canPickMany: false });
213159
if (!value) {
214160
return;
215161
}
216-
configurationService.updateValue(ALLOW_AUTOMATIC_TASKS, value === allowItem ? 'on' : 'off', ConfigurationTarget.WORKSPACE);
162+
configurationService.updateValue(ALLOW_AUTOMATIC_TASKS, value === allowItem ? 'on' : 'off', ConfigurationTarget.USER);
217163
}
218164
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -496,14 +496,13 @@ configurationRegistry.registerConfiguration({
496496
},
497497
[TaskSettingId.AllowAutomaticTasks]: {
498498
type: 'string',
499-
enum: ['on', 'auto', 'off'],
499+
enum: ['on', 'off'],
500500
enumDescriptions: [
501501
nls.localize('task.allowAutomaticTasks.on', "Always"),
502-
nls.localize('task.allowAutomaticTasks.auto', "Prompt for permission for each folder"),
503502
nls.localize('task.allowAutomaticTasks.off', "Never"),
504503
],
505-
description: nls.localize('task.allowAutomaticTasks', "Enable automatic tasks in the folder - note that tasks won't run in an untrusted workspace."),
506-
default: 'auto',
504+
description: nls.localize('task.allowAutomaticTasks', "Enable automatic tasks - note that tasks won't run in an untrusted workspace."),
505+
default: 'on',
507506
restricted: true
508507
},
509508
[TaskSettingId.ShowDecorations]: {

0 commit comments

Comments
 (0)