Skip to content

Commit 025a6ed

Browse files
author
Armin Motekalem
committed
document-more-complex-task-usecases
1 parent 89604d3 commit 025a6ed

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

example/fundamentals/tasks/3-anonymous-tasks/build.mill

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ def printFileData(fileName: String) = Task.Command {
2323
// anywhere and passed around any way you want, until you finally make use of them
2424
// within a downstream task or command.
2525
//
26+
// Anonymous tasks work well when parameters are known at task definition time. Dynamically
27+
// generated inputs can not be used with Anonymous Tasks, as this would break caching.
28+
//
2629
// While an anonymous task ``foo``'s own output is not cached, if it is used in a
2730
// downstream task `baz` and the upstream task `bar` hasn't changed,
2831
// ``baz``'s cached output will be used and ``foo``'s evaluation will be skipped
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package build
2+
import mill.*
3+
import mill.api.TaskCtx
4+
5+
trait BaseModule extends Module {
6+
def generate = Task {
7+
val output = Task.dest / "generated.txt"
8+
os.write(output, "hello")
9+
PathRef(output)
10+
}
11+
}
12+
13+
// This module uses the base implementation unchanged
14+
// Calling generate here will create:
15+
// out/moduleA/generate.dest/generated.txt
16+
object moduleA extends BaseModule
17+
18+
19+
// When overriding tasks, never modify the parent task's output.
20+
// Instead, create new output in your own task's destination folder
21+
// Here we will generate two files in the output:
22+
// out/moduleB/generate.super/BaseModule.dest/generated.txt (parent output)
23+
// out/moduleB/generate.dest/processed.txt (this module's output)
24+
// Note: We can not over write the original contents of
25+
// generated.txt, because it is in a different output destination
26+
object moduleB extends BaseModule {
27+
override def generate = Task {
28+
val parentResult = super.generate()
29+
val parentContent = os.read(parentResult.path)
30+
val processed = parentContent
31+
.replaceAll("hello", "world")
32+
.trim
33+
34+
writeData(processed, "processed.txt")
35+
}
36+
37+
// Helper function for writing data within a task context
38+
// Note: This is a regular function, not a Task, so it can accept
39+
// runtime parameters. It uses implicit TaskCtx to access Task.dest
40+
private def writeData(data: String, name: String)(implicit ctx: TaskCtx): PathRef = {
41+
val outputPath = ctx.dest / name
42+
os.write(outputPath, data)
43+
PathRef(outputPath)
44+
}
45+
}
46+
47+
// We can also acheive similar results by chaining tasks together.
48+
object moduleC extends BaseModule {
49+
override def generate = Task{
50+
processGenerated()
51+
}
52+
53+
def processGenerated = Task {
54+
val parentResult = super.generate()
55+
val parentContent = os.read(parentResult.path)
56+
val processed = parentContent
57+
.replaceAll("hello", "world")
58+
.trim
59+
val outputPath = Task.dest / "processed.txt"
60+
os.write(outputPath, processed)
61+
PathRef(outputPath)
62+
}
63+
}

website/docs/modules/ROOT/pages/fundamentals/tasks.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,7 @@ include::partial$example/fundamentals/tasks/6-workers.adoc[]
5252

5353
include::partial$example/fundamentals/tasks/3-anonymous-tasks.adoc[]
5454

55+
=== Inheritance and Chaining of Tasks
56+
57+
include::partial$example/fundamentals/tasks/8-inheritance-and-chaining.adoc[]
58+

0 commit comments

Comments
 (0)