Skip to content

Commit

Permalink
OSS-252 - add Riddl.toRiddlText
Browse files Browse the repository at this point in the history
  • Loading branch information
reidspencer committed Feb 8, 2025
1 parent 7a6c042 commit ad7fd62
Show file tree
Hide file tree
Showing 33 changed files with 284 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ package com.ossuminc.riddl.commands.hugo
import com.ossuminc.riddl.commands.hugo.themes.ThemeGenerator
import com.ossuminc.riddl.language.AST.*
import com.ossuminc.riddl.passes.symbols.Symbols
import com.ossuminc.riddl.passes.{CollectingPass, CollectingPassOutput, PassInput, PassesOutput, PassesResult}
import com.ossuminc.riddl.passes.{CollectingPass, CollectingPassOutput, PassesOutput, PassesResult, PassInput}
import com.ossuminc.riddl.language.Messages
import com.ossuminc.riddl.passes.PassRoot
import com.ossuminc.riddl.utils.PlatformContext

import java.nio.file.Path
Expand All @@ -25,7 +26,7 @@ case class GlossaryEntry(
)

case class GlossaryOutput(
root: Root,
root: PassRoot,
messages: Messages.Messages,
entries: Seq[GlossaryEntry]
) extends CollectingPassOutput[GlossaryEntry]
Expand Down Expand Up @@ -78,13 +79,13 @@ case class GlossaryPass(
entry
}

def result(root: Root): GlossaryOutput = {
def result(root: PassRoot): GlossaryOutput = {
GlossaryOutput(root, messages.toMessages, collectedValues.toSeq)
}

// Members declared in com.ossuminc.riddl.passes.Pass
def name: String = GlossaryPass.name
override def postProcess(root: Root): Unit = ()
override def postProcess(root: PassRoot): Unit = ()
}

object GlossaryPass {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ object HugoPass extends PassInfo[HugoPass.Options] {
}

case class HugoOutput(
root: Root = Root.empty,
root: PassRoot = Root.empty,
messages: Messages = Messages.empty
) extends PassOutput

Expand All @@ -139,7 +139,7 @@ case class HugoPass(
"Output path is empty"
)

val root: Root = input.root
val root: PassRoot = input.root
val name: String = HugoPass.name

protected val generator: ThemeGenerator = ThemeGenerator(options, input, outputs, messages)
Expand All @@ -152,7 +152,10 @@ case class HugoPass(
messages.addWarning((0, 0), "The input-file option was not provided")
}

private val maybeAuthor = root.authors.headOption.orElse { root.domains.headOption.flatMap(_.authors.headOption) }
private val maybeAuthor =
root.contents.filter[Author].headOption.orElse {
root.contents.filter[Domain].headOption.flatMap(_.authors.headOption)
}
writeConfigToml(options, maybeAuthor)

def makeWriter(parents: Seq[String], fileName: String): MarkdownWriter = {
Expand Down Expand Up @@ -207,12 +210,12 @@ case class HugoPass(
}
}

override def postProcess(root: AST.Root): Unit = {
override def postProcess(root: PassRoot): Unit = {
summarize()
close(root)
}

override def result(root: Root): HugoOutput = HugoOutput(root, messages.toMessages)
override def result(root: PassRoot): HugoOutput = HugoOutput(root, messages.toMessages)

private def deleteAll(directory: File): Boolean = {
if !directory.isDirectory then false
Expand Down Expand Up @@ -336,11 +339,14 @@ case class HugoPass(
}

private def makeSystemLandscapeView: Seq[String] = {
val rod = new RootOverviewDiagram(root)
rod.generate
root match
case r: Root =>
val rod = new RootOverviewDiagram(r)
rod.generate
case _ => Seq.empty
}

private def close(root: Root): Unit = {
private def close(root: PassRoot): Unit = {
Timer.time(s"Writing ${this.files.size} Files") {
writeFiles(pc.options.verbose || pc.options.debug)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import com.ossuminc.riddl.commands.hugo.themes.ThemeGenerator
import com.ossuminc.riddl.language.AST.*
import com.ossuminc.riddl.passes.resolve.ResolutionPass
import com.ossuminc.riddl.passes.symbols.SymbolsPass
import com.ossuminc.riddl.passes.{CollectingPass, CollectingPassOutput, PassCreator, PassInfo, PassInput, PassesOutput}
import com.ossuminc.riddl.passes.{CollectingPass, CollectingPassOutput, PassCreator, PassesOutput, PassInfo, PassInput}
import com.ossuminc.riddl.utils.PlatformContext
import com.ossuminc.riddl.language.Messages
import com.ossuminc.riddl.passes.PassRoot

import scala.collection.mutable

Expand All @@ -36,7 +37,7 @@ case class MessageInfo(
)

case class MessageOutput(
root: Root,
root: PassRoot,
messages: Messages.Messages,
collected: Seq[MessageInfo]
) extends CollectingPassOutput[MessageInfo]
Expand Down Expand Up @@ -89,15 +90,18 @@ case class MessagesPass(input: PassInput, outputs: PassesOutput, options: HugoPa
}
}

override def result(root: Root): MessageOutput = {
override def result(root: PassRoot): MessageOutput = {
val sortedList = collectedValues.sortBy(_.message).toSeq
MessageOutput(root, messages.toMessages, sortedList)
}
}

object MessagesPass extends PassInfo[HugoPass.Options] {
val name: String = "Messages"
def creator(options: HugoPass.Options)(using PlatformContext) = { (in: PassInput, out: PassesOutput) =>
MessagesPass(in, out, options)
def creator(
options: HugoPass.Options
)(using PlatformContext): (PassInput, PassesOutput) => MessagesPass = {
(in: PassInput, out: PassesOutput) =>
MessagesPass(in, out, options)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.ossuminc.riddl.language.AST.{Context, Domain, Entity, Parents, Root}
import com.ossuminc.riddl.passes.symbols.Symbols
import com.ossuminc.riddl.utils.{PlatformContext, Timer}
import com.ossuminc.riddl.diagrams.mermaid.RootOverviewDiagram
import com.ossuminc.riddl.language.AST.Author

import scala.reflect.ClassTag

Expand All @@ -32,8 +33,13 @@ trait Summarizer(using PlatformContext) {
val mdw = this.makeWriter(Seq.empty[String], "_index.md")
mdw.fileHead("Index", 10, Option("The main index to the content"))
mdw.h2("Root Overview")
val diagram = RootOverviewDiagram(root)
mdw.emitMermaidDiagram(diagram.generate)
root match
case r: Root =>
val diagram = RootOverviewDiagram(r)
diagram.generate
mdw.emitMermaidDiagram(diagram.generate)
case _ =>
end match
mdw.h2("Index")
val authors = Seq("[Authors](authors)")
val users = Seq("[Users](users)")
Expand Down Expand Up @@ -90,15 +96,23 @@ trait Summarizer(using PlatformContext) {
}

private def makeUsers(): Unit = {
val users = AST.getUsers(root)
val mdw = makeWriter(Seq.empty[String], fileName = "users.md")
mdw.emitUsers(usersWeight, users)
root match
case root1: Root =>
val users = AST.getUsers(root1)
val mdw = makeWriter(Seq.empty[String], fileName = "users.md")
mdw.emitUsers(usersWeight, users)
case _ =>
end match
}

private def makeAuthors(): Unit = {
val authors = root.authors ++ AST.getAuthors(root)
val mdw = makeWriter(Seq.empty[String], fileName = "authors.md")
mdw.emitAuthors(authorsWeight, authors)
root match
case root1: Root =>
val authors = root1.contents.filter[Author] ++ AST.getAuthors(root1)
val mdw = makeWriter(Seq.empty[String], fileName = "authors.md")
mdw.emitAuthors(authorsWeight, authors)
case _ =>
end match
}

protected def makeMessageSummary(parents: Parents, domain: Domain): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

package com.ossuminc.riddl.commands.hugo
import com.ossuminc.riddl.commands.hugo.themes.ThemeGenerator
import com.ossuminc.riddl.language.{Messages, AST}
import com.ossuminc.riddl.language.{AST, Messages}
import com.ossuminc.riddl.language.AST.*
import com.ossuminc.riddl.passes.{CollectingPass, CollectingPassOutput, PassCreator, PassInfo, PassInput, PassesOutput}
import com.ossuminc.riddl.passes.{CollectingPass, CollectingPassOutput, PassCreator, PassesOutput, PassInfo, PassInput}
import com.ossuminc.riddl.passes.PassRoot
import com.ossuminc.riddl.utils.PlatformContext

import scala.collection.mutable
Expand All @@ -21,7 +22,7 @@ case class ToDoItem(
)

case class ToDoListOutput(
root: Root,
root: PassRoot,
messages: Messages.Messages,
collected: Seq[ToDoItem] = Seq.empty
) extends CollectingPassOutput[ToDoItem]
Expand Down Expand Up @@ -76,7 +77,7 @@ case class ToDoListPass(
}
}

override def result(root: Root): ToDoListOutput = {
override def result(root: PassRoot): ToDoListOutput = {
ToDoListOutput(root, messages.toMessages, collectedValues.toSeq)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.ossuminc.riddl.language.AST
import com.ossuminc.riddl.language.AST.*
import com.ossuminc.riddl.language.parsing.{Keyword, Keywords}
import com.ossuminc.riddl.language.parsing.Keywords.*
import com.ossuminc.riddl.passes.PassRoot
import com.ossuminc.riddl.utils.{PlatformContext, TextFileWriter}

import scala.annotation.unused
Expand Down Expand Up @@ -39,8 +40,17 @@ trait MarkdownWriter(using pc: PlatformContext)
override def toString: String =
s"{name:\"$name\",href:\"$href\",children:[${children.map(_.toString).mkString(",")}]}"

def makeRootIndex(root: Root, indent: Int = 0): Unit =
for { topLevelDomain <- root.domains.sortBy(_.id.value) } do makeDomainIndex(topLevelDomain, indent)
def makeRootIndex(root: PassRoot, indent: Int = 0): Unit = {
root match
case root: Root =>
val tops = root.domains.sortBy(_.id.value)
for { topLevelDomain <- tops } do makeDomainIndex(topLevelDomain, indent)
case nebula: Nebula =>
// TODO: make an index with nebula.contents.toSeq.sortBy(_.id.value)
case vital: VitalDefinition[?] =>
// TODO: make an index with vital.contents.definitions.sortBy(_.id.value)
end match
}
end makeRootIndex

private def makeDomainIndex(domain: Domain, indent: Int = 0): Unit =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ package com.ossuminc.riddl.commands.hugo.mermaid
import com.ossuminc.riddl.diagrams.mermaid.DataFlowDiagram
import com.ossuminc.riddl.language.AST
import com.ossuminc.riddl.language.parsing.RiddlParserInput
import com.ossuminc.riddl.language.AST.Domain
import com.ossuminc.riddl.language.AST.Nebula
import com.ossuminc.riddl.language.AST.Root
import com.ossuminc.riddl.language.AST.VitalDefinition
import com.ossuminc.riddl.passes.PassesResult
import com.ossuminc.riddl.passes.validate.JVMAbstractValidatingTest
import com.ossuminc.riddl.utils.{Await, URL, ec, pc}
import com.ossuminc.riddl.utils.{ec, pc, Await, URL}
import org.scalatest.TestData

import java.nio.file.Path
Expand All @@ -26,7 +30,12 @@ class DataFlowDiagramTest extends JVMAbstractValidatingTest {
case Left(messages) => fail(messages.justErrors.format)
case Right(passesResult: PassesResult) =>
val dfd = DataFlowDiagram(passesResult)
val domains = AST.getTopLevelDomains(passesResult.root)
val domains =
passesResult.root match
case root1: Root => AST.getTopLevelDomains(root1)
case nebula: Nebula => nebula.contents.filter[Domain]
case vital: VitalDefinition[?] => vital.contents.filter[Domain]
end match
val contexts = AST.getContexts(domains.head)
val actual = dfd.generate(contexts.head)
val expected =
Expand Down
Loading

0 comments on commit ad7fd62

Please sign in to comment.