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

Commit 216e1b8

Browse files
hlovdaladiazulay
authored andcommitted
Merge build and verify
1 parent 2a2db50 commit 216e1b8

File tree

3 files changed

+11
-214
lines changed

3 files changed

+11
-214
lines changed

src/arduino/arduino.ts

+9-212
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,15 @@ export class ArduinoApp {
102102
}
103103

104104
/**
105-
* Upload code to selected board
106-
* @param {BuildMode} buildMode Build mode.
107-
* * BuildMode.Upload: Compile and upload
108-
* * BuildMode.UploadProgrammer: Compile and upload using the user
109-
* selectable programmer
110-
* @param {bool} [compile=true] - Indicates whether to compile the code when using the CLI to upload
105+
* Runs the arduino builder to build/compile and - if necessary - upload
106+
* the current sketch.
107+
* @param buildMode Build mode. BuildMode.Upload: Compile and upload,
108+
* BuildMode.UploadProgrammer: Compile and upload using the user selectable
109+
* programmer, BuildMode.Analyze: Compile, analyze the output and generate
110+
* IntelliSense configuration from it, BuildMode.Verify: Just compile.
111+
* @param {bool} compile - Indicates whether to compile the code when using the CLI to upload
112+
* @param buildDir Override the build directory set by the project settings
113+
* with the given directory.
111114
*/
112115
public async build(buildMode: BuildMode, compile: boolean, buildDir?: string): Promise<boolean> {
113116
const dc = DeviceContext.getInstance();
@@ -314,212 +317,6 @@ export class ArduinoApp {
314317
return success;
315318
}
316319

317-
public async verify(buildMode: BuildMode, buildDir?: string): Promise<boolean> {
318-
const compile = true;
319-
const dc = DeviceContext.getInstance();
320-
const args: string[] = [];
321-
let restoreSerialMonitor: boolean = false;
322-
let cocopa: ICoCoPaContext;
323-
324-
const boardDescriptor = this.getBoardBuildString();
325-
if (!boardDescriptor) {
326-
return false;
327-
}
328-
if (!this.useArduinoCli()) {
329-
args.push("--board", boardDescriptor);
330-
}
331-
332-
if (!ArduinoWorkspace.rootPath) {
333-
vscode.window.showWarningMessage("Cannot find the sketch file.");
334-
return false;
335-
}
336-
337-
if (!dc.sketch || !util.fileExistsSync(path.join(ArduinoWorkspace.rootPath, dc.sketch))) {
338-
await this.getMainSketch(dc);
339-
}
340-
341-
const selectSerial = async () => {
342-
const choice = await vscode.window.showInformationMessage(
343-
"Serial port is not specified. Do you want to select a serial port for uploading?",
344-
"Yes", "No");
345-
if (choice === "Yes") {
346-
vscode.commands.executeCommand("arduino.selectSerialPort");
347-
}
348-
}
349-
350-
if (buildMode === BuildMode.Upload) {
351-
if ((!dc.configuration || !/upload_method=[^=,]*st[^,]*link/i.test(dc.configuration)) && !dc.port) {
352-
await selectSerial();
353-
return false;
354-
}
355-
356-
if (!compile && !this.useArduinoCli()) {
357-
arduinoChannel.error("This command is only available when using the Arduino CLI");
358-
return;
359-
}
360-
361-
if (!this.useArduinoCli()) {
362-
args.push("--upload");
363-
} else {
364-
// TODO: add the --clean argument to the cli args when v 0.14 is released (this will clean up the build folder after uploading)
365-
if (compile) {
366-
args.push("compile", "--upload");
367-
} else {
368-
args.push("upload");
369-
}
370-
args.push("-b", boardDescriptor);
371-
}
372-
373-
if (dc.port) {
374-
args.push("--port", dc.port);
375-
}
376-
} else if (buildMode === BuildMode.UploadProgrammer) {
377-
const programmer = this.getProgrammerString();
378-
if (!programmer) {
379-
return false;
380-
}
381-
if (!dc.port) {
382-
await selectSerial();
383-
return false;
384-
}
385-
386-
if (!compile && !this.useArduinoCli()) {
387-
arduinoChannel.error("This command is only available when using the Arduino CLI");
388-
return;
389-
}
390-
391-
if (!this.useArduinoCli()) {
392-
args.push("--upload");
393-
} else {
394-
// TODO: add the --clean argument to the cli args when v 0.14 is released (this will clean up the build folder after uploading)
395-
if (compile) {
396-
args.push("compile", "--upload");
397-
} else {
398-
args.push("upload");
399-
}
400-
args.push("-b", boardDescriptor);
401-
}
402-
403-
if (this.useArduinoCli()) {
404-
args.push("--programmer", programmer)
405-
} else {
406-
args.push("--useprogrammer", "--pref", "programmer=arduino:" + programmer)
407-
}
408-
409-
args.push("--port", dc.port);
410-
} else if (buildMode === BuildMode.Analyze) {
411-
if (!isCompilerParserEnabled()) {
412-
return false;
413-
}
414-
cocopa = makeCompilerParserContext(dc);
415-
if (!this.useArduinoCli()) {
416-
args.push("--verify", "--verbose");
417-
} else {
418-
args.push("compile", "--verbose", "-b", boardDescriptor);
419-
}
420-
} else {
421-
if (!this.useArduinoCli()) {
422-
args.push("--verify");
423-
} else {
424-
args.push("compile", "-b", boardDescriptor);
425-
}
426-
}
427-
428-
const verbose = VscodeSettings.getInstance().logLevel === "verbose";
429-
if (buildMode !== BuildMode.Analyze && verbose) {
430-
args.push("--verbose");
431-
}
432-
433-
await vscode.workspace.saveAll(false);
434-
435-
arduinoChannel.show();
436-
arduinoChannel.start(`${buildMode} sketch '${dc.sketch}'`);
437-
438-
if (!await this.runPreBuildCommand(dc)) {
439-
return false;
440-
}
441-
442-
if ((buildDir || dc.output) && compile) {
443-
const outputPath = path.resolve(ArduinoWorkspace.rootPath, buildDir || dc.output);
444-
const dirPath = path.dirname(outputPath);
445-
if (!util.directoryExistsSync(dirPath)) {
446-
logger.notifyUserError("InvalidOutPutPath", new Error(constants.messages.INVALID_OUTPUT_PATH + outputPath));
447-
return false;
448-
}
449-
450-
if (this.useArduinoCli()) {
451-
args.push("--build-path", outputPath);
452-
453-
} else {
454-
args.push("--pref", `build.path=${outputPath}`);
455-
}
456-
457-
arduinoChannel.info(`Please see the build logs in output path: ${outputPath}`);
458-
} else {
459-
const msg = "Output path is not specified. Unable to reuse previously compiled files. Build will be slower. See README.";
460-
arduinoChannel.warning(msg);
461-
}
462-
463-
// stop serial monitor when everything is prepared and good
464-
// what makes restoring of its previous state easier
465-
if (buildMode === BuildMode.Upload || buildMode === BuildMode.UploadProgrammer) {
466-
restoreSerialMonitor = await SerialMonitor.getInstance().closeSerialMonitor(dc.port);
467-
UsbDetector.getInstance().pauseListening();
468-
}
469-
470-
let success = false;
471-
472-
// Push sketch as last argument
473-
args.push(path.join(ArduinoWorkspace.rootPath, dc.sketch));
474-
475-
const cleanup = async () => {
476-
if (cocopa) {
477-
cocopa.conclude();
478-
}
479-
if (buildMode === BuildMode.Upload || buildMode === BuildMode.UploadProgrammer) {
480-
UsbDetector.getInstance().resumeListening();
481-
if (restoreSerialMonitor) {
482-
await SerialMonitor.getInstance().openSerialMonitor();
483-
}
484-
}
485-
}
486-
487-
// TODO: Get rid of spawn's channel parameter and just support
488-
// stdout and stderr callbacks
489-
const stdoutCallback = (line: string) => {
490-
if (cocopa) {
491-
cocopa.callback(line);
492-
if (verbose) {
493-
arduinoChannel.channel.append(line);
494-
}
495-
} else {
496-
arduinoChannel.channel.append(line);
497-
}
498-
}
499-
500-
await util.spawn(
501-
this._settings.commandPath,
502-
arduinoChannel.channel,
503-
args,
504-
undefined,
505-
stdoutCallback,
506-
).then(async () => {
507-
await cleanup();
508-
arduinoChannel.end(`${buildMode} sketch '${dc.sketch}'${os.EOL}`);
509-
success = true;
510-
}, async (reason) => {
511-
await cleanup();
512-
const msg = reason.code ?
513-
`Exit with code=${reason.code}` :
514-
reason.message ?
515-
reason.message :
516-
JSON.stringify(reason);
517-
arduinoChannel.error(`${buildMode} sketch '${dc.sketch}': ${msg}${os.EOL}`);
518-
});
519-
520-
return success;
521-
}
522-
523320
public tryToUpdateIncludePaths() {
524321
const configFilePath = path.join(ArduinoWorkspace.rootPath, constants.CPP_CONFIG_FILE);
525322
if (!fs.existsSync(configFilePath)) {

src/debug/configurationProvider.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export class ArduinoDebugConfigurationProvider implements vscode.DebugConfigurat
136136
config.program = path.join(ArduinoWorkspace.rootPath, outputFolder, `${path.basename(dc.sketch)}.elf`);
137137

138138
// always compile elf to make sure debug the right elf
139-
if (!await ArduinoContext.arduinoApp.verify(BuildMode.Verify, outputFolder)) {
139+
if (!await ArduinoContext.arduinoApp.build(BuildMode.Verify, true, outputFolder)) {
140140
vscode.window.showErrorMessage("Failure to verify the program, please check output for details.");
141141
return false;
142142
}

src/extension.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export async function activate(context: vscode.ExtensionContext) {
124124
location: vscode.ProgressLocation.Window,
125125
title: "Arduino: Verifying...",
126126
}, async () => {
127-
await arduinoContextModule.default.arduinoApp.verify(BuildMode.Verify);
127+
await arduinoContextModule.default.arduinoApp.build(BuildMode.Verify, true);
128128
});
129129
} catch (ex) {
130130
}

0 commit comments

Comments
 (0)