geedeecee: fix DockerEnvUpdateTask
This commit is contained in:
parent
c01c19a8e2
commit
b171f88435
5 changed files with 101 additions and 29 deletions
|
|
@ -1,27 +1,95 @@
|
|||
package dev.adamko.geedeecee
|
||||
|
||||
import java.io.File
|
||||
import java.nio.charset.Charset
|
||||
import java.util.Properties
|
||||
import javax.inject.Inject
|
||||
import org.gradle.api.DefaultTask
|
||||
import org.gradle.api.file.RegularFileProperty
|
||||
import org.gradle.api.logging.LogLevel
|
||||
import org.gradle.api.model.ObjectFactory
|
||||
import org.gradle.api.provider.MapProperty
|
||||
import org.gradle.api.provider.Property
|
||||
import org.gradle.api.provider.Provider
|
||||
import org.gradle.api.tasks.CacheableTask
|
||||
import org.gradle.api.tasks.Internal
|
||||
import org.gradle.api.tasks.WriteProperties
|
||||
import org.gradle.api.tasks.Input
|
||||
import org.gradle.api.tasks.Optional
|
||||
import org.gradle.api.tasks.OutputFile
|
||||
import org.gradle.api.tasks.TaskAction
|
||||
|
||||
|
||||
@CacheableTask
|
||||
open class DockerEnvUpdateTask @Inject constructor(
|
||||
objects: ObjectFactory
|
||||
) : WriteProperties() {
|
||||
abstract class DockerEnvUpdateTask @Inject constructor(
|
||||
) : DefaultTask() {
|
||||
|
||||
@Internal
|
||||
// internal and not abstract so that it can delegate to `super.outputFile` in `init {}`
|
||||
val dotEnvFile: RegularFileProperty = objects.fileProperty()
|
||||
@get:Input
|
||||
abstract val envProperties: MapProperty<String, String>
|
||||
|
||||
@get:Input
|
||||
@get:Optional
|
||||
abstract val comment: Property<String>
|
||||
|
||||
@get:Input
|
||||
@get:Optional
|
||||
abstract val charset: Property<Charset>
|
||||
|
||||
@get:OutputFile
|
||||
abstract val dotEnvFile: RegularFileProperty
|
||||
|
||||
init {
|
||||
group = GDCPlugin.GCD_TASK_GROUP
|
||||
super.setOutputFile(dotEnvFile)
|
||||
super.getLogging().captureStandardOutput(LogLevel.LIFECYCLE)
|
||||
}
|
||||
|
||||
@TaskAction
|
||||
fun writeProperties() {
|
||||
val dotEnvFile: File = dotEnvFile.get().asFile
|
||||
val charset: Charset = charset.orNull ?: Charsets.UTF_8
|
||||
|
||||
val comment: String = comment.orNull
|
||||
?: " Do not edit manually. This file was created with task '$name'"
|
||||
|
||||
|
||||
dotEnvFile.writer(charset).use { writer ->
|
||||
val properties = Properties()
|
||||
properties.putAll(envProperties.get())
|
||||
properties.store(writer, comment)
|
||||
}
|
||||
|
||||
// make the file reproducible by sorting and filtering lines in the file
|
||||
val lines = dotEnvFile.useLines(charset) { lines ->
|
||||
lines
|
||||
.filterNot { it.isBlank() }
|
||||
.sorted()
|
||||
.toList()
|
||||
}
|
||||
|
||||
// first comment is our comment
|
||||
// second comment is the auto generated timestamp - which we will filter out, so the file is reproducible
|
||||
val parsedComment = lines.firstOrNull { it.isComment() } ?: ""
|
||||
val parsedLines = lines.filterNot { it.isComment() }.joinToString(separator = "\n")
|
||||
|
||||
dotEnvFile.writeText(
|
||||
charset = charset,
|
||||
text = """
|
||||
|$parsedComment
|
||||
|
|
||||
|$parsedLines
|
||||
|
|
||||
""".trimMargin()
|
||||
)
|
||||
}
|
||||
|
||||
fun envProperties(configure: MapProperty<String, String>.() -> Unit) {
|
||||
envProperties.configure()
|
||||
}
|
||||
|
||||
companion object {
|
||||
@JvmName("putProvider")
|
||||
fun <K : Any, V : Any> MapProperty<K, V>.put(entry: Pair<K, Provider<V>>) =
|
||||
put(entry.first, entry.second)
|
||||
|
||||
@JvmName("putValue")
|
||||
fun <K : Any, V : Any> MapProperty<K, V>.put(entry: Pair<K, V>) =
|
||||
put(entry.first, entry.second)
|
||||
|
||||
private fun String.isComment(): Boolean = trim().startsWith('#')
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ abstract class GDCPlugin @Inject constructor(
|
|||
val dockerComposeEnvUpdate by target.tasks.registering(DockerEnvUpdateTask::class) {
|
||||
dotEnvFile.set(gdcSettings.srcDir.file(".env"))
|
||||
|
||||
envProperties.convention(gdcSettings.dotEnv)
|
||||
// properties(
|
||||
// "COMPOSE_PROJECT_NAME" to gdcSettings.composeProjectName,
|
||||
// "KAFKATORIO_VERSION" to gdcSettings.composeProjectVersion,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue