Skip to content

Commit

Permalink
Tweak examples and visualize rendering (#2697)
Browse files Browse the repository at this point in the history
Makes both a bit prettier in preparation for usage in slides for
Scaladays 2023 Madrid

For the SVG visualization, I replaced the oval node shape with
rectangular nodes, and make it go from left-to-right with the arrows
pointing in the direction of the dataflow. This makes it much easier to
read because nodes tend to be more wide than tall, and the graph tends
to be more wide than deep, so having sibling nodes stack up vertically
ends up being much more compact than having them side-by-side
horizontally
  • Loading branch information
lihaoyi authored Aug 18, 2023
1 parent 0a3fc8b commit b5388e1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 65 deletions.
51 changes: 22 additions & 29 deletions example/tasks/1-task-graph/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,25 @@

import mill._

def mainClass: T[Option[String]] = Some("foo.Foo")

def sources = T.source(millSourcePath / "src")
def resources = T.source(millSourcePath / "resources")

def compile = T {
os.proc("javac", os.walk(sources().path), "-d", T.dest).call(cwd = T.dest)
val allSources = os.walk(sources().path)
os.proc("javac", allSources, "-d", T.dest).call()
PathRef(T.dest)
}

def classPath = T{ Seq(compile(), resources()) }

def jar = T {
for(cp <- classPath()) os.copy(cp.path, T.dest, mergeFolders = true)
os.proc("jar", "-cfe", T.dest / "foo.jar", "foo.Foo", ".").call(cwd = T.dest)
PathRef(T.dest / "foo.jar")
}
def assembly = T {
for(p <- Seq(compile(), resources())) os.copy(p.path, T.dest, mergeFolders = true)

def run(args: String*) = T.command {
val classPathStr = classPath()
.map(_.path)
.mkString(sys.props("path.separator"))
val mainFlags = mainClass().toSeq.flatMap(Seq("-e", _))
os.proc("jar", "-c", mainFlags, "-f", T.dest / s"assembly.jar", ".")
.call(cwd = T.dest)

os.proc("java", "-cp", classPathStr, "foo.Foo", args)
.call(stdout = os.Inherit)
PathRef(T.dest / s"assembly.jar")
}

// This example does not use any of Mill's builtin support for building Java or
Expand All @@ -34,30 +30,27 @@ def run(args: String*) = T.command {

/** Usage
> ./mill run hello # mac/linux
> ./mill show jar
".../out/jar.dest/foo.jar"
> ./mill show assembly
".../out/assembly.dest/assembly.jar"
> java -jar out/jar.dest/foo.jar i am cow
> java -jar out/assembly.dest/assembly.jar i am cow
Foo.value: 31337
args: i am cow
foo.txt resource: My Example Text
> unzip -p out/jar.dest/foo.jar foo.txt
> unzip -p out/assembly.dest/assembly.jar foo.txt
My Example Text
*/

// When you first evaluate `jar` (e.g. via `mill jar` at the command line), it
// will evaluate all the defined targets: `sources`, `allSources`,
// `compile`, `resources` and `jar`.
// When you first evaluate `assembly` (e.g. via `mill assembly` at the command
// line), it will evaluate all the defined targets: `mainClass`, `sources`,
// `compile`, and `assembly`.
//
// Subsequent invocations of `mill jar` will evaluate only as much as is
// Subsequent invocations of `mill assembly` will evaluate only as much as is
// necessary, depending on what input sources changed:
//
// * If the files in `sources` change, it will re-evaluate `allSources`,
// compiling to `compile`, and building the `jar`
// * If the files in `sources` change, it will re-evaluate
// `compile`, and `assembly`
//
// * If the files in `resources` change, it will only re-evaluate `jar` and
// use the cached output of `allSources` and `compile`
// * If the files in `resources` change, it will only re-evaluate `assembly`
// and use the cached output of `compile`
2 changes: 0 additions & 2 deletions example/tasks/1-task-graph/src/Foo.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,4 @@ public static void main(String[] args) throws IOException{
System.out.println("foo.txt resource: " + br.readLine());
}
}


}
38 changes: 11 additions & 27 deletions example/tasks/8-diy-java-modules/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,26 @@ trait DiyJavaModule extends Module{
def moduleDeps: Seq[DiyJavaModule] = Nil
def mainClass: T[Option[String]] = None

def upstreamClasses: T[Seq[PathRef]] = T{
T.traverse(moduleDeps)(_.classPath)().flatten
}

def upstream: T[Seq[PathRef]] = T{ T.traverse(moduleDeps)(_.classPath)().flatten }
def sources = T.source(millSourcePath / "src")

def name = T{ millSourcePath.relativeTo(T.workspace).segments.mkString("-") }

def cpFlag(cp: Seq[PathRef]) =
Seq("-cp", cp.map(_.path).mkString(sys.props("path.separator")))

def compile = T {
val allSources = os.walk(sources().path)
os.proc("javac", cpFlag(upstreamClasses()), allSources, "-d", T.dest)
.call(stdout = os.Inherit, cwd = T.dest)

val cpFlag = Seq("-cp", upstream().map(_.path).mkString(":"))
os.proc("javac", cpFlag, allSources, "-d", T.dest).call()
PathRef(T.dest)
}

def classPath = T{ Seq(compile()) ++ upstreamClasses() }
def classPath = T{ Seq(compile()) ++ upstream() }

def assembly = T {
for(cp <- classPath()) os.copy(cp.path, T.dest, mergeFolders = true)

val mainFlags = mainClass().toSeq.flatMap(Seq("-e", _))
os.proc("jar", "-c", mainFlags, "-f", T.dest / s"${name()}.jar", ".")
.call(stdout = os.Inherit, cwd = T.dest)
os.proc("jar", "-c", mainFlags, "-f", T.dest / s"assembly.jar", ".")
.call(cwd = T.dest)

PathRef(T.dest / s"${name()}.jar")
PathRef(T.dest / s"assembly.jar")
}
}

Expand Down Expand Up @@ -79,25 +70,18 @@ object qux extends DiyJavaModule {
"qux.sources": ".../qux/src"
}
> ./mill showNamed __.name
{
"foo.name": "foo",
"foo.bar.name": "foo-bar",
"qux.name": "qux"
}
> ./mill show qux.assembly
".../out/qux/assembly.dest/qux.jar"
".../out/qux/assembly.dest/assembly.jar"
> java -jar out/qux/assembly.dest/qux.jar
> java -jar out/qux/assembly.dest/assembly.jar
Foo.value: 31337
Bar.value: 271828
Qux.value: 9000
> ./mill show foo.assembly
".../out/foo/assembly.dest/foo.jar"
".../out/foo/assembly.dest/assembly.jar"
> java -jar out/foo/assembly.dest/foo.jar
> java -jar out/foo/assembly.dest/assembly.jar
Foo.value: 31337
Bar.value: 271828
Expand Down
17 changes: 10 additions & 7 deletions main/graphviz/src/mill/main/graphviz/GraphvizTools.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package mill.main.graphviz
import guru.nidi.graphviz.attribute.Style
import guru.nidi.graphviz.attribute.Rank.RankDir
import guru.nidi.graphviz.attribute.{Rank, Shape, Style}
import mill.define.NamedTask
import mill.eval.Graph

import org.jgrapht.graph.{DefaultEdge, SimpleDirectedGraph}
object GraphvizTools {
def apply(targets: Seq[NamedTask[Any]], rs: Seq[NamedTask[Any]], dest: os.Path) = {
Expand Down Expand Up @@ -42,20 +42,23 @@ object GraphvizTools {
val nodes = indexToTask.map(t =>
node(t.ctx.segments.render).`with` {
if (targets.contains(t)) Style.SOLID
else Style.DOTTED
}
else Style.DASHED
}.`with`(Shape.BOX)
)

var g = graph("example1").directed
for (i <- indexToTask.indices) {
val outgoing = for {
for {
e <- edges(i)._2
j = taskToIndex(e)
if jgraph.containsEdge(i, j)
} yield nodes(j)
g = g.`with`(nodes(i).link(outgoing: _*))
} {
g = g.`with`(nodes(j).link(nodes(i)))
}
}

g = g.graphAttr().`with`(Rank.dir(RankDir.LEFT_TO_RIGHT))

val gv = Graphviz.fromGraph(g).totalMemory(100 * 1000 * 1000)
val outputs = Seq(
Format.PLAIN -> "out.txt",
Expand Down

0 comments on commit b5388e1

Please sign in to comment.