10000 Use sketchbook.path preferences to get custom libraries/examples (#276) · Netoperz/vscode-arduino@97b0d5a · GitHub
[go: up one dir, main page]

Skip to content

Commit 97b0d5a

Browse files
Use sketchbook.path preferences to get custom libraries/examples (microsoft#276)
* Use sketchbook.path preferences to get custom libraries/examples * refactor usrsettings and arduinoSettings * Rename userSettings to vscodeSettings
1 parent 19d5a56 commit 97b0d5a

File tree

8 files changed

+289
-259
lines changed

8 files changed

+289
-259
lines changed

src/arduino/arduino.ts

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import * as vscode from "vscode";
1212
import * as constants from "../common/constants";
1313
import * as util from "../common/util";
1414
import * as Logger from "../logger/logger";
15-
import * as settings from "./settings";
1615

1716
import { DeviceContext, IDeviceContext } from "../deviceContext";
17+
import { IArduinoSettings } from "./arduinoSettings";
1818
import { BoardManager } from "./boardManager";
1919
import { ExampleManager } from "./exampleManager";
2020
import { LibraryManager } from "./libraryManager";
21+
import { VscodeSettings } from "./vscodeSettings";
2122

2223
import { arduinoChannel } from "../common/outputChannel";
2324
import { SerialMonitor } from "../serialmonitor/serialMonitor";
@@ -27,8 +28,6 @@ import { SerialMonitor } from "../serialmonitor/serialMonitor";
2728
*/
2829
export class ArduinoApp {
2930

30-
private _preferences: Map<string, string>;
31-
3231
private _boardManager: BoardManager;
3332

3433
private _libraryManager: LibraryManager;
@@ -38,18 +37,19 @@ export class ArduinoApp {
3837
/**
3938
* @param {IArduinoSettings} ArduinoSetting object.
4039
*/
41-
constructor(private _settings: settings.IArduinoSettings) {
40+
constructor(private _settings: IArduinoSettings) {
4241
}
4342

4443
/**
4544
* Need refresh Arduino IDE's setting when starting up.
4645
* @param {boolean} force - Whether force initialzie the arduino
4746
*/
4847
public async initialize(force: boolean = false) {
49-
if (!util.fileExistsSync(path.join(this._settings.packagePath, "preferences.txt"))) {
48+
if (!util.fileExistsSync(this._settings.preferencePath)) {
5049
try {
5150
// Use empty pref value to initialize preference.txt file
5251
await this.setPref("boardsmanager.additional.urls", "");
52+
this._settings.loadPreferences(); // reload preferences.
5353
} catch (ex) {
5454
}
5555
}
@@ -114,7 +114,7 @@ export class ArduinoApp {
114114

115115
const appPath = path.join(vscode.workspace.rootPath, dc.sketch);
116116
const args = ["--upload", "--board", boardDescriptor, "--port", dc.port, appPath];
117-
if (this._settings.logLevel === "verbose") {
117+
if (VscodeSettings.getIntance().logLevel === "verbose") {
118118
args.push("--verbose");
119119
}
120120
await util.spawn(this._settings.commandPath, arduinoChannel.channel, args).then(async (result) => {
@@ -143,7 +143,7 @@ export class ArduinoApp {
143143
arduinoChannel.start(`Verify sketch - ${dc.sketch}`);
144144
const appPath = path.join(vscode.workspace.rootPath, dc.sketch);
145145
const args = ["--verify", "--board", boardDescriptor, appPath];
146-
if (this._settings.logLevel === "verbose") {
146+
if (VscodeSettings.getIntance().logLevel === "verbose") {
147147
args.push("--verbose");
148148
}
149149
arduinoChannel.show();
@@ -350,7 +350,7 @@ export class ArduinoApp {
350350
}
351351

352352
// Step 1: Copy the example project to a temporary directory.
353-
const sketchPath = this.preferences.get("sketchbook.path") || path.dirname(this._settings.libPath);
353+
const sketchPath = path.join(this._settings.sketchbookPath, "generated_examples");
354354
if (!util.directoryExistsSync(sketchPath)) {
355355
util.mkdirRecursivelySync(sketchPath);
356356
}
@@ -404,13 +404,6 @@ export class ArduinoApp {
404404
return destExample;
405405
}
406406

407-
public get preferences() {
408-
if (!this._preferences) {
409-
this.loadPreferences();
410-
}
411-
return this._preferences;
412-
}
413-
414407
public get settings() {
415408
return this._settings;
416409
}
@@ -439,22 +432,6 @@ export class ArduinoApp {
439432
this._exampleManager = value;
440433
}
441434

442-
private loadPreferences() {
443-
this._preferences = new Map<string, string>();
444-
const lineRegex = /(\S+)=(\S+)/;
445-
446-
const rawText = fs.readFileSync(path.join(this._settings.packagePath, "preferences.txt"), "utf8");
447-
const lines = rawText.split("\n");
448-
lines.forEach((line) => {
449-
if (line) {
450-
const match = lineRegex.exec(line);
451-
if (match && match.length > 2) {
452-
this._preferences.set(match[1], match[2]);
453-
}
454-
}
455-
});
456-
}
457-
458435
private getBoardBuildString(deviceContext: IDeviceContext): string {
459436
const selectedBoard = this.boardManager.currentBoard;
460437
if (!selectedBoard) {

src/arduino/arduinoContentProvider.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import * as Constants from "../common/constants";
1010
import * as JSONHelper from "../common/cycle";
1111
import * as Logger from "../logger/logger";
1212
import { ArduinoApp } from "./arduino";
13+
import { IArduinoSettings } from "./arduinoSettings";
1314
import { BoardManager } from "./boardManager";
1415
import { LibraryManager } from "./libraryManager";
1516
import LocalWebServer from "./localWebServer";
16-
import { IArduinoSettings } from "./settings";
17+
import { VscodeSettings } from "./vscodeSettings";
1718

1819
export class ArduinoContentProvider implements vscode.TextDocumentContentProvider {
1920
private _webserver: LocalWebServer;
@@ -107,7 +108,7 @@ export class ArduinoContentProvider implements vscode.TextDocumentContentProvide
107108
}
108109

109110
public async getBoardPackages(req, res) {
110-
const update = (this._settings.autoUpdateIndexFiles && req.query.update === "true");
111+
const update = (VscodeSettings.getIntance().autoUpdateIndexFiles && req.query.update === "true");
111112
await this._arduinoApp.boardManager.loadPackages(update);
112113
return res.json({
113114
platforms: JSONHelper.decycle(this._arduinoApp.boardManager.platforms, undefined),
@@ -167,7 +168,7 @@ export class ArduinoContentProvider implements vscode.TextDocumentContentProvide
167168
}
168169

169170
public async getLibraries(req, res) {
170-
const update = (this._settings.autoUpdateIndexFiles && req.query.update === "true");
171+
const update = (VscodeSettings.getIntance().autoUpdateIndexFiles && req.query.update === "true");
171172
await this._arduinoApp.libraryManager.loadLibraries(update);
172173
return res.json({
173174
libraries: this._arduinoApp.libraryManager.libraries,

0 commit comments

Comments
 (0)
0