diff --git a/generator/__main__.py b/generator/__main__.py index e283012..ea2bbed 100644 --- a/generator/__main__.py +++ b/generator/__main__.py @@ -51,6 +51,12 @@ def get_parser() -> argparse.ArgumentParser: type=str, required=True, ) + parser.add_argument( + "--output-dir", + "-o", + help="Path to a directory where the generated content is written.", + type=str, + ) return parser @@ -83,13 +89,18 @@ def main(argv: Sequence[str]) -> None: plugin = args.plugin LOGGER.info(f"Running plugin {plugin}.") + output_dir = args.output_dir or os.fspath(PACKAGES_ROOT / plugin) + LOGGER.info(f"Writing output to {output_dir}") + # load model and generate types for each plugin to avoid # any conflicts between plugins. spec: model.LSPModel = model.create_lsp_model(json_models) try: + LOGGER.info(f"Loading plugin: {plugin}.") plugin_module = importlib.import_module(f"generator.plugins.{plugin}") - plugin_module.generate(spec, os.fspath(PACKAGES_ROOT / plugin)) + LOGGER.info(f"Running plugin: {plugin}.") + plugin_module.generate(spec, output_dir) LOGGER.info(f"Plugin {plugin} completed.") except Exception as e: LOGGER.error(f"Error running plugin {plugin}:", exc_info=e) diff --git a/generator/plugins/dotnet/dotnet_utils.py b/generator/plugins/dotnet/dotnet_utils.py index 51e8e91..4510527 100644 --- a/generator/plugins/dotnet/dotnet_utils.py +++ b/generator/plugins/dotnet/dotnet_utils.py @@ -21,22 +21,20 @@ def generate_from_spec(spec: model.LSPModel, output_dir: str) -> None: """Generate the code for the given spec.""" - cleanup(output_dir) - copy_custom_classes(output_dir) + 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" - pathlib.Path(output_dir, PACKAGE_DIR_NAME, file_name).write_text( - "\n".join(lines), encoding="utf-8" - ) - - LOGGER.info("Running dotnet format") - subprocess.run( - ["dotnet", "format"], cwd=os.fspath(pathlib.Path(output_dir, PACKAGE_DIR_NAME)) - ) + (output_path / file_name).write_text("\n".join(lines), encoding="utf-8") 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 generate_all_classes(spec, types) -def cleanup(output_dir: str) -> None: +def cleanup(output_path: pathlib.Path) -> None: """Cleanup the generated C# files.""" - output = pathlib.Path(output_dir, PACKAGE_DIR_NAME) - for file in output.glob("*.cs"): + for file in output_path.glob("*.cs"): file.unlink() -def copy_custom_classes(output_dir: str) -> None: +def copy_custom_classes(output_path: pathlib.Path) -> None: """Copy the custom classes to the output directory.""" - output = pathlib.Path(output_dir, PACKAGE_DIR_NAME) 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 / file.name).write_text("\n".join(lines), encoding="utf-8") + (output_path / file.name).write_text("\n".join(lines), encoding="utf-8") diff --git a/generator/plugins/python/utils.py b/generator/plugins/python/utils.py index 61c91a8..1d5f08d 100644 --- a/generator/plugins/python/utils.py +++ b/generator/plugins/python/utils.py @@ -21,10 +21,13 @@ def generate_from_spec(spec: model.LSPModel, output_dir: str) -> None: code = TypesCodeGenerator(spec).get_code() + + output_path = pathlib.Path(output_dir, PACKAGE_NAME) + if not output_path.exists(): + output_path.mkdir(parents=True, exist_ok=True) + for file_name in code: - pathlib.Path(output_dir, PACKAGE_NAME, file_name).write_text( - code[file_name], encoding="utf-8" - ) + (output_path / file_name).write_text(code[file_name], encoding="utf-8") def _generate_field_validator( diff --git a/generator/plugins/rust/rust_utils.py b/generator/plugins/rust/rust_utils.py index 718b9d9..d817070 100644 --- a/generator/plugins/rust/rust_utils.py +++ b/generator/plugins/rust/rust_utils.py @@ -22,10 +22,14 @@ def generate_from_spec(spec: model.LSPModel, output_dir: str) -> None: code = generate_package_code(spec) + + output_path = pathlib.Path(output_dir, PACKAGE_DIR_NAME) + if not output_path.exists(): + output_path.mkdir(parents=True, exist_ok=True) + (output_path / "src").mkdir(parents=True, exist_ok=True) + for file_name in code: - pathlib.Path(output_dir, PACKAGE_DIR_NAME, file_name).write_text( - code[file_name], encoding="utf-8" - ) + (output_path / file_name).write_text(code[file_name], encoding="utf-8") def generate_package_code(spec: model.LSPModel) -> List[str]: diff --git a/noxfile.py b/noxfile.py index 1bd1349..6fb1117 100644 --- a/noxfile.py +++ b/noxfile.py @@ -232,6 +232,7 @@ def generate_dotnet(session: nox.Session): session.run("python", "-m", "generator", "--plugin", "dotnet") with session.chdir("./packages/dotnet/lsprotocol"): + session.run("dotnet", "format", external=True) session.run("dotnet", "build", external=True)