django-language-server/.just/proto.just
Josh Thomas 0a6e975ca5
Get rid of all transport types and settle on Protobuf (#25)
* Get rid of all transport types and settle on Protobuf

hope i don't regret this

* Update Cargo.toml

* Update agent.py
2024-12-12 16:53:49 -06:00

105 lines
2.7 KiB
Text

set unstable := true
justfile := justfile_directory() + "/.just/proto.just"
[private]
default:
@just --list --justfile {{ justfile }}
[no-cd]
[private]
check:
#!/usr/bin/env sh
if ! command -v protoc > /dev/null 2>&1; then
echo "protoc is not installed. Please install protobuf-compiler"
exit 1
fi
[private]
fmt:
@just --fmt --justfile {{ justfile }}
# Generate protobuf code for both Rust and Python
[no-cd]
gen:
@just proto rust
@just proto py
# Generate protobuf code for Rust
[no-cd]
rust: check
@just proto clean-rust
cargo build -p djls-ipc
[private]
clean-rust:
#!/usr/bin/env python3
from pathlib import Path
import shutil
target_dir = Path("{{ justfile_directory() }}/target")
if target_dir.exists():
for item in target_dir.rglob('*djls[_-]ipc*'):
if item.is_file():
item.unlink()
elif item.is_dir():
shutil.rmtree(item)
# Generate protobuf code for Python
[no-cd]
py: check
@just proto clean-py
protoc -I=proto \
--python_out=python/djls/proto \
--pyi_out=python/djls/proto \
proto/v1/*.proto
fd -t f "(_pb2\.py|_pb2\.pyi)$" python/djls/proto -x sed -i 's/from v1 import/from . import/g' {}
fd -t d "." python/djls/proto -x touch {}//__init__.py
@just proto py-add-warnings
[private]
clean-py:
#!/usr/bin/env python3
from pathlib import Path
import shutil
proto_dir = Path("{{ justfile_directory() }}/python/djls/proto")
for item in proto_dir.iterdir():
if item.is_file():
item.unlink()
elif item.is_dir():
shutil.rmtree(item)
[private]
py-add-warnings:
#!/usr/bin/env python3
from pathlib import Path
def create_warning(proto_file: str) -> str:
return f'''# WARNING: This file is generated by protobuf. DO NOT EDIT!
# Any changes made to this file will be overwritten when the protobuf files are regenerated.
# Source: {proto_file}
'''
proto_dir = Path("{{ justfile_directory() }}/python/djls/proto")
proto_source_dir = Path("{{ justfile_directory() }}/proto")
proto_sources = {
path.stem: path.relative_to(proto_source_dir)
for path in proto_source_dir.glob('**/*.proto')
}
for file_path in proto_dir.glob("**/*.py*"): # Catches both .py and .pyi in all subdirs
proto_name = file_path.stem.removesuffix('_pb2')
source_proto = proto_sources.get(proto_name)
content = file_path.read_text()
if not content.startswith('# WARNING'):
warning = create_warning(
str(source_proto) if source_proto
else "generated by py-init"
)
file_path.write_text(warning + content)