diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 19:55:48 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 19:55:48 +0000 |
commit | 8be448d3881909fb0ce4b033cad71aa7575de0aa (patch) | |
tree | da33caff06645347a08c3c9c56dd703e4acb5aa3 /generator/plugins/dotnet/dotnet_utils.py | |
parent | Initial commit. (diff) | |
download | lsprotocol-8be448d3881909fb0ce4b033cad71aa7575de0aa.tar.xz lsprotocol-8be448d3881909fb0ce4b033cad71aa7575de0aa.zip |
Adding upstream version 2023.0.0.upstream/2023.0.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'generator/plugins/dotnet/dotnet_utils.py')
-rw-r--r-- | generator/plugins/dotnet/dotnet_utils.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/generator/plugins/dotnet/dotnet_utils.py b/generator/plugins/dotnet/dotnet_utils.py new file mode 100644 index 0000000..4510527 --- /dev/null +++ b/generator/plugins/dotnet/dotnet_utils.py @@ -0,0 +1,58 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import logging +import os +import pathlib +import subprocess +from typing import Dict, List + +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) -> 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") |