Content-Length: 68699 | pFad | https://github.com/github/copilot-sdk-java/raw/refs/heads/main/scripts/codegen/java.ts
h: 68687 /*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. *--------------------------------------------------------------------------------------------*/ /** * Java code generator for session-events and RPC types. * Generates Java source files under src/generated/java/ from JSON Schema files. */ import fs from "fs/promises"; import path from "path"; import { fileURLToPath } from "url"; import type { JSONSchema7 } from "json-schema"; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); /** Root of the copilot-sdk-java repo */ const REPO_ROOT = path.resolve(__dirname, "../.."); /** Event types to exclude from generation (internal/legacy types) */ const EXCLUDED_EVENT_TYPES = new Set(["session.import_legacy"]); const AUTO_GENERATED_HEADER = `// AUTO-GENERATED FILE - DO NOT EDIT`; const GENERATED_FROM_SESSION_EVENTS = `// Generated from: session-events.schema.json`; const GENERATED_FROM_API = `// Generated from: api.schema.json`; const GENERATED_ANNOTATION = `@javax.annotation.processing.Generated("copilot-sdk-codegen")`; const COPYRIGHT = `/*---------------------------------------------------------------------------------------------\n * Copyright (c) Microsoft Corporation. All rights reserved.\n *--------------------------------------------------------------------------------------------*/`; // ── Naming utilities ───────────────────────────────────────────────────────── function toPascalCase(name: string): string { return name.split(/[-_.]/).map((p) => p.charAt(0).toUpperCase() + p.slice(1)).join(""); } function toJavaClassName(typeName: string): string { return typeName.split(/[._]/).map((p) => p.charAt(0).toUpperCase() + p.slice(1)).join(""); } /** Java reserved keywords and Object method names that cannot be used as record component names. */ const JAVA_RESERVED_IDENTIFIERS = new Set([ "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum", "extends", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while", // Object methods that conflict with record component accessor names "wait", "notify", "notifyAll", "getClass", "clone", "finalize", "toString", "hashCode", "equals", ]); function toCamelCase(name: string): string { const pascal = toPascalCase(name); let result = pascal.charAt(0).toLowerCase() + pascal.slice(1); if (JAVA_RESERVED_IDENTIFIERS.has(result)) { result = result + "_"; } return result; } function toEnumConstant(value: string): string { return value.toUpperCase().replace(/[-. /:]/g, "_").replace(/^_+/, "").replace(/_+/g, "_"); } // ── Schema path resolution ─────────────────────────────────────────────────── async function getSessionEventsSchemaPath(): Promise