Skip to content

Commit

Permalink
Separate public API from implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-goral authored and mergify-bot committed Jul 26, 2021
1 parent e269678 commit 6300ce0
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package flank.exection.parallel.plantuml

import flank.exection.parallel.Tasks
import flank.exection.parallel.plantuml.internal.generatePlanUmlFile
import flank.exection.parallel.plantuml.internal.generatePlantUmlString
import java.io.File

/**
* Generates plant uml file and save on drive.
*
* @receiver Source object of execution. Used for generating filename and reducing tasks names.
* @param tasks Tasks relations graph required to generate diagram.
* @param dir optional path to directory for generated file.
*/
fun Any.generatePlanUml(
tasks: Tasks,
dir: String = ""
): File = generatePlanUmlFile(
tasks = tasks,
path = File(dir, javaClass.simpleName).absolutePath + "-execute.puml",
prefixToRemove = javaClass.name
)

/**
* Generates plant uml file and save on drive.
*
* @param tasks Tasks relations graph required to generate diagram.
* @param path Path to generated file.
* @param prefixToRemove Optional prefix to remove from each task name.
*/
fun generatePlanUml(
tasks: Tasks,
path: String,
prefixToRemove: String = "",
): File = generatePlanUmlFile(
tasks = tasks,
path = path,
prefixToRemove = prefixToRemove
)

/**
* Generates plant uml string.
*
* @receiver Source object of execution. Used for reducing tasks names.
* @param tasks Tasks relations graph required to generate diagram.
*/
fun Any.generatePlantUml(
tasks: Tasks
): String = generatePlantUmlString(
tasks = tasks,
prefixToRemove = javaClass.name
)

/**
* Generates plant uml string.
*
* @param tasks Tasks relations graph required to generate diagram.
* @param prefixToRemove Optional prefix to remove from each task name.
*/
fun generatePlantUml(
tasks: Tasks,
prefixToRemove: String = "",
): String = generatePlantUmlString(
tasks = tasks,
prefixToRemove = prefixToRemove
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package flank.exection.parallel.plantuml.internal

import flank.exection.parallel.Tasks
import java.io.File

internal fun generatePlanUmlFile(
tasks: Tasks,
path: String,
prefixToRemove: String,
): File {
val plant = generatePlantUmlString(tasks, prefixToRemove)
println(plant)
return File(path).apply { writeText(plant) }
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
package flank.exection.parallel.plantuml
package flank.exection.parallel.plantuml.internal

import flank.exection.parallel.Parallel
import flank.exection.parallel.Tasks
import java.awt.Color
import java.io.File

infix fun Any.generatePlanUml(
tasks: Tasks,
) {
val name = javaClass.simpleName
val plant = generatePlantUml(tasks)
println(plant)
File("$name-execute.puml").writeText(plant)
}
// =================== Internal API ===================

fun Any.generatePlantUml(
internal fun generatePlantUmlString(
tasks: Tasks,
prefixToRemove: String = "",
): String {
val source = javaClass.name + "$"
val graph: Graph = tasks.associate { task -> task.signature.type to task.signature.args }
val depth: Map<Node, Int> = graph.calculateDepth()
val maxDepth: Int = depth.values.maxOrNull() ?: 0
val colors: Colors = depth.mapValues { (_, value) -> calculateColor(maxDepth, value) }
val name = fun Any.() = javaClass.name.removePrefix(source).replace('$', '.').let { "[$it]" }
val name = fun Any.() = javaClass.name.removePrefix("$prefixToRemove$").replace('$', '.').let { "[$it]" }

return """
@startuml
Expand All @@ -43,6 +35,8 @@ ${graph.printRelations(name)}
""".trimIndent()
}

// =================== Private implementation ===================

private typealias Graph = Map<Node, Set<Node>>
private typealias Node = Parallel.Type<*>
private typealias Colors = Map<Node, String>
Expand Down

0 comments on commit 6300ce0

Please sign in to comment.