Baseline: tree as shared

This commit is contained in:
alexm committed 2026-10-05 03:03:38 +00:00
commit 31f2fb8a5a
1679 files changed
+1024562

No files matched your search

+75
View File
@@ -0,0 +1,75 @@
#!/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}`);