This repository was archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathdeviceContext.ts
387 lines (326 loc) · 13.2 KB
/
deviceContext.ts
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import * as fs from "fs";
import * as path from "path";
import * as vscode from "vscode";
import * as constants from "./common/constants";
import * as util from "./common/util";
import * as Logger from "./logger/logger";
import { VscodeSettings } from "./arduino/vscodeSettings";
import { ARDUINO_CONFIG_FILE } from "./common/constants";
import { ArduinoWorkspace } from "./common/workspace";
/**
* Interface that represents the arduino context information.
* @interface
*/
export interface IDeviceContext {
/**
* COM Port connect to the device
* @property {string}
*/
port: string;
/**
* Current selected Arduino board alias.
* @property {string}
*/
board: string;
/**
* Arduino main sketch file
* @property {string}
*/
sketch: string;
/**
* Arduino build output path
*/
output: string;
/**
* Arduino debugger
*/
debugger_: string;
/**
* Current selected programmer.
* @property {string}
*/
programmer: string;
/**
* Arduino custom board configuration
* @property {string}
*/
configuration: string;
onDidChange: vscode.Event<void>;
initialize(): void;
}
export class DeviceContext implements IDeviceContext, vscode.Disposable {
public static getInstance(): DeviceContext {
return DeviceContext._deviceContext;
}
private static _deviceContext: DeviceContext = new DeviceContext();
private _onDidChange = new vscode.EventEmitter<void>();
private _port: string;
private _board: string;
private _sketch: string;
private _output: string;
private _debugger: string;
private _configuration: string;
private _extensionPath: string;
private _watcher: vscode.FileSystemWatcher;
private _vscodeWatcher: vscode.FileSystemWatcher;
private _sketchStatusBar: vscode.StatusBarItem;
private _prebuild: string;
private _programmer: string;
private _vscodeSettings = VscodeSettings.getInstance();
/**
* @constructor
*/
private constructor() {
if (vscode.workspace && ArduinoWorkspace.rootPath) {
this._watcher = vscode.workspace.createFileSystemWatcher(path.join(ArduinoWorkspace.rootPath, ARDUINO_CONFIG_FILE));
// We only care about the deletion arduino.json in the .vscode folder:
this._vscodeWatcher = vscode.workspace.createFileSystemWatcher(path.join(ArduinoWorkspace.rootPath, ".vscode"), true, true, false);
this._watcher.onDidCreate(() => this.loadContext());
this._watcher.onDidChange(() => this.loadContext());
this._watcher.onDidDelete(() => this.loadContext());
this._vscodeWatcher.onDidDelete(() => this.loadContext());
this._sketchStatusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, constants.statusBarPriority.SKETCH);
this._sketchStatusBar.command = "arduino.setSketchFile";
this._sketchStatusBar.tooltip = "Sketch File";
vscode.window.onDidChangeActiveTextEditor(() => {
this.trySetOpenedFileAsSketch();
})
}
}
public dispose() {
if (this._watcher) {
this._watcher.dispose();
}
if (this._vscodeWatcher) {
this._vscodeWatcher.dispose();
}
}
public get extensionPath(): string {
return this._extensionPath;
}
public set extensionPath(value: string) {
this._extensionPath = value;
}
/**
* TODO: Current we use the Arduino default settings. For future release, this dependency might be removed
* and the setting only depends on device.json.
* @method
*/
public loadContext(): Thenable<object> {
return vscode.workspace.findFiles(ARDUINO_CONFIG_FILE, null, 1)
.then((files) => {
let deviceConfigJson: any = {};
if (files && files.length > 0) {
const configFile = files[0];
deviceConfigJson = util.tryParseJSON(fs.readFileSync(configFile.fsPath, "utf8"));
if (deviceConfigJson) {
this._port = deviceConfigJson.port;
this._board = deviceConfigJson.board;
this._sketch = deviceConfigJson.sketch;
this._configuration = deviceConfigJson.configuration;
this._output = deviceConfigJson.output;
this._debugger = deviceConfigJson["debugger"];
this._onDidChange.fire();
this._prebuild = deviceConfigJson.prebuild;
this._programmer = deviceConfigJson.programmer;
} else {
Logger.notifyUserError("arduinoFileError", new Error(constants.messages.ARDUINO_FILE_ERROR));
}
} else {
this._port = null;
this._board = null;
this._sketch = null;
this._configuration = null;
this._output = null;
this._debugger = null;
this._onDidChange.fire();
this._prebuild = null;
this._programmer = null;
}
return this;
}, (reason) => {
// Workaround for change in API.
// vscode.workspace.findFiles() for some reason now throws an error ehn path does not exist
// vscode.window.showErrorMessage(reason.toString());
// Logger.notifyUserError("arduinoFileUnhandleError", new Error(reason.toString()));
// Workaround for change in API, populate required props for arduino.json
this._port = null;
this._board = null;
this._sketch = null;
this._configuration = null;
this._output = null;
this._debugger = null;
this._onDidChange.fire();
this._prebuild = null;
this._programmer = null;
return this;
});
}
public showStatusBar() {
if (this.trySetOpenedFileAsSketch()) {
return;
}
if (!this._sketch) {
return false;
}
this._sketchStatusBar.text = this._sketch;
this._sketchStatusBar.show();
}
public saveContext() {
if (!ArduinoWorkspace.rootPath) {
return;
}
const deviceConfigFile = path.join(ArduinoWorkspace.rootPath, ARDUINO_CONFIG_FILE);
let deviceConfigJson: any = {};
if (util.fileExistsSync(deviceConfigFile)) {
deviceConfigJson = util.tryParseJSON(fs.readFileSync(deviceConfigFile, "utf8"));
}
if (!deviceConfigJson) {
Logger.notifyUserError("arduinoFileError", new Error(constants.messages.ARDUINO_FILE_ERROR));
return;
}
deviceConfigJson.sketch = this.sketch;
deviceConfigJson.port = this.port;
deviceConfigJson.board = this.board;
deviceConfigJson.output = this.output;
deviceConfigJson["debugger"] = this.debugger_;
deviceConfigJson.configuration = this.configuration;
deviceConfigJson.programmer = this.programmer;
util.mkdirRecursivelySync(path.dirname(deviceConfigFile));
fs.writeFileSync(deviceConfigFile, JSON.stringify(deviceConfigJson, (key, value) => {
if (value === null) {
return undefined;
}
return value;
}, 4));
}
public get onDidChange(): vscode.Event<void> {
return this._onDidChange.event;
}
public get port() {
return this._port;
}
public set port(value: string) {
this._port = value;
this.saveContext();
}
public get board() {
return this._board;
}
public set board(value: string) {
this._board = value;
this.saveContext();
}
public get sketch() {
return this._sketch;
}
public set sketch(value: string) {
this._sketch = value;
this.saveContext();
}
public get prebuild() {
return this._prebuild ? this._prebuild.trim() : "";
}
public get output() {
return this._output;
}
public set output(value: string) {
this._output = value;
this.saveContext();
}
public get debugger_() {
return this._debugger;
}
public set debugger_(value: string) {
this._debugger = value;
this.saveContext();
}
public get configuration() {
return this._configuration;
}
public set configuration(value: string) {
this._configuration = value;
this.saveContext();
}
public get programmer() {
return this._programmer;
}
public set programmer(value: string) {
this._programmer = value;
this.saveContext();
}
public async initialize() {
if (ArduinoWorkspace.rootPath && util.fileExistsSync(path.join(ArduinoWorkspace.rootPath, ARDUINO_CONFIG_FILE))) {
vscode.window.showInformationMessage("Arduino.json is already generated.");
return;
} else {
if (!ArduinoWorkspace.rootPath) {
vscode.window.showInformationMessage("Please open an folder first.");
return;
}
await this.resolveMainSketch();
if (this.sketch) {
await vscode.commands.executeCommand("arduino.changeBoardType");
vscode.window.showInformationMessage("The workspace is initialized with the Arduino extension support.");
} else {
vscode.window.showInformationMessage("No *.ino sketch file was found or selected, so skip initialize command.");
}
}
}
public async resolveMainSketch() {
return await vscode.workspace.findFiles("**/*.ino", null)
.then(async (fileUris) => {
if (fileUris.length === 0) {
let newSketchFileName = await vscode.window.showInputBox({
value: "app.ino",
prompt: "No .ino file was found on workspace, initialize sketch first",
placeHolder: "Input the sketch file name",
validateInput: (value) => {
if (value && /^\w+\.((ino)|(cpp)|c)$/.test(value.trim())) {
return null;
} else {
return "Invalid sketch file name. Should be *.ino/*.cpp/*.c";
}
},
});
newSketchFileName = (newSketchFileName && newSketchFileName.trim()) || "";
if (newSketchFileName) {
const snippets = fs.readFileSync(path.join(this.extensionPath, "snippets", "sample.ino"));
fs.writeFileSync(path.join(ArduinoWorkspace.rootPath, newSketchFileName), snippets);
this.sketch = newSketchFileName;
// Open the new sketch file.
const textDocument = await vscode.workspace.openTextDocument(path.join(ArduinoWorkspace.rootPath, newSketchFileName));
vscode.window.showTextDocument(textDocument, vscode.ViewColumn.One, true);
} else {
this._sketch = undefined;
}
} else if (fileUris.length === 1) {
this.sketch = path.relative(ArduinoWorkspace.rootPath, fileUris[0].fsPath);
} else if (fileUris.length > 1) {
const chosen = await vscode.window.showQuickPick(<vscode.QuickPickItem[]>fileUris.map((fileUri): vscode.QuickPickItem => {
return <vscode.QuickPickItem>{
label: path.relative(ArduinoWorkspace.rootPath, fileUri.fsPath),
description: fileUri.fsPath,
};
}), { placeHolder: "Select the main sketch file" });
if (chosen && chosen.label) {
this.sketch = chosen.label;
}
}
});
}
public trySetOpenedFileAsSketch() {
if (this._vscodeSettings.useActiveSketch) {
const openedFile = vscode.window.activeTextEditor.document.fileName
.slice(vscode.workspace.rootPath.length + 1);
if (/\.((ino)|(cpp)|c)$/.test(openedFile.trim())) {
this._sketch = openedFile;
this.saveContext();
return true;
}
}
return false;
};
}