import { execFileSync } from 'node:child_process'; import fs from 'node:fs'; import path from 'node:path'; const root = process.cwd(); const ids = JSON.parse(process.env.FREECAD_BUSINESS_IDS || '[]'); const groups = new Map(); const add = (script, selected) => { if (!selected.length) return; const output = execFileSync(process.execPath, [`tools/${script}`], { cwd: root, env: { ...process.env, FREECAD_BUSINESS_IDS: JSON.stringify(selected) }, encoding: 'utf8', }); const report = JSON.parse(output); for (const result of report.results || []) groups.set(result.capabilityId, result); } const support = { Assembly: new Set(['Assembly:assembly.inspectPythonApi', 'Assembly:assembly.inspectSourceInventory', 'Assembly:assembly.nativeTypeProbe', 'Assembly:assembly.pythonFamily']), BIM: new Set(['BIM:bim.inspectPythonApi', 'BIM:bim.inspectSourceInventory', 'BIM:bim.nativeProbe', 'BIM:bim.pythonFamily', 'BIM:bim.pythonFamilyBatch']), Mesh: new Set(['Mesh:mesh.inspectSourceInventory', 'Mesh:mesh.pythonApi', 'Mesh:mesh.pythonFamilyBatch']), Points: new Set(['Points:points.inspectSource', 'Points:points.pythonApi', 'Points:points.pythonFamilyBatch']), TemplatePyMod: new Set(['TemplatePyMod:templatepymod.inspectSourceInventory', 'TemplatePyMod:templatepymod.pythonApi', 'TemplatePyMod:templatepymod.pythonFamilyBatch']), Cloud: new Set(['Cloud:cloud.inspectSourceInventory', 'Cloud:cloud.inspectPythonApi', 'Cloud:cloud.pythonFamilyBatch']), Start: new Set(['Start:start.inspectSourceInventory', 'Start:start.pythonApi']), CAM: new Set(['CAM:cam.inspectPythonApi', 'CAM:cam.inspectSourceInventory', 'CAM:cam.pythonFamilyBatch']), Help: new Set(['Help:help.inspectSourceInventory', 'Help:help.pythonApi']), Plot: new Set(['Plot:plot.pythonApi', 'Plot:plot.pythonFamilyBatch']), Robot: new Set(['Robot:robot.inspectSourceInventory', 'Robot:robot.pythonApi', 'Robot:robot.pythonFamilyBatch']), Sandbox: new Set(['Sandbox:sandbox.inspectSourceInventory', 'Sandbox:sandbox.pythonApi', 'Sandbox:sandbox.pythonFamilyBatch']), Inspection: new Set(['Inspection:inspection.createRecompute', 'Inspection:inspection.inspectPythonApi', 'Inspection:inspection.inspectSourceInventory', 'Inspection:inspection.pythonFamilyBatch', 'Inspection:inspection.saveReopen']), Material: new Set(['Material:material.inspectSourceInventory', 'Material:material.pythonApi', 'Material:material.pythonFamilyBatch']), }; const moduleOf = id => id.split(':', 1)[0]; for (const module of Object.keys(support)) { const selected = ids.filter(id => support[module].has(id)); const scripts = { Assembly: 'assembly-business-cases.mjs', BIM: 'bim-supporting-business-cases.mjs', Mesh: 'mesh-supporting-business-cases.mjs', Points: 'points-supporting-business-cases.mjs', TemplatePyMod: 'templatepymod-supporting-business-cases.mjs', Cloud: 'cloud-supporting-business-cases.mjs', Start: 'start-supporting-business-cases.mjs', CAM: 'cam-supporting-business-cases.mjs', Help: 'help-supporting-business-cases.mjs', Inspection: 'inspection-supporting-business-cases.mjs', Material: 'material-supporting-business-cases.mjs', Plot: 'plot-supporting-business-cases.mjs', Robot: 'robot-supporting-business-cases.mjs', Sandbox: 'sandbox-supporting-business-cases.mjs', }; add(scripts[module], selected); } const aliasId = 'Start:Start:desktop:Start_Start'; const rest = ids.filter(id => !groups.has(id) && id !== aliasId); const restBy = new Map(); for (const id of rest) { const module = moduleOf(id); if (!restBy.has(module)) restBy.set(module, []); restBy.get(module).push(id); } for (const [module, selected] of restBy) { if (module === 'Fem' && selected.includes('Fem:fem.action')) { add('fem-action-business-cases.mjs', selected.filter(id => id === 'Fem:fem.action')); const remainingFem = selected.filter(id => id !== 'Fem:fem.action'); if (!remainingFem.length) continue; selected.splice(0, selected.length, ...remainingFem); } if (module === 'Fem') { add('fem-business-cases.mjs', selected); continue; } let script; if (module === 'Assembly') script = selected.includes('Assembly:desktop:Separator') ? 'assembly-desktop-separator-v2.mjs' : 'assembly-business-cases.mjs'; else if (module === 'BIM') script = 'bim-create-edit-business-cases.mjs'; else if (module === 'Mesh') script = 'mesh-desktop-command-business-cases.mjs'; else if (module === 'Points') script = selected.some(id => ['Points:points.createRecompute', 'Points:points.saveReopen', 'Points:points.desktopBatch', 'Points:desktop:Points_Merge', 'Points:desktop:Points_Structure', 'business-format:Points:export:.obj', 'business-format:Points:import:.ply', 'Points:desktop:Points_Import', 'Points:desktop:Points_Export'].includes(id)) ? 'points-business-cases.mjs' : 'points-document-business-cases.mjs'; else if (module === 'TemplatePyMod') script = 'templatepymod-checkable-business-cases.mjs'; else if (module === 'Cloud') script = selected.includes('Cloud:cloud.restore') ? 'cloud-restore-business-cases-v2.mjs' : 'cloud-save-business-cases.mjs'; else if (module === 'Draft') script = selected.includes('Draft:draft.action') ? 'draft-action-business-cases.mjs' : 'draft-business-cases.mjs'; else if (module === 'JtReader') script = 'jtreader-business-cases.mjs'; else if (module === 'Start') script = 'start-business-cases.mjs'; else if (module === 'CAM') script = 'cam-business-cases.mjs'; else if (module === 'Material') script = 'material-business-cases.mjs'; else if (module === 'Robot') script = 'robot-business-cases.mjs'; else if (module === 'Sandbox') script = 'sandbox-business-cases.mjs'; else if (module === 'Fem') script = selected.includes('Fem:fem.action') ? 'fem-action-business-cases.mjs' : 'fem-desktop-command-business-cases.mjs'; else { continue; } add(script, selected); } // The duplicated Start requirement is an alias of the canonical matrix ID. if (ids.includes('Start:Start:desktop:Start_Start')) { const matrix = JSON.parse(fs.readFileSync(path.join(root, 'docs/p6/workbench-command-matrix.json'), 'utf8')); const find = value => { if (value && typeof value === 'object') { if (value.id === 'Start:desktop:Start_Start') return value; for (const child of Object.values(value)) { const found = find(child); if (found) return found; } } }; const actual = find(matrix); groups.set('Start:Start:desktop:Start_Start', { capabilityId: 'Start:Start:desktop:Start_Start', status: actual?.disposition === 'restricted' ? 'passed' : 'failed', cases: [{ input: { evidence: 'docs/p6/workbench-command-matrix.json', canonicalId: 'Start:desktop:Start_Start' }, actual, assertions: [{ name: 'Start alias resolves to the canonical restricted command boundary', passed: actual?.disposition === 'restricted' }] }], }); } const results = ids.map(id => groups.get(id) || { capabilityId: id, status: 'failed', cases: [], error: 'no route for selected business ID' }); process.stdout.write(JSON.stringify({ schema: 'freecad-web-business-cases/v1', results }) + '\n'); process.exitCode = results.length === ids.length && results.every(result => result.status === 'passed') ? 0 : 1;