A python script to turn .csproj files into Mermaid diagrams syntax
import os
import sys
import xml.etree.ElementTree as ET
def get_csproj(path):
csproj = []
for (path, dirs, files) in os.walk(path):
for f in files:
if f.endswith('csproj'):
csproj.append(path + '/' + f)
return csproj
def find_dependencies(csproj):
dependencies = {}
for project in csproj:
references = {'packages': [], 'projects': []}
tree = ET.parse(project)
for group in tree.getroot().findall('ItemGroup'):
for reference in group.findall('PackageReference'):
references['packages'].append(reference.attrib['Include'])
for reference in group.findall('ProjectReference'):
references['projects'].append(package_from_path(reference.attrib['Include']))
namespace = package_from_path(project)
dependencies[namespace] = references
return dependencies
def create_graph(deps):
graph = 'graph TD\n'
for namespace, dep in deps.items():
for package in dep['packages']:
graph += namespace + '-->' + package + '\n'
for project in dep['projects']:
graph += namespace + '-.->' + project + '\n'
return graph
def package_from_path(path):
return '.'.join(path.split('/')[-1].split('\\')[-1].split('.')[:-1])
def print_usage():
print('usage:\n\n\tpython csproj.py [PATH]')
if len(sys.argv) != 2:
print_usage()
sys.exit(1)
csproj = get_csproj(sys.argv[1])
deps = find_dependencies(csproj)
graph = create_graph(deps)
print(graph)
It's pretty messy. Stripping out everything but local references (:2,$g!/-\.-/d
) and then Tests (:%g/Tests/d
) produces something much more readable:
graph TD Payments.DatabaseMigrator-.->Payments.Infrastructure Payments.EventListener.Worker-.->Payments.Application Payments.EventListener.Worker-.->Payments.Infrastructure Payments.Infrastructure-.->Payments.Domain Payments.Events-.->Payments.Domain Payments.Application-.->Payments.Infrastructure Payments.Application-.->Payments.Events Payments.CommandListener.Worker-.->Payments.Application Payments.CommandListener.Worker-.->Payments.Infrastructure Payments.Service-.->Payments.Application Payments.Service-.->Payments.Domain Payments.Service-.->Payments.Infrastructure