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

Commit

Permalink
Reworked util.spawn to mergable version
Browse files Browse the repository at this point in the history
  • Loading branch information
elektronikworkshop authored and adiazulay committed Jan 19, 2021
1 parent d408686 commit 4202508
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 62 deletions.
70 changes: 30 additions & 40 deletions src/arduino/arduino.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,6 @@ export class ArduinoApp {
UsbDetector.getInstance().pauseListening();
}

let success = false;

// Push sketch as last argument
args.push(path.join(ArduinoWorkspace.rootPath, dc.sketch));

Expand All @@ -378,25 +376,14 @@ export class ArduinoApp {
}
}

// TODO: Get rid of spawn's channel parameter and just support
// stdout and stderr callbacks
const stdoutCallback = (line: string) => {
if (cocopa) {
cocopa.callback(line);
if (verbose) {
arduinoChannel.channel.append(line);
}
} else {
arduinoChannel.channel.append(line);
}
}

await util.spawn(
return await util.spawn(
this._settings.commandPath,
arduinoChannel.channel,
args,
undefined,
stdoutCallback,
{
channel: !cocopa || cocopa && verbose ? arduinoChannel.channel : undefined,
stdout: cocopa ? cocopa.callback : undefined,
},
).then(async () => {
await cleanup();
if (buildMode !== BuildMode.Analyze) {
Expand All @@ -406,18 +393,15 @@ export class ArduinoApp {
arduinoChannel.info(`To rebuild your IntelliSense configuration run "${cmd}"`);
}
arduinoChannel.end(`${buildMode} sketch '${dc.sketch}'${os.EOL}`);
success = true;
return true;
}, async (reason) => {
await cleanup();
const msg = reason.code
? `Exit with code=${reason.code}`
: reason.message
? reason.message
: JSON.stringify(reason);
: JSON.stringify(reason);
arduinoChannel.error(`${buildMode} sketch '${dc.sketch}': ${msg}${os.EOL}`);
return false;
});

return success;
}

// Include the *.h header files from selected library to the arduino sketch.
Expand Down Expand Up @@ -481,14 +465,17 @@ export class ArduinoApp {
}
arduinoChannel.info(`${packageName}${arch && ":" + arch}${version && ":" + version}`);
try {
this.useArduinoCli() ?
if (this.useArduinoCli()) {
await util.spawn(this._settings.commandPath,
showOutput ? arduinoChannel.channel : null,
["core", "install", `${packageName}${arch && ":" + arch}${version && "@" + version}`]) :
["core", "install", `${packageName}${arch && ":" + arch}${version && "@" + version}`],
undefined,
{ channel: showOutput ? arduinoChannel.channel : null });
} else {
await util.spawn(this._settings.commandPath,
showOutput ? arduinoChannel.channel : null,
["--install-boards", `${packageName}${arch && ":" + arch}${version && ":" + version}`]);

["--install-boards", `${packageName}${arch && ":" + arch}${version && ":" + version}`],
undefined,
{ channel: showOutput ? arduinoChannel.channel : null });
}
if (updatingIndex) {
arduinoChannel.end("Updated package index files.");
} else {
Expand Down Expand Up @@ -530,14 +517,17 @@ export class ArduinoApp {
arduinoChannel.start(`Install library - ${libName}`);
}
try {
this.useArduinoCli() ?
await util.spawn(this._settings.commandPath,
showOutput ? arduinoChannel.channel : null,
["lib", "install", `${libName}${version && "@" + version}`]) :
await util.spawn(this._settings.commandPath,
showOutput ? arduinoChannel.channel : null,
["--install-library", `${libName}${version && ":" + version}`]);

if (this.useArduinoCli()) {
await util.spawn(this._settings.commandPath,
["lib", "install", `${libName}${version && "@" + version}`],
undefined,
{ channel: showOutput ? arduinoChannel.channel : undefined });
} else {
await util.spawn(this._settings.commandPath,
["--install-library", `${libName}${version && ":" + version}`],
undefined,
{ channel: showOutput ? arduinoChannel.channel : undefined });
}
if (updatingIndex) {
arduinoChannel.end("Updated library index files.");
} else {
Expand Down Expand Up @@ -671,9 +661,9 @@ export class ArduinoApp {
const cmd = args.shift();
try {
await util.spawn(cmd,
arduinoChannel.channel,
args,
{ shell: true, cwd: ArduinoWorkspace.rootPath });
{ shell: true, cwd: ArduinoWorkspace.rootPath },
{ channel: arduinoChannel.channel });
} catch (ex) {
arduinoChannel.error(`Running pre-build command failed: ${os.EOL}${ex.error}`);
return false;
Expand Down
51 changes: 29 additions & 22 deletions src/common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,6 @@ export function isArduinoFile(filePath): boolean {
return fileExistsSync(filePath) && (path.extname(filePath) === ".ino" || path.extname(filePath) === ".pde");
}

// TODO: remove output channel and just provide callback hooks for stdout and
// stderr
/**
* Send a command to arduino
* @param {string} command - base command path (either Arduino IDE or CLI)
Expand All @@ -197,14 +195,13 @@ export function isArduinoFile(filePath): boolean {
*/
export function spawn(
command: string,
outputChannel: vscode.OutputChannel,
args: string[] = [],
options: any = {},
stdoutCallback?: (s: string) => void,
output?: {channel?: vscode.OutputChannel,
stdout?: (s: string) => void,
stderr?: (s: string) => void},
): Thenable<object> {
return new Promise((resolve, reject) => {
const stdout = "";
const stderr = "";
options.cwd = options.cwd || path.resolve(path.join(__dirname, ".."));
const child = childProcess.spawn(command, args, options);

Expand All @@ -220,27 +217,37 @@ export function spawn(
}
}

if (outputChannel) {
child.stdout.on("data", (data: Buffer) => {
const decoded = decodeData(data, codepage);
if (stdoutCallback) {
stdoutCallback(decoded);
} else {
outputChannel.append(decoded);
}
});
child.stderr.on("data", (data: Buffer) => {
outputChannel.append(decodeData(data, codepage));
});
if (output) {
if (output.channel || output.stdout) {
child.stdout.on("data", (data: Buffer) => {
const decoded = decodeData(data, codepage);
if (output.stdout) {
output.stdout(decoded);
}
if (output.channel) {
output.channel.append(decoded);
}
});
}
if (output.channel || output.stderr) {
child.stderr.on("data", (data: Buffer) => {
const decoded = decodeData(data, codepage);
if (output.stderr) {
output.stderr(decoded);
}
if (output.channel) {
output.channel.append(decoded);
}
});
}
}

child.on("error", (error) => reject({ error, stderr, stdout }));

child.on("error", (error) => reject({ error }));
child.on("exit", (code) => {
if (code === 0) {
resolve({ code, stdout, stderr });
resolve({ code });
} else {
reject({ code, stdout, stderr });
reject({ code });
}
});
});
Expand Down

0 comments on commit 4202508

Please sign in to comment.