@@ -25,6 +25,7 @@ const HAS_PROMPTED_FOR_AUTOMATIC_TASKS = 'task.hasPromptedForAutomaticTasks';
25
25
const ALLOW_AUTOMATIC_TASKS = 'task.allowAutomaticTasks' ;
26
26
27
27
export class RunAutomaticTasks extends Disposable implements IWorkbenchContribution {
28
+ private _hasRunTasks : boolean = false ;
28
29
constructor (
29
30
@ITaskService private readonly _taskService : ITaskService ,
30
31
@IConfigurationService private readonly _configurationService : IConfigurationService ,
@@ -34,23 +35,33 @@ export class RunAutomaticTasks extends Disposable implements IWorkbenchContribut
34
35
@IOpenerService private readonly _openerService : IOpenerService ,
35
36
@INotificationService private readonly _notificationService : INotificationService ) {
36
37
super ( ) ;
37
- this . _tryRunTasks ( ) ;
38
+ if ( this . _workspaceTrustManagementService . isWorkspaceTrusted ( ) ) {
39
+ this . _tryRunTasks ( ) ;
40
+ }
41
+ this . _register ( this . _workspaceTrustManagementService . onDidChangeTrust ( async trusted => {
42
+ if ( trusted ) {
43
+ await this . _tryRunTasks ( ) ;
44
+ }
45
+ } ) ) ;
38
46
}
39
47
40
48
private async _tryRunTasks ( ) {
49
+ if ( this . _hasRunTasks || this . _configurationService . getValue ( ALLOW_AUTOMATIC_TASKS ) === 'off' ) {
50
+ return ;
51
+ }
52
+ this . _hasRunTasks = true ;
41
53
this . _logService . trace ( 'RunAutomaticTasks: Trying to run tasks.' ) ;
42
54
// Wait until we have task system info (the extension host and workspace folders are available).
43
55
if ( ! this . _taskService . hasTaskSystemInfo ) {
44
56
this . _logService . trace ( 'RunAutomaticTasks: Awaiting task system info.' ) ;
45
57
await Event . toPromise ( Event . once ( this . _taskService . onDidChangeTaskSystemInfo ) ) ;
46
58
}
47
-
48
59
const workspaceTasks = await this . _taskService . getWorkspaceTasks ( TaskRunSource . FolderOpen ) ;
49
60
this . _logService . trace ( `RunAutomaticTasks: Found ${ workspaceTasks . size } automatic tasks` ) ;
50
- await RunAutomaticTasks . runWithPermission ( this . _taskService , this . _storageService , this . _notificationService , this . _workspaceTrustManagementService , this . _openerService , this . _configurationService , workspaceTasks ) ;
61
+ await this . _runWithPermission ( this . _taskService , this . _storageService , this . _notificationService , this . _workspaceTrustManagementService , this . _openerService , this . _configurationService , workspaceTasks ) ;
51
62
}
52
63
53
- private static _runTasks ( taskService : ITaskService , tasks : Array < Task | Promise < Task | undefined > > ) {
64
+ private _runTasks ( taskService : ITaskService , tasks : Array < Task | Promise < Task | undefined > > ) {
54
65
tasks . forEach ( task => {
55
66
if ( task instanceof Promise ) {
56
67
task . then ( promiseResult => {
@@ -64,7 +75,7 @@ export class RunAutomaticTasks extends Disposable implements IWorkbenchContribut
64
75
} ) ;
65
76
}
66
77
67
- private static _getTaskSource ( source : TaskSource ) : URI | undefined {
78
+ private _getTaskSource ( source : TaskSource ) : URI | undefined {
68
79
const taskKind = TaskSourceKind . toConfigurationTarget ( source . kind ) ;
69
80
switch ( taskKind ) {
70
81
case ConfigurationTarget . WORKSPACE_FOLDER : {
@@ -77,7 +88,7 @@ export class RunAutomaticTasks extends Disposable implements IWorkbenchContribut
77
88
return undefined ;
78
89
}
79
90
80
- private static _findAutoTasks ( taskService : ITaskService , workspaceTaskResult : Map < string , IWorkspaceFolderTaskResult > ) : { tasks : Array < Task | Promise < Task | undefined > > ; taskNames : Array < string > ; locations : Map < string , URI > } {
91
+ private _findAutoTasks ( taskService : ITaskService , workspaceTaskResult : Map < string , IWorkspaceFolderTaskResult > ) : { tasks : Array < Task | Promise < Task | undefined > > ; taskNames : Array < string > ; locations : Map < string , URI > } {
81
92
const tasks = new Array < Task | Promise < Task | undefined > > ( ) ;
82
93
const taskNames = new Array < string > ( ) ;
83
94
const locations = new Map < string , URI > ( ) ;
@@ -89,7 +100,7 @@ export class RunAutomaticTasks extends Disposable implements IWorkbenchContribut
89
100
if ( task . runOptions . runOn === RunOnOptions . folderOpen ) {
90
101
tasks . push ( task ) ;
91
102
taskNames . push ( task . _label ) ;
92
- const location = RunAutomaticTasks . _getTaskSource ( task . _source ) ;
103
+ const location = this . _getTaskSource ( task . _source ) ;
93
104
if ( location ) {
94
105
locations . set ( location . fsPath , location ) ;
95
106
}
@@ -107,7 +118,7 @@ export class RunAutomaticTasks extends Disposable implements IWorkbenchContribut
107
118
} else {
108
119
taskNames . push ( configuredTask . configures . task ) ;
109
120
}
110
- const location = RunAutomaticTasks . _getTaskSource ( configuredTask . _source ) ;
121
+ const location = this . _getTaskSource ( configuredTask . _source ) ;
111
122
if ( location ) {
112
123
locations . set ( location . fsPath , location ) ;
113
124
}
@@ -119,35 +130,30 @@ export class RunAutomaticTasks extends Disposable implements IWorkbenchContribut
119
130
return { tasks, taskNames, locations } ;
120
131
}
121
132
122
- public static async runWithPermission ( taskService : ITaskService , storageService : IStorageService , notificationService : INotificationService , workspaceTrustManagementService : IWorkspaceTrustManagementService ,
133
+ private async _runWithPermission ( taskService : ITaskService , storageService : IStorageService , notificationService : INotificationService , workspaceTrustManagementService : IWorkspaceTrustManagementService ,
123
134
openerService : IOpenerService , configurationService : IConfigurationService , workspaceTaskResult : Map < string , IWorkspaceFolderTaskResult > ) {
124
- const isWorkspaceTrusted = workspaceTrustManagementService . isWorkspaceTrusted ;
125
- if ( ! isWorkspaceTrusted || configurationService . getValue ( ALLOW_AUTOMATIC_TASKS ) === 'off' ) {
126
- return ;
127
- }
128
135
129
136
const hasShownPromptForAutomaticTasks = storageService . getBoolean ( HAS_PROMPTED_FOR_AUTOMATIC_TASKS , StorageScope . WORKSPACE , false ) ;
130
- const { tasks, taskNames, locations } = RunAutomaticTasks . _findAutoTasks ( taskService , workspaceTaskResult ) ;
137
+ const { tasks, taskNames, locations } = this . _findAutoTasks ( taskService , workspaceTaskResult ) ;
131
138
132
139
if ( taskNames . length === 0 ) {
133
140
return ;
134
141
}
135
-
136
142
if ( configurationService . getValue ( ALLOW_AUTOMATIC_TASKS ) === 'on' ) {
137
- RunAutomaticTasks . _runTasks ( taskService , tasks ) ;
143
+ this . _runTasks ( taskService , tasks ) ;
138
144
} else if ( configurationService . getValue ( ALLOW_AUTOMATIC_TASKS ) === 'auto' && ! hasShownPromptForAutomaticTasks ) {
139
145
// by default, only prompt once per folder
140
146
// otherwise, this can be configured via the setting
141
147
this . _showPrompt ( notificationService , storageService , openerService , configurationService , taskNames , locations ) . then ( allow => {
142
148
if ( allow ) {
143
149
storageService . store ( HAS_PROMPTED_FOR_AUTOMATIC_TASKS , true , StorageScope . WORKSPACE , StorageTarget . USER ) ;
144
- RunAutomaticTasks . _runTasks ( taskService , tasks ) ;
150
+ this . _runTasks ( taskService , tasks ) ;
145
151
}
146
152
} ) ;
147
153
}
148
154
}
149
155
150
- private static _showPrompt ( notificationService : INotificationService , storageService : IStorageService ,
156
+ private _showPrompt ( notificationService : INotificationService , storageService : IStorageService ,
151
157
openerService : IOpenerService , configurationService : IConfigurationService , taskNames : Array < string > , locations : Map < string , URI > ) : Promise < boolean > {
152
158
return new Promise < boolean > ( resolve => {
153
159
notificationService . prompt ( Severity . Info , nls . localize ( 'tasks.run.allowAutomatic' ,
0 commit comments