-
Notifications
You must be signed in to change notification settings - Fork 3
Implement diagram output #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BartekBH
wants to merge
6
commits into
main
Choose a base branch
from
feature/diagram
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
domainDocs4s-core/src/main/scala/domaindocs4s/output/Writer.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package domaindocs4s.output | ||
|
|
||
| import java.nio.file.{Files, Path} | ||
|
|
||
| // TODO: for now it's only for decouple writing from generating, later it should be extended for more complex output handling | ||
| object Writer { | ||
|
|
||
| def apply(docs: String, path: String): Unit = { | ||
| val _ = Files.write(Path.of(path), docs.getBytes) | ||
| } | ||
|
|
||
| } | ||
|
BartekBH marked this conversation as resolved.
|
||
7 changes: 7 additions & 0 deletions
7
domainDocs4s-core/src/main/scala/domaindocs4s/output/diagram/Association.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package domaindocs4s.output.diagram | ||
|
|
||
| enum Association { | ||
| case ExactlyOne | ||
| case ZeroOrOne | ||
| case ZeroOrMore | ||
| } |
106 changes: 106 additions & 0 deletions
106
domainDocs4s-core/src/main/scala/domaindocs4s/output/diagram/Diagram.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| package domaindocs4s.output.diagram | ||
|
|
||
| import domaindocs4s.collector.{Documentation, DocumentedSymbol} | ||
| import tastyquery.Contexts.Context | ||
| import tastyquery.Symbols.{Symbol, TermSymbol} | ||
| import tastyquery.Trees.{DefDef, ValDef} | ||
| import tastyquery.Types.{AppliedType, Type, TypeRef} | ||
|
|
||
| case class Diagram(entities: List[Entity]) { | ||
|
|
||
| def asMarkdown(direction: Direction = Direction.None): String = { | ||
| if (entities.isEmpty) { | ||
| s""" | ||
| |```mermaid | ||
| |erDiagram | ||
| |``` | ||
| |""".stripMargin | ||
| } else { | ||
| val directionLine = if (direction == Direction.None) "" else s" direction $direction\n" | ||
| s""" | ||
| |```mermaid | ||
| |erDiagram | ||
| |$directionLine${entities.map(_.asMarkdown).mkString("\n")} | ||
| |``` | ||
| |""".stripMargin | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| object Diagram { | ||
|
|
||
| def build(docs: Documentation)(using ctx: Context): Diagram = { | ||
| val documentedSymbols = docs.symbols | ||
| val docEntities = docs.symbols.collect { | ||
| case sym if sym.symbol.isClass => buildDocEntity(sym, documentedSymbols)(using ctx) | ||
| } | ||
| Diagram(docEntities) | ||
| } | ||
|
|
||
| private def buildDocEntity(sym: DocumentedSymbol, documentedSymbols: List[DocumentedSymbol])(using ctx: Context): Entity = { | ||
| Entity( | ||
| name = sym.name, | ||
| associations = buildAssociations(sym, documentedSymbols)(using ctx), | ||
| fields = buildFields(sym), | ||
| ) | ||
| } | ||
|
|
||
| private def buildAssociations( | ||
| sym: DocumentedSymbol, | ||
| documentedSymbols: List[DocumentedSymbol], | ||
| )(using ctx: Context): Map[String, Association] = { | ||
| sym.declarations.flatMap { field => | ||
| documentedSymbols | ||
| .filter(s => isFieldRelatedToSymbol(field, s.symbol)(using ctx)) | ||
| .map(s => (s.name, determineRelationshipType(field)(using ctx))) | ||
| }.toMap | ||
| } | ||
|
BartekBH marked this conversation as resolved.
|
||
|
|
||
| private def buildFields(sym: DocumentedSymbol): Map[String, String] = { | ||
| sym.declarations.map(f => (f.name.toString, shortTypeName(typeOfTerm(f)))).toMap | ||
| } | ||
|
|
||
| private def isFieldRelatedToSymbol(field: TermSymbol, symbol: Symbol)(using ctx: Context): Boolean = { | ||
| termTypeSymbol(field)(using ctx).contains(symbol) || isTypeArgument(field, symbol)(using ctx) | ||
| } | ||
|
|
||
| private def isTypeArgument(field: TermSymbol, symbol: Symbol)(using ctx: Context): Boolean = { | ||
| def checkType(tp: Type): Boolean = tp match { | ||
| case at: AppliedType => | ||
| at.args.exists { | ||
| case argType: Type => checkType(argType) | ||
| case _ => false | ||
| } | ||
| case tr: TypeRef => tr.optSymbol.contains(symbol) | ||
| case _ => false | ||
| } | ||
|
|
||
| checkType(typeOfTerm(field)) | ||
| } | ||
|
|
||
| private def determineRelationshipType(field: TermSymbol)(using ctx: Context): Association = { | ||
| val fieldType = typeOfTerm(field).dealias | ||
| val optionCls = ctx.findTopLevelClass("scala.Option") | ||
| val iterableOnceCls = ctx.findTopLevelClass("scala.collection.IterableOnce") | ||
|
|
||
| if (fieldType.baseType(optionCls).isDefined) Association.ZeroOrOne | ||
| else if (fieldType.baseType(iterableOnceCls).isDefined) Association.ZeroOrMore | ||
| else Association.ExactlyOne | ||
| } | ||
|
|
||
| private def typeOfTerm(t: TermSymbol): Type = t.tree match { | ||
| case Some(v: ValDef) => v.tpt.toType | ||
| case Some(d: DefDef) => d.resultTpt.toType | ||
| case Some(other) => throw new Exception(s"Unexpected tree type for $t: ${other.getClass.getSimpleName}") | ||
| case None => throw new Exception(s"No tree found for term symbol $t") | ||
| } | ||
|
|
||
| private def termTypeSymbol(t: TermSymbol)(using ctx: Context): Option[Symbol] = typeOfTerm(t).dealias match { | ||
| case tr: TypeRef => tr.optSymbol | ||
| case _ => None | ||
| } | ||
|
|
||
| private def shortTypeName(tp: Type): String = | ||
| tp.showBasic.replaceAll("\\b(?:[A-Za-z_][$\\w]*\\.)+([A-Za-z_][$\\w]*)\\b", "$1") | ||
| } | ||
9 changes: 9 additions & 0 deletions
9
domainDocs4s-core/src/main/scala/domaindocs4s/output/diagram/Direction.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package domaindocs4s.output.diagram | ||
|
|
||
| enum Direction { | ||
| case TB | ||
| case BT | ||
| case LR | ||
| case RL | ||
| case None | ||
| } |
32 changes: 32 additions & 0 deletions
32
domainDocs4s-core/src/main/scala/domaindocs4s/output/diagram/Entity.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package domaindocs4s.output.diagram | ||
|
|
||
| case class Entity( | ||
| name: String, | ||
| fields: Map[String, String], | ||
| associations: Map[String, Association], | ||
| ) { | ||
|
BartekBH marked this conversation as resolved.
|
||
|
|
||
| def asMarkdown: String = { | ||
| val entityDefinition = renderEntityDefinition | ||
| val entityAssociations = renderAssociations | ||
| escapeMermaidSpecialChars(entityDefinition + entityAssociations) | ||
| } | ||
|
|
||
| private def renderEntityDefinition: String = | ||
| if (fields.isEmpty) name | ||
| else name + fields.map((fieldName, fieldType) => s"$fieldName $fieldType").mkString(" {\n ", "\n ", "\n}") | ||
|
|
||
| private def renderAssociations: String = | ||
| associations.map((childName, relType) => s"\n$name ${associationAsMermaid(relType)} $childName : has").mkString | ||
|
|
||
| private def associationAsMermaid(rel: Association): String = rel match { | ||
| case Association.ExactlyOne => "||--||" | ||
| case Association.ZeroOrOne => "||--o|" | ||
| case Association.ZeroOrMore => "||--o{" | ||
| } | ||
|
|
||
| private def escapeMermaidSpecialChars(text: String): String = | ||
| text | ||
| .replace("\"", "\\\"") // Escape double quotes | ||
| .replace("$", "") // Remove dollar signs to prevent Mermaid parsing issues | ||
| } | ||
2 changes: 1 addition & 1 deletion
2
...ain/scala/domaindocs4s/output/Entry.scala → .../domaindocs4s/output/glossary/Entry.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package domaindocs4s.output | ||
| package domaindocs4s.output.glossary | ||
|
|
||
| import scala.math.Ordering.Implicits.given | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 43 additions & 18 deletions
61
domainDocs4s-examples/src/main/scala/domaindocs4s/banking/Main.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,52 @@ | ||
| package domaindocs4s.banking | ||
|
|
||
| import domaindocs4s.collector.{DomainDocsContext, TastyContext, TastyQueryCollector} | ||
| import domaindocs4s.output.Glossary | ||
| object Main extends App { | ||
|
|
||
| object Main { | ||
| // start_collector | ||
| import domaindocs4s.collector.{DomainDocsContext, TastyContext, TastyQueryCollector} | ||
|
|
||
| def main(args: Array[String]): Unit = { | ||
| // Setup collector | ||
| given DomainDocsContext = TastyContext.fromCurrentProcess() | ||
| val collector = new TastyQueryCollector | ||
|
|
||
| // 1) Setup collector | ||
| given DomainDocsContext = TastyContext.fromCurrentProcess() | ||
| val collector = new TastyQueryCollector | ||
| // Collect documentation from your domain package | ||
| val docs = collector | ||
| .collectSymbols("domaindocs4s.banking.application") | ||
| // end_collector | ||
|
|
||
| // 2) Collect documentation model | ||
| val docs = collector | ||
| .collectSymbols("domaindocs4s.banking") | ||
| // start_glossary | ||
| import domaindocs4s.output.glossary.Glossary | ||
|
|
||
| // 3) Generate glossary markdown (or another supported output and format) | ||
| val glossary = | ||
| Glossary | ||
| .build(docs) // build glossary | ||
| .asMarkdown // render markdown | ||
| // Build glossary | ||
| val glossary = Glossary.build(docs) | ||
| // end_glossary | ||
|
|
||
| // 4) Write the result to file | ||
| Glossary.write(glossary, "glossary.md") | ||
| } | ||
| // start_md_glossary | ||
| import domaindocs4s.output.Writer | ||
|
|
||
| val glossaryMd = glossary.asMarkdown | ||
| Writer(glossaryMd, "glossary.md") | ||
| // end_md_glossary | ||
|
|
||
| // start_html_glossary | ||
| import domaindocs4s.output.Writer | ||
|
|
||
| val glossaryHtml = glossary.asHtml | ||
| Writer(glossaryHtml, "glossary.html") | ||
| // end_html_glossary | ||
|
|
||
| // start_diagram | ||
| // Build diagram and render it as markdown | ||
| import domaindocs4s.output.diagram.Diagram | ||
| import domaindocs4s.output.diagram.Direction | ||
| import domaindocs4s.output.Writer | ||
|
|
||
| val diagram = | ||
| Diagram | ||
| .build(docs) | ||
| .asMarkdown(Direction.TB) | ||
|
|
||
| // Write diagram to file | ||
| Writer(diagram, "diagram.md") | ||
| // end_diagram | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.