|
| 1 | +import { readFileSync, writeFileSync } from "node:fs"; |
| 2 | +import { fileURLToPath } from "node:url"; |
| 3 | +import { dirname, join, resolve } from "node:path"; |
| 4 | +import { parseDocument, isMap, isScalar } from "yaml"; |
| 5 | +import { loadAreas } from "./load.js"; |
| 6 | +import { collectFeatureIds, findMissingFeatureIds } from "./compliance.js"; |
| 7 | +import type { RawCompliance } from "./compliance.js"; |
| 8 | + |
| 9 | +// Inserts canonical capability IDs that are missing from an SDK's |
| 10 | +// sdk-compliance.yaml as `not_implemented`. Canonical IDs and compliance keys |
| 11 | +// share the `area.group.feature` shape, so each new ID is placed next to its |
| 12 | +// existing siblings (same `area.group.` prefix); an ID whose group has no local |
| 13 | +// sibling yet is appended under a comment for manual placement. |
| 14 | +// |
| 15 | +// Entries are added to the parsed YAML document and re-serialized with the yaml |
| 16 | +// writer, so the output is always valid YAML regardless of the source layout. |
| 17 | +// Re-serializing may collapse hand-wrapped `note:` scalars onto a single line. |
| 18 | +// That is fine; the only concern here is keeping the file valid. |
| 19 | + |
| 20 | +function repoRoot(): string { |
| 21 | + const here = dirname(fileURLToPath(import.meta.url)); |
| 22 | + return resolve(here, "..", "..", ".."); |
| 23 | +} |
| 24 | + |
| 25 | +function parseArguments(argv: string[]): { |
| 26 | + compliancePath: string; |
| 27 | + newIdsOutput: string; |
| 28 | +} { |
| 29 | + let compliancePath: string | undefined; |
| 30 | + let newIdsOutput = "new_ids.txt"; |
| 31 | + for (let index = 0; index < argv.length; index++) { |
| 32 | + const argument = argv[index]; |
| 33 | + if (argument === "--new-ids-output") { |
| 34 | + newIdsOutput = argv[++index]; |
| 35 | + } else if (!argument.startsWith("--") && compliancePath === undefined) { |
| 36 | + compliancePath = argument; |
| 37 | + } |
| 38 | + } |
| 39 | + if (!compliancePath) { |
| 40 | + console.error( |
| 41 | + "Usage: sync-compliance-cli.ts <path-to-sdk-compliance.yaml> [--new-ids-output <path>]", |
| 42 | + ); |
| 43 | + process.exit(1); |
| 44 | + } |
| 45 | + return { compliancePath, newIdsOutput }; |
| 46 | +} |
| 47 | + |
| 48 | +function main(): void { |
| 49 | + const { compliancePath, newIdsOutput } = parseArguments( |
| 50 | + process.argv.slice(2), |
| 51 | + ); |
| 52 | + |
| 53 | + const { areas, findings } = loadAreas(join(repoRoot(), "capabilities")); |
| 54 | + if (findings.some((finding) => finding.level === "error")) { |
| 55 | + console.error( |
| 56 | + "Failed to load canonical capability spec — check this repo's capabilities/*.yaml", |
| 57 | + ); |
| 58 | + process.exit(1); |
| 59 | + } |
| 60 | + const knownIds = collectFeatureIds(areas); |
| 61 | + |
| 62 | + const text = readFileSync(resolve(compliancePath), "utf8"); |
| 63 | + const doc = parseDocument(text); |
| 64 | + if (doc.errors.length > 0) { |
| 65 | + console.error(`YAML parse error: ${doc.errors[0].message}`); |
| 66 | + process.exit(1); |
| 67 | + } |
| 68 | + const raw = doc.toJS() as RawCompliance; |
| 69 | + |
| 70 | + const missing = findMissingFeatureIds(raw, knownIds); |
| 71 | + writeFileSync( |
| 72 | + resolve(newIdsOutput), |
| 73 | + missing.length ? missing.join("\n") + "\n" : "", |
| 74 | + ); |
| 75 | + if (missing.length === 0) { |
| 76 | + console.log("No new capability IDs."); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + const features = doc.get("features", true); |
| 81 | + if (!isMap(features)) { |
| 82 | + console.error("Compliance file has no `features` map to sync into."); |
| 83 | + process.exit(1); |
| 84 | + } |
| 85 | + |
| 86 | + const idIndex = new Map<string, number>(); |
| 87 | + features.items.forEach((pair, index) => { |
| 88 | + if (isScalar(pair.key) && typeof pair.key.value === "string") { |
| 89 | + idIndex.set(pair.key.value, index); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + const byPrefix = new Map<string, string[]>(); |
| 94 | + for (const id of missing) { |
| 95 | + const prefix = id.slice(0, id.lastIndexOf(".") + 1); |
| 96 | + const bucket = byPrefix.get(prefix); |
| 97 | + if (bucket) bucket.push(id); |
| 98 | + else byPrefix.set(prefix, [id]); |
| 99 | + } |
| 100 | + |
| 101 | + const insertions: { index: number; ids: string[] }[] = []; |
| 102 | + const orphans: string[] = []; |
| 103 | + for (const [prefix, ids] of byPrefix) { |
| 104 | + const siblingIndices = [...idIndex.entries()] |
| 105 | + .filter(([existingId]) => existingId.startsWith(prefix)) |
| 106 | + .map(([, index]) => index); |
| 107 | + if (siblingIndices.length === 0) { |
| 108 | + orphans.push(...ids); |
| 109 | + continue; |
| 110 | + } |
| 111 | + // Insert right after the last existing sibling so the new IDs land in-group. |
| 112 | + insertions.push({ index: Math.max(...siblingIndices) + 1, ids }); |
| 113 | + } |
| 114 | + |
| 115 | + // Splice back-to-front so earlier indices stay valid as items shift. |
| 116 | + for (const { index, ids } of insertions.sort((a, b) => b.index - a.index)) { |
| 117 | + const pairs = ids.map((id) => doc.createPair(id, "not_implemented")); |
| 118 | + features.items.splice(index, 0, ...pairs); |
| 119 | + } |
| 120 | + |
| 121 | + if (orphans.length > 0) { |
| 122 | + const pairs = orphans.map((id) => doc.createPair(id, "not_implemented")); |
| 123 | + (pairs[0].key as { commentBefore?: string }).commentBefore = |
| 124 | + " Newly synced from the canonical spec; no local group yet, place manually."; |
| 125 | + features.items.push(...pairs); |
| 126 | + } |
| 127 | + |
| 128 | + writeFileSync(resolve(compliancePath), doc.toString({ lineWidth: 0 })); |
| 129 | + console.log( |
| 130 | + `Added ${missing.length} new capability IDs (${orphans.length} without an existing group).`, |
| 131 | + ); |
| 132 | +} |
| 133 | + |
| 134 | +main(); |
0 commit comments