-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsproj_dependencies_to_mermaid.py
174 lines (137 loc) · 5.04 KB
/
csproj_dependencies_to_mermaid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import os
import xml.etree.ElementTree as ET
from enum import Enum, auto
class Orientation(Enum):
TOP_DOWN = auto()
LEFT_RIGHT = auto()
def _get_project_dependencies(csproj_file: str) -> list[str]:
"""
Retrieves the project dependencies from a given csproj file.
Args:
csproj_file (str): The csproj file name.
Returns:
list: A list of project dependencies.
"""
dependencies = []
tree = ET.parse(csproj_file)
root = tree.getroot()
for item in root.iter('{http://schemas.microsoft.com/developer/msbuild/2003}ProjectReference'):
reference = item.attrib['Include']
dependencies.append(reference)
return dependencies
def _process_csproj(csproj_file: str) -> str:
"""
Generates Mermaid markdown for a given csproj file.
Args:
csproj_file (str): The csproj file name.
Returns:
str: The generated Mermaid markdown.
"""
markdown = ""
# project name without extension
project_name = os.path.splitext(csproj_file)[0]
project_name = os.path.basename(project_name)
markdown += f" {project_name};\n"
dependencies = _get_project_dependencies(csproj_file)
for dependency in dependencies:
dependency_name = os.path.splitext(os.path.basename(dependency))[0]
markdown += f" {project_name} --> {dependency_name};\n"
return markdown
def _process_subdirectories_of_this_directory(directory: str) -> str:
"""
Generates Mermaid markdown for a graph that represents
the dependencies between csproj files in the subdirectories
of this directory.
Args:
directory (str): The path to the directory.
Returns:
str: The generated Mermaid markdown.
"""
mermaid_markdown = ""
sub_directories = [f.path for f in os.scandir(directory) if f.is_dir()]
for sub_directory in sub_directories:
mermaid_markdown += _process_recursively(sub_directory)
return mermaid_markdown
def _process_projects_in_this_directory(directory: str) -> str:
"""
Generates Mermaid markdown for a graph that represents
the dependencies between csproj files in this directory.
This function does not consider subdirectories.
Args:
directory (str): The path to the directory.
Returns:
str: The generated Mermaid markdown.
"""
mermaid_markdown = ""
for csproj_file in os.listdir(directory):
if csproj_file.endswith('.csproj'):
mermaid_markdown += _process_csproj(os.path.join(directory, csproj_file))
return mermaid_markdown
def _process_recursively(directory: str) -> str:
"""
Generates Mermaid markdown for a graph that represents
the dependencies between csproj files. All csproj files in
this folder and its subfolders are considered, recursively.
Args:
directory (str): The path to the directory.
Returns:
str: The generated Mermaid markdown.
"""
mermaid_markdown = ""
mermaid_markdown += _process_projects_in_this_directory(directory)
mermaid_markdown += _process_subdirectories_of_this_directory(directory)
return mermaid_markdown
def _create_mermaid_header(orientation: Orientation) -> str:
"""
Creates the Mermaid header.
Args:
orientation (Orientation): The orientation of the graph.
Returns:
str: The Mermaid header.
"""
if orientation == Orientation.TOP_DOWN:
return "```mermaid\ngraph TD;\n"
elif orientation == Orientation.LEFT_RIGHT:
return "```mermaid\ngraph LR;\n"
else:
raise ValueError("Invalid orientation.")
def _create_mermaid_footer() -> str:
"""
Creates the Mermaid footer.
Returns:
str: The Mermaid footer.
"""
return "```\n"
def generate(directory: str, orientation: Orientation = Orientation.TOP_DOWN) -> str:
"""
Generates Mermaid markdown for a graph that represents
the dependencies between csproj files. All csproj files in
this folder and its subfolders are considered, recursively.
Args:
directory (str): The path to the directory.
Returns:
str: The generated Mermaid markdown.
"""
mermaid_markdown = _create_mermaid_header(orientation)
mermaid_markdown += _process_recursively(directory)
mermaid_markdown += _create_mermaid_footer()
return mermaid_markdown
def parse_orientation(arg: str) -> Orientation:
"""
Parses the orientation from a string. Returns TOP_DOWN if the
argument is not recognized.
Args:
arg (str): The string to parse. This should be one of "TD" or "LR".
Returns:
Orientation: The parsed orientation.
"""
if arg.upper() == "LR":
return Orientation.LEFT_RIGHT
else: # Default to TOP_DOWN if the argument is not recognized
return Orientation.TOP_DOWN
# test
if __name__ == "__main__":
import sys
directory = sys.argv[1] if len(sys.argv) > 1 else r'ExampleSolution\Demo'
orientation = parse_orientation(sys.argv[2]) if len(sys.argv) > 2 else Orientation.TOP_DOWN
print(generate(directory, orientation))