-
-
Notifications
You must be signed in to change notification settings - Fork 210
/
Copy pathtasks.js
335 lines (302 loc) · 9.88 KB
/
tasks.js
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
* Copyright (c) 2017-present PlatformIO <contact@platformio.org>
* All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
import { IS_WINDOWS, STATUS_BAR_PRIORITY_START } from '../constants';
import { disposeSubscriptions, listCoreSerialPorts } from '../utils';
import { getProjectItemState, updateProjectItemState } from './helpers';
import ProjectTasksTreeProvider from './task-tree';
import { extension } from '../main';
import path from 'path';
import vscode from 'vscode';
export default class ProjectTaskManager {
static PROVIDER_TYPE = 'PlatformIO';
static TASKS_VIEW_ID = 'platformio-ide.projectTasks';
static AUTO_REFRESH_DELAY = 500; // 0.5 sec
constructor(projectDir, projectObserver) {
this.projectDir = projectDir;
this.projectObserver = projectObserver;
this.subscriptions = [];
this._sid = Math.random();
this._multienvTaskExplorer = false;
this._refreshTimeout = undefined;
this._startedTask = undefined;
this._tasksToRestore = [];
this._sbPortSwitcher = undefined;
this._customPort = getProjectItemState(projectDir, 'customPort');
this.refresh();
}
dispose() {
disposeSubscriptions(this.subscriptions);
}
toggleMultiEnvExplorer() {
this._multienvTaskExplorer = !this._multienvTaskExplorer;
this.refresh({ force: true });
}
requestRefresh() {
if (this._refreshTimeout) {
clearTimeout(this._refreshTimeout);
}
this._refreshTimeout = setTimeout(
this.refresh.bind(this),
ProjectTaskManager.AUTO_REFRESH_DELAY,
);
}
async refresh({ force = false } = {}) {
this.dispose();
if (force) {
this.projectObserver.resetCache();
this._sid = Math.random();
}
const projectEnvs = (await this.projectObserver.getConfig()).envs();
const projectTasks = [...(await this.projectObserver.getDefaultTasks())];
for (const env of projectEnvs) {
projectTasks.push(...((await this.projectObserver.getLoadedEnvTasks(env)) || []));
}
const taskViewer = vscode.window.createTreeView(ProjectTaskManager.TASKS_VIEW_ID, {
treeDataProvider: new ProjectTasksTreeProvider(
this._sid,
projectEnvs,
projectTasks,
this.projectObserver.getSelectedEnv(),
this._multienvTaskExplorer,
),
showCollapseAll: true,
});
this.subscriptions.push(
taskViewer,
// pre-fetch expanded env tasks
taskViewer.onDidExpandElement(async ({ element }) => {
if (element.env) {
await this.onDidRequestEnvTasks(element.env);
}
}),
// register VSCode Task Provider
vscode.tasks.registerTaskProvider(ProjectTaskManager.PROVIDER_TYPE, {
provideTasks: () => projectTasks.map((task) => this.toVSCodeTask(task)),
resolveTask: () => {
return undefined;
},
}),
vscode.tasks.onDidEndTaskProcess((event) => this.onDidEndTaskProcess(event)),
);
this.registerTaskBasedCommands(projectTasks);
this.registerPortSwitcher();
vscode.commands.executeCommand('setContext', 'pioProjectTasksReady', true);
vscode.commands.executeCommand(
'setContext',
'pioMultiEnvProject',
projectEnvs.length > 1,
);
}
async onDidRequestEnvTasks(name) {
if (await this.projectObserver.getLoadedEnvTasks(name)) {
return;
}
await this.projectObserver.loadEnvTasks(name);
return this.requestRefresh();
}
toVSCodeTask(projectTask) {
const envClone = Object.assign({}, process.env);
if (process.env.PLATFORMIO_PATH) {
envClone.PATH = process.env.PLATFORMIO_PATH;
envClone.Path = process.env.PLATFORMIO_PATH;
}
const vscodeTask = new vscode.Task(
{
type: ProjectTaskManager.PROVIDER_TYPE,
task: projectTask.id,
},
vscode.workspace.getWorkspaceFolder(vscode.Uri.file(this.projectDir)),
projectTask.id,
ProjectTaskManager.PROVIDER_TYPE,
new vscode.ProcessExecution(
IS_WINDOWS ? 'platformio.exe' : 'platformio',
projectTask.getCoreArgs({ port: this._customPort }),
{
cwd: this.projectDir,
env: envClone,
},
),
'$platformio',
);
vscodeTask.presentationOptions = {
panel: vscode.TaskPanelKind.Dedicated,
};
if (projectTask.isBuild()) {
vscodeTask.group = vscode.TaskGroup.Build;
} else if (projectTask.isClean()) {
vscodeTask.group = vscode.TaskGroup.Clean;
} else if (projectTask.isTest()) {
vscodeTask.group = vscode.TaskGroup.Test;
}
return vscodeTask;
}
runTask(task) {
this._autoCloseSerialMonitor(task);
// use string-based task defination for Win 7 // issue #3481
vscode.commands.executeCommand(
'workbench.action.tasks.runTask',
`${ProjectTaskManager.PROVIDER_TYPE}: ${task.id}`,
);
}
async _autoCloseSerialMonitor(startedTask) {
this._startedTask = startedTask;
this._tasksToRestore = [];
const closeMonitorConds = [
extension.getConfiguration('autoCloseSerialMonitor'),
['upload', 'test'].some((arg) =>
this.getTaskArgs(this._startedTask).includes(arg),
),
];
if (!closeMonitorConds.every((value) => value)) {
return;
}
// skip "native" dev-platform
// const platform = (await this.projectObserver.getConfig()).getEnvPlatform(
// await this.projectObserver.revealActiveEnvironment()
// );
// if (platform === 'native') {
// return;
// }
vscode.tasks.taskExecutions.forEach((event) => {
const isCurrentTask = this.areTasksEqual(this._startedTask, event.task);
const skipConds = [
// skip non-PlatformIO task
event.task.definition.type !== ProjectTaskManager.PROVIDER_TYPE,
!this.getTaskArgs(event.task).includes('monitor'),
this.isMonitorAndUploadTask(event.task) && !isCurrentTask,
];
if (skipConds.some((value) => value)) {
return;
}
// do not restart the same tasks
if (!isCurrentTask) {
this._tasksToRestore.push(event.task);
}
event.terminate();
});
}
onDidEndTaskProcess(event) {
const skipConds = [
!this._startedTask,
!this.areTasksEqual(this._startedTask, event.execution.task),
event.exitCode !== 0,
!this._tasksToRestore.length,
];
if (skipConds.some((value) => value)) {
return;
}
this._startedTask = undefined;
setTimeout(
() => {
while (this._tasksToRestore.length) {
vscode.tasks.executeTask(this._tasksToRestore.pop());
}
},
parseInt(extension.getConfiguration('reopenSerialMonitorDelay')),
);
}
getTaskArgs(task) {
return task.args || task.execution.args;
}
isMonitorAndUploadTask(task) {
const args = this.getTaskArgs(task);
return ['--target', 'upload', 'monitor'].every((arg) => args.includes(arg));
}
areTasksEqual(task1, task2) {
if (!task1 || !task2) {
return task1 === task2;
}
const args1 = this.getTaskArgs(task1);
const args2 = this.getTaskArgs(task2);
return (
args1.length === args2.length &&
args1.every((value, index) => value === args2[index])
);
}
registerTaskBasedCommands(tasks) {
const _runTask = (name) => {
const candidates = tasks.filter(
(task) =>
task.name === name && task.coreEnv === this.projectObserver.getSelectedEnv(),
);
this.runTask(candidates[0]);
};
this.subscriptions.push(
vscode.commands.registerCommand('platformio-ide.build', () => _runTask('Build')),
vscode.commands.registerCommand('platformio-ide.upload', () =>
_runTask('Upload'),
),
vscode.commands.registerCommand('platformio-ide.uploadAndMonitor', () =>
_runTask('Upload and Monitor'),
),
vscode.commands.registerCommand('platformio-ide.clean', () => _runTask('Clean')),
vscode.commands.registerCommand('platformio-ide.test', () => _runTask('Test')),
vscode.commands.registerCommand('platformio-ide.serialMonitor', () =>
_runTask('Monitor'),
),
);
}
registerPortSwitcher() {
this._sbPortSwitcher = vscode.window.createStatusBarItem(
'pio-port-switcher',
vscode.StatusBarAlignment.Left,
STATUS_BAR_PRIORITY_START,
);
this._sbPortSwitcher.name = 'PlatformIO: Port Switcher';
this._sbPortSwitcher.tooltip = 'Set upload/monitor/test port';
this._sbPortSwitcher.command = 'platformio-ide.setProjectPort';
this.switchPort(this._customPort);
this.subscriptions.push(
this._sbPortSwitcher,
vscode.commands.registerCommand('platformio-ide.setProjectPort', () =>
this.pickProjectPort(),
),
);
}
async pickProjectPort() {
const serialPorts = await listCoreSerialPorts();
const pickedItem = await vscode.window.showQuickPick(
[
{ label: 'Auto' },
...serialPorts.map((port) => ({
label: port.port,
description: [port.description, port.hwid]
.filter((value) => !!value)
.join(' | '),
})),
{ label: 'Custom...' },
],
{
matchOnDescription: true,
},
);
if (!pickedItem) {
return;
}
if (pickedItem.label === 'Custom...') {
const value = await vscode.window.showInputBox({
title: 'Enter custom upload/monitor/test port',
placeHolder: 'Examples: COM3, /dev/ttyUSB*, 192.168.0.13, /media/disk',
});
if (!value) {
return;
}
this.switchPort(value.trim());
} else {
this.switchPort(pickedItem.label !== 'Auto' ? pickedItem.label : undefined);
}
}
switchPort(port = undefined) {
updateProjectItemState(this.projectDir, 'customPort', port);
this._customPort = port;
this._sbPortSwitcher.text = `$(plug) ${
this._customPort ? path.basename(this._customPort) : 'Auto'
}`;
this._sbPortSwitcher.show();
}
}