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

io.github.kotools.csv.common.Target.kt Maven / Gradle / Ivy

There is a newer version: 2.2.1
Show newest version
package io.github.kotools.csv.common

import io.github.kotools.csv.writer.Writer
import java.io.File
import java.io.InputStream
import java.nio.file.Path
import kotlin.io.path.Path
import kotlin.io.path.createDirectory
import kotlin.io.path.notExists
import kotlin.io.path.toPath

private val loader: ClassLoader by lazy { ClassLoader.getSystemClassLoader() }

internal fun findTarget(path: String): Target =
    findTargetOrNull(path) ?: error("$path doesn't exist")

internal fun findTargetOrNull(path: String): Target? =
    findFile(path) ?: findStream(path)

internal fun  Writer.createTarget(): Target = loader.getResource("")
    ?.toURI()
    ?.let { Path("${it.toPath()}$folder") }
    ?.also { if (it.notExists()) createDirectoryAt(it) }
    ?.let { File("$it/$file") }
    ?.let(Target::File)
    ?: error("Unable to create file at $filePath")

private fun findFile(path: String): Target.File? = loader.getResource(path)
    ?.let { File(it.path) }
    ?.takeIf(File::exists)
    ?.let(Target::File)

private fun findStream(path: String): Target.Stream? = loader
    .getResourceAsStream(path)
    ?.let(Target::Stream)

private infix fun  Writer.createDirectoryAt(path: Path) {
    path.createDirectory()
    overwrite = true
}

internal sealed class Target {
    class File(val file: java.io.File) : Target()
    class Stream(val stream: InputStream) : Target()
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy