From 8f34128fe7f2df69c86ed1c64bfb69d468ba8021 Mon Sep 17 00:00:00 2001 From: ewuerger Date: Thu, 2 Nov 2023 12:45:25 +0100 Subject: [PATCH] fix: Fix fragment semantic resources --- capella_git_hooks/fix_links.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/capella_git_hooks/fix_links.py b/capella_git_hooks/fix_links.py index 2150a36..7afd675 100644 --- a/capella_git_hooks/fix_links.py +++ b/capella_git_hooks/fix_links.py @@ -27,7 +27,7 @@ ) fragment_pattern = re.compile( - rf"{prefix_pattern}fragments/.*\.(capella|aird)fragment" + rf"({prefix_pattern}fragments/).*\.(capella|aird)fragment" ) fragment_reference_pattern = re.compile( f"{fragment_pattern.pattern}{reference_pattern.pattern}" @@ -72,14 +72,24 @@ def fix_semantic_resources( """Patch the semantic resource file paths and return a bool.""" changed = False danalysis = next(root.iterchildren(DIAGRAM_OVERVIEW_TAG)) + is_fragment = "fragments" in root.base for resource in danalysis.iterchildren("semanticResources"): - if resource.text is None: + if not resource.text or root.base in resource.text: + danalysis.remove(resource) + changed = True continue text = search_and_replace( fragment_pattern, resource.text, fragment_repl ) - text = search_and_replace(root_pattern, resource.text, root_repl) + if resource.text == text: + replacement = fragment_repl if is_fragment else root_repl + text = search_and_replace(root_pattern, resource.text, replacement) + + if is_fragment and not resource.text.endswith("fragment"): + if not resource.text.startswith(root_repl): + text = f"{root_repl}{resource.text}" + if resource.text != text: resource.text = text changed = True