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

Commit 6913309

Browse files
elektronikworkshophlovdal
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 microsoft#975
1 parent 37a53e8 commit 6913309

File tree

3 files changed

+58
-12
lines changed

3 files changed

+58
-12
lines changed

src/arduino/arduino.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -524,11 +524,19 @@ Please make sure the folder is not occupied by other procedures .`);
524524
args.push("--upload",
525525
"--port", dc.port,
526526
"--useprogrammer",
527-
"--pref", "programmer=" + programmer);
527+
"--pref", `programmer=${programmer}`);
528528
} else {
529529
args.push("--verify");
530530
}
531531

532+
if (dc.buildPreferences) {
533+
for (const pref of dc.buildPreferences) {
534+
// Note: BuildPrefSetting makes sure that each preference
535+
// value consists of exactly two items (key and value).
536+
args.push("--pref", `${pref[0]}=${pref[1]}`);
537+
}
538+
}
539+
532540
// We always build verbosely but filter the output based on the settings
533541
args.push("--verbose-build");
534542
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

+45-11
Original file line numberDiff line numberDiff line change
@@ -99,21 +99,51 @@ 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.
105134
*/
106135
export class DeviceSettings {
107-
public port: StrSetting = new StrSetting();
108-
public board: StrSetting = new StrSetting();
109-
public sketch: StrSetting = new StrSetting();
110-
public output: StrSetting = new StrSetting();
111-
public debugger: StrSetting = new StrSetting();
112-
public intelliSenseGen: StrSetting = new StrSetting();
113-
public configuration: StrSetting = new StrSetting();
114-
public prebuild: StrSetting = new StrSetting();
115-
public postbuild: StrSetting = new StrSetting();
116-
public programmer: StrSetting = new StrSetting();
136+
public port = new StrSetting();
137+
public board = new StrSetting();
138+
public sketch = new StrSetting();
139+
public output = new StrSetting();
140+
public debugger = new StrSetting();
141+
public intelliSenseGen = new StrSetting();
142+
public configuration = new StrSetting();
143+
public prebuild = new StrSetting();
144+
public postbuild = new StrSetting();
145+
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)