11import json
2- import os
32
43from dataclasses import asdict
54from pathlib import Path
65
7- from .common import write_file_if_diff
86from .protocol_version import version_hash
97
108from .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