Skip to content

Commit 9d02f3c

Browse files
author
Krystian Panek
committed
Features, features...
1 parent ec60231 commit 9d02f3c

File tree

6 files changed

+69
-11
lines changed

6 files changed

+69
-11
lines changed

src/main/kotlin/com/cognifide/gradle/environment/docker/Container.kt

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,36 @@ class Container(val docker: Docker, val name: String) {
182182
this.exitCodes = exitCode?.run { listOf(this) } ?: listOf()
183183
}
184184

185-
fun ensureDir(vararg paths: String) = paths.forEach { path ->
186-
execShell("Ensuring directory at path '$path'", "mkdir -p $path")
185+
fun ensureFile(vararg paths: String) = ensureFile(paths.asIterable())
186+
187+
fun ensureFile(paths: Iterable<String>) {
188+
ensureDir(paths.map { it.substringBeforeLast("/") })
189+
190+
val command = "touch ${paths.joinToString(" ")}"
191+
when (paths.count()) {
192+
1 -> execShell("Ensuring file '${paths.first()}'", command)
193+
else -> execShell("Ensuring files (${paths.count()})", command)
194+
}
187195
}
188196

189-
fun cleanDir(vararg paths: String) = paths.forEach { path ->
190-
execShell("Cleaning directory contents at path '$path'", "rm -fr $path/*")
197+
fun ensureDir(vararg paths: String) = ensureDir(paths.toList())
198+
199+
fun ensureDir(paths: Iterable<String>) {
200+
val command = "mkdir -p ${paths.joinToString(" ")}"
201+
when (paths.count()) {
202+
1 -> execShell("Ensuring directory '${paths.first()}'", command)
203+
else -> execShell("Ensuring directories (${paths.count()})", command)
204+
}
205+
}
206+
207+
fun cleanDir(vararg paths: String) = cleanDir(paths.asIterable())
208+
209+
fun cleanDir(paths: Iterable<String>) {
210+
val command = "rm -fr ${paths.joinToString(" ") { "$it/*" }}"
211+
when (paths.count()) {
212+
1 -> execShell("Cleaning contents of directory at path '${paths.first()}'", command)
213+
else -> execShell("Cleaning contents of directories (${paths.count()})", command)
214+
}
191215
}
192216

193217
private fun exec(spec: ExecSpec): DockerResult {

src/main/kotlin/com/cognifide/gradle/environment/docker/ContainerManager.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ class ContainerManager(private val docker: Docker) {
66

77
val defined = mutableListOf<Container>()
88

9+
val dependent = common.obj.boolean {
10+
convention(true)
11+
common.prop.boolean("environment.docker.container.dependent")?.let { set(it) }
12+
}
13+
914
/**
1015
* Define container.
1116
*/
@@ -51,14 +56,22 @@ class ContainerManager(private val docker: Docker) {
5156
fun up() {
5257
common.progress {
5358
message = "Configuring container(s): ${defined.names}"
54-
common.parallel.each(defined) { it.up() }
59+
if (dependent.get()) {
60+
defined.forEach { it.up() }
61+
} else {
62+
common.parallel.each(defined) { it.up() }
63+
}
5564
}
5665
}
5766

5867
fun reload() {
5968
common.progress {
6069
message = "Reloading container(s): ${defined.names}"
61-
common.parallel.each(defined) { it.reload() }
70+
if (dependent.get()) {
71+
defined.forEach { it.reload() }
72+
} else {
73+
common.parallel.each(defined) { it.reload() }
74+
}
6275
}
6376
}
6477
}

src/main/kotlin/com/cognifide/gradle/environment/docker/Docker.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class Docker(val environment: EnvironmentExtension) {
4343

4444
val composeTemplateFile = common.obj.relativeFile(environment.sourceDir, "docker-compose.yml.peb")
4545

46+
val composeProperties = common.obj.map<String, Any?> { convention(mapOf()) }
47+
4648
// Shorthands useful to be used in template: 'docker-compose.yml.peb'
4749

4850
val configPath get() = runtime.determinePath(environment.sourceDir.get().asFile)
@@ -68,7 +70,10 @@ class Docker(val environment: EnvironmentExtension) {
6870

6971
targetFile.takeIf { it.exists() }?.delete()
7072
templateFile.copyTo(targetFile)
71-
common.prop.expand(targetFile, mapOf("docker" to this))
73+
common.prop.expand(targetFile, composeProperties.get() + mapOf(
74+
"docker" to this,
75+
"project" to common.project
76+
))
7277
}
7378

7479
fun up() {

src/main/kotlin/com/cognifide/gradle/environment/docker/container/HostFileManager.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,29 @@ class HostFileManager(val container: Container) {
3737
return files
3838
}
3939

40+
fun ensureFile(vararg paths: String, content: String = "") = paths.forEach { path ->
41+
ensureDir(path.substringBeforeLast("/"))
42+
file(path).apply {
43+
if (!exists()) {
44+
logger.info("Ensuring file '$this' for container '${container.name}'")
45+
writeText(content)
46+
}
47+
}
48+
}
49+
4050
fun ensureDir() {
4151
rootDir.get().asFile.apply {
4252
logger.info("Ensuring root directory '$this' for container '${container.name}'")
4353
mkdirs()
4454
}
4555
}
4656

47-
fun ensureDir(vararg paths: String) = paths.forEach { path ->
48-
file(path).apply {
49-
logger.info("Ensuring directory '$this' for container '${container.name}'")
50-
mkdirs()
57+
fun ensureDir(vararg paths: String) {
58+
paths.forEach { path ->
59+
file(path).apply {
60+
logger.info("Ensuring directory '$this' for container '${container.name}'")
61+
mkdirs()
62+
}
5163
}
5264
}
5365

src/main/kotlin/com/cognifide/gradle/environment/hosts/Host.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ class Host(val url: String) {
2929
}
3030

3131
fun tag(vararg ids: String) = tag(ids.asIterable())
32+
33+
override fun toString(): String = "Host(url='$url', ip='$ip', tags=$tags)"
3234
}

src/main/kotlin/com/cognifide/gradle/environment/hosts/HostOptions.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class HostOptions(environment: EnvironmentExtension) : Serializable {
3535

3636
operator fun String.invoke(options: Host.() -> Unit = {}) = define(this, options)
3737

38+
operator fun String.invoke(vararg tags: String) = define(this) { tag(tags.asIterable()) }
39+
3840
fun define(url: String, options: Host.() -> Unit = {}) {
3941
defined.add(common.obj.provider { Host(url).apply { ip = ipDefault.get(); options() } })
4042
}

0 commit comments

Comments
 (0)