com.avito.utils.File.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of files Show documentation
Show all versions of files Show documentation
Collection of infrastructure libraries and gradle plugins of Avito Android project
package com.avito.utils
import java.io.File
import java.util.Properties
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
public fun File.createOrClear() {
if (exists()) {
writer().use { it.write("") }
} else {
parentFile.mkdirs()
createNewFile()
}
}
public fun File.rewriteNewLineList(listOfData: Iterable) {
rewriteIterable(listOfData) { it.joinToString("\n") }
}
public fun File.rewriteNewLineMap(data: Map) {
rewriteIterable(data) {
it.map { (key, value) -> "$key -> $value" }
.joinToString(separator = "\n")
}
}
public fun File.rewriteIterable(data: T, transform: (T) -> String) {
createOrClear()
writer(Charsets.UTF_8).use { it.write(transform.invoke(data)) }
}
@OptIn(ExperimentalContracts::class)
public fun File?.hasFileContent(): Boolean {
contract {
returns(true) implies (this@hasFileContent != null)
}
if (this == null) return false
return this.exists() && this.isFile && this.length() > 0
}
public fun File.loadProperties(): Properties {
val properties = Properties()
this.bufferedReader().use { reader ->
properties.load(reader)
}
return properties
}
public fun loadProperties(path: String): Properties = File(path).loadProperties()