Skip to content

Commit

Permalink
fix(mermaid): Fix group by roles name (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
haidaraM authored May 28, 2023
1 parent b56b73b commit 54aaf7b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 31 deletions.
7 changes: 3 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

## What's Changed

- Add support for MermaidJS. See https://github.com/haidaraM/ansible-playbook-grapher/issues/137
- 🚀🚀Add support for MermaidJS 🚀🚀. See https://github.com/haidaraM/ansible-playbook-grapher/issues/137
- Update various Dependencies: pytest, pytest-cov, ansible-core, pyquery etc...
- ci: Add dependabot for github-actions
- Rename some tests files
- ...
- Rename some tests files..

## Breaking changes

This version contains the following breaking changes. Some of them may likely affect you if you were using the grapher
as a library inside another project:

- Completely refactor the rendering part of the part by making it more extensible in order to support Mermaid.
- Completely refactor the rendering part of the part by making it more extensible in order to support Mermaid.
- Fill the plays, blocks and node with color to make them more visible in the output
- Rename the file `graph.py` to `graph_model.py`
- Use the concatenation of the playbook names as the output filename when graphing multiple playbooks instead of the
Expand Down
23 changes: 11 additions & 12 deletions ansibleplaybookgrapher/renderer/graphviz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,21 +221,9 @@ def build_role(self, role_node: RoleNode, color: str, fontcolor: str, **kwargs):
:return:
"""

# check if we already built this role
if role_node in self.roles_built:
return

self.roles_built.add(role_node)

digraph = kwargs["digraph"]

if role_node.include_role: # For include_role, we point to a file
url = self.get_node_url(role_node, "file")
else: # For normal role invocation, we point to the folder
url = self.get_node_url(role_node, "folder")

role_edge_label = f"{role_node.index} {role_node.when}"

# from parent to the role node
digraph.edge(
role_node.parent.id,
Expand All @@ -248,6 +236,17 @@ def build_role(self, role_node: RoleNode, color: str, fontcolor: str, **kwargs):
labeltooltip=role_edge_label,
)

# check if we already built this role
if role_node in self.roles_built:
return

self.roles_built.add(role_node)

if role_node.include_role: # For include_role, we point to a file
url = self.get_node_url(role_node, "file")
else: # For normal role invocation, we point to the folder
url = self.get_node_url(role_node, "folder")

plays_using_this_role = self.roles_usage[role_node]
if len(plays_using_this_role) > 1:
# If the role is used in multiple plays, we take black as the default color
Expand Down
37 changes: 22 additions & 15 deletions ansibleplaybookgrapher/renderer/mermaid.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,37 +216,44 @@ def build_role(self, role_node: RoleNode, color: str, fontcolor: str, **kwargs):
:param kwargs:
:return:
"""
self.add_comment(f"Start of the role '{role_node.name}'")

plays_using_this_role = len(self.roles_usage[role_node])
node_color = color
if plays_using_this_role > 1:
# If the role is used in multiple plays, we take black as the default color
node_color = "#000000" # black

# From parent to role
self.add_link(
source_id=role_node.parent.id,
text=f"{role_node.index} {role_node.when}",
dest_id=role_node.id,
style=f"stroke:{color},color:{node_color}",
)

# check if we already built this role
# Check if we already built this role
if role_node in self.roles_built:
return

self.roles_built.add(role_node)

# Role node
self.add_comment(f"Start of the role '{role_node.name}'")

self.add_text(f'{role_node.id}("[role] {role_node.name}")')
self.add_text(
f"style {role_node.id} fill:{color},color:{fontcolor},stroke:{color}"
)

# from parent to role
self.add_link(
source_id=role_node.parent.id,
text=f"{role_node.index} {role_node.when}",
dest_id=role_node.id,
style=f"stroke:{color},color:{color}",
f"style {role_node.id} fill:{node_color},color:{fontcolor},stroke:{node_color}"
)

# role tasks
# Role tasks
self._identation_level += 1
for role_task in role_node.tasks:
self.build_node(
node=role_task,
color=color,
color=node_color,
fontcolor=fontcolor,
)
self._identation_level -= 1

self.add_comment(f"End of the role '{role_node.name}'")

def build_block(self, block_node: BlockNode, color: str, fontcolor: str, **kwargs):
Expand Down Expand Up @@ -328,7 +335,7 @@ def add_text(self, text: str):
:param text:
:return:
"""
self.mermaid_code += f"{self.indentation}{text}\n"
self.mermaid_code += f"{self.indentation}{text.strip()}\n"

@property
def indentation(self) -> str:
Expand Down

0 comments on commit 54aaf7b

Please sign in to comment.