File: dotnet_utils.py

package info (click to toggle)
lsprotocol 2025.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,408 kB
  • sloc: python: 7,567; cs: 1,225; sh: 15; makefile: 4
file content (56 lines) | stat: -rw-r--r-- 1,886 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import logging
import pathlib
from typing import Dict

import generator.model as model

from .dotnet_classes import generate_all_classes
from .dotnet_commons import TypeData
from .dotnet_constants import NAMESPACE, PACKAGE_DIR_NAME
from .dotnet_enums import generate_enums
from .dotnet_helpers import namespace_wrapper
from .dotnet_special_classes import generate_special_classes

LOGGER = logging.getLogger("dotnet")


def generate_from_spec(spec: model.LSPModel, output_dir: str, _test_dir: str) -> None:
    """Generate the code for the given spec."""
    output_path = pathlib.Path(output_dir, PACKAGE_DIR_NAME)
    if not output_path.exists():
        output_path.mkdir(parents=True, exist_ok=True)

    cleanup(output_path)
    copy_custom_classes(output_path)

    LOGGER.info("Generating code in C#")
    types = TypeData()
    generate_package_code(spec, types)

    for name, lines in types.get_all():
        file_name = f"{name}.cs"
        (output_path / file_name).write_text("\n".join(lines), encoding="utf-8")


def generate_package_code(spec: model.LSPModel, types: TypeData) -> Dict[str, str]:
    generate_enums(spec, types)
    generate_special_classes(spec, types)
    generate_all_classes(spec, types)


def cleanup(output_path: pathlib.Path) -> None:
    """Cleanup the generated C# files."""
    for file in output_path.glob("*.cs"):
        file.unlink()


def copy_custom_classes(output_path: pathlib.Path) -> None:
    """Copy the custom classes to the output directory."""
    custom = pathlib.Path(__file__).parent / "custom"
    for file in custom.glob("*.cs"):
        lines = file.read_text(encoding="utf-8").splitlines()
        lines = namespace_wrapper(NAMESPACE, [], lines)
        (output_path / file.name).write_text("\n".join(lines), encoding="utf-8")