Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

internal: Luban can say whether the generated files are behind the tables

yhh47d0e5e5991d
diff --git a/pipeline/src/tools/lubanRun.ts b/pipeline/src/tools/lubanRun.ts
index 5fa724d23..a1f4cb17c 100644
--- a/pipeline/src/tools/lubanRun.ts
+++ b/pipeline/src/tools/lubanRun.ts
@@ -111,6 +111,9 @@ export async function runLuban(options: LubanRunOptions): Promise<LubanRunResult
const finalCode = inside(root, s.codeDir);
const finalData = inside(root, s.dataDir);
+ // Taken before the run reads anything: a table saved while Luban works is newer than this, so it is seen next time
+ // instead of being counted as already generated.
+ const startedAt = Date.now();
const scratch = mkdtempSync(path.join(tmpdir(), 'estella-luban-'));
try {
const outCode = path.join(scratch, 'code');
@@ -151,9 +154,53 @@ export async function runLuban(options: LubanRunOptions): Promise<LubanRunResult
}
for (const rel of stale) rmSync(path.join(root, rel), { force: true });
mkdirSync(path.dirname(ledgerFile), { recursive: true });
- writeFileSync(ledgerFile, JSON.stringify({ note: 'Written by estella luban gen. Files listed here are replaced or removed by the next run.', files: made.map((m) => m.rel) }, null, 2) + '\n');
+ writeFileSync(ledgerFile, JSON.stringify({ note: 'Written by estella luban gen. Files listed here are replaced or removed by the next run.', generatedAt: startedAt, files: made.map((m) => m.rel) }, null, 2) + '\n');
return { ok: true, errors: [], changed: [...differing.map((m) => m.rel), ...stale], log: output };
} finally {
rmSync(scratch, { recursive: true, force: true, maxRetries: 10, retryDelay: 50 });
}
}
+
+export type Staleness = 'fresh' | 'stale' | 'never';
+
+/** Files an editor or Excel leaves beside a document while it is open: not something a designer wrote. */
+const SCRATCH = /^(~\$|\.~lock\.|\.#|.*\.(tmp|bak)$)/i;
+
+function newestInput(dir: string): number {
+ let newest = 0;
+ for (const entry of readdirSync(dir, { withFileTypes: true })) {
+ if (SCRATCH.test(entry.name)) continue;
+ const full = path.join(dir, entry.name);
+ newest = Math.max(newest, entry.isDirectory() ? newestInput(full) : statSync(full).mtimeMs);
+ }
+ return newest;
+}
+
+export interface StalenessReport {
+ state: Staleness;
+ /** When the newest file under the Luban folder was saved (ms), 0 when there is none. A caller that remembers a failed run by this can tell "still the same mistake" from "edited since". */
+ newest: number;
+}
+
+/**
+ * Do the project's generated files still say what its tables say? A look at modification times, no Luban run:
+ * cheap enough to ask every time a window is focused. `never` means no run has been recorded (or a file it wrote is
+ * gone); `stale` means something under the Luban folder was saved after the last run began. A run that changed
+ * nothing still counts as a run, so saving an Excel file without changing a value is answered once, not for ever.
+ */
+export function lubanStalenessReport(projectDir: string, setting: LubanSetting): StalenessReport {
+ const root = path.resolve(projectDir);
+ const confDir = path.dirname(inside(root, setting.conf));
+ const newest = existsSync(confDir) ? newestInput(confDir) : 0;
+ const ledgerFile = path.join(root, LEDGER);
+ if (!existsSync(ledgerFile) || !existsSync(confDir)) return { state: 'never', newest };
+ let ledger: { files?: string[]; generatedAt?: number };
+ try { ledger = JSON.parse(readFileSync(ledgerFile, 'utf8')); } catch { return { state: 'never', newest }; }
+ if (typeof ledger.generatedAt !== 'number' || !Array.isArray(ledger.files)) return { state: 'never', newest };
+ if (ledger.files.some((rel) => !existsSync(path.join(root, rel)))) return { state: 'never', newest };
+ return { state: newest > ledger.generatedAt ? 'stale' : 'fresh', newest };
+}
+
+export function lubanStaleness(projectDir: string, setting: LubanSetting): Staleness {
+ return lubanStalenessReport(projectDir, setting).state;
+}
diff --git a/pipeline/tests/luban-run.test.ts b/pipeline/tests/luban-run.test.ts
index 7a922f04c..b7a639c3d 100644
--- a/pipeline/tests/luban-run.test.ts
+++ b/pipeline/tests/luban-run.test.ts
@@ -149,3 +149,47 @@ describe.skipIf(!base || !existsSync(path.join(base, 'external-tools.json')))('L
expect(existsSync(path.join(root, 'assets/data/tbitem.json'))).toBe(true);
}, 120_000);
});
+
+describe('is the generated output behind the tables?', () => {
+ it('answers from modification times alone, and ignores what Excel leaves beside an open file', async () => {
+ const { lubanStaleness } = await import('../src/tools/lubanRun');
+ const { utimesSync } = await import('node:fs');
+ const root = mkdtempSync(path.join(tmpdir(), 'estella-stale-'));
+ try {
+ const setting: LubanSetting = { enabled: true, ...LUBAN_DEFAULTS };
+ const datas = path.join(root, 'config/luban/Datas');
+ mkdirSync(datas, { recursive: true });
+ writeFileSync(path.join(root, 'config/luban/luban.conf'), '{}');
+ writeFileSync(path.join(datas, 'items.xlsx'), 'x');
+ expect(lubanStaleness(root, setting)).toBe('never');
+
+ // A run is recorded: what it wrote, and when it began.
+ const out = path.join(root, 'src/gen/data/tbitem.json');
+ mkdirSync(path.dirname(out), { recursive: true });
+ writeFileSync(out, '[]');
+ mkdirSync(path.join(root, '.esengine'), { recursive: true });
+ const begun = Date.now();
+ writeFileSync(path.join(root, '.esengine/luban-generated.json'), JSON.stringify({ generatedAt: begun, files: ['src/gen/data/tbitem.json'] }));
+ const at = (file: string, ms: number) => utimesSync(file, new Date(ms), new Date(ms));
+ for (const f of [path.join(root, 'config/luban/luban.conf'), path.join(datas, 'items.xlsx')]) at(f, begun - 5000);
+ expect(lubanStaleness(root, setting)).toBe('fresh');
+
+ // Excel's lock file appears while a designer has the table open: not a change.
+ writeFileSync(path.join(datas, '~$items.xlsx'), 'lock');
+ at(path.join(datas, '~$items.xlsx'), begun + 5000);
+ expect(lubanStaleness(root, setting)).toBe('fresh');
+
+ // Saving a table afterwards is.
+ at(path.join(datas, 'items.xlsx'), begun + 5000);
+ expect(lubanStaleness(root, setting)).toBe('stale');
+
+ // A file the run wrote has gone: start again.
+ rmSync(out);
+ expect(lubanStaleness(root, setting)).toBe('never');
+ writeFileSync(path.join(root, '.esengine/luban-generated.json'), 'not json');
+ expect(lubanStaleness(root, setting)).toBe('never');
+ } finally {
+ rmSync(root, { recursive: true, force: true });
+ }
+ });
+});