Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --output-dir switch to code generator #239

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion generator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
Expand Down
28 changes: 12 additions & 16 deletions generator/plugins/dotnet/dotnet_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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")
9 changes: 6 additions & 3 deletions generator/plugins/python/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 7 additions & 3 deletions generator/plugins/rust/rust_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
1 change: 1 addition & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down