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

Export compiled binary #773

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"activationEvents": [
"*",
"onCommand:arduino.verify",
"onCommand:arduino.exportCompiledBinary",
"onCommand:arduino.upload",
"onCommand:arduino.uploadUsingProgrammer",
"onCommand:arduino.selectProgrammer",
Expand Down Expand Up @@ -77,6 +78,10 @@
"light": "images/ArduinoVerify_16.svg"
}
},
{
"command": "arduino.exportCompiledBinary",
"title": "Arduino: Export Compiled Binary"
},
{
"command": "arduino.upload",
"title": "Arduino: Upload",
Expand Down Expand Up @@ -538,6 +543,7 @@
],
"devDependencies": {
"@types/compare-versions": "^3.0.0",
"@types/fs-extra": "^5.0.4",
"@types/mocha": "^2.2.32",
"@types/node": "^6.0.40",
"@types/winreg": "^1.2.30",
Expand Down Expand Up @@ -568,6 +574,7 @@
"compare-versions": "^3.4.0",
"eventemitter2": "^4.1.0",
"express": "^4.14.1",
"fs-extra": "^7.0.1",
"glob": "^7.1.1",
"iconv-lite": "^0.4.18",
"properties": "^1.2.1",
Expand Down
46 changes: 44 additions & 2 deletions src/arduino/arduino.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
// Licensed under the MIT license.

import * as fs from "fs";
import * as fs_extra from "fs-extra"; // TODO: Remove to keep only standard fs when upgrading to Node 8+
import * as glob from "glob";
import * as os from "os";
import * as path from "path";
import * as uuidv4 from "uuid/v4";
import * as vscode from "vscode";

import * as constants from "../common/constants";
Expand Down Expand Up @@ -239,7 +241,7 @@ export class ArduinoApp {
});
}

public async verify(output: string = "") {
public async verify(output: string = "", exportBinary: boolean = false) {
const dc = DeviceContext.getInstance();
const boardDescriptor = this.getBoardBuildString();
if (!boardDescriptor) {
Expand Down Expand Up @@ -276,8 +278,10 @@ export class ArduinoApp {
if (VscodeSettings.getInstance().logLevel === "verbose") {
args.push("--verbose");
}
let outputPath;
let isTmp = false;
if (output || dc.output) {
const outputPath = path.resolve(ArduinoWorkspace.rootPath, output || dc.output);
outputPath = path.resolve(ArduinoWorkspace.rootPath, output || dc.output);
const dirPath = path.dirname(outputPath);
if (!util.directoryExistsSync(dirPath)) {
Logger.notifyUserError("InvalidOutPutPath", new Error(constants.messages.INVALID_OUTPUT_PATH + outputPath));
Expand All @@ -289,13 +293,47 @@ export class ArduinoApp {
} else {
const msg = "Output path is not specified. Unable to reuse previously compiled files. Verify could be slow. See README.";
arduinoChannel.warning(msg);
if (exportBinary) {
const uuid = uuidv4();
outputPath = path.join(os.tmpdir(), uuid);
args.push("--pref", `build.path=${outputPath}`);
isTmp = true;
}
}

arduinoChannel.show();
// we need to return the result of verify
try {
await util.spawn(this._settings.commandPath, arduinoChannel.channel, args);
arduinoChannel.end(`Finished verify sketch - ${dc.sketch}${os.EOL}`);
if (exportBinary) {
const options = {
nodir: true,
realpath: true,
absolute: true,
};
glob(path.join(outputPath, "/*.{hex,bin,elf}"), options, (err, files: string[]) => {
if (err) {
arduinoChannel.warning(`Couldn't find binary files, glob returned ${err}`);
} else {
files.forEach(async (bin) => {
// TODO: Replace fs_extra.copy by fs.copyFile when upgrading to Node 8+
await fs_extra.copy(bin, path.join(ArduinoWorkspace.rootPath, path.basename(bin)), (copyError: Error) => {
if (copyError) {
arduinoChannel.warning(`Couldn't copy binary file ${bin}, copy returned ${copyError}`);
}
});
});
if (isTmp) {
fs_extra.remove(outputPath, (removeError: Error) => {
if (removeError) {
arduinoChannel.warning(`Couldn't remove temporary build directory ${outputPath}, rm returned ${removeError}`);
}
});
}
}
});
}
return true;
} catch (reason) {
arduinoChannel.error(`Exit with code=${reason.code}${os.EOL}`);
Expand All @@ -304,6 +342,10 @@ export class ArduinoApp {

}

public async exportCompiledBinary(output: string = "") {
return this.verify(output, true);
}

// Add selected library path to the intellisense search path.
public addLibPath(libraryPath: string) {
let libPaths;
Expand Down
18 changes: 18 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,24 @@ export async function activate(context: vscode.ExtensionContext) {
return { board: (ArduinoContext.boardManager.currentBoard === null) ? null : ArduinoContext.boardManager.currentBoard.name };
});

registerArduinoCommand("arduino.exportCompiledBinary", async () => {
if (!status.compile) {
status.compile = "verify";
try {
await vscode.window.withProgress({
location: vscode.ProgressLocation.Window,
title: "Arduino: Exporting Compiled Binary...",
}, async () => {
await ArduinoContext.arduinoApp.exportCompiledBinary();
});
} catch (ex) {
}
delete status.compile;
}
}, () => {
return { board: (ArduinoContext.boardManager.currentBoard === null) ? null : ArduinoContext.boardManager.currentBoard.name };
});

registerArduinoCommand("arduino.upload", async () => {
if (!status.compile) {
status.compile = "upload";
Expand Down
15 changes: 15 additions & 0 deletions test/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,19 @@ suite("Arduino: Commands Tests", () => {
}
});

// tslint:disable-next-line: only-arrow-functions
test("should be able to run command: arduino exportCompiledBinary", function(done) {
// Same timeout as verify, being the longest part of the command
this.timeout(3 * 60 * 1000);
try {
// run "Arduino: Export Compiled Binary" command.
vscode.commands.executeCommand("arduino.exportCompiledBinary").then((result) => {
done();
});

} catch (error) {
done(new Error(error));
}
});

});
1 change: 1 addition & 0 deletions test/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ suite("Arduino: Extension Tests", () => {
return vscode.commands.getCommands(true).then((commands) => {
const ARDUINO_COMMANDS = [
"arduino.verify",
"arduino.exportCompiledBinary",
"arduino.upload",
"arduino.uploadUsingProgrammer",
"arduino.selectProgrammer",
Expand Down