Skip to content

Commit 3280435

Browse files
authored
Merge pull request #115 from microavia/feat/separate-types-js
Support separated types and protocol generation for TS and JS
2 parents 8ceaa1f + 0df41a1 commit 3280435

22 files changed

+331
-519
lines changed

.github/workflows/js.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ on:
88

99
jobs:
1010
build:
11-
if: false # disable the entire workflow
1211
runs-on: ubuntu-latest
1312
steps:
1413
- uses: actions/checkout@v3

messgen/json_generator.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import json
2-
import os
32

43
from dataclasses import asdict
54
from pathlib import Path
65

7-
from .common import write_file_if_diff
86
from .protocol_version import version_hash
97

108
from .validation import validate_protocol
@@ -31,17 +29,27 @@ def validate(self, types: dict[str, MessgenType], protocols: dict[str, Protocol]
3129
validate_protocol(proto_def, types)
3230

3331
def generate_types(self, out_dir: Path, types: dict[str, MessgenType]) -> None:
34-
for type_name, type_def in types.items():
35-
if type_def.type_class not in [TypeClass.struct, TypeClass.enum]:
36-
continue
37-
file_name = out_dir / (type_name + self._FILE_EXT)
38-
file_name.parent.mkdir(parents=True, exist_ok=True)
39-
write_file_if_diff(file_name, json.dumps(asdict(type_def), indent=2).splitlines())
32+
combined: list = []
33+
34+
for type_def in types.values():
35+
if type_def.type_class in [TypeClass.struct, TypeClass.enum]:
36+
combined.append(asdict(type_def))
37+
38+
self._write_file(out_dir, "types", combined)
4039

4140
def generate_protocols(self, out_dir: Path, protocols: dict[str, Protocol]) -> None:
42-
for proto_name, proto_def in protocols.items():
43-
file_name = out_dir / (proto_name + self._FILE_EXT)
44-
file_name.parent.mkdir(parents=True, exist_ok=True)
41+
combined: list = []
42+
43+
for proto_def in protocols.values():
4544
proto_dict = asdict(proto_def)
4645
proto_dict["version"] = version_hash(proto_dict)
47-
write_file_if_diff(file_name, json.dumps(asdict(proto_def), indent=2).splitlines())
46+
combined.append(proto_dict)
47+
48+
self._write_file(out_dir, "protocols", combined)
49+
50+
def _write_file(self, out_dir: Path, name: str, data: list) -> None:
51+
file_name = out_dir / (name + self._FILE_EXT)
52+
file_name.parent.mkdir(parents=True, exist_ok=True)
53+
54+
with open(file_name, "w", encoding="utf-8") as f: json.dump(data, f, indent=2)
55+

0 commit comments

Comments
 (0)