ShtukbaseStandard
包文件→ .shtukbase/packages/blueprint-software-factory/actions/setup.ts

actions/setup.ts

77 行, 4.2 KB.

文件按包中的原样显示:代码及其注释不做翻译。

  1. // `shtuk run blueprint-software-factory setup` (also on install): carries the answers of the start questions into
  2. // the project where a page mark cannot. The shtuk tool itself fills the language, branch and strictness in the head of
  3. // /base/software-factory.md and the harness in the agent pages ({{fact:…}}). This action writes the test command
  4. // (auto: by the language) to that head; the harness's model to the three agent pages; the strictness to the `mode` of the norms of the installed code-*
  5. // and std-* packages (observe: all but floor rules observe; enforce: MUST and SHOULD rules enforce, MAY left alone);
  6. // the kind Review to the placement map; the missing lines for secrets and runtime files to .gitignore.
  7. import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
  8. import { dirname, join } from 'node:path';
  9. import { readPage } from '../../collections/checks/lib/pages.ts';
  10. import { place } from '../../agents/lib/place.ts';
  11. import { update } from '../../agents/lib/write.ts';
  12. import { facts, str } from '../lib/facts.ts';
  13. import { PAGE } from '../lib/factory.ts';
  14. const root = process.cwd();
  15. const f = facts(dirname(import.meta.dir), root);
  16. const at = (p: string) => join(root, p.replace(/^\/+/, ''));
  17. const say: string[] = [];
  18. /** Writes only the fields whose value differs, so a page the answers do not change stays as the tool laid it. */
  19. function set(path: string, fields: Record<string, unknown>): void {
  20. const fm = readPage(path).fm;
  21. const diff = Object.fromEntries(Object.entries(fields).filter(([k, v]) => JSON.stringify(fm[k]) !== JSON.stringify(v)));
  22. if (Object.keys(diff).length) update(path, diff as any);
  23. }
  24. // 1. the factory page
  25. const TESTS: Record<string, string> = {
  26. typescript: 'bun test', python: 'python3 -m unittest discover -q', rust: 'cargo test', go: 'go test ./...', other: 'make test',
  27. };
  28. const language = str(f['stack.language']) || 'typescript';
  29. const asked = str(f['factory.test']);
  30. const test = !asked || asked === 'auto' ? TESTS[language] ?? 'make test' : asked;
  31. const branch = str(f['factory.branch']) || 'main';
  32. const strict = str(f['factory.strictness']) === 'enforce' ? 'enforce' : 'observe';
  33. if (existsSync(at(PAGE))) set(at(PAGE), { test });
  34. say.push(`tests "${test}", branch ${branch}`);
  35. // 2. the model of the agents (the harness itself is {{fact:factory.harness}} in their pages)
  36. const harness = str(f['factory.harness']) || 'claude';
  37. const MODELS: Record<string, string> = { claude: 'haiku', codex: 'gpt-6-luna', hermes: 'deepseek-v4.1-flash' };
  38. for (const a of ['sf-executor', 'sf-reviewer', 'sf-tester'])
  39. if (existsSync(at(`/agents/${a}.md`))) set(at(`/agents/${a}.md`), { model: MODELS[harness] ?? 'haiku' });
  40. say.push(`agents: ${harness}, ${MODELS[harness] ?? 'haiku'}`);
  41. // 3. strictness of code and standard norms
  42. let changed = 0;
  43. const rulesDir = at('/norms/rules');
  44. for (const pkg of existsSync(rulesDir) ? readdirSync(rulesDir) : []) {
  45. if (!/^(code|std)-/.test(pkg)) continue;
  46. for (const file of readdirSync(join(rulesDir, pkg)).filter((n) => n.endsWith('.md'))) {
  47. const path = join(rulesDir, pkg, file);
  48. const fm = readPage(path).fm;
  49. const level = str(fm.level);
  50. const want = strict === 'observe' ? (fm.floor === true ? null : 'observe') : level === 'MAY' ? null : 'enforce';
  51. if (want && str(fm.mode) !== want) {
  52. update(path, { mode: want });
  53. changed++;
  54. }
  55. }
  56. }
  57. say.push(`strictness ${strict}: the mode of code and standard norms changed in ${changed}`);
  58. // 4. the kind Review in the placement map
  59. place([{ itemType: 'Review', path: '/work/reviews', title: 'Reviews', pkg: 'blueprint-software-factory' }], root);
  60. // 5. .gitignore: secrets (SCR-R03) and the factory's runtime files; only the lines that are missing
  61. const IGNORE = ['.env', '.env.*', '!.env.example', '.shtukbase/state/', 'runs/logs/'];
  62. const gi = at('/.gitignore');
  63. const had = existsSync(gi) ? readFileSync(gi, 'utf8') : '';
  64. const lines = new Set(had.split('\n').map((l) => l.trim()));
  65. const add = IGNORE.filter((l) => !lines.has(l));
  66. if (add.length) {
  67. writeFileSync(gi, `${had}${had && !had.endsWith('\n') ? '\n' : ''}${had ? '\n' : ''}# shtukbase: software factory\n${add.join('\n')}\n`);
  68. say.push(`.gitignore: added ${add.join(' ')}`);
  69. }
  70. console.log(`software factory: ${say.join('; ')}`);