Skip to content
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

Some unfinished useful tools (path finding, class grouping in class mode, dot generation) #51

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ object ClassMode {
path.elems.span(isPackage) match {
case (packages, next +: _) =>
next.kind match {
case EntityKind.Trait =>
Some(Path(packages :+ next.copy(kind = EntityKind.Class)))
// collapse Module/ModuleClass distinction
case EntityKind.Module | EntityKind.ModuleClass =>
Some(Path(packages :+ next.copy(kind = EntityKind.Class)))
case k if isClassKind(k) =>
Some(Path(packages :+ next))
// collapse Module/ModuleClass distinction
case EntityKind.Module =>
Some(Path(packages :+ next.copy(kind = EntityKind.ModuleClass)))
// ignore strange dependencies on bare terms;
// see https://github.com/lightbend/scala-sculpt/issues/28
case EntityKind.Term if packages.isEmpty =>
Expand Down
62 changes: 62 additions & 0 deletions src/main/scala/com/lightbend/tools/sculpt/model/Cycles.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,66 @@ object Cycles {
.mkString}
.mkString

def path(from: Node, to: Node): Option[Seq[Node]] = {
type Result = (Set[Node], Option[Seq[Node]])
def findPath(current: Node, currentPath: List[Node], visitedNodes: Set[Node]): Result =
if (current == to) Set.empty -> Some((current :: currentPath).reverse)
else {
val newVisited = visitedNodes + current
val targets = current.edgesOut.map(_.to)
val newPath = current :: currentPath
targets.foldLeft((newVisited, None): Result) { (res, next) =>
res match {
case res@(_, Some(_)) => res
case res@(newVisited, None) =>
if (!newVisited(next))
findPath(next, newPath, newVisited)
else
res
}
}
}

findPath(from, Nil, Set.empty)._2
}
def path(nodes: Nodes, from: String, to: String): Option[Seq[Node]] = {
def get(name: String): Node =
nodes.find(_.path.nameString.endsWith(name)).getOrElse(throw new NoSuchElementException(name))

path(get(from), get(to))
}

def pp(nodes: Nodes, from: String, to: String): Unit = {
println("Forward")
path(nodes, from ,to).foreach(_.foreach(n => println(n.path.simpleString)))
println("Backward")
path(nodes, to,from).foreach(_.foreach(n => println(n.path.simpleString)))
}

def dot(graph: Graph): String = {
def nodeFilter(node: Node): Boolean =
node.path.simpleString.contains(":akka.http")
/*!node.path.simpleString.contains(":scala") &&
!node.path.simpleString.contains(":java") &&
!node.path.simpleString.contains(":akka.stream") &&
!node.path.simpleString.contains(":akka.util") &&
!node.path.simpleString.contains(":akka.parboiled2") &&
!node.path.simpleString.contains(":akka.actor")*/

def edgeFilter(edge: Edge): Boolean =
nodeFilter(edge.from) && nodeFilter(edge.to)

def formatNode(node: Node): String = s""""${node.path.simpleString}""""
def formatEdge(edge: Edge): String = s""""${edge.from.path.simpleString}"->"${edge.to.path.simpleString}""""

s"""|digraph "dependency-graph" {
| graph[rankdir="LR"]
| edge [
| arrowtail="none"
| ]
| ${graph.nodes.iterator.filter(nodeFilter).map(formatNode).mkString("\n ")}
| ${graph.edges.iterator.filter(edgeFilter).map(formatEdge).mkString("\n ")}
|}
|""".stripMargin
}
}