Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | 9x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 11x 11x 11x 27x 38x 38x 38x 27x 27x 27x 27x 27x 9x 9x 27x 18x 27x 27x 27x 27x 27x 27x 7x 20x 1x 1x 1x 1x 19x 19x 19x 14x 5x | import { Logger } from "@shared/logger";
import { readFile, readdir } from "node:fs/promises";
import { dirname, join } from "node:path";
import { ProcessExecutor } from "../process-executor";
import type { CompilationError } from "./compiler-output-parser";
import { parseCompilerDiagnostics } from "../compiler-diagnostics";
const logger = new Logger("CLIRunner");
export interface CLICompileConfig {
fqbn: string;
buildPath?: string;
buildCachePath?: string;
}
export interface CLICompileResult {
success: boolean;
output: string;
errors?: string;
parsedErrors?: CompilationError[];
binary?: Buffer;
}
/**
* Builds arduino-cli compile arguments.
*/
export function buildCompileArgs(
config: CLICompileConfig,
sketchDir: string,
): string[] {
const args = [
"compile",
"--fqbn",
config.fqbn,
"--verbose",
];
Iif (config.buildPath) {
args.push("--build-path", config.buildPath);
}
args.push(sketchDir);
return args;
}
/**
* Executes arduino-cli compilation.
*/
export async function compileWithArduinoCli(
sketchFile: string,
config: CLICompileConfig,
processExecutor: ProcessExecutor,
): Promise<CLICompileResult> {
// Arduino CLI expects the sketch directory; the public compiler passes the .ino path.
const sketchDir = dirname(sketchFile);
const args = buildCompileArgs(config, sketchDir);
logger.info(`Executing arduino-cli ${args.join(" ")}`);
try {
const result = await processExecutor.execute("arduino-cli", args, {
timeout: 60000, // 60s timeout for compilation
stdio: "pipe",
});
// Check for spawn/execution errors
if (result.error) {
const errorMessage = `Failed to execute arduino-cli: ${result.error.message}. Make sure arduino-cli is installed and in PATH.`;
logger.error(errorMessage);
return {
success: false,
output: "",
errors: errorMessage,
parsedErrors: [{
file: "system",
line: 0,
column: 0,
type: "error",
message: errorMessage,
}],
};
}
const output = result.stdout || "";
const errors = result.stderr || "";
const code = result.code;
if (code === 0) {
const parsedOutput = parseCompilerOutput(output);
const binary = await discoverBuildBinary(config.buildPath || sketchDir);
return {
success: true,
output: parsedOutput,
errors: "",
parsedErrors: [],
binary,
};
} else E{
const cleanedErrors = cleanErrorMessage(errors, sketchFile);
const parsedErrors = parseCompilerDiagnostics(cleanedErrors, 0);
return {
success: false,
output: "",
errors: cleanedErrors,
parsedErrors,
};
}
} catch (error) {
const errorMessage = `Failed to execute arduino-cli: ${error instanceof Error ? error.message : String(error)}. Make sure arduino-cli is installed and in PATH.`;
logger.error(errorMessage);
return {
success: false,
output: "",
errors: errorMessage,
parsedErrors: [{
file: "system",
line: 0,
column: 0,
type: "error",
message: errorMessage,
}],
};
}
}
/**
* Cleans error messages by removing sketch directory paths.
*/
function cleanErrorMessage(errors: string, sketchDir: string): string {
let cleanedErrors = errors;
if (sketchDir) {
cleanedErrors = cleanedErrors.replaceAll(sketchDir, "sketch.ino");
}
return cleanedErrors;
}
/**
* Reads the HEX artifact emitted by arduino-cli from the build output directory.
*/
async function discoverBuildBinary(buildOutputDir: string): Promise<Buffer | undefined> {
try {
const hexCandidates = (await readdir(buildOutputDir))
.filter((entry) => entry.endsWith(".hex"))
.sort((a, b) => a.localeCompare(b));
const preferred = hexCandidates.find((entry) => !entry.includes("with_bootloader")) || hexCandidates[0];
Iif (preferred) {
return await readFile(join(buildOutputDir, preferred));
}
} catch (error) {
logger.debug(`[CompileCache] failed to read build hex output: ${error instanceof Error ? error.message : String(error)}`);
}
return undefined;
}
/**
* Parses arduino-cli output to extract memory usage and format it.
*/
function parseCompilerOutput(output: string): string {
const progSizeRegex = /(Sketch uses[^\n]*\.|Der Sketch verwendet[^\n]*\.)/;
const ramSizeRegex = /(Global variables use[^\n]*\.|Globale Variablen verwenden[^\n]*\.)/;
const progSizeMatch = progSizeRegex.exec(output);
const ramSizeMatch = ramSizeRegex.exec(output);
if (progSizeMatch && ramSizeMatch) {
return `${progSizeMatch[0]}\n${ramSizeMatch[0]}\n\nBoard: Arduino UNO`;
} else if (progSizeMatch || ramSizeMatch) {
// Partial memory info
const parts = [];
Eif (progSizeMatch) parts.push(progSizeMatch[0]);
Iif (ramSizeMatch) parts.push(ramSizeMatch[0]);
return `${parts.join("\n")}\n\nBoard: Arduino UNO`;
} else {
// No memory info - check if there's any success output
const trimmed = output.trim();
Eif (trimmed) {
// If output looks like a generic success message, use (Simulation) suffix
if (trimmed.toLowerCase().includes("success") || trimmed.toLowerCase().includes("done")) {
return `${trimmed}\n\nBoard: Arduino UNO (Simulation)`;
}
return `${trimmed}\n\nBoard: Arduino UNO`;
}
return `Board: Arduino UNO (Simulation)`;
}
}
|