add customizer

This commit is contained in:
ochafik 2024-07-08 02:21:33 +01:00
commit 76492bc903
10 changed files with 341 additions and 16 deletions

View file

@ -5,20 +5,23 @@ import { getParentDir } from '../fs/filesystem';
import { spawnOpenSCAD } from "./openscad-runner";
import { processMergedOutputs } from "./output-parser";
import { AbortablePromise, turnIntoDelayableExecution } from '../utils';
import { ParameterSet } from '../state/customizer-types';
const syntaxDelay = 300;
type SyntaxCheckOutput = {logText: string, markers: monaco.editor.IMarkerData[]};
type SyntaxCheckOutput = {logText: string, markers: monaco.editor.IMarkerData[], parameterSet?: ParameterSet};
export const checkSyntax =
turnIntoDelayableExecution(syntaxDelay, (source: string, sourcePath: string) => {
// const timestamp = Date.now();
source = '$preview=true;\n' + source;
const outFile = 'out.json';
const job = spawnOpenSCAD({
inputs: [[sourcePath, source + '\n']],
args: [sourcePath, "-o", "out.ast"],
args: [sourcePath, "-o", outFile, "--export-format=param"],
// workingDir: sourcePath.startsWith('/') ? getParentDir(sourcePath) : '/home'
outputPaths: [outFile],
});
return AbortablePromise<SyntaxCheckOutput>((res, rej) => {
@ -26,10 +29,28 @@ export const checkSyntax =
try {
const result = await job;
// console.log(result);
res(processMergedOutputs(result.mergedOutputs, {shiftSourceLines: {
sourcePath,
skipLines: 1,
}}));
let parameterSet: ParameterSet | undefined = undefined;
if (result.outputs && result.outputs.length == 1) {
let [[, content]] = result.outputs;
content = new TextDecoder().decode(content as any);
try {
parameterSet = JSON.parse(content)
// console.log('PARAMETER SET', JSON.stringify(parameterSet, null, 2))
} catch (e) {
console.error(`Error while parsing parameter set: ${e}\n${content}`);
}
} else {
console.error('No output from runner!');
}
res({
...processMergedOutputs(result.mergedOutputs, {shiftSourceLines: {
sourcePath,
skipLines: 1,
}}),
parameterSet,
});
} catch (e) {
console.error(e);
rej(e);
@ -45,12 +66,23 @@ export type RenderOutput = {stlFile: File, logText: string, markers: monaco.edit
export type RenderArgs = {
source: string,
sourcePath: string,
vars?: {[name: string]: any},
features?: string[],
extraArgs?: string[],
isPreview: boolean
}
function formatValue(any: any): string {
if (typeof any === 'string') {
return `"${any}"`;
} else if (any instanceof Array) {
return `[${any.map(formatValue).join(', ')}]`;
} else {
return `${any}`;
}
}
export const render =
turnIntoDelayableExecution(renderDelay, ({sourcePath, source, isPreview, features, extraArgs}: RenderArgs) => {
turnIntoDelayableExecution(renderDelay, ({sourcePath, source, isPreview, vars, features, extraArgs}: RenderArgs) => {
const prefixLines: string[] = [];
if (isPreview) {
@ -62,6 +94,7 @@ export const render =
sourcePath,
"-o", "out.stl",
"--export-format=binstl",
...(Object.entries(vars ?? {}).flatMap(([k, v]) => [`-D${k}=${formatValue(v)}`])),
...(features ?? []).map(f => `--enable=${f}`),
...(extraArgs ?? [])
]