Application.kt
/**
* Dracon - An IntelliJ-Pijul integration.
* Copyright 2021 JonathanxD <jhrldev@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.github.jonathanxd.dracon.util
import com.github.jonathanxd.dracon.config.PijulSettings
import com.intellij.openapi.components.service
import java.io.File
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
fun findPijul(): String =
service<PijulSettings>().getPathToPijul() ?: findBinary("pijul")
fun findPijulOrNull(): String? =
service<PijulSettings>().getPathToPijul() ?: findBinaryOrNull("pijul")
fun findEditorServer(): String =
service<PijulSettings>().getPathToEditorServer() ?: findBinary("editor-server")
fun findEditorServerOrNull(): String? =
service<PijulSettings>().getPathToEditorServer() ?: findBinaryOrNull("editor-server")
fun findBinary(app: String): String {
return findBinaryOrNull(app) ?: app
}
fun findBinaryOrNull(app: String): String? {
// Works in Windows, Linux and MacOS when app is installed with cargo.
val localPijul = findCargo(app)
// For Homebrew/Linuxbrew installations
val brewPijul = findBrew(app)
// For *nix only.
val usrBinPijul = Paths.get(File.pathSeparator, "usr", "bin", app).asExecutableStringOrNull()
val usrLocalBinPijul = Paths.get(File.pathSeparator, "usr", "local", "bin", app).asExecutableStringOrNull()
val binPijul = Paths.get(File.pathSeparator, "bin", app).asExecutableStringOrNull()
// For Windows only.
// Windows could download Pijul binaries from https://github.com/boringcactus/pijul-windows-builds/releases/latest
// However, Dracon plugin could not detect Pijul outside from these directories, for the cases where
// Pijul is not in these locations, a search through $PATH variable will be made, so if the $PATH is correctly
// configured to point to Pijul executable, then it will be found, this applies to all OSes.
val programFiles = Paths.get("C:"+ File.pathSeparator, "Program Files", app, "$app.exe").asExecutableStringOrNull()
val programFiles86 = Paths.get("C:"+ File.pathSeparator, "Program Files (x86)", app, "$app.exe").asExecutableStringOrNull()
return localPijul
?: brewPijul
?: usrBinPijul
?: usrLocalBinPijul
?: binPijul
?: programFiles
?: programFiles86
?: findExecutableOnPath(app)
}
private fun Path.asExecutableStringOrNull(): String? =
this.existsOrNull()?.isRegularFileOrNull()?.isExecutableOrNull()?.toAbsolutePath()?.toString()
private fun findCargo(app: String): String? {
return System.getProperty("user.dir")?.let {
Paths.get(it, ".cargo", "bin", app)
}?.asExecutableStringOrNull()
}
private fun findBrew(app: String): String? {
return System.getenv("HOMEBREW_PREFIX")?.ifBlank { null }?.ifEmpty { null }?.let {
Paths.get(it, "bin", app)
}?.asExecutableStringOrNull()
}
fun findExecutableOnPath(name: String): String? {
for (dirname in System.getenv("PATH").split(File.pathSeparator)) {
val path = Paths.get(dirname, name)
if (Files.isRegularFile(path) && Files.isExecutable(path)) {
return path.toAbsolutePath().toString()
}
}
return null
}