Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit 9543ba7

Browse files
elektronikworkshopadiazulay
authored andcommitted
Support for Arduino preferences in arduino.json. These preferences are applied during any build and can be used to set custom compiler flags and defines for instance.
Addresses #975
1 parent f9cd3c5 commit 9543ba7

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

src/arduino/arduino.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ export class ArduinoApp {
298298
if (this.useArduinoCli()) {
299299
args.push("--programmer", programmer)
300300
} else {
301-
args.push("--useprogrammer", "--pref", "programmer=arduino:" + programmer)
301+
args.push("--useprogrammer", "--pref", `programmer=arduino:${programmer}`);
302302
}
303303

304304
args.push("--port", dc.port);
@@ -315,6 +315,14 @@ export class ArduinoApp {
315315
}
316316
}
317317

318+
if (dc.buildPreferences) {
319+
for (const pref of dc.buildPreferences) {
320+
// Note: BuildPrefSetting makes sure that each preference
321+
// value consists of exactly two items (key and value).
322+
args.push("--pref", `${pref[0]}=${pref[1]}`);
323+
}
324+
}
325+
318326
// We always build verbosely but filter the output based on the settings
319327
args.push("--verbose-build");
320328
if (verbose) {

src/deviceContext.ts

+4
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ export class DeviceContext implements IDeviceContext, vscode.Disposable {
264264
this.saveContext();
265265
}
266266

267+
public get buildPreferences() {
268+
return this._settings.buildPreferences.value;
269+
}
270+
267271
public async initialize() {
268272
if (ArduinoWorkspace.rootPath && util.fileExistsSync(path.join(ArduinoWorkspace.rootPath, ARDUINO_CONFIG_FILE))) {
269273
vscode.window.showInformationMessage("Arduino.json already generated.");

src/deviceSettings.ts

+35-1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,35 @@ class StrSetting extends Setting<string> {
9999
}
100100
}
101101

102+
class BuildPrefSetting extends Setting<string[][]> {
103+
public get value() {
104+
return super.value;
105+
}
106+
public set value(value: string[][]) {
107+
if (!Array.isArray(value)) {
108+
super.value = super.default;
109+
return;
110+
}
111+
if (value.length <= 0) {
112+
super.value = super.default;
113+
return;
114+
}
115+
for (const pref of value) {
116+
if (!Array.isArray(pref) || pref.length !== 2) {
117+
super.value = super.default;
118+
return;
119+
}
120+
for (const i of pref) {
121+
if (typeof i !== "string") {
122+
super.value = super.default;
123+
return;
124+
}
125+
}
126+
}
127+
super.value = value;
128+
}
129+
}
130+
102131
/**
103132
* This class encapsulates all device/project specific settings and
104133
* provides common operations on them.
@@ -114,6 +143,7 @@ export class DeviceSettings {
114143
public prebuild = new StrSetting();
115144
public postbuild = new StrSetting();
116145
public programmer = new StrSetting();
146+
public buildPreferences = new BuildPrefSetting();
117147

118148
/**
119149
* @returns true if any of the settings values has its modified flag
@@ -129,7 +159,8 @@ export class DeviceSettings {
129159
this.configuration.modified ||
130160
this.prebuild.modified ||
131161
this.postbuild.modified ||
132-
this.programmer.modified;
162+
this.programmer.modified ||
163+
this.buildPreferences.modified;
133164
}
134165
/**
135166
* Clear modified flags of all settings values.
@@ -145,6 +176,7 @@ export class DeviceSettings {
145176
this.prebuild.commit();
146177
this.postbuild.commit();
147178
this.programmer.commit();
179+
this.buildPreferences.commit();
148180
}
149181
/**
150182
* Resets all settings values to their default values.
@@ -162,6 +194,7 @@ export class DeviceSettings {
162194
this.prebuild.reset();
163195
this.postbuild.reset();
164196
this.programmer.reset();
197+
this.buildPreferences.reset();
165198
if (commit) {
166199
this.commit();
167200
}
@@ -187,6 +220,7 @@ export class DeviceSettings {
187220
this.prebuild.value = settings.prebuild;
188221
this.postbuild.value = settings.postbuild;
189222
this.programmer.value = settings.programmer;
223+
this.buildPreferences.value = settings.buildPreferences;
190224
if (commit) {
191225
this.commit();
192226
}

0 commit comments

Comments
 (0)