Preliminary refactoring of cargo.ts

This commit is contained in:
veetaha 2020-05-06 01:22:02 +03:00 committed by Craig Disselkoen
parent 3e603a8fdd
commit a78dd06951
2 changed files with 19 additions and 33 deletions

View file

@ -10,17 +10,9 @@ interface CompilationArtifact {
} }
export class Cargo { export class Cargo {
rootFolder: string; constructor(readonly rootFolder: string, readonly output: OutputChannel) { }
env?: Record<string, string>;
output: OutputChannel;
public constructor(cargoTomlFolder: string, output: OutputChannel, env: Record<string, string> | undefined = undefined) { private async artifactsFromArgs(cargoArgs: string[]): Promise<CompilationArtifact[]> {
this.rootFolder = cargoTomlFolder;
this.output = output;
this.env = env;
}
public async artifactsFromArgs(cargoArgs: string[]): Promise<CompilationArtifact[]> {
const artifacts: CompilationArtifact[] = []; const artifacts: CompilationArtifact[] = [];
try { try {
@ -37,17 +29,13 @@ export class Cargo {
isTest: message.profile.test isTest: message.profile.test
}); });
} }
} } else if (message.reason === 'compiler-message') {
else if (message.reason === 'compiler-message') {
this.output.append(message.message.rendered); this.output.append(message.message.rendered);
} }
}, },
stderr => { stderr => this.output.append(stderr),
this.output.append(stderr);
}
); );
} } catch (err) {
catch (err) {
this.output.show(true); this.output.show(true);
throw new Error(`Cargo invocation has failed: ${err}`); throw new Error(`Cargo invocation has failed: ${err}`);
} }
@ -55,9 +43,8 @@ export class Cargo {
return artifacts; return artifacts;
} }
public async executableFromArgs(args: string[]): Promise<string> { async executableFromArgs(args: readonly string[]): Promise<string> {
const cargoArgs = [...args]; // to remain args unchanged const cargoArgs = [...args, "--message-format=json"];
cargoArgs.push("--message-format=json");
const artifacts = await this.artifactsFromArgs(cargoArgs); const artifacts = await this.artifactsFromArgs(cargoArgs);
@ -70,24 +57,20 @@ export class Cargo {
return artifacts[0].fileName; return artifacts[0].fileName;
} }
runCargo( private runCargo(
cargoArgs: string[], cargoArgs: string[],
onStdoutJson: (obj: any) => void, onStdoutJson: (obj: any) => void,
onStderrString: (data: string) => void onStderrString: (data: string) => void
): Promise<number> { ): Promise<number> {
return new Promise<number>((resolve, reject) => { return new Promise((resolve, reject) => {
const cargo = cp.spawn('cargo', cargoArgs, { const cargo = cp.spawn('cargo', cargoArgs, {
stdio: ['ignore', 'pipe', 'pipe'], stdio: ['ignore', 'pipe', 'pipe'],
cwd: this.rootFolder, cwd: this.rootFolder
env: this.env,
}); });
cargo.on('error', err => { cargo.on('error', err => reject(new Error(`could not launch cargo: ${err}`)));
reject(new Error(`could not launch cargo: ${err}`));
}); cargo.stderr.on('data', chunk => onStderrString(chunk.toString()));
cargo.stderr.on('data', chunk => {
onStderrString(chunk.toString());
});
const rl = readline.createInterface({ input: cargo.stdout }); const rl = readline.createInterface({ input: cargo.stdout });
rl.on('line', line => { rl.on('line', line => {
@ -103,4 +86,4 @@ export class Cargo {
}); });
}); });
} }
} }

View file

@ -119,8 +119,11 @@ export function debugSingle(ctx: Ctx): Cmd {
} }
if (!debugEngine) { if (!debugEngine) {
vscode.window.showErrorMessage(`Install [CodeLLDB](https://marketplace.visualstudio.com/items?itemName=${lldbId})` vscode.window.showErrorMessage(
+ ` or [MS C++ tools](https://marketplace.visualstudio.com/items?itemName=${cpptoolsId}) extension for debugging.`); `Install [CodeLLDB](https://marketplace.visualstudio.com/items?itemName=${lldbId}) ` +
`or [MS C++ tools](https://marketplace.visualstudio.com/items?itemName=${cpptoolsId}) ` +
`extension for debugging.`
);
return; return;
} }