summaryrefslogtreecommitdiffstats
path: root/generator/plugins/testdata/testdata_utils.py
blob: 879ce2f940a356abf5b1b285b6bd28fe391d68df (plain)
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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import logging
import pathlib
from typing import Dict, List

import generator.model as model

from .testdata_generator import generate

logger = logging.getLogger("testdata")


def generate_from_spec(spec: model.LSPModel, output_dir: str) -> None:
    """Generate the code for the given spec."""
    output = pathlib.Path(output_dir)

    if not output.exists():
        output.mkdir(parents=True, exist_ok=True)

    cleanup(output)
    # key is the relative path to the file, value is the content
    code: Dict[str, str] = generate(spec, logger)
    for file_name in code:
        # print file size
        file = output / file_name
        file.write_text(code[file_name], encoding="utf-8")


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