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
+ }
0 commit comments