forked from wttech/gradle-aem-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InstanceOptions.kt
111 lines (92 loc) · 3.32 KB
/
InstanceOptions.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
package com.cognifide.gradle.aem.common.instance
import com.cognifide.gradle.aem.AemException
import com.cognifide.gradle.aem.AemExtension
import com.fasterxml.jackson.annotation.JsonIgnore
import java.io.Serializable
open class InstanceOptions(private val aem: AemExtension) : Serializable {
/**
* List of AEM instances on which packages could be deployed.
* Instance stored in map ensures name uniqueness and allows to be referenced in expanded properties.
*/
val defined: MutableMap<String, Instance> = mutableMapOf()
/**
* Customize default options for instance services.
*/
fun sync(options: InstanceSync.() -> Unit) {
syncOptions = options
}
@get:JsonIgnore
internal var syncOptions: InstanceSync.() -> Unit = {}
/**
* Define local instance (created on local file system).
*/
fun local(httpUrl: String) {
local(httpUrl) {}
}
/**
* Define local instance (created on local file system).
*/
fun local(httpUrl: String, name: String) {
local(httpUrl) { this.name = name }
}
/**
* Define local instance (created on local file system).
*/
fun local(httpUrl: String, options: LocalInstance.() -> Unit) {
define(LocalInstance.create(aem, httpUrl, options))
}
/**
* Define remote instance (available on any host).
*/
fun remote(httpUrl: String) {
remote(httpUrl) {}
}
/**
* Define remote instance (available on any host).
*/
fun remote(httpUrl: String, name: String) {
remote(httpUrl) { this.name = name }
}
/**
* Define remote instance (available on any host).
*/
fun remote(httpUrl: String, options: RemoteInstance.() -> Unit) {
define(RemoteInstance.create(aem, httpUrl, options))
}
/**
* Get defined instance by name or create temporary definition if URL provided.
*/
fun parse(urlOrName: String): Instance {
return defined[urlOrName] ?: Instance.parse(aem, urlOrName).ifEmpty {
throw AemException("Instance cannot be determined by value '$urlOrName'.")
}.single().apply { validate() }
}
private fun define(instances: Iterable<Instance>) {
instances.forEach { define(it) }
}
private fun define(instance: Instance) {
if (defined.containsKey(instance.name)) {
throw AemException("Instance named '${instance.name}' is already defined. " +
"Enumerate instance types (for example 'author1', 'author2') " +
"or distinguish environments (for example 'local', 'int', 'stg').")
}
defined[instance.name] = instance
}
init {
// Define through command line
val instancesForced = aem.props.string("instance.list") ?: ""
if (instancesForced.isNotBlank()) {
define(Instance.parse(aem, instancesForced) { environment = Instance.ENVIRONMENT_CMD })
}
// Define through properties ]
define(Instance.properties(aem))
aem.project.afterEvaluate { _ ->
// Ensure defaults if still no instances defined at all
if (defined.isEmpty()) {
define(Instance.defaults(aem) { environment = aem.env })
}
// Validate all
defined.values.forEach { it.validate() }
}
}
}