All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dump.DumpDsl.kt Maven / Gradle / Ivy

There is a newer version: 0.4.0
Show newest version
package opensavvy.gradle.vite.base.dump

import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Provider

private const val notConfigured = "(none)"

/**
 * Creates an information dump. See [dump].
 */
class DumpDsl internal constructor() {
	private val sections = ArrayList()

	fun section(name: String, block: SectionDsl.() -> Unit) {
		sections += SectionDsl(name).apply(block)
	}

	override fun toString(): String = buildString {
		val paddingSize = 2 + sections
			.flatMap { it.stored.keys }
			.maxOf { it.length }
		val padding = " ".repeat(paddingSize)

		for (section in sections) {
			appendLine("» ${section.name}")

			for ((title, values) in section.stored) {
				val spacingSize = paddingSize - title.length
				val spacing = " ".repeat(spacingSize)
				appendLine("$title${spacing}${values.firstOrNull() ?: notConfigured}")

				for (value in values.drop(1)) {
					appendLine("$padding$value")
				}
			}

			appendLine()
		}
	}

	inner class SectionDsl internal constructor(internal val name: String) {
		internal val stored = HashMap>()

		private fun Any?.customToString(): String = when (this) {
			is Provider<*> -> map { it.customToString() }.getOrElse(notConfigured)
			else -> toString()
		}

		fun value(title: String, value: Any) {
			when (value) {
				is Iterable<*> -> stored[title] = value.map { it.customToString() }.toList()
				is ListProperty<*> -> value(title, value.getOrElse(emptyList()))
				else -> stored[title] = listOf(value.customToString())
			}
		}
	}
}

/**
 * Creates an information dump.
 *
 * ### Example
 *
 * ```kotlin
 * println(dump {
 *     section("Hello") {
 *         value("Path", "first")
 *         value("Other", "first", "second")
 *     }
 * })
 * ```
 */
fun dump(block: DumpDsl.() -> Unit): String = DumpDsl()
	.apply(block)
	.toString()




© 2015 - 2024 Weber Informatics LLC | Privacy Policy