diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fe9eebc..c610b5cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/ansibleplaybookgrapher/renderer/graphviz/__init__.py b/ansibleplaybookgrapher/renderer/graphviz/__init__.py index 1d114732..f238cfcf 100644 --- a/ansibleplaybookgrapher/renderer/graphviz/__init__.py +++ b/ansibleplaybookgrapher/renderer/graphviz/__init__.py @@ -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, @@ -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 diff --git a/ansibleplaybookgrapher/renderer/mermaid.py b/ansibleplaybookgrapher/renderer/mermaid.py index 138760de..002c36b5 100644 --- a/ansibleplaybookgrapher/renderer/mermaid.py +++ b/ansibleplaybookgrapher/renderer/mermaid.py @@ -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): @@ -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: