Files
tsrust/ts/mod_cargo/classes.cargorunner.ts
2026-02-09 09:32:20 +00:00

60 lines
1.5 KiB
TypeScript

import * as plugins from '../plugins.js';
export interface ICargoRunResult {
success: boolean;
exitCode: number;
stdout: string;
}
export class CargoRunner {
private shell: plugins.smartshell.Smartshell;
private rustDir: string;
constructor(rustDir: string) {
this.rustDir = rustDir;
this.shell = new plugins.smartshell.Smartshell({
executor: 'bash',
});
}
public async checkCargoInstalled(): Promise<boolean> {
const result = await this.shell.execSilent('cargo --version');
return result.exitCode === 0;
}
public async getCargoVersion(): Promise<string> {
const result = await this.shell.execSilent('cargo --version');
return result.stdout.trim();
}
public async build(options: { debug?: boolean; clean?: boolean } = {}): Promise<ICargoRunResult> {
if (options.clean) {
console.log('Cleaning previous build...');
await this.clean();
}
const profile = options.debug ? '' : ' --release';
const command = `cd ${this.rustDir} && cargo build${profile}`;
console.log(`Running: cargo build${profile}`);
const result = await this.shell.exec(command);
return {
success: result.exitCode === 0,
exitCode: result.exitCode,
stdout: result.stdout,
};
}
public async clean(): Promise<ICargoRunResult> {
const command = `cd ${this.rustDir} && cargo clean`;
const result = await this.shell.exec(command);
return {
success: result.exitCode === 0,
exitCode: result.exitCode,
stdout: result.stdout,
};
}
}