Skip to content

Commit dd39433

Browse files
committed
Add --output-dir switch to code generator
1 parent f3f7f30 commit dd39433

File tree

5 files changed

+37
-24
lines changed

5 files changed

+37
-24
lines changed

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]:

generator/__main__.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ def get_parser() -> argparse.ArgumentParser:
5151
type=str,
5252
action="append",
5353
)
54+
parser.add_argument(
55+
"--output-dir",
56+
"-o",
57+
help="Path to a directory where the generated content is written.",
58+
type=str,
59+
)
5460
return parser
5561

5662

@@ -97,15 +103,18 @@ def main(argv: Sequence[str]) -> None:
97103
LOGGER.info("Starting code generation.")
98104

99105
for plugin in plugins:
100-
LOGGER.info(f"Running plugin {plugin}.")
106+
output_dir = args.output or os.fspath(PACKAGES_ROOT / plugin)
107+
LOGGER.info(f"Writing output to {output_dir}")
101108

102109
# load model and generate types for each plugin to avoid
103110
# any conflicts between plugins.
104111
spec: model.LSPModel = model.create_lsp_model(json_models)
105112

106113
try:
114+
LOGGER.info(f"Loading plugin: {plugin}.")
107115
plugin_module = importlib.import_module(f"generator-plugins.{plugin}")
108-
plugin_module.generate(spec, os.fspath(PACKAGES_ROOT / plugin))
116+
LOGGER.info(f"Running plugin: {plugin}.")
117+
plugin_module.generate(spec, output_dir)
109118
LOGGER.info(f"Plugin {plugin} completed.")
110119
except Exception as e:
111120
LOGGER.error(f"Error running plugin {plugin}:", exc_info=e)

noxfile.py

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

231231
session.run("python", "-m", "generator", "--plugin", "dotnet")
232232
with session.chdir("./packages/dotnet/lsprotocol"):
233+
session.run("dotnet", "format", external=True)
233234
session.run("dotnet", "build", external=True)
234235

235236

0 commit comments

Comments
 (0)