-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathextension.js
407 lines (370 loc) · 14.5 KB
/
extension.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
const childProcess = require('child_process');
const fs = require('fs');
const path = require('path');
const vscode = require('vscode');
let mosPort = ''; // Selected port
let mosBoard = ''; // Selected board
let mosProcess = undefined; // Currently running mos command
let deviceFiles = []; // Device file list
let numPortWaiters = 0; // Number of commands waiting for port
const uartOut = vscode.window.createOutputChannel('Mongoose OS');
const cmdOut = uartOut;
const mgosStatusBarIcon = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 50);
const boards = {
'STM32 B-L475E-IOT01A': '--platform stm32 --build-var BOARD=B-L475E-IOT01A',
'STM32 DISCO-F746NG': '--platform stm32 --build-var BOARD=DISCO-F746NG',
'STM32 NUCLEO-F746ZG': '--platform stm32 --build-var BOARD=NUCLEO-F746ZG',
'TI CC3220': '--platform cc3220',
'TI CC3200': '--platform cc3200',
'ESP32': '--platform esp32',
'ESP32 Olimex EVB': '--platform esp32 --build-var BOARD=ESP32-EVB',
'ESP8266': '--platform esp8266',
'ESP8266, flash 1M': '--platform esp8266 --build-var BOARD=esp8266-1M',
'ESP8266, flash 2M': '--platform esp8266 --build-var BOARD=esp8266-2M',
};
const killMosCommandAndWait = () => new Promise((resolve, reject) => {
if (mosProcess) {
numPortWaiters++;
mosProcess.kill(9); // Kill and then wait
const tid = setInterval(() => {
if (mosProcess) return;
clearInterval(tid);
numPortWaiters--;
resolve();
}, 300);
} else {
resolve(); // No mos process is running
}
});
const runMosCommand = (args, out, nomarks) => new Promise((resolve, reject) => {
return killMosCommandAndWait().then(() => {
let fullArgs = args;
if (mosPort) fullArgs = fullArgs.concat(['--port', mosPort]);
if (args[0] === 'build' && boards[mosBoard]) {
fullArgs = fullArgs.concat(boards[mosBoard].split(/\s+/));
}
let extra = vscode.workspace.getConfiguration('mos').get('flags');
if (extra) fullArgs = fullArgs.concat(extra.split(/\s+/));
const uri = vscode.workspace.workspaceFolders[0].uri;
const cwd = vscode.Uri.parse(uri).fsPath;
// console.log('Running', fullArgs.join(' '));
mosProcess = childProcess.spawn('mos', fullArgs, { cwd });
if (!nomarks) out.append(`\n--[command: mos ${fullArgs.join(' ')}]\n`);
mosProcess.stdout.on('data', b => out.append(b.toString()));
mosProcess.stderr.on('data', b => out.append(b.toString()));
mosProcess.on('error', (err) => reject(err));
mosProcess.on('exit', (code) => {
if (!nomarks) out.append('--[command complete]');
if (code) {
reject(`MGOS: Command "mos ${args[0]} ..." failed`);
} else {
resolve();
}
mosProcess = undefined;
});
});
});
// When idle, run `mos console` command if the port is chosen
setInterval(() => {
if (!mosPort || mosProcess || numPortWaiters) return;
runMosCommand(['console'], uartOut).catch(() => { });
}, 1000);
const runMosCommandGetOutput = args => {
const obj = { out: [], append: x => obj.out.push(x) };
return runMosCommand(args, obj, true).then(() => obj.out.join(''));
};
const mosView = {
setupStatusBarIcon: () => {
mgosStatusBarIcon.command = "mos.showPanel"
mgosStatusBarIcon.text = " $(circuit-board) "
mgosStatusBarIcon.tooltip = "Mongoose OS Output Panel"
mgosStatusBarIcon.show()
},
_onDidChangeTreeData: new vscode.EventEmitter(),
getChildren: el => {
let rootItems = [
{
label: 'Build locally',
command: { command: 'mos.buildLocally' },
iconPath: {
light: path.join(__filename, '..', 'resources', 'icons', 'light', 'build-local.svg'),
dark: path.join(__filename, '..', 'resources', 'icons', 'dark', 'build-local.svg')
}
},
{
label: 'Build',
command: { command: 'mos.build' },
iconPath: {
light: path.join(__filename, '..', 'resources', 'icons', 'light', 'build-online.svg'),
dark: path.join(__filename, '..', 'resources', 'icons', 'dark', 'build-online.svg')
}
},
{
label: 'Flash',
command: { command: 'mos.flash' },
iconPath: {
light: path.join(__filename, '..', 'resources', 'icons', 'light', 'flash.svg'),
dark: path.join(__filename, '..', 'resources', 'icons', 'dark', 'flash.svg')
}
},
{
label: 'Console',
command: { command: 'mos.console' },
iconPath: {
light: path.join(__filename, '..', 'resources', 'icons', 'light', 'console.svg'),
dark: path.join(__filename, '..', 'resources', 'icons', 'dark', 'console.svg')
}
},
{
label: 'Device info',
command: { command: 'mos.sysInfo' },
iconPath: {
light: path.join(__filename, '..', 'resources', 'icons', 'light', 'device-info.svg'),
dark: path.join(__filename, '..', 'resources', 'icons', 'dark', 'device-info.svg')
}
},
{
label: 'RPC List',
command: { command: 'mos.rpcList' },
iconPath: {
light: path.join(__filename, '..', 'resources', 'icons', 'light', 'rpc-list.svg'),
dark: path.join(__filename, '..', 'resources', 'icons', 'dark', 'rpc-list.svg')
}
},
{
label: 'Run command...',
command: { command: 'mos.runCommand' },
iconPath: {
light: path.join(__filename, '..', 'resources', 'icons', 'light', 'run-command.svg'),
dark: path.join(__filename, '..', 'resources', 'icons', 'dark', 'run-command.svg')
}
},
{
label: 'Reboot device',
command: { command: 'mos.rebootDevice' },
iconPath: {
light: path.join(__filename, '..', 'resources', 'icons', 'light', 'reboot.svg'),
dark: path.join(__filename, '..', 'resources', 'icons', 'dark', 'reboot.svg')
}
},
{
label: `Port: ${mosPort || '<click to set>'}`,
command: { command: 'mos.setPort' }
},
{
label: `Board: ${mosBoard || '<click to set>'}`,
command: { command: 'mos.setBoard' }
},
];
if (mosPort) {
rootItems.push({
label: 'Device configuration',
command: { command: 'mos.openConfig' },
iconPath: vscode.ThemeIcon.File,
});
rootItems.push({
label: 'Device files',
collapsibleState: vscode.TreeItemCollapsibleState.Expanded,
iconPath: vscode.ThemeIcon.Folder,
});
}
if (!el) return rootItems;
return deviceFiles.map(function (name) {
return {
label: name, iconPath: vscode.ThemeIcon.File,
command: { command: 'mos.openFile', arguments: [name] },
}
});
},
getTreeItem: item => item,
};
mosView.onDidChangeTreeData = mosView._onDidChangeTreeData.event;
const refreshFS = () => {
return runMosCommandGetOutput(['ls'])
.then(output => {
deviceFiles = output.replace(/^\s+|\s+$/g, '').split(/\s+/);
mosView._onDidChangeTreeData.fire();
})
.catch(err => vscode.window.showErrorMessage(err));
};
module.exports = {
activate: function (context) {
console.log('MOS IDE activated.');
mosView.setupStatusBarIcon();
const dir = context.storagePath;
if (!fs.existsSync(dir)) fs.mkdirSync(dir);
mosPort = vscode.workspace.getConfiguration('mos').get('port');
mosBoard = vscode.workspace.getConfiguration('mos').get('board');
if (mosPort) refreshFS();
runMosCommandGetOutput(['ports']).catch(
() => vscode.window.showErrorMessage(
'Too old mos tool: "mos ports" failed. Run "mos update latest"'));
vscode.window.createTreeView('mos', { treeDataProvider: mosView });
vscode.commands.registerCommand('mos.setPort', () => {
childProcess.exec('mos ports', (error, stdout, stderr) => {
const items = (stdout || '').replace(/\s+$/, '').split(/\s+/);
vscode.window.showQuickPick(items).then(v => {
mosPort = v || '';
vscode.workspace.getConfiguration('mos').update('port', mosPort)
mosView._onDidChangeTreeData.fire();
if (mosProcess) mosProcess.kill();
if (v) {
refreshFS();
} else {
deviceFiles = [];
mosView._onDidChangeTreeData.fire();
}
});
});
});
vscode.commands.registerCommand('mos.setBoard', () => {
vscode.window.showQuickPick(Object.keys(boards)).then(v => {
mosBoard = v;
vscode.workspace.getConfiguration('mos').update('board', v)
mosView._onDidChangeTreeData.fire();
});
});
vscode.commands.registerCommand('mos.openFile', (name) => {
runMosCommandGetOutput(['get', name]).then(output => {
const local = path.resolve(dir, name);
fs.writeFileSync(local, output);
vscode.window.showTextDocument(vscode.Uri.file(local));
}, err => console.log('File open error:', err));
});
vscode.commands.registerCommand('mos.openConfig', () => {
runMosCommandGetOutput(['config-get']).then(output => {
const local = path.resolve(dir, '__config.json');
fs.writeFileSync(local, output);
vscode.window.showTextDocument(vscode.Uri.file(local));
}, err => console.log('config open error:', err));
});
vscode.commands.registerCommand('mos.runCommand', () => {
vscode.window.showInputBox().then(input => {
input = (input || '').replace(/^mos\s*/i, '').replace(/\s+$/, '');
if (!input) return;
runMosCommand(input.split(/\s+/), cmdOut)
.then(() => cmdOut.show(true))
.catch(err => vscode.window.showErrorMessage(err));
});
});
vscode.commands.registerCommand('mos.rebootDevice', () => {
return runMosCommand(['call', 'Sys.Reboot'], cmdOut)
.then(() => vscode.window.showInformationMessage('MGOS: Device rebooted'))
.catch(err => vscode.window.showErrorMessage(err));
});
vscode.commands.registerCommand('mos.sysInfo', () => {
return runMosCommand(['call', 'Sys.GetInfo'], cmdOut)
.then(() => cmdOut.show(true))
.catch(err => vscode.window.showErrorMessage(err));
});
vscode.commands.registerCommand('mos.console', () => {
return runMosCommand(['console'], cmdOut)
.then(() => cmdOut.show(true))
.catch(err => vscode.window.showErrorMessage(err));
});
vscode.commands.registerCommand('mos.rpcList', () => {
return runMosCommand(['call', 'RPC.List'], cmdOut)
.then(() => cmdOut.show(true))
.catch(err => vscode.window.showErrorMessage(err));
});
vscode.commands.registerCommand('mos.buildLocally', () => {
return runMosCommand(['build', "--local", "--verbose"], cmdOut)
.then(() => vscode.window.showInformationMessage('MGOS: Build succeeded!'))
.catch(err => {
cmdOut.show(true)
vscode.window.showErrorMessage(err)
});
});
vscode.commands.registerCommand('mos.build', () => {
return runMosCommand(['build'], cmdOut)
.then(() => vscode.window.showInformationMessage('MGOS: Build succeeded!'))
.catch(err => {
cmdOut.show(true)
vscode.window.showErrorMessage(err)
});
});
vscode.commands.registerCommand('mos.flash', () => {
return runMosCommand(['flash'], cmdOut)
.then(() => vscode.window.showInformationMessage('MGOS: Flash succeeded!'))
.catch(err => vscode.window.showErrorMessage(err));
});
vscode.commands.registerCommand('mos.refreshDeviceFiles', refreshFS);
vscode.commands.registerCommand('mos.showPanel', () => { cmdOut.show(true) });
const mkdiff = (x, y) => {
try {
const a = JSON.parse(x), b = JSON.parse(y);
const isEmpty = o => {
for (var p in o) return false;
return true;
};
const cmp = (obj1, obj2) => {
var ret = {};
for (var i in obj2) {
if (typeof (obj2[i]) === 'object') {
const x = cmp(obj1[i], obj2[i]);
if (!isEmpty(x)) ret[i] = x;
} else {
if (!obj1 || obj2[i] !== obj1[i]) ret[i] = obj2[i];
}
}
return ret;
};
const ret = cmp(a, b);
return isEmpty(ret) ? '' : JSON.stringify(ret);
} catch (e) {
return '';
}
};
vscode.workspace.onDidSaveTextDocument((document) => {
const local = path.normalize(document.fileName);
const cfg = path.resolve(dir, '__config.json');
if (local === cfg) {
runMosCommandGetOutput(['config-get']).then(output => {
const diff = mkdiff(output, document.getText());
if (!diff) {
vscode.window.showInfoMessage('Config not changed. Save aborted.');
return;
};
const setArgs = ['call', 'Config.Set', `{"config":${diff}}`];
const saveArgs = ['call', 'Config.Save', '{"reboot": true}'];
return runMosCommand(setArgs, cmdOut)
.then(() => runMosCommand(saveArgs, cmdOut))
.then(() => vscode.window.showInformationMessage('Config saved'))
.catch(err => vscode.window.showErrorMessage(err));
});
} else if (local.startsWith(dir)) {
const remote = path.basename(document.fileName);
return runMosCommand(['put', local, remote], cmdOut)
.then(() => vscode.window.showInformationMessage('File saved'))
.catch(err => vscode.window.showErrorMessage(err));
}
});
// Autocompletion support
// The symbols.json should be taken from here:
// https://raw.githubusercontent.com/cesanta/mongoose-os-docs/master/symbols.json
const symbols = { c: [], js: [] };
try {
const sp = path.join(context.extensionPath, 'resources', 'symbols.json');
JSON.parse(fs.readFileSync(sp), 'utf-8').forEach(e => {
const m = e.file.match(/\((.+)\)/);
(e.lang === 'c' ? symbols.c : symbols.js).push({
label: e.name,
kind: vscode.CompletionItemKind.Function,
detail: m[1],
documentation: new vscode.MarkdownString(e.doc),
});
});
console.log(` ${symbols.c.length} C, ${symbols.js.length} JS`);
} catch (err) {
vscode.window.showErrorMessage(err);
}
const ac = {
provideCompletionItems: doc =>
doc.fileName.match(/\.js$/) ? symbols.js : symbols.c,
};
vscode.languages.registerCompletionItemProvider('javascript', ac);
vscode.languages.registerCompletionItemProvider('c', ac);
vscode.languages.registerCompletionItemProvider('cpp', ac);
},
deactivate: function () { console.log('MOS IDE deactivated.'); },
}