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

Commit

Permalink
Make build and verify identical
Browse files Browse the repository at this point in the history
  • Loading branch information
hlovdal authored and adiazulay committed Jan 19, 2021
1 parent 2039739 commit 2a2db50
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 14 deletions.
138 changes: 125 additions & 13 deletions src/arduino/arduino.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,12 @@ export class ArduinoApp {
* selectable programmer
* @param {bool} [compile=true] - Indicates whether to compile the code when using the CLI to upload
*/
public async build(buildMode: BuildMode, compile: boolean = true) {
public async build(buildMode: BuildMode, compile: boolean, buildDir?: string): Promise<boolean> {
const dc = DeviceContext.getInstance();
const args: string[] = [];
let restoreSerialMonitor: boolean = false;
let cocopa: ICoCoPaContext;

const boardDescriptor = this.getBoardBuildString();
if (!boardDescriptor) {
return false;
Expand Down Expand Up @@ -199,10 +201,26 @@ export class ArduinoApp {
}

args.push("--port", dc.port);
} else if (buildMode === BuildMode.Analyze) {
if (!isCompilerParserEnabled()) {
return false;
}
cocopa = makeCompilerParserContext(dc);
if (!this.useArduinoCli()) {
args.push("--verify", "--verbose");
} else {
args.push("compile", "--verbose", "-b", boardDescriptor);
}
} else {
if (!this.useArduinoCli()) {
args.push("--verify");
} else {
args.push("compile", "-b", boardDescriptor);
}
}

const verbose = VscodeSettings.getInstance().logLevel === "verbose";
if (verbose) {
if (buildMode !== BuildMode.Analyze && verbose) {
args.push("--verbose");
}

Expand All @@ -215,8 +233,8 @@ export class ArduinoApp {
return false;
}

if (dc.output && compile) {
const outputPath = path.resolve(ArduinoWorkspace.rootPath, dc.output);
if ((buildDir || dc.output) && compile) {
const outputPath = path.resolve(ArduinoWorkspace.rootPath, buildDir || dc.output);
const dirPath = path.dirname(outputPath);
if (!util.directoryExistsSync(dirPath)) {
logger.notifyUserError("InvalidOutPutPath", new Error(constants.messages.INVALID_OUTPUT_PATH + outputPath));
Expand All @@ -230,9 +248,9 @@ export class ArduinoApp {
args.push("--pref", `build.path=${outputPath}`);
}

arduinoChannel.info(`Please see the build logs in Output path: ${outputPath}`);
arduinoChannel.info(`Please see the build logs in output path: ${outputPath}`);
} else {
const msg = "Output path is not specified. Unable to reuse previously compiled files. Upload could be slow. See README.";
const msg = "Output path is not specified. Unable to reuse previously compiled files. Build will be slower. See README.";
arduinoChannel.warning(msg);
}

Expand All @@ -249,6 +267,9 @@ export class ArduinoApp {
args.push(path.join(ArduinoWorkspace.rootPath, dc.sketch));

const cleanup = async () => {
if (cocopa) {
cocopa.conclude();
}
if (buildMode === BuildMode.Upload || buildMode === BuildMode.UploadProgrammer) {
UsbDetector.getInstance().resumeListening();
if (restoreSerialMonitor) {
Expand All @@ -260,7 +281,14 @@ export class ArduinoApp {
// TODO: Get rid of spawn's channel parameter and just support
// stdout and stderr callbacks
const stdoutCallback = (line: string) => {
arduinoChannel.channel.append(line);
if (cocopa) {
cocopa.callback(line);
if (verbose) {
arduinoChannel.channel.append(line);
}
} else {
arduinoChannel.channel.append(line);
}
}

await util.spawn(
Expand All @@ -282,12 +310,15 @@ export class ArduinoApp {
JSON.stringify(reason);
arduinoChannel.error(`${buildMode} sketch '${dc.sketch}': ${msg}${os.EOL}`);
});

return success;
}

public async verify(buildMode: BuildMode, buildDir: string = "") {
public async verify(buildMode: BuildMode, buildDir?: string): Promise<boolean> {
const compile = true;
const dc = DeviceContext.getInstance();
const args: string[] = [];
let restoreSerialMonitor: boolean = false;
let cocopa: ICoCoPaContext;

const boardDescriptor = this.getBoardBuildString();
Expand All @@ -307,7 +338,76 @@ export class ArduinoApp {
await this.getMainSketch(dc);
}

if (buildMode === BuildMode.Analyze) {
const selectSerial = async () => {
const choice = await vscode.window.showInformationMessage(
"Serial port is not specified. Do you want to select a serial port for uploading?",
"Yes", "No");
if (choice === "Yes") {
vscode.commands.executeCommand("arduino.selectSerialPort");
}
}

if (buildMode === BuildMode.Upload) {
if ((!dc.configuration || !/upload_method=[^=,]*st[^,]*link/i.test(dc.configuration)) && !dc.port) {
await selectSerial();
return false;
}

if (!compile && !this.useArduinoCli()) {
arduinoChannel.error("This command is only available when using the Arduino CLI");
return;
}

if (!this.useArduinoCli()) {
args.push("--upload");
} else {
// TODO: add the --clean argument to the cli args when v 0.14 is released (this will clean up the build folder after uploading)
if (compile) {
args.push("compile", "--upload");
} else {
args.push("upload");
}
args.push("-b", boardDescriptor);
}

if (dc.port) {
args.push("--port", dc.port);
}
} else if (buildMode === BuildMode.UploadProgrammer) {
const programmer = this.getProgrammerString();
if (!programmer) {
return false;
}
if (!dc.port) {
await selectSerial();
return false;
}

if (!compile && !this.useArduinoCli()) {
arduinoChannel.error("This command is only available when using the Arduino CLI");
return;
}

if (!this.useArduinoCli()) {
args.push("--upload");
} else {
// TODO: add the --clean argument to the cli args when v 0.14 is released (this will clean up the build folder after uploading)
if (compile) {
args.push("compile", "--upload");
} else {
args.push("upload");
}
args.push("-b", boardDescriptor);
}

if (this.useArduinoCli()) {
args.push("--programmer", programmer)
} else {
args.push("--useprogrammer", "--pref", "programmer=arduino:" + programmer)
}

args.push("--port", dc.port);
} else if (buildMode === BuildMode.Analyze) {
if (!isCompilerParserEnabled()) {
return false;
}
Expand Down Expand Up @@ -339,7 +439,7 @@ export class ArduinoApp {
return false;
}

if (buildDir || dc.output) {
if ((buildDir || dc.output) && compile) {
const outputPath = path.resolve(ArduinoWorkspace.rootPath, buildDir || dc.output);
const dirPath = path.dirname(outputPath);
if (!util.directoryExistsSync(dirPath)) {
Expand All @@ -354,12 +454,19 @@ export class ArduinoApp {
args.push("--pref", `build.path=${outputPath}`);
}

arduinoChannel.info(`Please see the build logs in Output path: ${outputPath}`);
arduinoChannel.info(`Please see the build logs in output path: ${outputPath}`);
} else {
const msg = "Output path is not specified. Unable to reuse previously compiled files. Verify could be slow. See README.";
const msg = "Output path is not specified. Unable to reuse previously compiled files. Build will be slower. See README.";
arduinoChannel.warning(msg);
}

// stop serial monitor when everything is prepared and good
// what makes restoring of its previous state easier
if (buildMode === BuildMode.Upload || buildMode === BuildMode.UploadProgrammer) {
restoreSerialMonitor = await SerialMonitor.getInstance().closeSerialMonitor(dc.port);
UsbDetector.getInstance().pauseListening();
}

let success = false;

// Push sketch as last argument
Expand All @@ -369,7 +476,12 @@ export class ArduinoApp {
if (cocopa) {
cocopa.conclude();
}
await Promise.resolve();
if (buildMode === BuildMode.Upload || buildMode === BuildMode.UploadProgrammer) {
UsbDetector.getInstance().resumeListening();
if (restoreSerialMonitor) {
await SerialMonitor.getInstance().openSerialMonitor();
}
}
}

// TODO: Get rid of spawn's channel parameter and just support
Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export async function activate(context: vscode.ExtensionContext) {
location: vscode.ProgressLocation.Window,
title: "Arduino: Uploading...",
}, async () => {
await arduinoContextModule.default.arduinoApp.build(BuildMode.Upload);
await arduinoContextModule.default.arduinoApp.build(BuildMode.Upload, true);
});
} catch (ex) {
}
Expand Down

0 comments on commit 2a2db50

Please sign in to comment.