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; private envPrefix: string; constructor(rustDir: string, envPrefix: string = '') { this.rustDir = rustDir; this.envPrefix = envPrefix; this.shell = new plugins.smartshell.Smartshell({ executor: 'bash', }); } public async checkCargoInstalled(): Promise { const result = await this.shell.execSilent(`${this.envPrefix}cargo --version`); return result.exitCode === 0; } public async getCargoVersion(): Promise { const result = await this.shell.execSilent(`${this.envPrefix}cargo --version`); return result.stdout.trim(); } public async build(options: { debug?: boolean; clean?: boolean; target?: string } = {}): Promise { if (options.clean) { console.log('Cleaning previous build...'); await this.clean(); } if (options.target) { await this.ensureTarget(options.target); } const profile = options.debug ? '' : ' --release'; const targetFlag = options.target ? ` --target ${options.target}` : ''; const command = `${this.envPrefix}cd ${this.rustDir} && cargo build${profile}${targetFlag}`; console.log(`Running: cargo build${profile}${targetFlag}`); const result = await this.shell.exec(command); return { success: result.exitCode === 0, exitCode: result.exitCode, stdout: result.stdout, }; } /** * Ensures a rustup target is installed. If not present, installs it via `rustup target add`. */ public async ensureTarget(triple: string): Promise { const listResult = await this.shell.execSilent(`${this.envPrefix}rustup target list --installed`); const installedTargets = listResult.stdout.split('\n').map((l) => l.trim()); if (installedTargets.includes(triple)) { return; } console.log(`Installing rustup target: ${triple}`); const addResult = await this.shell.exec(`${this.envPrefix}rustup target add ${triple}`); if (addResult.exitCode !== 0) { throw new Error(`Failed to install rustup target ${triple}`); } } public async clean(): Promise { const command = `${this.envPrefix}cd ${this.rustDir} && cargo clean`; const result = await this.shell.exec(command); return { success: result.exitCode === 0, exitCode: result.exitCode, stdout: result.stdout, }; } }