forked from wttech/gradle-aem-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PackageCompose.kt
404 lines (325 loc) · 13.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package com.cognifide.gradle.aem.pkg.tasks
import com.cognifide.gradle.aem.AemExtension
import com.cognifide.gradle.aem.AemTask
import com.cognifide.gradle.aem.bundle.tasks.BundleCompose
import com.cognifide.gradle.aem.bundle.BundlePlugin
import com.cognifide.gradle.aem.common.build.DependencyOptions
import com.cognifide.gradle.aem.common.instance.service.pkg.Package
import com.cognifide.gradle.aem.common.pkg.PackageFile
import com.cognifide.gradle.aem.common.pkg.PackageFileFilter
import com.cognifide.gradle.aem.common.pkg.PackageValidator
import com.cognifide.gradle.aem.common.pkg.vlt.FilterFile
import com.cognifide.gradle.aem.common.pkg.vlt.FilterType
import com.cognifide.gradle.aem.common.pkg.vlt.VltDefinition
import com.cognifide.gradle.aem.common.tasks.ZipTask
import com.cognifide.gradle.aem.common.utils.Patterns
import com.cognifide.gradle.aem.pkg.PackagePlugin
import com.cognifide.gradle.aem.pkg.tasks.compose.BundleDependency
import com.cognifide.gradle.aem.pkg.tasks.compose.PackageDependency
import com.cognifide.gradle.aem.pkg.tasks.compose.ProjectMergingOptions
import java.io.File
import org.apache.commons.lang3.StringUtils
import org.gradle.api.Project
import org.gradle.api.file.CopySpec
import org.gradle.api.file.DuplicatesStrategy
import org.gradle.api.tasks.*
import org.gradle.api.tasks.bundling.Jar
@Suppress("TooManyFunctions", "LargeClass")
open class PackageCompose : ZipTask() {
/**
* Shorthand for built CRX package file.
*/
@get:Internal
val composedFile: File
get() = archiveFile.get().asFile
/**
* Shorthand for directory of built CRX package file.
*/
@get:Internal
val composedDir: File
get() = composedFile.parentFile
/**
* Absolute path to JCR content to be included in CRX package.
*
* Must be absolute or relative to current working directory.
*/
@Internal
var contentDir: File = aem.packageOptions.contentDir
/**
* Content path for OSGi bundle jars being placed in CRX package.
*/
@Input
var bundlePath: String = aem.packageOptions.installPath
/**
* Content path for CRX sub-packages being placed in CRX package being built.
*/
@Internal
var packagePath: String = aem.packageOptions.storagePath
@Nested
var validator = PackageValidator(aem)
fun validator(options: PackageValidator.() -> Unit) {
validator.apply(options)
}
@get:InputDirectory
var metaDir = File(contentDir, Package.META_PATH)
/**
* Defines properties being used to generate CRX package metadata files.
*/
@Nested
val vaultDefinition = VltDefinition(aem)
fun vaultDefinition(options: VltDefinition.() -> Unit) {
vaultDefinition.apply(options)
}
@get:Internal
val vaultDir: File
get() = File(contentDir, Package.VLT_PATH)
@get:Internal
val vaultFilterOriginFile: File
get() = File(metaDir, "${Package.VLT_DIR}/${FilterFile.ORIGIN_NAME}")
@get:Internal
val vaultFilterFile: File
get() = File(vaultDir, FilterFile.BUILD_NAME)
@get:Internal
val vaultNodeTypesFile: File
get() = File(vaultDir, Package.VLT_NODETYPES_FILE)
@get:Internal
val vaultNodeTypesSyncFile = aem.packageOptions.nodeTypesSyncFile
@Nested
val fileFilter = PackageFileFilter(aem)
fun fileFilter(configurer: PackageFileFilter.() -> Unit) {
fileFilter.apply(configurer)
}
@Internal
var fileFilterDelegate: ((CopySpec) -> Unit) = { fileFilter.filter(it, fileProperties) }
@get:Internal
val fileProperties
get() = mapOf("definition" to vaultDefinition)
@Internal
var fromConvention = true
private var mergingOptions = ProjectMergingOptions()
fun merging(options: ProjectMergingOptions.() -> Unit) {
this.mergingOptions.apply(options)
}
private var fromProjects = mutableListOf<() -> Unit>()
private var fromTasks = mutableListOf<() -> Unit>()
private val bundleDependencies = mutableListOf<BundleDependency>()
private val packageDependencies = mutableListOf<PackageDependency>()
/**
* Configures extra files to be observed in case of Gradle task caching.
*/
@get:InputFiles
val inputFiles: List<File>
get() = mutableListOf<File>().apply {
add(vaultNodeTypesSyncFile)
addAll(bundleDependencies.flatMap { it.configuration.resolve() })
addAll(packageDependencies.flatMap { it.configuration.resolve() })
}.filter { it.exists() }
override fun projectEvaluated() {
if (fromConvention) {
fromConvention()
}
}
override fun projectsEvaluated() {
fromProjects.forEach { it() }
fromTasks.forEach { it() }
}
@TaskAction
override fun copy() {
vaultDefinition.apply {
ensureDefaults()
if (mergingOptions.vaultFilters && vaultFilterOriginFile.exists()) {
filterElements(vaultFilterOriginFile)
}
if (vaultNodeTypesSyncFile.exists()) {
nodeTypes(vaultNodeTypesSyncFile)
}
}
super.copy()
validator.apply {
workDir = File(composedDir, Package.OAKPAL_OPEAR_PATH)
perform(composedFile)
}
aem.notifier.notify("Package composed", composedFile.name)
}
fun fromConvention() {
fromMeta()
fromProject()
}
fun fromProject() {
fromProject(project, mergingOptions)
}
fun fromProject(path: String, options: ProjectMergingOptions.() -> Unit) = fromProject(path, ProjectMergingOptions().apply(options))
fun fromProject(path: String, options: ProjectMergingOptions = ProjectMergingOptions()) = fromProject(project.project(path), options)
fun fromProjects(pathFilter: String, options: ProjectMergingOptions.() -> Unit) = fromProjects(pathFilter, ProjectMergingOptions().apply(options))
fun fromProjects(pathFilter: String, options: ProjectMergingOptions = ProjectMergingOptions()) {
project.allprojects
.filter { Patterns.wildcard(it.path, pathFilter) }
.forEach { fromProject(it, options) }
}
fun fromSubprojects(options: ProjectMergingOptions.() -> Unit) = fromSubprojects(ProjectMergingOptions().apply(options))
fun fromSubprojects(options: ProjectMergingOptions = ProjectMergingOptions()) {
if (project == project.rootProject) {
fromProjects(":*", options)
} else {
fromProjects("${project.path}:*", options)
}
}
fun fromMeta() = fromMeta(metaDir)
fun fromMeta(metaDir: File) {
into(Package.META_PATH) { spec ->
spec.from(metaDir)
fileFilterDelegate(spec)
}
}
fun fromProject(project: Project, options: ProjectMergingOptions.() -> Unit) = fromProject(project, ProjectMergingOptions().apply(options))
fun fromProject(project: Project, options: ProjectMergingOptions = ProjectMergingOptions()) {
fromProjects.add {
val other by lazy { AemExtension.of(project) }
if (project.plugins.hasPlugin(PackagePlugin.ID)) {
options.composeTasks(other).forEach {
fromPackage(it, options)
}
}
if (project.plugins.hasPlugin(BundlePlugin.ID)) {
options.bundleTasks(other).forEach {
fromBundle(it, options)
}
}
}
}
fun fromPackage(composeTaskPath: String) {
fromPackage(aem.tasks.get(composeTaskPath, PackageCompose::class.java), ProjectMergingOptions())
}
@Suppress("ComplexMethod")
private fun fromPackage(other: PackageCompose, options: ProjectMergingOptions) {
fromTasks.add {
if (this@PackageCompose != other) {
dependsOn(other.dependsOn)
}
if (options.composeContent) {
val contentDir = File(other.contentDir, Package.JCR_ROOT)
if (contentDir.exists()) {
into(Package.JCR_ROOT) { spec ->
spec.from(contentDir)
fileFilterDelegate(spec)
}
}
}
if (options.vaultFilters && other.vaultFilterFile.exists()) {
vaultDefinition.filterElements(other.vaultFilterFile)
}
if (options.vaultNodeTypes && other.vaultNodeTypesFile.exists()) {
vaultDefinition.nodeTypes(other.vaultNodeTypesFile)
}
if (options.vaultProperties) {
other.vaultDefinition.properties.forEach { (name, value) ->
vaultDefinition.properties.putIfAbsent(name, value)
}
}
if (options.vaultHooks) {
val hooksDir = File(other.contentDir, Package.VLT_HOOKS_PATH)
if (hooksDir.exists()) {
into(Package.VLT_HOOKS_PATH) { spec ->
spec.from(hooksDir)
fileFilterDelegate(spec)
}
}
}
if (options.bundleDependent) {
other.bundleDependencies.forEach {
fromJarInternal(it.file, it.installPath, it.vaultFilter)
}
}
if (options.packageDependent) {
other.packageDependencies.forEach {
fromZipInternal(it.file, it.storagePath, it.vaultFilter)
}
}
}
}
fun fromBundle(composeTaskPath: String) {
fromBundle(aem.tasks.get(composeTaskPath, BundleCompose::class.java), ProjectMergingOptions())
}
private fun fromBundle(bundle: BundleCompose, options: ProjectMergingOptions) {
if (options.bundleBuilt) {
fromJar(bundle, Package.bundlePath(bundle.installPath, bundle.installRunMode), bundle.vaultFilter)
}
}
fun fromJar(dependencyOptions: DependencyOptions.() -> Unit, installPath: String? = null, vaultFilter: Boolean? = null) {
val dependency = DependencyOptions.create(aem, dependencyOptions)
bundleDependencies.add(BundleDependency(aem, dependency,
installPath ?: this.bundlePath,
vaultFilter ?: mergingOptions.vaultFilters
))
}
fun fromJar(dependencyNotation: String, installPath: String? = null, vaultFilter: Boolean? = null) {
val dependency = DependencyOptions.create(aem, dependencyNotation)
bundleDependencies.add(BundleDependency(aem, dependency,
installPath ?: this.bundlePath,
vaultFilter ?: mergingOptions.vaultFilters
))
}
fun fromJar(jar: Jar, bundlePath: String? = null, vaultFilter: Boolean? = null) {
fromTasks.add {
dependsOn(jar)
fromJarInternal(jar.archiveFile.get().asFile, bundlePath, vaultFilter)
}
}
fun fromJar(jar: File, bundlePath: String? = null, vaultFilter: Boolean? = null) {
fromJars(listOf(jar), bundlePath, vaultFilter)
}
fun fromJars(jars: Collection<File>, bundlePath: String? = null, vaultFilter: Boolean? = null) {
fromTasks.add {
jars.forEach { fromJarInternal(it, bundlePath, vaultFilter) }
}
}
private fun fromJarInternal(jar: File, bundlePath: String? = null, vaultFilter: Boolean? = null) {
val effectiveBundlePath = bundlePath ?: this.bundlePath
fromArchiveInternal(vaultFilter, effectiveBundlePath, jar)
}
fun fromZip(dependencyOptions: DependencyOptions.() -> Unit, storagePath: String? = null, vaultFilter: Boolean? = null) {
val dependency = DependencyOptions.create(aem) { apply(dependencyOptions); ext = "zip" }
packageDependencies.add(PackageDependency(aem, dependency,
storagePath ?: packagePath,
vaultFilter ?: mergingOptions.vaultFilters
))
}
fun fromZip(dependencyNotation: String, storagePath: String? = null, vaultFilter: Boolean? = null) {
val dependency = DependencyOptions.create(aem, StringUtils.appendIfMissing(dependencyNotation, "@zip"))
packageDependencies.add(PackageDependency(aem, dependency,
storagePath ?: packagePath,
vaultFilter ?: mergingOptions.vaultFilters
))
}
fun fromZip(zip: File, packagePath: String? = null, vaultFilter: Boolean? = null) {
fromZips(listOf(zip), packagePath, vaultFilter)
}
fun fromZips(zips: Collection<File>, packagePath: String? = null, vaultFilter: Boolean? = null) {
fromTasks.add {
zips.forEach { fromZipInternal(it, packagePath, vaultFilter) }
}
}
private fun fromZipInternal(file: File, packagePath: String? = null, vaultFilter: Boolean? = null) {
val effectivePackageDir = aem.packageOptions.storageDir(PackageFile(file))
val effectivePackagePath = "${packagePath ?: this.packagePath}/$effectivePackageDir"
fromArchiveInternal(vaultFilter, effectivePackagePath, file)
}
private fun fromArchiveInternal(vaultFilter: Boolean?, effectivePath: String, file: File) {
if (vaultFilter ?: mergingOptions.vaultFilters) {
vaultDefinition.filter("$effectivePath/${file.name}") { type = FilterType.FILE }
}
into("${Package.JCR_ROOT}/$effectivePath") { spec ->
spec.from(file)
fileFilterDelegate(spec)
}
}
init {
description = "Composes CRX package from JCR content and built OSGi bundles"
archiveBaseName.set(aem.baseName)
destinationDirectory.set(AemTask.temporaryDir(aem.project, name))
duplicatesStrategy = DuplicatesStrategy.WARN
}
companion object {
const val NAME = "packageCompose"
}
}