diff --git a/.github/workflows/build-codemirror.yml b/.github/workflows/build-codemirror.yml index c42fcc5..2e5b31d 100644 --- a/.github/workflows/build-codemirror.yml +++ b/.github/workflows/build-codemirror.yml @@ -8,11 +8,11 @@ on: required: true type: string default: "6.0.2" - autocompletion_version: - description: "Version of @codemirror/autocomplete" + base: + description: "CodeMirror base packages as package:version pairs, comma-separated" required: true type: string - default: "6.0.0" + default: "autocomplete:6.0.0,language:6.0.0" lang_yaml_version: description: "Version of @codemirror/lang-yaml" required: true @@ -39,9 +39,10 @@ jobs: env: CODEMIRROR_VERSION: ${{ inputs.codemirror_version }} + BASE_PACKAGES: ${{ inputs.base }} + BASE_SCOPE: "@codemirror" LANG_YAML_VERSION: ${{ inputs.lang_yaml_version }} ESBUILD_VERSION: ${{ inputs.esbuild_version }} - AUTOCOMPLETION_VERSION: ${{ inputs.autocompletion_version}} steps: - name: Checkout @@ -52,11 +53,36 @@ jobs: with: node-version: 22 + - name: Generate CodeMirror entry point + run: python3 scripts/generate_entry.py "${BASE_PACKAGES}" + - name: Install selected dependencies + shell: bash run: | + BASE_INSTALL_ARGS=() + IFS=',' read -r -a BASE_SPECS <<< "${BASE_PACKAGES}" + + for spec in "${BASE_SPECS[@]}"; do + package="${spec%%:*}" + version="${spec#*:}" + + if [[ "${spec}" != *:* || ! "${package}" =~ ^[a-z0-9-]+$ || -z "${version}" ]]; then + echo "::error::Invalid base package specification: ${spec}" + echo "Expected comma-separated package:version pairs (without the @codemirror scope)." + exit 1 + fi + + BASE_INSTALL_ARGS+=("${BASE_SCOPE}/${package}@${version}") + done + + if [[ ${#BASE_INSTALL_ARGS[@]} -eq 0 ]]; then + echo "::error::The base input must contain at least one package." + exit 1 + fi + npm install --no-save --no-package-lock --no-audit --no-fund \ "codemirror@${CODEMIRROR_VERSION}" \ - "@codemirror/autocomplete@${AUTOCOMPLETION_VERSION}" \ + "${BASE_INSTALL_ARGS[@]}" \ "@codemirror/lang-yaml@${LANG_YAML_VERSION}" \ "esbuild@${ESBUILD_VERSION}" @@ -65,8 +91,8 @@ jobs: run: | t0="cm-${CODEMIRROR_VERSION}" t1="-yaml-${LANG_YAML_VERSION}" - t2="-autocmp-${AUTOCOMPLETION_VERSION}" - echo "name=$t0$t1$t2" >> "$GITHUB_OUTPUT" + base_tag="$(printf '%s' "${BASE_PACKAGES}" | sed -E 's/:/-/g; s/,/_/g')" + echo "name=${t0}${t1}-${base_tag}" >> "$GITHUB_OUTPUT" - name: Build ES module bundle run: | @@ -91,7 +117,7 @@ jobs: - name: Upload CodeMirror bundle uses: actions/upload-artifact@v7 with: - name: codemirror-${{ steps.js.outputs.name }}-${{ inputs.esbuild_version }} + name: ${{ steps.js.outputs.name }}-esbuild${{ inputs.esbuild_version }} path: build/codemirror/dist/${{ steps.js.outputs.name }}.js if-no-files-found: error retention-days: 30 diff --git a/build/codemirror/scripts/generate_entry.py b/build/codemirror/scripts/generate_entry.py new file mode 100644 index 0000000..0e6d090 --- /dev/null +++ b/build/codemirror/scripts/generate_entry.py @@ -0,0 +1,101 @@ +#!/usr/bin/env python3 +"""Generate the CodeMirror bundle entry point from selected base packages.""" + +import argparse +import re +from pathlib import Path + + +# Public exports exposed by the OBM CodeMirror bundle. +# Keys are package suffixes used in the workflow's "base" input. +PACKAGE_EXPORTS = { + "autocomplete": ["autocompletion"], + "language": ["syntaxTree"], +} + +# Dependencies that are always installed by the workflow. +FIXED_EXPORTS = [ + ("codemirror", ["EditorView", "basicSetup"]), + ("@codemirror/lang-yaml", ["yaml"]), +] + + +def parse_packages(value: str) -> list[str]: + """Parse and validate comma-separated package:version pairs.""" + packages = [] + + for item in value.split(","): + item = item.strip() + if not item or ":" not in item: + raise ValueError(f"Invalid package specification: {item!r}") + + package, version = (part.strip() for part in item.split(":", 1)) + + if not re.fullmatch(r"[a-z0-9-]+", package): + raise ValueError(f"Invalid package name: {package!r}") + if not version: + raise ValueError(f"Missing version for package: {package}") + if package in packages: + raise ValueError(f"Duplicate package: {package}") + if package not in PACKAGE_EXPORTS: + supported = ", ".join(PACKAGE_EXPORTS) + raise ValueError( + f"Unsupported package: {package!r}. Supported packages: {supported}" + ) + + packages.append(package) + + if not packages: + raise ValueError("At least one base package must be specified.") + + return packages + + +def generate_entry(packages: list[str]) -> str: + """Build the ES module entry point.""" + lines = [ + "// Generated automatically by scripts/generate_entry.py.", + "// Do not edit manually.", + "", + ] + + for module, exports in FIXED_EXPORTS: + lines.append(f'export {{ {", ".join(exports)} }} from "{module}";') + + for package in packages: + exports = PACKAGE_EXPORTS[package] + module = f"@codemirror/{package}" + if exports: + lines.append(f'export {{ {", ".join(exports)} }} from "{module}";') + + return "\n".join(lines) + "\n" + + +def main() -> None: + parser = argparse.ArgumentParser( + description="Generate src/index.js from the workflow base package input." + ) + parser.add_argument( + "base", + help="Comma-separated package:version pairs (without @codemirror/)", + ) + parser.add_argument( + "--output", + default="src/index.js", + help="Output JavaScript entry point (default: src/index.js)", + ) + args = parser.parse_args() + + try: + packages = parse_packages(args.base) + except ValueError as exc: + parser.error(str(exc)) + + output = Path(args.output) + output.parent.mkdir(parents=True, exist_ok=True) + output.write_text(generate_entry(packages), encoding="utf-8") + print(f"Generated {output}") + + +if __name__ == "__main__": + main() diff --git a/build/codemirror/src/index.js b/build/codemirror/src/index.js index e857ac2..5d41624 100644 --- a/build/codemirror/src/index.js +++ b/build/codemirror/src/index.js @@ -1,4 +1,7 @@ -// Public API exposed by the local OBM CodeMirror bundle. +// Generated automatically by scripts/generate_entry.py. +// Do not edit manually. + export { EditorView, basicSetup } from "codemirror"; export { yaml } from "@codemirror/lang-yaml"; -export { autocompletion } from '@codemirror/autocomplete'; +export { autocompletion } from "@codemirror/autocomplete"; +export { syntaxTree } from "@codemirror/language";