-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #444 from Cognifide/environment-hosts-conventions
Environment hosts conventions
- Loading branch information
Showing
4 changed files
with
103 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 77 additions & 13 deletions
90
src/main/kotlin/com/cognifide/gradle/aem/environment/hosts/HostOptions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,90 @@ | ||
package com.cognifide.gradle.aem.environment.hosts | ||
|
||
class HostOptions { | ||
import com.cognifide.gradle.aem.environment.Environment | ||
import com.cognifide.gradle.aem.environment.EnvironmentException | ||
import net.minidev.json.annotate.JsonIgnore | ||
import java.io.Serializable | ||
|
||
/** | ||
* Manages host definitions in case of different purposes indicated by tags. | ||
*/ | ||
class HostOptions(environment: Environment) : Serializable { | ||
|
||
var defined = mutableListOf<Host>() | ||
|
||
fun define(ip: String, name: String) { | ||
defined.add(Host(ip, name)) | ||
} | ||
@JsonIgnore | ||
var ipDefault = environment.dockerRuntime.hostIp | ||
|
||
fun define(ip: String, vararg names: String) { | ||
define(ip, names.asIterable()) | ||
fun define(url: String, options: Host.() -> Unit = {}) { | ||
defined.add(Host(url).apply { ip = ipDefault; options() }) | ||
} | ||
|
||
fun define(ip: String, names: Iterable<String>) { | ||
names.forEach { define(ip, it) } | ||
} | ||
fun define(vararg urls: String, options: Host.() -> Unit = {}) = define(urls.asIterable(), options) | ||
|
||
fun define(urls: Iterable<String>, options: Host.() -> Unit = {}) = urls.forEach { define(it, options) } | ||
|
||
fun find(vararg tags: String) = find(tags.asIterable()) | ||
|
||
fun find(tags: Iterable<String>) = all(tags).first() | ||
|
||
fun define(vararg values: String) { | ||
define(values.asIterable()) | ||
fun all(vararg tags: String) = all(tags.asIterable()) | ||
|
||
fun all(tags: Iterable<String>) = defined.filter { h -> tags.all { t -> h.tags.contains(t) } }.ifEmpty { | ||
throw EnvironmentException("Environment has no hosts tagged with '$tags'!") | ||
} | ||
|
||
fun define(values: Iterable<String>) { | ||
defined.addAll(values.map { Host.of(it) }) | ||
// ----- DSL shorthands / conventions ----- | ||
|
||
/** | ||
* Get host responsible for accessing AEM author instance. | ||
*/ | ||
@get:JsonIgnore | ||
val author: Host | ||
get() = find(TAG_AUTHOR) | ||
|
||
/** | ||
* Get hosts responsible for accessing AEM author instances. | ||
*/ | ||
@get:JsonIgnore | ||
val authors: List<Host> | ||
get() = all(TAG_AUTHOR) | ||
|
||
fun author(vararg urls: String, options: Host.() -> Unit = {}) = author(urls.asIterable(), options) | ||
|
||
fun author(urls: Iterable<String>, options: Host.() -> Unit = {}) = define(urls) { tag(TAG_AUTHOR); options() } | ||
|
||
/** | ||
* Get host responsible for accessing AEM author instance. | ||
*/ | ||
@get:JsonIgnore | ||
val publish: Host | ||
get() = find(TAG_PUBLISH) | ||
|
||
/** | ||
* Get hosts responsible for accessing AEM publish instances. | ||
*/ | ||
@get:JsonIgnore | ||
val publishes: List<Host> | ||
get() = all(TAG_PUBLISH) | ||
|
||
fun publish(vararg urls: String, options: Host.() -> Unit = {}) = publish(urls.asIterable(), options) | ||
|
||
fun publish(urls: Iterable<String>, options: Host.() -> Unit = {}) = define(urls) { tag(TAG_PUBLISH); options() } | ||
|
||
@get:JsonIgnore | ||
val others: List<Host> | ||
get() = all(TAG_OTHER) | ||
|
||
fun other(vararg urls: String, options: Host.() -> Unit = {}) = other(urls.asIterable(), options) | ||
|
||
fun other(urls: Iterable<String>, options: Host.() -> Unit = {}) = define(urls) { tag(TAG_OTHER); options() } | ||
|
||
companion object { | ||
|
||
val TAG_AUTHOR = "author" | ||
|
||
val TAG_PUBLISH = "publish" | ||
|
||
val TAG_OTHER = "other" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters