-
Notifications
You must be signed in to change notification settings - Fork 32
/
AemExtension.kt
431 lines (354 loc) · 15.9 KB
/
AemExtension.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package com.cognifide.gradle.aem
import com.cognifide.gradle.aem.bundle.BundlePlugin
import com.cognifide.gradle.aem.bundle.tasks.BundleCompose
import com.cognifide.gradle.aem.common.CommonOptions
import com.cognifide.gradle.aem.common.CommonPlugin
import com.cognifide.gradle.aem.common.instance.*
import com.cognifide.gradle.aem.common.instance.service.groovy.GroovyEvaluator
import com.cognifide.gradle.aem.common.instance.service.groovy.GroovyEvalSummary
import com.cognifide.gradle.aem.common.pkg.PackageDefinition
import com.cognifide.gradle.aem.common.pkg.PackageFile
import com.cognifide.gradle.aem.common.pkg.PackageOptions
import com.cognifide.gradle.aem.common.pkg.PackageValidator
import com.cognifide.gradle.aem.common.pkg.vault.FilterFile
import com.cognifide.gradle.aem.instance.*
import com.cognifide.gradle.aem.pkg.PackagePlugin
import com.cognifide.gradle.aem.pkg.tasks.PackageCompose
import com.cognifide.gradle.aem.common.instance.rcp.RcpClient
import com.cognifide.gradle.aem.common.pkg.vault.VaultClient
import com.cognifide.gradle.aem.common.pkg.vault.VaultSummary
import com.cognifide.gradle.aem.pkg.PackageSyncPlugin
import com.cognifide.gradle.common.CommonExtension
import com.cognifide.gradle.common.common
import com.cognifide.gradle.common.utils.Patterns
import java.io.File
import java.io.Serializable
import org.gradle.api.Project
import org.gradle.api.plugins.JavaPlugin
/**
* Core of library, facade for implementing tasks.
*/
@Suppress("TooManyFunctions")
class AemExtension(val project: Project) : Serializable {
// Shorthands
val common = CommonExtension.of(project)
val logger = common.logger
val prop = common.prop
val obj = common.obj
// ===
val commonOptions by lazy { CommonOptions(this) }
/**
* Defines common settings like environment name, line endings when generating files etc
*/
fun common(options: CommonOptions.() -> Unit) {
commonOptions.apply(options)
}
val packageOptions by lazy { PackageOptions(this) }
/**
* Defines common settings for built packages and deployment related behavior.
*/
fun `package`(options: PackageOptions.() -> Unit) {
packageOptions.apply(options)
}
/**
* Defines common settings for built packages and deployment related behavior.
*/
fun pkg(options: PackageOptions.() -> Unit) = `package`(options)
/**
* Read CRX package properties of specified ZIP file.
*/
fun `package`(file: File) = PackageFile(file)
/**
* Read CRX package properties of specified ZIP file.
*/
fun pkg(file: File) = `package`(file)
/**
* Defines instances to work with.
*/
fun instance(options: InstanceManager.() -> Unit) {
instanceManager.apply(options)
}
val instanceManager by lazy { InstanceManager(this) }
/**
* Define common settings valid only for instances created at local file system.
*/
fun localInstance(options: LocalInstanceManager.() -> Unit) = localInstanceManager.apply(options)
val localInstanceManager by lazy { LocalInstanceManager(this) }
/**
* Collection of all java packages from all projects applying bundle plugin.
*
* Use with caution as of this property is eagerly configuring all tasks building bundles.
*/
val bundlesBuilt: List<BundleCompose>
get() = project.rootProject.allprojects
.filter { it.plugins.hasPlugin(BundlePlugin.ID) }
.flatMap { p -> p.common.tasks.getAll<BundleCompose>() }
/**
* Collection of all java packages from all projects applying bundle plugin.
*/
val javaPackages: List<String> get() = bundlesBuilt.mapNotNull { it.javaPackage.orNull }
/**
* Collection of Vault definitions from all packages from all projects applying package plugin.
*
* Use with caution as of this property is eagerly configuring all tasks building packages.
*/
val packagesBuilt: List<PackageCompose>
get() = project.rootProject.allprojects
.filter { it.plugins.hasPlugin(PackagePlugin.ID) }
.flatMap { p -> p.common.tasks.getAll<PackageCompose>() }
/**
* All instances matching default filtering.
*
* @see <https://github.com/Cognifide/gradle-aem-plugin#filter-instances-to-work-with>
*/
val instances get() = filterInstances()
/**
* Work in parallel with instances matching default filtering.
*/
fun instances(consumer: (Instance) -> Unit) = common.parallel.with(instances, consumer)
/**
* Work in parallel with instances which name is matching specified wildcard filter.
*/
fun instances(filter: String, consumer: (Instance) -> Unit) = common.parallel.with(filterInstances(filter), consumer)
/**
* Shorthand method for getting defined instance or creating temporary instance by URL.
*/
fun instance(urlOrName: String): Instance = instanceManager.parse(urlOrName)
/**
* Shorthand method for getting defined instances or creating temporary instances by URLs.
*/
fun instances(urlsOrNames: Iterable<String>): List<Instance> = urlsOrNames.map { instance(it) }
/**
* Get instance from command line parameter named 'instance' which holds instance name or URL.
* If it is not specified, then first instance matching default filtering fill be returned.
*
* Purpose of this method is to easily get any instance to work with (no matter how it will be defined).
*
* @see <https://github.com/Cognifide/gradle-aem-plugin#filter-instances-to-work-with>
*/
val anyInstance: Instance
get() {
val cmdInstanceArg = prop.string("instance")
if (!cmdInstanceArg.isNullOrBlank()) {
return instance(cmdInstanceArg)
}
return findInstance(Instance.FILTER_ANY) ?: Instance.defaultAuthor(this)
}
/**
* Get available instance of any type (most often first defined).
*/
val availableInstance: Instance? get() = instances.asSequence().firstOrNull { it.available }
/**
* Find instance which name is matching wildcard filter specified via command line parameter 'instance.name'.
* By default, this method respects current environment which is used to work only with instances running locally.
*/
fun findInstance(desiredName: String? = prop.string("instance.name"), defaultName: String = "${commonOptions.env.get()}-*"): Instance? {
return filterInstances(desiredName ?: defaultName).firstOrNull()
}
/**
* Get instance which name is matching wildcard filter specified via command line parameter 'instance.name'.
* By default, this method respects current environment which is used to work only with instances running locally.
*
* If instance not found, throws exception.
*/
fun namedInstance(desiredName: String? = prop.string("instance.name"), defaultName: String = "${commonOptions.env.get()}-*"): Instance {
return findInstance(desiredName, defaultName)
?: throw AemException("Instance named '${desiredName ?: defaultName}' is not defined.")
}
/**
* Find all instances which names are matching wildcard filter specified via command line parameter 'instance.name'.
*/
fun filterInstances(nameMatcher: String = prop.string("instance.name") ?: "${commonOptions.env.get()}-*"): List<Instance> {
val all = instanceManager.defined.get()
// Specified by command line should not be filtered
val cmd = all.filter { it.env == Instance.ENV_CMD }
if (cmd.isNotEmpty()) {
return cmd
}
// Defined by build script, via properties or defaults are filterable by name
return all.filter { instance ->
when {
prop.flag("instance.author", "instance.authors") -> instance.author
prop.flag("instance.publish", "instance.publishes", "instance.publishers") -> instance.publish
else -> Patterns.wildcard(instance.name, nameMatcher)
}
}
}
/**
* Get all author instances running on current environment.
*/
val authorInstances: List<Instance> get() = filterInstances().filter { it.author }
val authorInstance: Instance get() = authorInstances.firstOrNull()
?: throw AemException("No author instances defined!")
/**
* Work in parallel with all author instances running on current environment.
*/
fun authorInstances(consumer: (Instance) -> Unit) = common.parallel.with(authorInstances, consumer)
/**
* Get all publish instances running on current environment.
*/
val publishInstances: List<Instance> get() = filterInstances().filter { it.publish }
val publishInstance: Instance get() = publishInstances.firstOrNull()
?: throw AemException("No publish instances defined!")
/**
* Work in parallel with all publish instances running on current environment.
*/
fun publishInstances(consumer: Instance.() -> Unit) = common.parallel.with(publishInstances, consumer)
/**
* Get all local instances.
*/
val localInstances: List<LocalInstance> get() = instances.filterIsInstance(LocalInstance::class.java)
/**
* Work in parallel with all local instances.
*/
fun localInstances(consumer: LocalInstance.() -> Unit) = common.parallel.with(localInstances, consumer)
/**
* Get all remote instances.
*/
val remoteInstances: List<Instance> get() = instances - localInstances
/**
* Work in parallel with all remote instances.
*/
fun remoteInstances(consumer: Instance.() -> Unit) = common.parallel.with(remoteInstances, consumer)
/**
* Get CRX package defined to be built (could not yet exist).
*/
@Suppress("VariableNaming")
val `package`: File get() = common.tasks.get<PackageCompose>(PackageCompose.NAME).composedFile
val pkg: File get() = `package`
/**
* Get all CRX packages defined to be built.
*/
val packages: List<File> get() = common.tasks.getAll<PackageCompose>().map { it.composedFile }
/**
* Get OSGi bundle defined to be built (could not yet exist).
*/
val bundle: File get() = common.tasks.get<BundleCompose>(BundleCompose.NAME).composedFile
/**
* Get all OSGi bundles defined to be built.
*/
val bundles: List<File> get() = common.tasks.getAll<BundleCompose>().map { it.composedFile }
/**
* Shorthand for embedding code inside OSGi bundle being composed when configuration on demand is enabled.
* When this feature is not enabled, it is preferred to use [BundleCompose.embedPackage].
*/
fun bundleEmbed(dependencyNotation: Any, pkgs: Iterable<String>, export: Boolean = false) {
project.dependencies.add(JavaPlugin.COMPILE_ONLY_CONFIGURATION_NAME, dependencyNotation)
common.tasks.named<BundleCompose>(BundleCompose.NAME) {
when {
export -> exportPackage(pkgs)
else -> privatePackage(pkgs)
}
}
}
/**
* Shorthand for embedding code inside OSGi bundle being composed when configuration on demand is enabled.
* When this feature is not enabled, it is preferred to use [BundleCompose.embedPackage].
*/
fun bundleEmbed(dependencyNotation: Any, vararg pkgs: String, export: Boolean = false) = bundleEmbed(dependencyNotation, pkgs.asIterable(), export)
/**
* In parallel, work with services of all instances matching default filtering.
*/
fun sync(action: InstanceSync.() -> Unit) = sync(instances, action)
/**
* In parallel, work with services of all instances matching default filtering.
*/
fun syncInstances(action: InstanceSync.() -> Unit) = sync(action)
/**
* Work with instance services of specified instance.
*/
fun <T> sync(instance: Instance, action: InstanceSync.() -> T) = instance.sync(action)
/**
* Work with instance services of specified instance.
*/
fun <T> syncInstance(instance: Instance, action: InstanceSync.() -> T) = sync(instance, action)
/**
* In parallel, work with services of all specified instances.
*/
fun sync(instances: Iterable<Instance>, action: InstanceSync.() -> Unit) {
common.parallel.with(instances) { this.sync.apply(action) }
}
/**
* In parallel, work with services of all specified instances.
*/
fun syncInstances(instances: Iterable<Instance>, action: InstanceSync.() -> Unit) = sync(instances, action)
/**
* In parallel, work with built packages and services of instances matching default filtering.
*/
fun syncPackages(action: InstanceSync.(File) -> Unit) = syncFiles(instances, packages, action)
/**
* In parallel, work with built OSGi bundles and services of instances matching default filtering.
*/
fun syncBundles(action: InstanceSync.(File) -> Unit) = syncFiles(instances, bundles, action)
/**
* In parallel, work with built packages and services of specified instances.
*/
fun syncFiles(instances: Iterable<Instance>, packages: Iterable<File>, action: InstanceSync.(File) -> Unit) {
packages.forEach { pkg -> // single AEM instance dislikes parallel CRX package / OSGi bundle installation
common.parallel.with(instances) { // but same file could be in parallel deployed on different AEM instances
sync.apply { action(pkg) }
}
}
}
/**
* Build minimal CRX package in-place / only via code.
* All details like Vault properties, archive destination directory, file name are customizable.
*/
fun composePackage(definition: PackageDefinition.() -> Unit): File = PackageDefinition(this).apply(definition).compose()
/**
* Validate any CRX packages.
*/
fun validatePackage(vararg packages: File, options: PackageValidator.() -> Unit = {}) = validatePackage(packages.asIterable(), options)
/**
* Validate any CRX packages.
*/
fun validatePackage(packages: Iterable<File>, options: PackageValidator.() -> Unit = {}) = PackageValidator(this).apply(options).perform(packages)
/**
* Vault filter determined by convention and properties.
*/
val filter: FilterFile get() = FilterFile.default(this)
/**
* Get Vault filter object for specified file.
*/
fun filter(file: File) = FilterFile(file)
/**
* Get Vault filter object for specified path.
*/
fun filter(path: String) = filter(project.file(path))
/**
* Execute any Vault command.
*/
fun vlt(command: String): VaultSummary = vlt { this.command.set(command); run() }
/**
* Execute any Vault command with customized options like content directory.
*/
fun <T> vlt(options: VaultClient.() -> T) = VaultClient(this).run(options)
/**
* Execute any Vault JCR content remote copying with customized options like content directory.
*/
fun <T> rcp(options: RcpClient.() -> T) = RcpClient(this).run(options)
/**
* Execute Groovy script(s) using specified options.
*/
fun <T> groovyEval(options: GroovyEvaluator.() -> T) = GroovyEvaluator(this).run(options)
/**
* Execute Groovy script(s) matching file pattern on AEM instances.
*/
fun groovyEval(scriptPattern: String): GroovyEvalSummary = groovyEval { this.scriptPattern.set(scriptPattern); eval() }
companion object {
const val NAME = "aem"
private val PLUGIN_IDS = listOf(
CommonPlugin.ID,
PackagePlugin.ID,
PackageSyncPlugin.ID,
BundlePlugin.ID,
InstancePlugin.ID,
LocalInstancePlugin.ID
)
fun of(project: Project): AemExtension {
return project.extensions.findByType(AemExtension::class.java)
?: throw AemException("${project.displayName.capitalize()} must have at least one of following plugins applied: $PLUGIN_IDS")
}
}
}
val Project.aem get() = AemExtension.of(this)