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

Commit

Permalink
Support for Arduino preferences in arduino.json. These preferences …
Browse files Browse the repository at this point in the history
…are applied during any build and can be used to set custom compiler flags and defines for instance.

Addresses #975
  • Loading branch information
elektronikworkshop authored and adiazulay committed Jan 19, 2021
1 parent f9cd3c5 commit 9543ba7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/arduino/arduino.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ export class ArduinoApp {
if (this.useArduinoCli()) {
args.push("--programmer", programmer)
} else {
args.push("--useprogrammer", "--pref", "programmer=arduino:" + programmer)
args.push("--useprogrammer", "--pref", `programmer=arduino:${programmer}`);
}

args.push("--port", dc.port);
Expand All @@ -315,6 +315,14 @@ export class ArduinoApp {
}
}

if (dc.buildPreferences) {
for (const pref of dc.buildPreferences) {
// Note: BuildPrefSetting makes sure that each preference
// value consists of exactly two items (key and value).
args.push("--pref", `${pref[0]}=${pref[1]}`);
}
}

// We always build verbosely but filter the output based on the settings
args.push("--verbose-build");
if (verbose) {
Expand Down
4 changes: 4 additions & 0 deletions src/deviceContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ export class DeviceContext implements IDeviceContext, vscode.Disposable {
this.saveContext();
}

public get buildPreferences() {
return this._settings.buildPreferences.value;
}

public async initialize() {
if (ArduinoWorkspace.rootPath && util.fileExistsSync(path.join(ArduinoWorkspace.rootPath, ARDUINO_CONFIG_FILE))) {
vscode.window.showInformationMessage("Arduino.json already generated.");
Expand Down
36 changes: 35 additions & 1 deletion src/deviceSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,35 @@ class StrSetting extends Setting<string> {
}
}

class BuildPrefSetting extends Setting<string[][]> {
public get value() {
return super.value;
}
public set value(value: string[][]) {
if (!Array.isArray(value)) {
super.value = super.default;
return;
}
if (value.length <= 0) {
super.value = super.default;
return;
}
for (const pref of value) {
if (!Array.isArray(pref) || pref.length !== 2) {
super.value = super.default;
return;
}
for (const i of pref) {
if (typeof i !== "string") {
super.value = super.default;
return;
}
}
}
super.value = value;
}
}

/**
* This class encapsulates all device/project specific settings and
* provides common operations on them.
Expand All @@ -114,6 +143,7 @@ export class DeviceSettings {
public prebuild = new StrSetting();
public postbuild = new StrSetting();
public programmer = new StrSetting();
public buildPreferences = new BuildPrefSetting();

/**
* @returns true if any of the settings values has its modified flag
Expand All @@ -129,7 +159,8 @@ export class DeviceSettings {
this.configuration.modified ||
this.prebuild.modified ||
this.postbuild.modified ||
this.programmer.modified;
this.programmer.modified ||
this.buildPreferences.modified;
}
/**
* Clear modified flags of all settings values.
Expand All @@ -145,6 +176,7 @@ export class DeviceSettings {
this.prebuild.commit();
this.postbuild.commit();
this.programmer.commit();
this.buildPreferences.commit();
}
/**
* Resets all settings values to their default values.
Expand All @@ -162,6 +194,7 @@ export class DeviceSettings {
this.prebuild.reset();
this.postbuild.reset();
this.programmer.reset();
this.buildPreferences.reset();
if (commit) {
this.commit();
}
Expand All @@ -187,6 +220,7 @@ export class DeviceSettings {
this.prebuild.value = settings.prebuild;
this.postbuild.value = settings.postbuild;
this.programmer.value = settings.programmer;
this.buildPreferences.value = settings.buildPreferences;
if (commit) {
this.commit();
}
Expand Down

0 comments on commit 9543ba7

Please sign in to comment.