diff --git a/README.md b/README.md index 8f77137b..269d30b6 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,8 @@ The following settings are per sketch settings of the Arduino extension. You can "port": "COM5", "board": "adafruit:samd:adafruit_feather_m0", "output": "../build", - "debugger": "jlink" + "debugger": "jlink", + "prebuild": "bash prebuild.sh" } ``` - `sketch` - The main sketch file name of Arduino. @@ -84,6 +85,7 @@ The following settings are per sketch settings of the Arduino extension. You can - `board` - Current selected Arduino board alias. Can be set by the `Arduino: Change Board Type` command. Also, you can find the board list there. - `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. - `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. +- `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. ## Debugging Arduino Code preview 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. diff --git a/src/arduino/arduino.ts b/src/arduino/arduino.ts index d5627f0f..3403d017 100644 --- a/src/arduino/arduino.ts +++ b/src/arduino/arduino.ts @@ -119,6 +119,18 @@ export class ArduinoApp { UsbDetector.getInstance().pauseListening(); await vscode.workspace.saveAll(false); + if (dc.prebuild) { + arduinoChannel.info(`Run prebuild command: ${dc.prebuild}`); + const prebuildargs = dc.prebuild.split(" "); + const prebuildCommand = prebuildargs.shift(); + try { + await util.spawn(prebuildCommand, arduinoChannel.channel, prebuildargs, {shell: true, cwd: ArduinoWorkspace.rootPath}); + } catch (ex) { + arduinoChannel.error(`Run prebuild failed: \n${ex.error}`); + return; + } + } + const appPath = path.join(ArduinoWorkspace.rootPath, dc.sketch); const args = ["--upload", "--board", boardDescriptor, "--port", dc.port, appPath]; if (VscodeSettings.getInstance().logLevel === "verbose") { @@ -161,6 +173,19 @@ export class ArduinoApp { await vscode.workspace.saveAll(false); arduinoChannel.start(`Verify sketch - ${dc.sketch}`); + + if (dc.prebuild) { + arduinoChannel.info(`Run prebuild command: ${dc.prebuild}`); + const prebuildargs = dc.prebuild.split(" "); + const prebuildCommand = prebuildargs.shift(); + try { + await util.spawn(prebuildCommand, arduinoChannel.channel, prebuildargs, {shell: true, cwd: ArduinoWorkspace.rootPath}); + } catch (ex) { + arduinoChannel.error(`Run prebuild failed: \n${ex.error}`); + return; + } + } + const appPath = path.join(ArduinoWorkspace.rootPath, dc.sketch); const args = ["--verify", "--board", boardDescriptor, appPath]; if (VscodeSettings.getInstance().logLevel === "verbose") { diff --git a/src/deviceContext.ts b/src/deviceContext.ts index c9eb046c..0f08ef82 100644 --- a/src/deviceContext.ts +++ b/src/deviceContext.ts @@ -86,6 +86,8 @@ export class DeviceContext implements IDeviceContext, vscode.Disposable { private _sketchStatusBar: vscode.StatusBarItem; + private _prebuild: string; + /** * @constructor */ @@ -142,6 +144,7 @@ export class DeviceContext implements IDeviceContext, vscode.Disposable { this._output = deviceConfigJson.output; this._debugger = deviceConfigJson["debugger"]; this._onDidChange.fire(); + this._prebuild = deviceConfigJson.prebuild; } else { Logger.notifyUserError("arduinoFileError", new Error(constants.messages.ARDUINO_FILE_ERROR)); } @@ -153,6 +156,7 @@ export class DeviceContext implements IDeviceContext, vscode.Disposable { this._output = null; this._debugger = null; this._onDidChange.fire(); + this._prebuild = null; } return this; }, (reason) => { @@ -169,6 +173,7 @@ export class DeviceContext implements IDeviceContext, vscode.Disposable { this._output = null; this._debugger = null; this._onDidChange.fire(); + this._prebuild = null; return this; }); @@ -243,6 +248,10 @@ export class DeviceContext implements IDeviceContext, vscode.Disposable { this.saveContext(); } + public get prebuild() { + return this._prebuild.trim(); + } + public get output() { return this._output; }