From 0c1c2367004d5104614c62f7cfb8d43251499482 Mon Sep 17 00:00:00 2001 From: Ryan McNally Date: Thu, 23 Nov 2023 09:59:21 +0000 Subject: [PATCH 1/3] Added links to modules from mermaid --- README.md | 42 ++++++------ .../test/flow/doc/ModuleDiagramTest.java | 66 +++++++++++++------ example/README.md | 45 +++++++------ 3 files changed, 89 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index d642513b9c..3389d6c4f9 100644 --- a/README.md +++ b/README.md @@ -50,27 +50,27 @@ These tests produce a rich execution report, [for example](https://mastercard.gi ```mermaid graph TB subgraph com.mastercard.test.flow - api - assert-core - assert-filter - assert-junit4 - assert-junit5 - builder - coppice - duct - message-core - message-http - message-json - message-sql - message-text - message-web - message-xml - model - report-core - report-ng - validation-core - validation-junit4 - validation-junit5 + api[api] + assert-core[assert-core] + assert-filter[assert-filter] + assert-junit4[assert-junit4] + assert-junit5[assert-junit5] + builder[builder] + coppice[coppice] + duct[duct] + message-core[message-core] + message-http[message-http] + message-json[message-json] + message-sql[message-sql] + message-text[message-text] + message-web[message-web] + message-xml[message-xml] + model[model] + report-core[report-core] + report-ng[report-ng] + validation-core[validation-core] + validation-junit4[validation-junit4] + validation-junit5[validation-junit5] end api --> message-core api --> builder diff --git a/doc/src/test/java/com/mastercard/test/flow/doc/ModuleDiagramTest.java b/doc/src/test/java/com/mastercard/test/flow/doc/ModuleDiagramTest.java index bed2442160..909a225c1f 100644 --- a/doc/src/test/java/com/mastercard/test/flow/doc/ModuleDiagramTest.java +++ b/doc/src/test/java/com/mastercard/test/flow/doc/ModuleDiagramTest.java @@ -5,6 +5,7 @@ import static java.util.stream.Collectors.toCollection; import static java.util.stream.Collectors.toSet; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collections; @@ -66,9 +67,10 @@ class ModuleDiagramTest { */ @Test void framework() throws Exception { - Util.insert( Paths.get( "../README.md" ), + Path path = Paths.get( "../README.md" ); + Util.insert( path, "", - s -> diagram( "TB", l -> l.isTo( "com.mastercard.test.flow" ), s ), + s -> diagram( path, "TB", l -> l.isTo( "com.mastercard.test.flow" ), s ), "" ); } @@ -79,13 +81,15 @@ void framework() throws Exception { */ @Test void example() throws Exception { - Util.insert( Paths.get( "../example/README.md" ), + Path path = Paths.get( "../example/README.md" ); + Util.insert( path, "", - s -> diagram( "LR", l -> l.isTo( "com.mastercard.test.flow.example" ), s ), + s -> diagram( path, "LR", l -> l.isTo( "com.mastercard.test.flow.example" ), s ), "" ); } - private static String diagram( String orientation, Predicate inclusion, String existing ) { + private static String diagram( Path docPath, String orientation, Predicate inclusion, + String existing ) { PomData root = new PomData( null, Paths.get( "../pom.xml" ) ); @@ -120,14 +124,30 @@ private static String diagram( String orientation, Predicate inclusion, St // remove links that don't fit in the requested diagram links.values().forEach( ll -> ll.removeIf( l -> !inclusion.test( l ) ) ); + // remove artifacts with no dependencies (e.g.: parent poms + groups.values().forEach( arts -> arts + .removeIf( pd -> !links.values().stream() + .map( ll -> ll.stream() + .filter( l -> l.involves( pd ) ) + .findAny() ) + .filter( Optional::isPresent ) + .map( Optional::get ) + .findAny().isPresent() ) ); + groups.values().removeIf( Set::isEmpty ); + + Set existingModules = extractModules( existing ); + Set desiredModules = groups.values().stream() + .flatMap( Set::stream ) + .map( pd -> moduleLink( docPath, pd ) ) + .collect( toCollection( TreeSet::new ) ); Set existingLinks = extractLinks( existing ); Set desiredLinks = links.values().stream() .flatMap( List::stream ) .map( Link::toString ) .collect( toCollection( TreeSet::new ) ); - if( existingLinks.equals( desiredLinks ) ) { - // The existing diagram has all the links we want to show, so let's leave it as - // it is + if( existingModules.equals( desiredModules ) && existingLinks.equals( desiredLinks ) ) { + // The existing diagram has all the modules and links we want to show, so let's + // leave it as it is return existing; // This allows us to make manual edits for reasons of layout while still // ensuring the dependency structure is correct @@ -135,17 +155,6 @@ private static String diagram( String orientation, Predicate inclusion, St // The existing diagram is not accurate, so overwrite it with one that is - // remove artifacts with no dependencies (e.g.: parent poms - groups.values().forEach( arts -> arts - .removeIf( pd -> !links.values().stream() - .map( ll -> ll.stream() - .filter( l -> l.involves( pd ) ) - .findAny() ) - .filter( Optional::isPresent ) - .map( Optional::get ) - .findAny().isPresent() ) ); - groups.values().removeIf( Set::isEmpty ); - StringBuilder mermaid = new StringBuilder( "```mermaid\ngraph " ) .append( orientation ) .append( "\n" ); @@ -154,7 +163,8 @@ private static String diagram( String orientation, Predicate inclusion, St mermaid.append( " subgraph " ).append( groupId ).append( "\n" ); pomdatas.stream() .sorted( comparing( PomData::artifactId ) ) - .forEach( pd -> mermaid.append( " " ).append( pd.artifactId() ).append( "\n" ) ); + .forEach( pd -> mermaid + .append( " " ).append( moduleLink( docPath, pd ) ).append( "\n" ) ); mermaid.append( " end\n" ); } ); @@ -165,6 +175,22 @@ private static String diagram( String orientation, Predicate inclusion, St return mermaid.toString(); } + private static final String moduleLink( Path docPath, PomData pom ) { + return String.format( "%s[%s]", + pom.artifactId(), + docPath.getParent().relativize( pom.dirPath() ).toString().replace( '\\', '/' ), + pom.artifactId() ); + } + + private static final Set extractModules( String mermaid ) { + Set modules = new TreeSet<>(); + Matcher m = Pattern.compile( "\\S+\\[\\S+\\]" ).matcher( mermaid ); + while( m.find() ) { + modules.add( m.group() ); + } + return modules; + } + private static final Set extractLinks( String mermaid ) { Set links = new TreeSet<>(); Matcher m = Pattern.compile( "\\S+ -\\.?-> \\S+" ).matcher( mermaid ); diff --git a/example/README.md b/example/README.md index f917924775..ca78f5efcd 100644 --- a/example/README.md +++ b/example/README.md @@ -67,39 +67,38 @@ graph TD ```mermaid - graph LR subgraph core - builder - model + builder[builder] + model[model] end subgraph message - message-http - message-json - message-sql - message-text - message-web + message-http[message-http] + message-json[message-json] + message-sql[message-sql] + message-text[message-text] + message-web[message-web] end subgraph validation - validation-junit5 - coppice + validation-junit5[validation-junit5] + coppice[coppice] end subgraph assert - assert-junit5 - duct + assert-junit5[assert-junit5] + duct[duct] end subgraph example - app-api - app-assert - app-core - app-framework - app-histogram - app-itest - app-model - app-queue - app-store - app-ui - app-web-ui + app-api[app-api] + app-assert[app-assert] + app-core[app-core] + app-framework[app-framework] + app-histogram[app-histogram] + app-itest[app-itest] + app-model[app-model] + app-queue[app-queue] + app-store[app-store] + app-ui[app-ui] + app-web-ui[app-web-ui] end app-api --> app-web-ui app-api --> app-ui From 88ad1ca302485cb3b4b0047c7f9f462f0350f1e1 Mon Sep 17 00:00:00 2001 From: Ryan McNally Date: Thu, 23 Nov 2023 10:13:28 +0000 Subject: [PATCH 2/3] Apparently relative links don't work --- README.md | 42 +++++++++--------- .../test/flow/doc/ModuleDiagramTest.java | 16 +++---- example/README.md | 44 +++++++++---------- 3 files changed, 51 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 3389d6c4f9..8583d5500c 100644 --- a/README.md +++ b/README.md @@ -50,27 +50,27 @@ These tests produce a rich execution report, [for example](https://mastercard.gi ```mermaid graph TB subgraph com.mastercard.test.flow - api[api] - assert-core[assert-core] - assert-filter[assert-filter] - assert-junit4[assert-junit4] - assert-junit5[assert-junit5] - builder[builder] - coppice[coppice] - duct[duct] - message-core[message-core] - message-http[message-http] - message-json[message-json] - message-sql[message-sql] - message-text[message-text] - message-web[message-web] - message-xml[message-xml] - model[model] - report-core[report-core] - report-ng[report-ng] - validation-core[validation-core] - validation-junit4[validation-junit4] - validation-junit5[validation-junit5] + api[api] + assert-core[assert-core] + assert-filter[assert-filter] + assert-junit4[assert-junit4] + assert-junit5[assert-junit5] + builder[builder] + coppice[coppice] + duct[duct] + message-core[message-core] + message-http[message-http] + message-json[message-json] + message-sql[message-sql] + message-text[message-text] + message-web[message-web] + message-xml[message-xml] + model[model] + report-core[report-core] + report-ng[report-ng] + validation-core[validation-core] + validation-junit4[validation-junit4] + validation-junit5[validation-junit5] end api --> message-core api --> builder diff --git a/doc/src/test/java/com/mastercard/test/flow/doc/ModuleDiagramTest.java b/doc/src/test/java/com/mastercard/test/flow/doc/ModuleDiagramTest.java index 909a225c1f..a058bcf59f 100644 --- a/doc/src/test/java/com/mastercard/test/flow/doc/ModuleDiagramTest.java +++ b/doc/src/test/java/com/mastercard/test/flow/doc/ModuleDiagramTest.java @@ -70,7 +70,7 @@ void framework() throws Exception { Path path = Paths.get( "../README.md" ); Util.insert( path, "", - s -> diagram( path, "TB", l -> l.isTo( "com.mastercard.test.flow" ), s ), + s -> diagram( "TB", l -> l.isTo( "com.mastercard.test.flow" ), s ), "" ); } @@ -84,11 +84,11 @@ void example() throws Exception { Path path = Paths.get( "../example/README.md" ); Util.insert( path, "", - s -> diagram( path, "LR", l -> l.isTo( "com.mastercard.test.flow.example" ), s ), + s -> diagram( "LR", l -> l.isTo( "com.mastercard.test.flow.example" ), s ), "" ); } - private static String diagram( Path docPath, String orientation, Predicate inclusion, + private static String diagram( String orientation, Predicate inclusion, String existing ) { PomData root = new PomData( null, Paths.get( "../pom.xml" ) ); @@ -138,7 +138,7 @@ private static String diagram( Path docPath, String orientation, Predicate Set existingModules = extractModules( existing ); Set desiredModules = groups.values().stream() .flatMap( Set::stream ) - .map( pd -> moduleLink( docPath, pd ) ) + .map( pd -> moduleLink( root.dirPath(), pd ) ) .collect( toCollection( TreeSet::new ) ); Set existingLinks = extractLinks( existing ); Set desiredLinks = links.values().stream() @@ -164,7 +164,7 @@ private static String diagram( Path docPath, String orientation, Predicate pomdatas.stream() .sorted( comparing( PomData::artifactId ) ) .forEach( pd -> mermaid - .append( " " ).append( moduleLink( docPath, pd ) ).append( "\n" ) ); + .append( " " ).append( moduleLink( root.dirPath(), pd ) ).append( "\n" ) ); mermaid.append( " end\n" ); } ); @@ -175,10 +175,10 @@ private static String diagram( Path docPath, String orientation, Predicate return mermaid.toString(); } - private static final String moduleLink( Path docPath, PomData pom ) { - return String.format( "%s[%s]", + private static final String moduleLink( Path root, PomData pom ) { + return String.format( "%s[%s]", pom.artifactId(), - docPath.getParent().relativize( pom.dirPath() ).toString().replace( '\\', '/' ), + root.relativize( pom.dirPath() ).toString().replace( '\\', '/' ), pom.artifactId() ); } diff --git a/example/README.md b/example/README.md index ca78f5efcd..3c805001ce 100644 --- a/example/README.md +++ b/example/README.md @@ -69,36 +69,36 @@ graph TD ```mermaid graph LR subgraph core - builder[builder] - model[model] + builder[builder] + model[model] end subgraph message - message-http[message-http] - message-json[message-json] - message-sql[message-sql] - message-text[message-text] - message-web[message-web] + message-http[message-http] + message-json[message-json] + message-sql[message-sql] + message-text[message-text] + message-web[message-web] end subgraph validation - validation-junit5[validation-junit5] - coppice[coppice] + validation-junit5[validation-junit5] + coppice[coppice] end subgraph assert - assert-junit5[assert-junit5] - duct[duct] + assert-junit5[assert-junit5] + duct[duct] end subgraph example - app-api[app-api] - app-assert[app-assert] - app-core[app-core] - app-framework[app-framework] - app-histogram[app-histogram] - app-itest[app-itest] - app-model[app-model] - app-queue[app-queue] - app-store[app-store] - app-ui[app-ui] - app-web-ui[app-web-ui] + app-api[app-api] + app-assert[app-assert] + app-core[app-core] + app-framework[app-framework] + app-histogram[app-histogram] + app-itest[app-itest] + app-model[app-model] + app-queue[app-queue] + app-store[app-store] + app-ui[app-ui] + app-web-ui[app-web-ui] end app-api --> app-web-ui app-api --> app-ui From 27465e40cad0368cda52441ca3b9df120ec5aef5 Mon Sep 17 00:00:00 2001 From: Ryan McNally Date: Thu, 23 Nov 2023 10:17:39 +0000 Subject: [PATCH 3/3] Fully qualified only! --- README.md | 42 +++++++++--------- .../test/flow/doc/ModuleDiagramTest.java | 2 +- example/README.md | 44 +++++++++---------- 3 files changed, 44 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 8583d5500c..b0f16b426d 100644 --- a/README.md +++ b/README.md @@ -50,27 +50,27 @@ These tests produce a rich execution report, [for example](https://mastercard.gi ```mermaid graph TB subgraph com.mastercard.test.flow - api[api] - assert-core[assert-core] - assert-filter[assert-filter] - assert-junit4[assert-junit4] - assert-junit5[assert-junit5] - builder[builder] - coppice[coppice] - duct[duct] - message-core[message-core] - message-http[message-http] - message-json[message-json] - message-sql[message-sql] - message-text[message-text] - message-web[message-web] - message-xml[message-xml] - model[model] - report-core[report-core] - report-ng[report-ng] - validation-core[validation-core] - validation-junit4[validation-junit4] - validation-junit5[validation-junit5] + api[api] + assert-core[assert-core] + assert-filter[assert-filter] + assert-junit4[assert-junit4] + assert-junit5[assert-junit5] + builder[builder] + coppice[coppice] + duct[duct] + message-core[message-core] + message-http[message-http] + message-json[message-json] + message-sql[message-sql] + message-text[message-text] + message-web[message-web] + message-xml[message-xml] + model[model] + report-core[report-core] + report-ng[report-ng] + validation-core[validation-core] + validation-junit4[validation-junit4] + validation-junit5[validation-junit5] end api --> message-core api --> builder diff --git a/doc/src/test/java/com/mastercard/test/flow/doc/ModuleDiagramTest.java b/doc/src/test/java/com/mastercard/test/flow/doc/ModuleDiagramTest.java index a058bcf59f..7c464f42c9 100644 --- a/doc/src/test/java/com/mastercard/test/flow/doc/ModuleDiagramTest.java +++ b/doc/src/test/java/com/mastercard/test/flow/doc/ModuleDiagramTest.java @@ -176,7 +176,7 @@ private static String diagram( String orientation, Predicate inclusion, } private static final String moduleLink( Path root, PomData pom ) { - return String.format( "%s[%s]", + return String.format( "%s[%s]", pom.artifactId(), root.relativize( pom.dirPath() ).toString().replace( '\\', '/' ), pom.artifactId() ); diff --git a/example/README.md b/example/README.md index 3c805001ce..959fdab7b8 100644 --- a/example/README.md +++ b/example/README.md @@ -69,36 +69,36 @@ graph TD ```mermaid graph LR subgraph core - builder[builder] - model[model] + builder[builder] + model[model] end subgraph message - message-http[message-http] - message-json[message-json] - message-sql[message-sql] - message-text[message-text] - message-web[message-web] + message-http[message-http] + message-json[message-json] + message-sql[message-sql] + message-text[message-text] + message-web[message-web] end subgraph validation - validation-junit5[validation-junit5] - coppice[coppice] + validation-junit5[validation-junit5] + coppice[coppice] end subgraph assert - assert-junit5[assert-junit5] - duct[duct] + assert-junit5[assert-junit5] + duct[duct] end subgraph example - app-api[app-api] - app-assert[app-assert] - app-core[app-core] - app-framework[app-framework] - app-histogram[app-histogram] - app-itest[app-itest] - app-model[app-model] - app-queue[app-queue] - app-store[app-store] - app-ui[app-ui] - app-web-ui[app-web-ui] + app-api[app-api] + app-assert[app-assert] + app-core[app-core] + app-framework[app-framework] + app-histogram[app-histogram] + app-itest[app-itest] + app-model[app-model] + app-queue[app-queue] + app-store[app-store] + app-ui[app-ui] + app-web-ui[app-web-ui] end app-api --> app-web-ui app-api --> app-ui