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

Commit d61fd7a

Browse files
authored
add prebuild for build and verify (#551)
1 parent c792acb commit d61fd7a

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,16 @@ The following settings are per sketch settings of the Arduino extension. You can
7676
"port": "COM5",
7777
"board": "adafruit:samd:adafruit_feather_m0",
7878
"output": "../build",
79-
"debugger": "jlink"
79+
"debugger": "jlink",
80+
"prebuild": "bash prebuild.sh"
8081
}
8182
```
8283
- `sketch` - The main sketch file name of Arduino.
8384
- `port` - Name of the serial port connected to the device. Can be set by the `Arduino: Select Serial Port` command.
8485
- `board` - Current selected Arduino board alias. Can be set by the `Arduino: Change Board Type` command. Also, you can find the board list there.
8586
- `output` - Arduino build output path. If not set, Arduino will create a new temporary output folder each time, which means it cannot reuse the intermediate result of the previous build, leading to long verify/upload time. So it is recommended to set the field. Arduino requires that the output path should not be the workspace itself or subfolder of the workspace, otherwise, it may not work correctly. By default, this option is not set.
8687
- `debugger` - The short name of the debugger that will be used when the board itself does not have any debugger and there are more than one debugger available. You can find the list of debuggers [here](https://github.com/Microsoft/vscode-arduino/blob/master/misc/debuggerUsbMapping.json). By default, this option is not set.
88+
- `prebuild` - External command before build sketch file. You should only set one prebuild command. `command1 && command2` doesn't work. If you need run multiple commands before build, write them into a script.
8789

8890
## Debugging Arduino Code <sup>preview</sup>
8991
Before you start debug your Arduino code, read [this doc](https://code.visualstudio.com/docs/editor/debugging) and get to know the basic mechanism about debugging in Visual Studio Code. Also see [debugging for C++ in VSCode](https://code.visualstudio.com/docs/languages/cpp#_debugging) for your reference.

src/arduino/arduino.ts

+25
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ export class ArduinoApp {
119119
UsbDetector.getInstance().pauseListening();
120120
await vscode.workspace.saveAll(false);
121121

122+
if (dc.prebuild) {
123+
arduinoChannel.info(`Run prebuild command: ${dc.prebuild}`);
124+
const prebuildargs = dc.prebuild.split(" ");
125+
const prebuildCommand = prebuildargs.shift();
126+
try {
127+
await util.spawn(prebuildCommand, arduinoChannel.channel, prebuildargs, {shell: true, cwd: ArduinoWorkspace.rootPath});
128+
} catch (ex) {
129+
arduinoChannel.error(`Run prebuild failed: \n${ex.error}`);
130+
return;
131+
}
132+
}
133+
122134
const appPath = path.join(ArduinoWorkspace.rootPath, dc.sketch);
123135
const args = ["--upload", "--board", boardDescriptor, "--port", dc.port, appPath];
124136
if (VscodeSettings.getInstance().logLevel === "verbose") {
@@ -161,6 +173,19 @@ export class ArduinoApp {
161173
await vscode.workspace.saveAll(false);
162174

163175
arduinoChannel.start(`Verify sketch - ${dc.sketch}`);
176+
177+
if (dc.prebuild) {
178+
arduinoChannel.info(`Run prebuild command: ${dc.prebuild}`);
179+
const prebuildargs = dc.prebuild.split(" ");
180+
const prebuildCommand = prebuildargs.shift();
181+
try {
182+
await util.spawn(prebuildCommand, arduinoChannel.channel, prebuildargs, {shell: true, cwd: ArduinoWorkspace.rootPath});
183+
} catch (ex) {
184+
arduinoChannel.error(`Run prebuild failed: \n${ex.error}`);
185+
return;
186+
}
187+
}
188+
164189
const appPath = path.join(ArduinoWorkspace.rootPath, dc.sketch);
165190
const args = ["--verify", "--board", boardDescriptor, appPath];
166191
if (VscodeSettings.getInstance().logLevel === "verbose") {

src/deviceContext.ts

+9
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ export class DeviceContext implements IDeviceContext, vscode.Disposable {
8686

8787
private _sketchStatusBar: vscode.StatusBarItem;
8888

89+
private _prebuild: string;
90+
8991
/**
9092
* @constructor
9193
*/
@@ -142,6 +144,7 @@ export class DeviceContext implements IDeviceContext, vscode.Disposable {
142144
this._output = deviceConfigJson.output;
143145
this._debugger = deviceConfigJson["debugger"];
144146
this._onDidChange.fire();
147+
this._prebuild = deviceConfigJson.prebuild;
145148
} else {
146149
Logger.notifyUserError("arduinoFileError", new Error(constants.messages.ARDUINO_FILE_ERROR));
147150
}
@@ -153,6 +156,7 @@ export class DeviceContext implements IDeviceContext, vscode.Disposable {
153156
this._output = null;
154157
this._debugger = null;
155158
this._onDidChange.fire();
159+
this._prebuild = null;
156160
}
157161
return this;
158162
}, (reason) => {
@@ -169,6 +173,7 @@ export class DeviceContext implements IDeviceContext, vscode.Disposable {
169173
this._output = null;
170174
this._debugger = null;
171175
this._onDidChange.fire();
176+
this._prebuild = null;
172177

173178
return this;
174179
});
@@ -243,6 +248,10 @@ export class DeviceContext implements IDeviceContext, vscode.Disposable {
243248
this.saveContext();
244249
}
245250

251+
public get prebuild() {
252+
return this._prebuild.trim();
253+
}
254+
246255
public get output() {
247256
return this._output;
248257
}

0 commit comments

Comments
 (0)