Skip to content

Commit 13cdbd1

Browse files
authored
Add --output-dir switch to code generator (#239)
1 parent 21e1e13 commit 13cdbd1

File tree

5 files changed

+38
-23
lines changed

5 files changed

+38
-23
lines changed

generator/__main__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ def get_parser() -> argparse.ArgumentParser:
4545
type=str,
4646
required=True,
4747
)
48+
parser.add_argument(
49+
"--output-dir",
50+
"-o",
51+
help="Path to a directory where the generated content is written.",
52+
type=str,
53+
)
4854
return parser
4955

5056

@@ -73,13 +79,18 @@ def main(argv: Sequence[str]) -> None:
7379
plugin = args.plugin
7480
LOGGER.info(f"Running plugin {plugin}.")
7581

82+
output_dir = args.output_dir or os.fspath(PACKAGES_ROOT / plugin)
83+
LOGGER.info(f"Writing output to {output_dir}")
84+
7685
# load model and generate types for each plugin to avoid
7786
# any conflicts between plugins.
7887
spec: model.LSPModel = model.create_lsp_model(json_models)
7988

8089
try:
90+
LOGGER.info(f"Loading plugin: {plugin}.")
8191
plugin_module = importlib.import_module(f"generator.plugins.{plugin}")
82-
plugin_module.generate(spec, os.fspath(PACKAGES_ROOT / plugin))
92+
LOGGER.info(f"Running plugin: {plugin}.")
93+
plugin_module.generate(spec, output_dir)
8394
LOGGER.info(f"Plugin {plugin} completed.")
8495
except Exception as e:
8596
LOGGER.error(f"Error running plugin {plugin}:", exc_info=e)

generator/plugins/dotnet/dotnet_utils.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,20 @@
2121

2222
def generate_from_spec(spec: model.LSPModel, output_dir: str) -> None:
2323
"""Generate the code for the given spec."""
24-
cleanup(output_dir)
25-
copy_custom_classes(output_dir)
24+
output_path = pathlib.Path(output_dir, PACKAGE_DIR_NAME)
25+
if not output_path.exists():
26+
output_path.mkdir(parents=True, exist_ok=True)
27+
28+
cleanup(output_path)
29+
copy_custom_classes(output_path)
2630

2731
LOGGER.info("Generating code in C#")
2832
types = TypeData()
2933
generate_package_code(spec, types)
34+
3035
for name, lines in types.get_all():
3136
file_name = f"{name}.cs"
32-
pathlib.Path(output_dir, PACKAGE_DIR_NAME, file_name).write_text(
33-
"\n".join(lines), encoding="utf-8"
34-
)
35-
36-
LOGGER.info("Running dotnet format")
37-
subprocess.run(
38-
["dotnet", "format"], cwd=os.fspath(pathlib.Path(output_dir, PACKAGE_DIR_NAME))
39-
)
37+
(output_path / file_name).write_text("\n".join(lines), encoding="utf-8")
4038

4139

4240
def generate_package_code(spec: model.LSPModel, types: TypeData) -> Dict[str, str]:
@@ -45,18 +43,16 @@ def generate_package_code(spec: model.LSPModel, types: TypeData) -> Dict[str, st
4543
generate_all_classes(spec, types)
4644

4745

48-
def cleanup(output_dir: str) -> None:
46+
def cleanup(output_path: pathlib.Path) -> None:
4947
"""Cleanup the generated C# files."""
50-
output = pathlib.Path(output_dir, PACKAGE_DIR_NAME)
51-
for file in output.glob("*.cs"):
48+
for file in output_path.glob("*.cs"):
5249
file.unlink()
5350

5451

55-
def copy_custom_classes(output_dir: str) -> None:
52+
def copy_custom_classes(output_path: pathlib.Path) -> None:
5653
"""Copy the custom classes to the output directory."""
57-
output = pathlib.Path(output_dir, PACKAGE_DIR_NAME)
5854
custom = pathlib.Path(__file__).parent / "custom"
5955
for file in custom.glob("*.cs"):
6056
lines = file.read_text(encoding="utf-8").splitlines()
6157
lines = namespace_wrapper(NAMESPACE, [], lines)
62-
(output / file.name).write_text("\n".join(lines), encoding="utf-8")
58+
(output_path / file.name).write_text("\n".join(lines), encoding="utf-8")

generator/plugins/python/utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@
2121

2222
def generate_from_spec(spec: model.LSPModel, output_dir: str) -> None:
2323
code = TypesCodeGenerator(spec).get_code()
24+
25+
output_path = pathlib.Path(output_dir, PACKAGE_NAME)
26+
if not output_path.exists():
27+
output_path.mkdir(parents=True, exist_ok=True)
28+
2429
for file_name in code:
25-
pathlib.Path(output_dir, PACKAGE_NAME, file_name).write_text(
26-
code[file_name], encoding="utf-8"
27-
)
30+
(output_path / file_name).write_text(code[file_name], encoding="utf-8")
2831

2932

3033
def _generate_field_validator(

generator/plugins/rust/rust_utils.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@
2222

2323
def generate_from_spec(spec: model.LSPModel, output_dir: str) -> None:
2424
code = generate_package_code(spec)
25+
26+
output_path = pathlib.Path(output_dir, PACKAGE_DIR_NAME)
27+
if not output_path.exists():
28+
output_path.mkdir(parents=True, exist_ok=True)
29+
(output_path / "src").mkdir(parents=True, exist_ok=True)
30+
2531
for file_name in code:
26-
pathlib.Path(output_dir, PACKAGE_DIR_NAME, file_name).write_text(
27-
code[file_name], encoding="utf-8"
28-
)
32+
(output_path / file_name).write_text(code[file_name], encoding="utf-8")
2933

3034

3135
def generate_package_code(spec: model.LSPModel) -> List[str]:

noxfile.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ def generate_dotnet(session: nox.Session):
232232

233233
session.run("python", "-m", "generator", "--plugin", "dotnet")
234234
with session.chdir("./packages/dotnet/lsprotocol"):
235+
session.run("dotnet", "format", external=True)
235236
session.run("dotnet", "build", external=True)
236237

237238

0 commit comments

Comments
 (0)