net.dankito.utils.os.PackageInstallerDetector.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-utils Show documentation
Show all versions of java-utils Show documentation
Some basic utils needed in many projects
package net.dankito.utils.os
import net.dankito.utils.process.CommandConfig
import net.dankito.utils.process.CommandExecutor
import net.dankito.utils.process.ICommandExecutor
open class PackageInstallerDetector(
protected val commandExecutor: ICommandExecutor = CommandExecutor()
) {
open fun findOsPackageInstaller(): PackageInstaller {
findLinuxPackageInstaller()?.let { packageInstaller ->
return packageInstaller
}
findMacOsPackageInstaller()?.let { packageInstaller ->
return packageInstaller
}
return PackageInstaller.Unknown
}
open fun findLinuxPackageInstaller(): PackageInstaller? {
return when {
isPackageInstallerInstalled("apt-get", "-v") -> PackageInstaller.apt
isPackageInstallerInstalled("dnf", "-v") -> PackageInstaller.dnf
isPackageInstallerInstalled("zypper", "-v") -> PackageInstaller.zypper
isPackageInstallerInstalled("pacman", "--version") -> PackageInstaller.pacman
else -> null
}
}
open fun findMacOsPackageInstaller(): PackageInstaller? {
return when {
isPackageInstallerInstalled("brew", "-v") -> PackageInstaller.HomeBrew
isPackageInstallerInstalled("port", "-v") -> PackageInstaller.MacPorts // TODO: is this command really working
else -> null
}
}
protected open fun isPackageInstallerInstalled(vararg commandArgs: String): Boolean {
val config = CommandConfig(commandArgs.toList(), logErrors = false)
return commandExecutor.executeCommandWithLittleOutput(config).successful
}
}