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 patharduinoContentProvider.ts
199 lines (181 loc) · 8.29 KB
/
arduinoContentProvider.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
/*--------------------------------------------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*-------------------------------------------------------------------------------------------*/
import * as path from "path";
import * as vscode from "vscode";
import * as Constants from "../common/constants";
import * as JSONHelper from "../common/cycle";
import { ArduinoApp } from "./arduino";
import { BoardManager } from "./boardManager";
import { LibraryManager } from "./libraryManager";
import LocalWebServer from "./localWebServer";
import { IArduinoSettings } from "./settings";
export class ArduinoContentProvider implements vscode.TextDocumentContentProvider {
private _webserver: LocalWebServer;
private _onDidChange = new vscode.EventEmitter<vscode.Uri>();
constructor(
private _settings: IArduinoSettings,
private _arduinoApp: ArduinoApp,
private _boardManager: BoardManager,
private _libraryManager: LibraryManager,
private _extensionPath: string) {
this.initialize();
}
public initialize() {
this._webserver = new LocalWebServer(this._extensionPath);
this._webserver.addHandler("/boardmanager", (req, res) => this.getBoardManagerView(req, res));
this._webserver.addHandler("/api/boardpackages", async (req, res) => await this.getBoardPackages(req, res));
this._webserver.addPostHandler("/api/installboard", async (req, res) => await this.installPackage(req, res));
this._webserver.addPostHandler("/api/uninstallboard", async (req, res) => await this.uninstallPackage(req, res));
this._webserver.addPostHandler("/api/openlink", async (req, res) => await this.openLink(req, res));
this._webserver.addHandler("/librarymanager", (req, res) => this.getLibraryManagerView(req, res));
this._webserver.addHandler("/api/libraries", async (req, res) => await this.getLibraries(req, res));
this._webserver.addPostHandler("/api/installlibrary", async (req, res) => await this.installLibrary(req, res));
this._webserver.addPostHandler("/api/uninstalllibrary", async (req, res) => await this.uninstallLibrary(req, res));
this._webserver.addPostHandler("/api/addlibpath", async (req, res) => await this.addLibPath(req, res));
this._webserver.start();
}
public provideTextDocumentContent(uri: vscode.Uri) {
let type = "";
if (uri.toString() === Constants.BOARD_MANAGER_URI.toString()) {
type = "boardmanager";
} else if (uri.toString() === Constants.LIBRARY_MANAGER_URI.toString()) {
type = "librarymanager";
}
let timeNow = new Date().getTime();
return `
<html>
<head>
<script type="text/javascript">
window.onload = function(event) {
console.log('reloaded results window at time ${timeNow}ms');
var doc = document.documentElement;
var styles = window.getComputedStyle(doc);
var backgroundcolor = styles.getPropertyValue('--background-color');
var color = styles.getPropertyValue('--color');
var theme = document.body.className;
var url = "${this._webserver.getEndpointUri(type)}?" +
"theme=" + theme +
"&backgroundcolor=" + backgroundcolor +
"&color=" + color;
document.getElementById('frame').src = url;
};
</script>
</head>
<body style="margin: 0; padding: 0; height: 100%; overflow: hidden;">
<iframe id="frame" width="100%" height="100%" frameborder="0" style="position:absolute; left: 0; right: 0; bottom: 0; top: 0px;"/>
</body>
</html>`;
}
get onDidChange(): vscode.Event<vscode.Uri> {
return this._onDidChange.event;
}
public update(uri: vscode.Uri) {
this._onDidChange.fire(uri);
}
public getBoardManagerView(req, res) {
return res.sendFile(path.join(this._extensionPath, "./out/html/index.html"));
}
public async getBoardPackages(req, res) {
const update = (this._settings.autoUpdateIndexFiles && req.query.update === "true");
await this._boardManager.loadPackages(update);
return res.json({
platforms: JSONHelper.decycle(this._boardManager.platforms, undefined),
});
}
public async installPackage(req, res) {
if (!req.body.packageName || !req.body.arch) {
return res.status(400).send("BAD Request! Missing { packageName, arch } parameters!");
} else {
try {
await this._arduinoApp.installBoard(req.body.packageName, req.body.arch, req.body.version);
return res.json({
status: "OK",
});
} catch (error) {
return res.status(500).send(`Install board failed with message "code:${error.code}, err:${error.stderr}"`);
}
}
}
public async uninstallPackage(req, res) {
if (!req.body.packagePath) {
return res.status(400).send("BAD Request! Missing { packagePath } parameter!");
} else {
try {
await this._arduinoApp.uninstallBoard(req.body.boardName, req.body.packagePath);
return res.json({
status: "OK",
});
} catch (error) {
return res.status(500).send(`Uninstall board failed with message "${error}"`);
}
}
}
public async openLink(req, res) {
if (!req.body.link) {
return res.status(400).send("BAD Request! Missing { link } parameter!");
} else {
try {
await vscode.commands.executeCommand("vscode.open", vscode.Uri.parse(req.body.link));
return res.json({
status: "OK",
});
} catch (error) {
return res.status(500).send(`Cannot open the link with error message "${error}"`);
}
}
}
public getLibraryManagerView(req, res) {
return res.sendFile(path.join(this._extensionPath, "./out/html/index.html"));
}
public async getLibraries(req, res) {
const update = (this._settings.autoUpdateIndexFiles && req.query.update === "true");
await this._libraryManager.loadLibraries(update);
return res.json({
libraries: this._libraryManager.libraries,
});
}
public async installLibrary(req, res) {
if (!req.body.libraryName) {
return res.status(400).send("BAD Request! Missing { libraryName } parameters!");
} else {
try {
await this._arduinoApp.installLibrary(req.body.libraryName, req.body.version);
return res.json({
status: "OK",
});
} catch (error) {
return res.status(500).send(`Install library failed with message "code:${error.code}, err:${error.stderr}"`);
}
}
}
public async uninstallLibrary(req, res) {
if (!req.body.libraryPath) {
return res.status(400).send("BAD Request! Missing { libraryPath } parameters!");
} else {
try {
await this._arduinoApp.uninstallLibrary(req.body.libraryName, req.body.libraryPath);
return res.json({
status: "OK",
});
} catch (error) {
return res.status(500).send(`Uninstall library failed with message "code:${error.code}, err:${error.stderr}"`);
}
}
}
public async addLibPath(req, res) {
if (!req.body.path) {
return res.status(400).send("BAD Request! Missing { path } parameters!");
} else {
try {
await this._arduinoApp.addLibPath(req.body.path);
return res.json({
status: "OK",
});
} catch (error) {
return res.status(500).send(`Add library path failed with message "code:${error.code}, err:${error.stderr}"`);
}
}
}
}