forked from microsoft/vscode-arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogrammerManager.ts
107 lines (85 loc) · 3.59 KB
/
programmerManager.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
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 { DeviceContext } from "../deviceContext";
import { ArduinoApp } from "./arduino";
import { IArduinoSettings } from "./arduinoSettings";
export class ProgrammerManager {
private static _programmerManager: ProgrammerManager = null;
private _currentProgrammerName: string;
private _programmerStatusBar: vscode.StatusBarItem;
private _programmers: Map<string, string>;
constructor(private _settings: IArduinoSettings, private _arduinoApp: ArduinoApp) {
this._programmerStatusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, constants.statusBarPriority.PROGRAMMER);
this._programmerStatusBar.command = "arduino.selectProgrammer";
this._programmerStatusBar.tooltip = "Select Programmer";
this._programmerStatusBar.text = "<Select Programmer>";
this._programmerStatusBar.show();
}
public get currentProgrammerID(): string {
return this._programmers.get(this._currentProgrammerName);
}
public loadConfig() {
this.loadProgrammers();
this.updateStatusBar();
const dc = DeviceContext.getInstance();
dc.onDidChange(() => {
this.updateStatusBar();
});
}
public async selectProgrammer() {
const chosen: string | undefined = await vscode.window.showQuickPick(Array.from(this._programmers.keys()),
{ placeHolder: "Select programmer" });
if (!chosen) {
return;
}
this._currentProgrammerName = chosen;
this._programmerStatusBar.text = this._currentProgrammerName;
const dc = DeviceContext.getInstance();
dc.programmer = this._currentProgrammerName;
}
private loadProgrammers() {
this._programmers = new Map<string, string>();
const boardLineRegex = /([^\.]+)\.(\S+)=(.+)/;
this._arduinoApp.boardManager.platforms.forEach(((plat) => {
if (plat.rootBoardPath === undefined) {
return;
}
const programmmerFilePath = path.join(plat.rootBoardPath, "programmers.txt");
if (util.fileExistsSync(programmmerFilePath)) {
const boardContent = fs.readFileSync(programmmerFilePath, "utf8");
const lines = boardContent.split(/[\r|\r\n|\n]/);
lines.forEach((line) => {
// Ignore comments.
if (line.startsWith("#")) {
return;
}
const match = boardLineRegex.exec(line);
if (match && match.length > 3) {
if (match[2] === "name") {
this._programmers.set(match[3], match[1]);
}
}
});
}
}));
}
private updateStatusBar(show: boolean = true): void {
if (show) {
this._programmerStatusBar.show();
const dc = DeviceContext.getInstance();
const selectedProgrammer = this._programmers.get(dc.programmer);
if (selectedProgrammer) {
this._currentProgrammerName = dc.programmer;
this._programmerStatusBar.text = dc.programmer;
} else {
this._currentProgrammerName = null;
this._programmerStatusBar.text = "<Select Programmer>";
}
} else {
this._programmerStatusBar.hide();
}
}
}