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

net.dankito.utils.os.PackageInstallerDetector.kt Maven / Gradle / Ivy

There is a newer version: 1.0.20
Show newest version
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
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy