First working version of Mermaid class diagram generator

This commit is contained in:
Bartek Kryza
2023-09-06 21:20:57 +02:00
parent 6822930a12
commit 084bb20ef7
372 changed files with 1102 additions and 821 deletions

47
util/generate_mermaid.py Normal file
View File

@@ -0,0 +1,47 @@
#!/usr/bin/python3
##
## util/validate_json.py
##
## Copyright (c) 2021-2023 Bartek Kryza <bkryza@gmail.com>
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
import sys
import subprocess
from pathlib import Path
def print_usage():
print(f'Usage: ./generate_mermaid.py file1.mmd file2.mmd ...')
files = sys.argv[1:]
if not files:
print_usage()
sys.exit(1)
ok = 0
for f in files:
try:
print(f'Generating Mermaid diagram from {f}')
f_svg = Path(f).with_suffix('.png')
subprocess.check_call(['mmdc', '-i', f, '-o', f_svg])
except subprocess.CalledProcessError:
ok = 1
print(f'ERROR: Generating Mermaid diagram from {f} failed')
sys.exit(ok)

View File

@@ -69,12 +69,18 @@ with open(r'tests/test_cases.yaml') as f:
# Copy and link the diagram image
config_dict = yaml.full_load(config)
tc.write("## Generated UML diagrams\n")
tc.write("## Generated PlantUML diagrams\n")
for diagram_name, _ in config_dict['diagrams'].items():
copyfile(f'debug/tests/puml/{diagram_name}.svg',
copyfile(f'debug/tests/diagrams/puml/{diagram_name}.svg',
f'docs/test_cases/{diagram_name}.svg')
tc.write(f'![{diagram_name}](./{diagram_name}.svg "{test_case["title"]}")\n')
tc.write("## Generated Mermaid diagrams\n")
for diagram_name, _ in config_dict['diagrams'].items():
copyfile(f'debug/tests/diagrams/mermaid/{diagram_name}.svg',
f'docs/test_cases/{diagram_name}_mmd.svg')
tc.write(f'![{diagram_name}](./{diagram_name}_mmd.svg "{test_case["title"]}")\n')
tc.write("## Generated JSON models\n")
for diagram_name, _ in config_dict['diagrams'].items():
if os.path.exists(f'debug/tests/puml/{diagram_name}.json'):

View File

@@ -1,5 +1,5 @@
compilation_database_dir: ..
output_directory: puml
output_directory: diagrams
diagrams:
{{ name }}_{{ type }}:
type: {{ type }}

View File

@@ -38,7 +38,7 @@ TEST_CASE("{{ name }}", "[test-case][{{ type }}]")
{{ examples }}
save_puml(
config.output_directory() + "/" + diagram->name + ".puml", puml);
config.output_directory(), diagram->name + ".puml", puml);
}
{
@@ -46,7 +46,7 @@ TEST_CASE("{{ name }}", "[test-case][{{ type }}]")
using namespace json;
save_json(config.output_directory() + "/" + diagram->name + ".json", j);
save_json(config.output_directory(), diagram->name + ".json", j);
}
}

46
util/validate_json.py Normal file
View File

@@ -0,0 +1,46 @@
#!/usr/bin/python3
##
## util/validate_json.py
##
## Copyright (c) 2021-2023 Bartek Kryza <bkryza@gmail.com>
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
import sys
import json
def print_usage():
print(f'Usage: ./validate_json.py file1.json file2.json ...')
files = sys.argv[1:]
if not files:
print_usage()
sys.exit(1)
ok = 0
for f in files:
with open(f, 'r') as file:
try:
json.load(file)
print(f'File {f} is valid')
except ValueError:
ok = 1
print(f'File {f} is invalid')
sys.exit(ok)