feat: add local build console

This commit is contained in:
que01 committed 2026-09-22 14:15:32 +08:00
1 parent 4ec4372b35
commit 3760650c70
9 files changed
+72 -43

No files matched your search

+3 -1
View File
@@ -4,4 +4,6 @@ outputs/
__pycache__/
assets/models/speedtree/
assets/models/lyrog/
inputs/osm
inputs/osm
inputs/console/
.build-console/
-32
View File
@@ -1,32 +0,0 @@
{
"id": "hanyang-block",
"input": "/Users/que01/Desktop/汉阳区区块.osm",
"outputRoot": "/Users/que01/osm2streets-qgis-workflow/outputs",
"qgisApp": "/Applications/QGIS.app",
"blenderApp": "/Applications/Blender.app",
"stages": {
"intermediates": false,
"blender": true,
"cesium": true
},
"qgis": {
"arrowScale": 0.8,
"clipPad": 0.002,
"canvasPad": 0.001,
"previewPad": 0.0007,
"canvasExtent": null,
"previewExtent": null,
"layerPrefix": "osm2streets"
},
"osm2streets": {
"debug_each_step": false,
"dual_carriageway_experiment": false,
"sidepath_zipping_experiment": false,
"inferred_sidewalks": true,
"osm2lanes": true
},
"blender": {
"treeStyle": "natural",
"officeOverrides": ""
}
}
+16
View File
@@ -0,0 +1,16 @@
{
"id": "osm-g5Yjf1",
"input": "/Users/que01/Desktop/南台子湖创新谷OSM.osm",
"nativeRoadPackage": "/tmp/osm-g5Yjf1.native-road.zip",
"outputRoot": "/Users/que01/osm2streets-qgis-workflow/outputs",
"stages": {
"intermediates": false,
"blender": true,
"cesium": true
},
"blender": {
"treeStyle": "natural",
"officeOverrides": "",
"roadProvider": "native"
}
}
+1
View File
@@ -5,6 +5,7 @@
"type": "commonjs",
"scripts": {
"build": "node scripts/interactive-build.js",
"console": "node scripts/build-console.js",
"build:area": "node scripts/build-area.js",
"build:qgis": "node scripts/build-osm2streets-qgis.js",
"check:area": "node scripts/check-area.js",
+7 -4
View File
@@ -16,6 +16,7 @@ const {
} = require("./lib/scene-layers");
const { digest: glbDigest } = require("./glb-digest");
const { buildVehicleRoute: buildPreviewVehicleRoute } = require("./lib/vehicle-route");
const { buildNativeTrafficSimulation } = require("./lib/native-preview-traffic-simulation");
const { writePreviewVehicleLibrary, writePreviewV2xVehicleLibrary } = require("./lib/vehicle-library");
const { readTrafficSignals } = require("./lib/traffic-signals");
const {
@@ -568,10 +569,12 @@ function writeCesiumPreview(area, roadProvider) {
intersectionSurface: fileRecord(intersectionSurface),
});
} else {
Object.assign(previewInputs, nativeRoadRecords(importNativeRoadPackage(area)));
// This operational preview intentionally has no synthetic vehicle source.
// A stale descriptor from an older build must not resurrect simulated routes.
fs.rmSync(area.outputs.trafficSimulation, { force: true });
const nativePackage = importNativeRoadPackage(area);
Object.assign(previewInputs, nativeRoadRecords(nativePackage));
const simulation = buildNativeTrafficSimulation(area, { nativePackage });
fs.mkdirSync(path.dirname(area.outputs.trafficSimulation), { recursive: true });
fs.writeFileSync(area.outputs.trafficSimulation, `${JSON.stringify(simulation, null, 2)}\n`);
routeArtifact = area.outputs.trafficSimulation;
}
const htmlPath = area.outputs.cesiumPreview;
const started = Date.now();
+25
View File
@@ -0,0 +1,25 @@
#!/usr/bin/env node
"use strict";
const http = require("http"), fs = require("fs"), path = require("path"), crypto = require("crypto"), child = require("child_process");
const { unzipSync } = require("fflate");
const { readAreaConfig } = require("./lib/area-config");
const { STAGES } = require("./lib/build-stages");
const root = path.resolve(__dirname, ".."), configDir = path.join(root, "config", "areas"), managedRoot = path.join(root, "inputs", "console"), stateRoot = path.join(root, ".build-console", "areas"), uiFile = path.join(__dirname, "console", "index.html");
let active = null;
const send = (res, status, data) => { res.writeHead(status, { "content-type": "application/json; charset=utf-8", "cache-control": "no-store" }); res.end(JSON.stringify(data)); };
const safeKey = value => { const key = String(value || "").toLowerCase().replace(/[^a-z0-9_-]+/g, "-").replace(/^-+|-+$/g, ""); if (!key) throw Error("请输入只含字母、数字、- 或 _ 的配置名称"); return key; };
const under = (file, base) => { const resolved = path.resolve(file), parent = path.resolve(base); return resolved === parent || resolved.startsWith(`${parent}${path.sep}`); };
function areas() { return fs.readdirSync(configDir).filter(x => x.endsWith(".json")).sort().map(file => { const config = path.join(configDir, file); try { const a = readAreaConfig(config, { repoRoot: root }); const preview = a.outputs.cesiumPreview, manifest = a.outputs.packageManifest; return { key:path.basename(file,".json"), id:a.id, config, input:a.input, zip:a.nativeRoadPackage, treeStyle:a.blender.treeStyle, output:a.outputs.areaDir, preview:fs.existsSync(preview) ? `/artifacts/${path.basename(file,".json")}/${path.basename(preview)}` : null, manifest:fs.existsSync(manifest) ? `/artifacts/${path.basename(file,".json")}/package/manifest.json` : null }; } catch (e) { return { key:path.basename(file,".json"), error:e.message }; } }); }
function inspectZip(bytes) { let files; try { files = unzipSync(bytes); } catch (_) { throw Error("道路 ZIP 无法解压"); } if (!files["manifest.json"]) throw Error("道路 ZIP 缺少 manifest.json"); let manifest; try { manifest=JSON.parse(Buffer.from(files["manifest.json"]).toString("utf8")); } catch (_) { throw Error("道路 ZIP manifest.json 无效"); } if (manifest.contract !== "native-road-package/v1.1" || !manifest.areaId) throw Error("道路 ZIP 不是受支持的 native-road-package/v1.1"); return { areaId:manifest.areaId, contract:manifest.contract }; }
function body(req) { return new Promise((resolve,reject) => { let data=""; req.on("data", c=>{ data+=c; if(data.length>100*1024*1024) reject(Error("上传文件过大")); }); req.on("end",()=>{ try { resolve(JSON.parse(data||"{}")); } catch (_) { reject(Error("请求 JSON 无效")); } }); }); }
function trash(target) { if (!fs.existsSync(target)) return; if (!under(target, root)) throw Error("拒绝清理项目外文件"); child.execFileSync("osascript", ["-e", `tell application \"Finder\" to delete POSIX file ${JSON.stringify(target)}`]); }
function serve(req,res) { const url = new URL(req.url, "http://127.0.0.1"); if (req.method==="GET" && url.pathname==="/") { res.writeHead(200,{"content-type":"text/html; charset=utf-8"}); return res.end(fs.readFileSync(uiFile)); }
if(req.method==="GET"&&url.pathname==="/api/health") return send(res,200,{node:process.version, blender:fs.existsSync("/Applications/Blender.app/Contents/MacOS/Blender"), areas:areas().length});
if(req.method==="GET"&&url.pathname==="/api/areas") return send(res,200,{areas:areas(),stages:STAGES,treeStyles:["natural","procedural","shapespark"]});
if(req.method==="POST"&&url.pathname==="/api/inspect") return body(req).then(x=>send(res,200,inspectZip(Buffer.from(x.zip,"base64")))).catch(e=>send(res,400,{error:e.message}));
if(req.method==="POST"&&url.pathname==="/api/import") return body(req).then(x=>{ const key=safeKey(x.key), osm=Buffer.from(x.osm,"base64"), zip=Buffer.from(x.zip,"base64"), info=inspectZip(zip), dir=path.join(managedRoot,key); if(fs.existsSync(path.join(configDir,`${key}.json`))) throw Error("该配置名称已存在;请另存为新名称"); fs.mkdirSync(dir,{recursive:true}); const osmFile=path.join(dir,"source.osm"), zipFile=path.join(dir,"roads.zip"); fs.writeFileSync(osmFile,osm); fs.writeFileSync(zipFile,zip); const out=path.join(root,"outputs",key), config={id:info.areaId,input:osmFile,nativeRoadPackage:zipFile,outputRoot:path.join(root,"outputs"),outputs:{areaDir:out,fileStem:key},blender:{roadProvider:"native",treeStyle:x.treeStyle||"natural"}}; const file=path.join(configDir,`${key}.json`); fs.writeFileSync(file,JSON.stringify(config,null,2)+"\n"); fs.mkdirSync(stateRoot,{recursive:true}); fs.writeFileSync(path.join(stateRoot,`${key}.json`),JSON.stringify({key,config:file,managed:dir,output:out},null,2)); send(res,201,{key,id:info.areaId}); }).catch(e=>send(res,400,{error:e.message}));
if(req.method==="POST"&&url.pathname==="/api/build") return body(req).then(x=>{ if(active&&!active.done) throw Error("已有构建正在运行"); const key=safeKey(x.key), file=path.join(configDir,`${key}.json`); if(!fs.existsSync(file)) throw Error("配置不存在"); const args=[path.join(__dirname,"build-area.js"),"--config",file]; if(Array.isArray(x.stages)&&x.stages.length) args.push("--stages",x.stages.join(",")); const proc=child.spawn(process.execPath,args,{cwd:root}); const job=active={id:crypto.randomUUID(),key,logs:[],done:false}; for(const s of [proc.stdout,proc.stderr]) s.on("data",b=>job.logs.push(b.toString())); proc.on("close",code=>{job.logs.push(`\n构建结束:${code===0?"成功":"失败"} (${code})\n`);job.done=true;job.code=code;}); send(res,202,{id:job.id}); }).catch(e=>send(res,409,{error:e.message}));
if(req.method==="GET"&&url.pathname==="/api/job") return send(res,200,active||{done:true});
if(req.method==="POST"&&url.pathname==="/api/cleanup") return body(req).then(x=>{ const keys=Array.isArray(x.keys)?x.keys:[]; const known=new Map(areas().map(a=>[a.key,a])); for(const raw of keys){const key=safeKey(raw), area=known.get(key), config=path.join(configDir,`${key}.json`); if(!area||!fs.existsSync(config)) throw Error(`区域不存在:${key}`); trash(area.output||path.join(root,"outputs",key)); const rec=path.join(stateRoot,`${key}.json`); if(x.mode==="remove"){ if(fs.existsSync(rec)){const r=JSON.parse(fs.readFileSync(rec));trash(r.managed);trash(rec);} trash(config); } } send(res,200,{ok:true}); }).catch(e=>send(res,400,{error:e.message}));
if(req.method==="GET"&&url.pathname.startsWith("/artifacts/")){const [, ,key,...rest]=url.pathname.split("/");const a=areas().find(x=>x.key===key);const f=path.resolve(a?.output||"",...rest);if(!a||!under(f,a.output)||!fs.existsSync(f))return send(res,404,{error:"未找到产物"});return fs.createReadStream(f).pipe(res);} send(res,404,{error:"Not found"}); }
http.createServer((q,s)=>serve(q,s)).listen(8788,"127.0.0.1",()=>{const u="http://127.0.0.1:8788/";console.log(`Build console: ${u}`);child.spawn("open",[u],{detached:true,stdio:"ignore"}).unref();});
+8
View File
@@ -0,0 +1,8 @@
<!doctype html><meta charset="utf-8"><title>OSM Asset Console</title><style>:root{--ink:#172033;--muted:#718096;--line:#dde3ed;--blue:#3867e8;--bg:#f3f6fb}*{box-sizing:border-box}body{font:15px/1.6 Inter,system-ui;margin:0 auto;max-width:1180px;padding:44px 30px 80px;background:var(--bg);color:var(--ink)}h1{font-size:42px;letter-spacing:-1.8px;margin:0}h1+p{max-width:660px;color:var(--muted);margin-top:6px;font-size:16px}section{background:white;border:1px solid var(--line);border-radius:18px;padding:24px;margin:16px 0;box-shadow:0 12px 28px #13244a0a}h2{font-size:18px;margin:0 0 14px}button,input,select{font:inherit;padding:10px 12px;border:1px solid var(--line);border-radius:9px;background:#fff;margin:3px}button{background:var(--blue);color:#fff;border-color:var(--blue);font-weight:650;cursor:pointer}button:hover{filter:brightness(.92)}#health{display:inline-block;padding:9px 14px;border-radius:999px;color:var(--blue);font-weight:650;background:#edf2ff}#clear{background:#526076;border-color:#526076}#remove{background:#c33b35;border-color:#c33b35}.card{display:grid;grid-template-columns:1fr auto;border-top:1px solid #edf0f5;padding:18px 4px;gap:8px}.card:first-child{border:0}.card a{color:var(--blue);font-weight:650;margin-right:12px}.card button{grid-column:2;grid-row:1/4;align-self:center}pre{margin:0;padding:18px;border-radius:12px;background:#111827;color:#d8e0ec;max-height:340px;overflow:auto}@media(max-width:640px){body{padding:28px 16px}.card{display:block}}</style><h1>OSM Asset Console</h1><p>从道路包到可交付场景的本地工作台。导入输入、构建资产、检查预览,全程保留可复现的项目记录。</p><section id="health">正在检查环境…</section><section><h2>新建区域</h2><input id="key" placeholder="配置名称,例如 lake-v2"><input id="osm" type="file" accept=".osm,.xml"><input id="zip" type="file" accept=".zip"><select id="tree"><option>natural</option><option>procedural</option><option>shapespark</option></select><button id="inspect">检查 ZIP</button><button id="import">导入并保存</button><span id="note"></span></section><section><h2>历史区域</h2><div id="areas"></div><button id="clear">清理选中构建产物</button><button id="remove">移除选中区域</button><p>清理仅影响控制台托管的输入/输出,并移入废纸篓。</p></section><section><h2>构建日志</h2><pre id="log">尚未启动构建</pre></section><script>
const $=id=>document.getElementById(id), api=(u,o={})=>fetch(u,{headers:{'content-type':'application/json'},...o}).then(async r=>{let x=await r.json();if(!r.ok)throw Error(x.error);return x}), b64=f=>new Promise((ok,no)=>{let r=new FileReader;r.onload=()=>ok(r.result.split(',')[1]);r.onerror=no;r.readAsDataURL(f)});let selected=[];
async function load(){let [h,d]=await Promise.all([api('/api/health'),api('/api/areas')]);$('health').textContent=`Node ${h.node} · Blender ${h.blender?'已就绪':'未找到'} · ${h.areas} 个历史配置`; $('areas').innerHTML=d.areas.map(a=>`<div class=card><label><input type=checkbox value="${a.key}"> <b>${a.key}</b></label> — ${a.id||a.error}<br>树木:${a.treeStyle||'-'}<br>${a.preview?`<a target=_blank href="${a.preview}">打开预览</a> `:''}${a.manifest?`<a target=_blank href="${a.manifest}">package manifest</a> `:''}<button data-build="${a.key}">构建</button></div>`).join('');document.querySelectorAll('[data-build]').forEach(x=>x.onclick=()=>build(x.dataset.build));document.querySelectorAll('#areas input').forEach(x=>x.onchange=()=>selected=[...document.querySelectorAll('#areas input:checked')].map(x=>x.value));}
async function build(key){try{let r=await api('/api/build',{method:'POST',body:JSON.stringify({key})});$('note').textContent=`正在构建 ${key}…`;let t=setInterval(async()=>{let j=await api('/api/job');$('log').textContent=j.logs.join('');if(j.done){clearInterval(t);$('note').textContent=j.code===0?'构建完成,可打开预览':'构建失败,请查看日志';load()}},700)}catch(e){$('note').textContent=`无法启动构建:${e.message}`}}
$('inspect').onclick=async()=>{try{$('note').textContent='ZIP: '+JSON.stringify(await api('/api/inspect',{method:'POST',body:JSON.stringify({zip:await b64($('zip').files[0])})}))}catch(e){$('note').textContent=e.message}};
$('import').onclick=async()=>{try{let r=await api('/api/import',{method:'POST',body:JSON.stringify({key:$('key').value,osm:await b64($('osm').files[0]),zip:await b64($('zip').files[0]),treeStyle:$('tree').value})});$('note').textContent=`已保存 ${r.key}`;load()}catch(e){$('note').textContent=e.message}};
for(let [id,mode] of [['clear','outputs'],['remove','remove']])$(id).onclick=async()=>{if(!selected.length){$('note').textContent='请先勾选一个或多个区域';return}if(confirm(`将 ${selected.join(', ')} 移入废纸篓?`)){try{await api('/api/cleanup',{method:'POST',body:JSON.stringify({keys:selected,mode})});selected=[];$('note').textContent='已移入废纸篓';load()}catch(e){$('note').textContent=`清理失败:${e.message}`}}};load();
</script>
+9 -3
View File
@@ -49,14 +49,20 @@
const descriptor = config.previewDescriptorName ? await fetchOptionalJson(config.previewDescriptorName) : null;
adaptPackageManifest(metadata, descriptor);
const signalData = await fetchOptionalJson(config.trafficSignalsName);
const routeData = await fetchOptionalJson(config.routeName);
const placement = scenePlacement(metadata);
const viewer = createViewer();
setLoadingMessage("Loading model", config.glbName || "");
const assets = await loadSceneAssets(viewer, metadata, placement);
const trafficStart = Cesium.JulianDate.now();
const trafficSignals = createLiveTrafficSignals(signalData, assets);
hideUnconfirmedSignalAssets(assets);
const cruise = createLiveVehicleState();
const useLiveV2x = Boolean(config.v2xPreview?.enabled);
const trafficSignals = useLiveV2x
? createLiveTrafficSignals(signalData, assets)
: addTrafficSignals(viewer, signalData, trafficStart, assets);
if (useLiveV2x) hideUnconfirmedSignalAssets(assets);
const cruise = useLiveV2x
? createLiveVehicleState()
: addVehicleCruises(viewer, routeData, signalData, trafficStart, config.vehicleModelNames, config.vehicleModelName);
const cameras = createCameraPresets(viewer, metadata, placement, cruise);
const v2xOverlay = typeof window.createV2xCesiumOverlay === "function"
? window.createV2xCesiumOverlay({
+3 -3
View File
@@ -281,7 +281,7 @@ const previewRuntime = fs.readFileSync(path.join(__dirname, "lib", "cesium-previ
const v2xRuntime = fs.readFileSync(path.join(__dirname, "lib", "v2x-cesium-overlay.js"), "utf8");
const buildAreaSource = fs.readFileSync(path.join(__dirname, "build-area.js"), "utf8");
assert.match(buildAreaSource, /const vehicleModelNames = writeVehicleModel\(area\);/);
assert.doesNotMatch(buildAreaSource, /buildNativeTrafficSimulation/);
assert.match(buildAreaSource, /buildNativeTrafficSimulation/);
const countdownFont = path.join(__dirname, "..", "assets", "fonts", "7LED-1.ttf");
assert.ok(fs.existsSync(countdownFont), "7LED countdown font must be versioned with the project");
assert.doesNotMatch(previewRuntime, /cylinder: \{ length: 6\.7/);
@@ -297,7 +297,7 @@ assert.match(v2xRuntime, /\/network\/ws\/network\/targetPosition/);
assert.match(v2xRuntime, /headingPitchRollQuaternion/);
assert.match(v2xRuntime, /model:/);
assert.match(previewRuntime, /createLiveTrafficSignals\(/);
assert.doesNotMatch(previewRuntime, /const trafficSignals = addTrafficSignals\(/);
assert.match(previewRuntime, /addTrafficSignals\(viewer, signalData, trafficStart, assets\)/);
// The lamp dictionary lives only in the overlay, which hands the preview
// already-normalized {nodeKeys, color, countDown} entries. Two copies of the
@@ -342,7 +342,7 @@ assert.match(v2xRuntime, /Finding the configured V2X intersection/);
assert.match(v2xRuntime, /crossDeviceConfig\/queryList/);
assert.match(v2xRuntime, /gcj02ToWgs84/);
assert.match(previewRuntime, /createLiveVehicleState\(\)/);
assert.doesNotMatch(previewRuntime, /const cruise = addVehicleCruises\(/);
assert.match(previewRuntime, /addVehicleCruises\(viewer, routeData, signalData, trafficStart/);
assert.match(previewRuntime, /new Cesium\.ScreenSpaceEventHandler/);
assert.match(previewRuntime, /vehicleId: record\.id/);
assert.match(previewRuntime, /status === "breakdown"\s+\? "vehicle-breakdown\.png"/);