-
Notifications
You must be signed in to change notification settings - Fork 32
/
PackageCompose.kt
333 lines (266 loc) · 10.6 KB
/
PackageCompose.kt
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package com.cognifide.gradle.aem.pkg.tasks
import com.cognifide.gradle.aem.AemTask
import com.cognifide.gradle.aem.aem
import com.cognifide.gradle.aem.bundle.BundlePlugin
import com.cognifide.gradle.aem.bundle.tasks.BundleCompose
import com.cognifide.gradle.aem.common.instance.service.pkg.Package
import com.cognifide.gradle.aem.common.pkg.PackageFileFilter
import com.cognifide.gradle.aem.common.pkg.vault.FilterFile
import com.cognifide.gradle.aem.common.pkg.vault.FilterType
import com.cognifide.gradle.aem.common.pkg.vault.VaultDefinition
import com.cognifide.gradle.aem.pkg.tasks.compose.*
import com.cognifide.gradle.common.tasks.ZipTask
import com.cognifide.gradle.common.utils.using
import org.gradle.api.file.CopySpec
import org.gradle.api.file.DuplicatesStrategy
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.provider.ListProperty
import org.gradle.api.tasks.*
@Suppress("TooManyFunctions")
open class PackageCompose : ZipTask(), AemTask {
final override val aem = project.aem
/**
* Shorthand for built CRX package file.
*/
@get:Internal
val composedFile get() = archiveFile.get().asFile
/**
* Shorthand for directory of built CRX package file.
*/
@get:Internal
val composedDir get() = composedFile.parentFile
@Internal
val contentDir = aem.obj.dir { convention(aem.packageOptions.contentDir) }
@Internal
val jcrRootDir = aem.obj.relativeDir(contentDir, Package.JCR_ROOT)
@Internal
val metaDir = aem.obj.relativeDir(contentDir, Package.META_PATH)
/**
* Content path for OSGi bundle jars being placed in CRX package.
*/
@Input
val bundlePath = aem.obj.string { convention(aem.packageOptions.installPath) }
/**
* Controls running tests for built bundles before placing them at [bundlePath].
*/
@Input
val bundleTest = aem.obj.boolean {
convention(true)
aem.prop.boolean("package.bundleTest")?.let { set(it) }
}
/**
* Allows to customize install path or run mode of built bundle to be installed.
* Affects only project having both package and bundle plugins applied.
*/
fun bundleBuilt(options: BundleInstalledBuilt.() -> Unit) {
this.bundleBuiltOptions = options
}
private var bundleBuiltOptions: BundleInstalledBuilt.() -> Unit = {}
/**
* Content path for CRX sub-packages being placed in CRX package being built.
*/
@Input
val nestedPath = aem.obj.string { convention(aem.packageOptions.storagePath) }
/**
* Controls validating built packages before placing them at [nestedPath].
*/
@Input
val nestedValidation = aem.obj.boolean {
convention(true)
aem.prop.boolean("package.nestedValidation")?.let { set(it) }
}
/**
* Defines properties being used to generate CRX package metadata files.
*/
@Nested
val vaultDefinition = VaultDefinition(aem)
fun vaultDefinition(options: VaultDefinition.() -> Unit) {
vaultDefinition.apply(options)
}
@Internal
val vaultDir = aem.obj.relativeDir(contentDir, Package.VLT_PATH)
@Internal
val vaultHooksDir = aem.obj.relativeDir(contentDir, Package.VLT_HOOKS_PATH)
@Internal
val vaultFilterOriginFile = aem.obj.relativeFile(metaDir, "${Package.VLT_DIR}/${FilterFile.ORIGIN_NAME}")
@Internal
val vaultFilterFile = aem.obj.relativeFile(vaultDir, FilterFile.BUILD_NAME)
@Internal
val vaultFilters = aem.obj.boolean { convention(true) }
@Internal
val vaultNodeTypesFile = aem.obj.relativeFile(vaultDir, Package.VLT_NODETYPES_FILE)
@Nested
val bundlesInstalled = aem.obj.list<BundleInstalled> { convention(listOf()) }
@Nested
val packagesNested = aem.obj.list<PackageNested> { convention(listOf()) }
private var definitions = mutableListOf<() -> Unit>()
override fun projectsEvaluated() {
super.projectsEvaluated()
(definitions + definition).forEach { it() }
}
fun fromDefaults() {
withBundleBuilt()
withVaultFilters(vaultFilterOriginFile)
fromMeta(metaDir)
fromRoot(jcrRootDir)
fromBundlesInstalled(bundlesInstalled)
fromPackagesNested(packagesNested)
fromVaultHooks(vaultHooksDir)
}
fun fromMeta(metaDir: Any) {
into(Package.META_PATH) { spec ->
spec.from(metaDir)
fileFilterDelegate(spec)
}
}
fun fromRoot(dir: Any) {
into(Package.JCR_ROOT) { spec ->
spec.from(dir)
fileFilterDelegate(spec)
}
}
fun fromBundlesInstalled(bundles: ListProperty<BundleInstalled>) = bundles.get().forEach { fromArchive(it) }
fun fromPackagesNested(pkgs: ListProperty<PackageNested>) = pkgs.get().forEach { fromArchive(it) }
private fun fromArchive(archive: RepositoryArchive) {
val dirPath = archive.dirPath.map { path ->
when {
archive is BundleInstalled && archive.runMode.isPresent -> "$path.${archive.runMode.get()}"
else -> path
}
}
if (archive.vaultFilter.get()) {
vaultDefinition.filter(dirPath.map { "$it/${archive.fileName.get()}" }) { type = FilterType.FILE }
}
into("${Package.JCR_ROOT}/${dirPath.get()}") { spec ->
spec.from(archive.file)
fileFilterDelegate(spec)
}
}
fun fromVaultHooks(dir: Any) {
into(Package.VLT_HOOKS_PATH) { spec ->
spec.from(dir)
fileFilterDelegate(spec)
}
}
fun withBundleBuilt() {
if (!project.plugins.hasPlugin(BundlePlugin::class.java)) {
return
}
val compose = project.tasks.named(BundleCompose.NAME, BundleCompose::class.java)
dependsOn(compose)
bundlesInstalled.add(BundleInstalledBuilt(this, compose).apply(bundleBuiltOptions))
if (bundleTest.get()) {
dependsOn(project.tasks.named(JavaPlugin.TEST_TASK_NAME))
}
}
fun withBundlesInstalled(others: ListProperty<BundleInstalled>) {
bundlesInstalled.addAll(others)
}
fun withPackagesNested(others: ListProperty<PackageNested>) {
packagesNested.addAll(others)
}
fun withVaultFilters(file: RegularFileProperty) {
vaultDefinition.filters(file)
}
fun withVaultNodeTypes(file: RegularFileProperty) {
vaultDefinition.nodeTypes(file)
}
fun withVaultDefinition(other: VaultDefinition) {
vaultDefinition.properties.putAll(other.properties)
vaultDefinition.nodeTypeLibs.addAll(other.nodeTypeLibs)
vaultDefinition.nodeTypeLines.addAll(other.nodeTypeLines)
}
fun mergePackageProject(projectPath: String) = mergePackage("$projectPath:$NAME")
fun mergePackage(taskPath: String) = mergePackage(common.tasks.pathed(taskPath))
fun mergePackage(task: TaskProvider<PackageCompose>) {
dependsOn(task)
definitions.add { task.get().merging(this) }
}
fun nestPackage(dependencyNotation: Any, options: PackageNestedResolved.() -> Unit = {}) {
definitions.add { packagesNested.add(PackageNestedResolved(this, dependencyNotation).apply(options)) }
}
fun nestPackageProject(projectPath: String, options: PackageNestedBuilt.() -> Unit = {}) {
nestPackageBuilt("$projectPath:$NAME", options)
if (nestedValidation.get()) {
dependsOn("$projectPath:${PackageValidate.NAME}")
}
}
fun nestPackageBuilt(taskPath: String, options: PackageNestedBuilt.() -> Unit = {}) {
nestPackageBuilt(common.tasks.pathed(taskPath), options)
}
fun nestPackageBuilt(task: TaskProvider<PackageCompose>, options: PackageNestedBuilt.() -> Unit = {}) {
dependsOn(task)
definitions.add { packagesNested.add(PackageNestedBuilt(this, task).apply(options)) }
}
fun installBundle(dependencyNotation: Any, options: BundleInstalledResolved.() -> Unit = {}) {
definitions.add { bundlesInstalled.add(BundleInstalledResolved(this, dependencyNotation).apply(options)) }
}
fun installBundleProject(projectPath: String, options: BundleInstalledBuilt.() -> Unit = {}) {
installBundleBuilt("$projectPath:${BundleCompose.NAME}", options)
if (bundleTest.get()) {
dependsOn("$projectPath:${JavaPlugin.TEST_TASK_NAME}")
}
}
fun installBundleBuilt(taskPath: String, options: BundleInstalledBuilt.() -> Unit = {}) {
installBundleBuilt(common.tasks.pathed(taskPath), options)
}
fun installBundleBuilt(task: TaskProvider<BundleCompose>, options: BundleInstalledBuilt.() -> Unit = {}) {
dependsOn(task)
definitions.add { bundlesInstalled.add(BundleInstalledBuilt(this, task).apply(options)) }
}
private var definition: () -> Unit = {
fromDefaults()
}
/**
* Override default behavior for composing this package.
*/
fun definition(definition: () -> Unit) {
this.definition = definition
}
/**
* Clear default behavior for composing this package.
* After calling this method, particular 'from*()' methods need to be called.
*/
fun noDefaults() = definition {}
private var merging: (PackageCompose) -> Unit = { other ->
other.withVaultFilters(vaultFilterFile)
other.withVaultNodeTypes(vaultNodeTypesFile)
other.withVaultDefinition(vaultDefinition)
other.withBundlesInstalled(bundlesInstalled)
other.withPackagesNested(packagesNested)
other.fromRoot(jcrRootDir)
other.fromVaultHooks(vaultHooksDir)
}
/**
* Override default behavior for merging this package into assembly package.
*/
fun merging(action: (PackageCompose) -> Unit) {
this.merging = action
}
/**
* Add some extra behavior when merging this package into assembly package.
*/
fun merged(action: (PackageCompose) -> Unit) {
val defaultAction = this.merging
this.merging = { other ->
defaultAction(other)
action(other)
}
}
@Nested
val fileFilter = PackageFileFilter(this)
fun fileFilter(configurer: PackageFileFilter.() -> Unit) = fileFilter.using(configurer)
@Internal
var fileFilterDelegate: ((CopySpec) -> Unit) = { fileFilter.filter(it, vaultDefinition.fileProperties) }
init {
group = AemTask.GROUP
description = "Composes CRX package from JCR content and built or resolved OSGi bundles"
archiveBaseName.set(aem.commonOptions.baseName)
destinationDirectory.set(project.layout.buildDirectory.dir(name))
duplicatesStrategy = DuplicatesStrategy.WARN
}
companion object {
const val NAME = "packageCompose"
}
}