Files
pplus-web/core/scripts/new-exercise.mjs
T
alexmandClaude 82e18f7966 Move the engine core into core/, a project of its own (pplus-core)
What programs mean (the languages, translators, Mojo checker and
interpreter, prover, fuzzer, machine model, catalog problems and the Mojo
corpus with godbolt's stored answers) was mixed into the site's tree, so a
change to the prover and a change to a button went through the same gate
and the same docs. This moves all of it under core/, laid out as its own
repository, ready to be extracted with its history and pinned here as a
git submodule.

- core/: src/{engines,services,sync,mojo,exercises,curriculum,common},
  mojo/, syntax/mojo.xml, its tests (with Workspace and workForProblem in
  place of the site's Session), scripts (corpus pipeline, play, the
  exercise and probe starters, engines export/diff), and its docs
  (engines, trust, mojo-corpus, exercises, coverage). Its own pixi.toml,
  package.json, README, AGENTS.md and CI workflow.
- core/tests/boundary.test.mjs: library modules use no DOM, import no
  page widget, and import nothing outside src/.
- core/scripts/cli.mjs: pixi run cli check|run|prove|fuzz|translate.
- core/scripts/dafny.py: Dafny 4.11.0 pinned by version and SHA-256 per
  platform, fetched into .tools/ (pixi run dafny-fetch, pixi run dafny).
- The site imports core only as #core/<path>.js: an import map in
  index.html, "imports" in package.json for node, core's declarations for
  tsc. The site's TypeScript builds core first (npm run build).
- assemble.sh, the Quarto engine copy, the render cache key, check-pages,
  quarto-export and quarto-css read core/dist and core/mojo; quarto-css
  copies core's syntax/mojo.xml into the extension.
- Site tests that covered engines moved to core; the rest keep Session.
- README, AGENTS.md, docs/app.md, docs/deploy.md and the CI workflow
  describe the split (checkout with submodules; core's tests and corpus
  status run against the pinned commit).

Gate: site 110 pass; core 307 pass; corpus status --strict passes.
Manual check: the assembled site opens Bridge (count-base-proved),
Developer and Hardware (count-base) with the editor, the stepper and the
board, and no module errors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FgvbRdoEheAHqsRmr2WuKZ
2026-10-05 05:43:58 +00:00

76 lines
3.8 KiB
JavaScript

#!/usr/bin/env node
/**
* Start a new exercise whose Mojo you write in a plain .mojo file.
*
* pixi run new-exercise my-thing
* node scripts/new-exercise.mjs my-thing --title "My thing" --group "Loop forms" # with a title and group
*
* If mojo/local/my-thing.mojo already exists it is kept as written and only the
* exercise is made, so you can start from a .mojo file. Otherwise it creates
* mojo/local/my-thing.mojo a runnable skeleton with the header verify.py reads
* src/exercises/my-thing.ts an exercise that names it (`mojoProgram`)
* and registers the exercise in src/exercises/index.ts (before the demo playground,
* which stays last). Then `pixi run play my-thing` runs it, checks it on godbolt,
* builds it into the app, and tests everything.
*/
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const ROOT = fileURLToPath(new URL('..', import.meta.url));
const argv = process.argv.slice(2);
const flag = (name) => { const i = argv.indexOf(`--${name}`); return i >= 0 ? argv[i + 1] : undefined; };
const id = argv.find((a, i) => !a.startsWith('--') && !(i > 0 && argv[i - 1].startsWith('--')));
function die(msg) { console.error(`new: ${msg}`); process.exit(1); }
if (!id) die('usage: pixi run new-exercise <id>\n or: node scripts/new-exercise.mjs <id> [--title "Title"] [--group "Home-page section"]\n <id> is lowercase words joined by hyphens, e.g. rolling-sum');
if (!/^[a-z0-9]+(-[a-z0-9]+)*$/.test(id)) die(`"${id}" is not a valid id: lowercase letters and digits, joined by single hyphens`);
const mojoPath = path.join(ROOT, 'mojo/local', `${id}.mojo`);
const tsPath = path.join(ROOT, 'src/exercises', `${id}.ts`);
const indexPath = path.join(ROOT, 'src/exercises/index.ts');
// A .mojo file you already wrote is adopted as it is: only the exercise is made.
const adopt = fs.existsSync(mojoPath);
if (fs.existsSync(tsPath)) die(`${path.relative(ROOT, tsPath)} already exists; nothing was changed`);
const title = flag('title') ?? id.charAt(0).toUpperCase() + id.slice(1).replace(/-/g, ' ');
const group = flag('group') ?? 'Your own';
const ident = (/^\d/.test(id) ? 'x-' : '') + id;
const camel = ident.replace(/-([a-z0-9])/g, (_, c) => c.toUpperCase());
let index = fs.readFileSync(indexPath, 'utf8');
if (new RegExp(`\\bimport ${camel} from`).test(index)) die(`src/exercises/index.ts already imports something called ${camel}; pick another id`);
const anchorImport = "import blank from './demo.js';";
const anchorList = ' blank,\n];';
if (!index.includes(anchorImport) || !index.includes(anchorList)) die('src/exercises/index.ts no longer has the demo playground as its last entry, so I do not know where to register the exercise. Add the import and list entry by hand.');
if (!adopt) fs.writeFileSync(mojoPath, `# origin: Programming+ exercise ${id}
# status: unverified; \`corpus.py verify\` checks it on the pinned compiler
# probe: what this program shows, in one line (edit me)
# mode: both
# args: -O0 --debug-level=line-tables
# expect-stdout: Hello from ${id}
def main():
print("Hello from ${id}")
`);
fs.writeFileSync(tsPath, `import { exercise } from './types.js';
export default exercise({
id: '${id}',
mojoProgram: '${id}', // mojo/local/${id}.mojo; \`pixi run play ${id}\` runs, verifies and builds it
title: ${JSON.stringify(title)},
group: ${JSON.stringify(group)},
blurb: 'One or two sentences: what the student does here and what they will notice.',
meta: { level: 'core' },
sources: {},
lang: 'mojo',
});
`);
index = index.replace(anchorImport, `import ${camel} from './${id}.js';\n${anchorImport}`).replace(anchorList, ` ${camel},\n blank,\n];`);
fs.writeFileSync(indexPath, index);
console.log(`${adopt ? 'adopted ' : 'created '} mojo/local/${id}.mojo\ncreated src/exercises/${id}.ts\nupdated src/exercises/index.ts\n\nnext: pixi run play ${id}`);