org.danbrough.xtras.extensions.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of plugin Show documentation
Show all versions of plugin Show documentation
Kotlin support for common native libraries.
The newest version!
package org.danbrough.xtras
import org.gradle.api.Project
import org.gradle.kotlin.dsl.environment
import org.gradle.kotlin.dsl.extra
import org.gradle.kotlin.dsl.findByType
import org.gradle.kotlin.dsl.getByName
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
import org.jetbrains.kotlin.gradle.plugin.mpp.Executable
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.NativeBinary
import org.jetbrains.kotlin.gradle.plugin.mpp.NativeBuildType
import org.jetbrains.kotlin.gradle.plugin.mpp.SharedLibrary
import org.jetbrains.kotlin.konan.target.HostManager
import org.jetbrains.kotlin.konan.target.KonanTarget
import java.io.File
import java.util.Locale
fun Project.xtrasPath(path: XtrasPath): File {
//println("Project.xtrasPath::getting ${path.propertyName} from extra")
val pathValue = if (extra.has(path.propertyName)) extra[path.propertyName]?.toString() else null
return if (pathValue == null) if (path == XtrasPath.XTRAS) error("${XtrasPath.XTRAS} not set")
else xtrasPath(XtrasPath.XTRAS).resolve(path.name.lowercase())
else File(pathValue)
}
fun String.capitalized() =
replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
fun String.decapitalized() = replaceFirstChar { it.lowercase(Locale.getDefault()) }
private val hostIsMingw = HostManager.hostIsMingw
val File.mixedPath: String
get() = absolutePath.let {
if (hostIsMingw) it.replace("\\", "/") else it
}
private val Project.cygpath: String
get() = projectProperty("xtras.cygpath") {
xtrasMsysDir.resolveAll("usr", "bin", "cygpath.exe").absolutePath
}
fun Project.unixPath(file: File): String = if (HostManager.hostIsMingw) Runtime.getRuntime()
.exec(arrayOf(cygpath, "-up", file.absolutePath)).inputStream.bufferedReader().readText()
else file.absolutePath
fun Project.pathOf(paths: Collection): String =
paths.mapNotNull {
when (it) {
is File -> unixPath(it)
is Collection<*> -> pathOf(it).ifEmpty { null }
else -> it?.toString()?.ifBlank { null }
}
}.joinToString(File.pathSeparator)
fun Project.pathOf(vararg paths: Any?): String = pathOf(paths.toList())
fun File.resolveAll(vararg paths: String): File = resolveAll(paths.toList())
fun File.resolveAll(paths: List): File =
paths.fold(this) { file, path -> file.resolve(path) }
fun Project.kotlinBinaries(binariesFilter: (NativeBinary) -> Boolean): List =
extensions.findByType()?.targets?.withType()
?.flatMap { it.binaries }
?.filter(binariesFilter)
?: emptyList()
fun Executable.xtrasLibraryPath(): String =
project.pathOf(
project.xtrasExtension.ldLibraryPath(this.target.konanTarget, buildType),
runTask!!.environment[HostManager.host.envLibraryPathName]
)
val NativeBinary.jniLibsDir: File
get() = project.file("src").resolveAll(
if (buildType.debuggable) "debug" else "release",
target.konanTarget.androidLibDir!!
)
val NativeBinary.runsOnHost: Boolean
get() = this.target.konanTarget == HostManager.host