Added linux packager info

This commit is contained in:
nathan 2018-10-31 20:35:58 +01:00
parent 793fcc563c
commit 86db1a3411

View File

@ -454,60 +454,107 @@ class OSUtil {
} }
/**
* @return true if the package is installed
*/
public static public static
boolean isPackageInstalled(final String packageName) { class PackageManager {
// dpkg public enum Type {
// dpkg -L libappindicator3 APT("apt install"),
// dpkg-query: package 'libappindicator3' is not installed APTGET("apt-get install"),
boolean is_dpkg = new File("/usr/bin/dpkg").canExecute(); YUM("yum install"),
if (is_dpkg) { PACMAN("pacman -S "),
final ShellExecutor shell = new ShellExecutor(); ;
shell.setExecutable("dpkg");
shell.addArgument("-L");
shell.addArgument(packageName);
shell.start();
String output = shell.getOutput(); private final String installString;
return !output.contains("is not installed");
Type(final String installString) {
this.installString = installString;
}
public
String installString() {
return installString;
}
} }
// rpm public static
// rpm -q libappindicator234 Type get() {
// package libappindicator234 is not installed if (new File("/usr/bin/apt").canExecute()) {
boolean is_rpm = new File("/usr/bin/rpm").canExecute(); return Type.APT;
if (is_rpm) { }
final ShellExecutor shell = new ShellExecutor();
shell.setExecutable("rpm");
shell.addArgument("-q");
shell.addArgument(packageName);
shell.start();
String output = shell.getOutput(); if (new File("/usr/bin/apt-get").canExecute()) {
return !output.contains("is not installed"); return Type.APTGET;
}
if (new File("/usr/bin/yum").canExecute()) {
return Type.YUM;
}
if (new File("/usr/bin/pacman").canExecute()) {
return Type.PACMAN;
}
// default is apt-get, even if it isn't correct
return Type.APTGET;
} }
// pacman /**
// pacman -Qi <packageName> * @return true if the package is installed
// use the exit code to determine if the packages existes on the system or not (0 the package exists, 1 it doesn't) */
boolean is_pacmac = new File("/usr/bin/pacman").canExecute(); public static
if (is_rpm) { boolean isPackageInstalled(final String packageName) {
final ShellExecutor shell = new ShellExecutor(); // dpkg
shell.setExecutable("pacman"); // dpkg -L libappindicator3
shell.addArgument("-Qi"); // dpkg-query: package 'libappindicator3' is not installed
shell.addArgument(packageName); boolean is_dpkg = new File("/usr/bin/dpkg").canExecute();
int start = shell.start(); if (is_dpkg) {
final ShellExecutor shell = new ShellExecutor();
shell.setExecutable("dpkg");
shell.addArgument("-L");
shell.addArgument(packageName);
shell.start();
// 0 the package exists, 1 it doesn't String output = shell.getOutput();
return start == 0; return !output.contains("is not installed");
}
// rpm
// rpm -q libappindicator234
// package libappindicator234 is not installed
boolean is_rpm = new File("/usr/bin/rpm").canExecute();
if (is_rpm) {
final ShellExecutor shell = new ShellExecutor();
shell.setExecutable("rpm");
shell.addArgument("-q");
shell.addArgument(packageName);
shell.start();
String output = shell.getOutput();
return !output.contains("is not installed");
}
// pacman
// pacman -Qi <packageName>
// use the exit code to determine if the packages existes on the system or not (0 the package exists, 1 it doesn't)
boolean is_pacmac = new File("/usr/bin/pacman").canExecute();
if (is_rpm) {
final ShellExecutor shell = new ShellExecutor();
shell.setExecutable("pacman");
shell.addArgument("-Qi");
shell.addArgument(packageName);
int start = shell.start();
// 0 the package exists, 1 it doesn't
return start == 0;
}
return false;
} }
return false;
} }
} }
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")