/
build.gradle.kts
127 lines (108 loc) · 3.75 KB
/
build.gradle.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import org.example.*
plugins {
base
}
val processTemplates by tasks.registering(ProcessTemplates::class) {
templateEngine = TemplateEngineType.FREEMARKER
sourceFiles.from(fileTree("src/templates"))
templateData.name = "test"
templateData.variables = mapOf("year" to "2012")
outputDir = file(layout.buildDirectory.dir("genOutput"))
}
interface Injected {
@get:Inject val fs: FileSystemOperations
}
// tag::ad-hoc-task[]
tasks.register("processTemplatesAdHoc") {
inputs.property("engine", TemplateEngineType.FREEMARKER)
inputs.files(fileTree("src/templates"))
.withPropertyName("sourceFiles")
.withPathSensitivity(PathSensitivity.RELATIVE)
inputs.property("templateData.name", "docs")
inputs.property("templateData.variables", mapOf("year" to "2013"))
outputs.dir(layout.buildDirectory.dir("genOutput2"))
.withPropertyName("outputDir")
// end::ad-hoc-task[]
val buildDirectory = layout.buildDirectory
val objectFactory = project.objects
val injected = objectFactory.newInstance<Injected>()
// tag::ad-hoc-task[]
doLast {
// Process the templates here
// end::ad-hoc-task[]
injected.fs.copy {
into(buildDirectory.dir("genOutput2"))
from(objectFactory.fileTree().from("src/templates"))
expand("year" to "2012")
}
// tag::ad-hoc-task[]
}
}
// end::ad-hoc-task[]
// tag::ad-hoc-task-skip-when-empty[]
tasks.register("processTemplatesAdHocSkipWhenEmpty") {
// ...
inputs.files(fileTree("src/templates") {
include("**/*.fm")
})
.skipWhenEmpty()
.withPropertyName("sourceFiles")
.withPathSensitivity(PathSensitivity.RELATIVE)
.ignoreEmptyDirectories()
// ...
// end::ad-hoc-task-skip-when-empty[]
inputs.property("engine", TemplateEngineType.FREEMARKER)
inputs.property("templateData.name", "docs")
inputs.property("templateData.variables", mapOf("year" to "2013"))
outputs.dir(layout.buildDirectory.dir("genOutput2"))
.withPropertyName("outputDir")
val buildDirectory = layout.buildDirectory
val objectFactory = project.objects
val injected = objectFactory.newInstance<Injected>()
doLast {
injected.fs.copy {
into(buildDirectory.dir("genOutput2"))
from(objectFactory.fileTree().from("src/templates"))
expand("year" to "2013")
}
}
// tag::ad-hoc-task-skip-when-empty[]
}
// end::ad-hoc-task-skip-when-empty[]
// tag::custom-class-runtime-api[]
tasks.register<ProcessTemplates>("processTemplatesWithExtraInputs") {
// ...
// end::custom-class-runtime-api[]
templateEngine = TemplateEngineType.FREEMARKER
sourceFiles.from(fileTree("src/templates"))
templateData.name = "test"
templateData.variables = mapOf("year" to "2014")
outputDir = file(layout.buildDirectory.dir("genOutput3"))
// tag::custom-class-runtime-api[]
inputs.file("src/headers/headers.txt")
.withPropertyName("headers")
.withPathSensitivity(PathSensitivity.NONE)
}
// end::custom-class-runtime-api[]
// tag::inferred-task-dep-via-outputs[]
tasks.register<Zip>("packageFiles") {
from(processTemplates.map { it.outputDir })
}
// end::inferred-task-dep-via-outputs[]
// tag::inferred-task-dep-via-task[]
tasks.register<Zip>("packageFiles2") {
from(processTemplates)
}
// end::inferred-task-dep-via-task[]
// tag::adhoc-destroyable-task[]
tasks.register("removeTempDir") {
val tmpDir = layout.projectDirectory.dir("tmpDir")
destroyables.register(tmpDir)
doLast {
tmpDir.asFile.deleteRecursively()
}
}
// end::adhoc-destroyable-task[]
tasks.build {
dependsOn(processTemplates, "processTemplatesAdHoc", "processTemplatesAdHocSkipWhenEmpty", "processTemplatesWithExtraInputs")
}