Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run auto tasks if trusted & on #165570

Merged
merged 4 commits into from Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 11 additions & 65 deletions src/vs/workbench/contrib/tasks/browser/runAutomaticTasks.ts
Expand Up @@ -9,19 +9,15 @@ import { Disposable } from 'vs/base/common/lifecycle';
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
import { ITaskService, IWorkspaceFolderTaskResult } from 'vs/workbench/contrib/tasks/common/taskService';
import { RunOnOptions, Task, TaskRunSource, TaskSource, TaskSourceKind, TASKS_CATEGORY, WorkspaceFileTaskSource, IWorkspaceTaskSource } from 'vs/workbench/contrib/tasks/common/tasks';
import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage';
import { INotificationService, Severity } from 'vs/platform/notification/common/notification';
import { IQuickPickItem, IQuickInputService } from 'vs/platform/quickinput/common/quickInput';
import { Action2 } from 'vs/platform/actions/common/actions';
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { IWorkspaceTrustManagementService } from 'vs/platform/workspace/common/workspaceTrust';
import { ConfigurationTarget, IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IOpenerService } from 'vs/platform/opener/common/opener';
import { URI } from 'vs/base/common/uri';
import { Event } from 'vs/base/common/event';
import { ILogService } from 'vs/platform/log/common/log';

const HAS_PROMPTED_FOR_AUTOMATIC_TASKS = 'task.hasPromptedForAutomaticTasks';
const ALLOW_AUTOMATIC_TASKS = 'task.allowAutomaticTasks';

export class RunAutomaticTasks extends Disposable implements IWorkbenchContribution {
Expand All @@ -30,10 +26,7 @@ export class RunAutomaticTasks extends Disposable implements IWorkbenchContribut
@ITaskService private readonly _taskService: ITaskService,
@IConfigurationService private readonly _configurationService: IConfigurationService,
@IWorkspaceTrustManagementService private readonly _workspaceTrustManagementService: IWorkspaceTrustManagementService,
@ILogService private readonly _logService: ILogService,
@IStorageService private readonly _storageService: IStorageService,
@IOpenerService private readonly _openerService: IOpenerService,
@INotificationService private readonly _notificationService: INotificationService) {
@ILogService private readonly _logService: ILogService) {
super();
if (this._workspaceTrustManagementService.isWorkspaceTrusted()) {
this._tryRunTasks();
Expand All @@ -58,7 +51,7 @@ export class RunAutomaticTasks extends Disposable implements IWorkbenchContribut
}
const workspaceTasks = await this._taskService.getWorkspaceTasks(TaskRunSource.FolderOpen);
this._logService.trace(`RunAutomaticTasks: Found ${workspaceTasks.size} automatic tasks`);
await this._runWithPermission(this._taskService, this._storageService, this._notificationService, this._workspaceTrustManagementService, this._openerService, this._configurationService, workspaceTasks);
await this._runWithPermission(this._taskService, this._configurationService, workspaceTasks);
}

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

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

const hasShownPromptForAutomaticTasks = storageService.getBoolean(HAS_PROMPTED_FOR_AUTOMATIC_TASKS, StorageScope.WORKSPACE, false);
const { tasks, taskNames, locations } = this._findAutoTasks(taskService, workspaceTaskResult);
const { tasks, taskNames } = this._findAutoTasks(taskService, workspaceTaskResult);

if (taskNames.length === 0) {
return;
}
if (configurationService.getValue(ALLOW_AUTOMATIC_TASKS) === 'on') {
this._runTasks(taskService, tasks);
} else if (configurationService.getValue(ALLOW_AUTOMATIC_TASKS) === 'auto' && !hasShownPromptForAutomaticTasks) {
// by default, only prompt once per folder
// otherwise, this can be configured via the setting
this._showPrompt(notificationService, storageService, openerService, configurationService, taskNames, locations).then(allow => {
if (allow) {
storageService.store(HAS_PROMPTED_FOR_AUTOMATIC_TASKS, true, StorageScope.WORKSPACE, StorageTarget.USER);
this._runTasks(taskService, tasks);
}
});
if (configurationService.getValue(ALLOW_AUTOMATIC_TASKS) === 'off') {
return;
}
}

private _showPrompt(notificationService: INotificationService, storageService: IStorageService,
openerService: IOpenerService, configurationService: IConfigurationService, taskNames: Array<string>, locations: Map<string, URI>): Promise<boolean> {
return new Promise<boolean>(resolve => {
notificationService.prompt(Severity.Info, nls.localize('tasks.run.allowAutomatic',
"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?",
taskNames.join(', '),
Array.from(locations.keys()).join(', ')
),
[{
label: nls.localize('allow', "Allow and run"),
run: () => {
resolve(true);
configurationService.updateValue(ALLOW_AUTOMATIC_TASKS, 'on', ConfigurationTarget.WORKSPACE);
}
},
{
label: nls.localize('disallow', "Disallow"),
run: () => {
resolve(false);
configurationService.updateValue(ALLOW_AUTOMATIC_TASKS, 'off', ConfigurationTarget.WORKSPACE);

}
},
{
label: locations.size === 1 ? nls.localize('openTask', "Open file") : nls.localize('openTasks', "Open files"),
run: async () => {
for (const location of locations) {
await openerService.open(location[1]);
}
resolve(false);
}
}]
);
storageService.store(HAS_PROMPTED_FOR_AUTOMATIC_TASKS, true, StorageScope.WORKSPACE, StorageTarget.MACHINE);
});
this._runTasks(taskService, tasks);
}
}

export class ManageAutomaticTaskRunning extends Action2 {

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

constructor() {
super({
Expand All @@ -207,12 +153,12 @@ export class ManageAutomaticTaskRunning extends Action2 {
public async run(accessor: ServicesAccessor): Promise<any> {
const quickInputService = accessor.get(IQuickInputService);
const configurationService = accessor.get(IConfigurationService);
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 allowItem: IQuickPickItem = { label: nls.localize('workbench.action.tasks.allowAutomaticTasks', "Allow Automatic Tasks") };
const disallowItem: IQuickPickItem = { label: nls.localize('workbench.action.tasks.disallowAutomaticTasks', "Disallow Automatic Tasks") };
const value = await quickInputService.pick([allowItem, disallowItem], { canPickMany: false });
if (!value) {
return;
}
configurationService.updateValue(ALLOW_AUTOMATIC_TASKS, value === allowItem ? 'on' : 'off', ConfigurationTarget.WORKSPACE);
configurationService.updateValue(ALLOW_AUTOMATIC_TASKS, value === allowItem ? 'on' : 'off', ConfigurationTarget.USER);
}
}
7 changes: 3 additions & 4 deletions src/vs/workbench/contrib/tasks/browser/task.contribution.ts
Expand Up @@ -496,14 +496,13 @@ configurationRegistry.registerConfiguration({
},
[TaskSettingId.AllowAutomaticTasks]: {
type: 'string',
enum: ['on', 'auto', 'off'],
enum: ['on', 'off'],
enumDescriptions: [
nls.localize('task.allowAutomaticTasks.on', "Always"),
nls.localize('task.allowAutomaticTasks.auto', "Prompt for permission for each folder"),
nls.localize('task.allowAutomaticTasks.off', "Never"),
],
description: nls.localize('task.allowAutomaticTasks', "Enable automatic tasks in the folder - note that tasks won't run in an untrusted workspace."),
default: 'auto',
description: nls.localize('task.allowAutomaticTasks', "Enable automatic tasks - note that tasks won't run in an untrusted workspace."),
default: 'on',
restricted: true
},
[TaskSettingId.ShowDecorations]: {
Expand Down