[platform] restart ability check optimization and diagnostic

This commit is contained in:
Roman Shevchenko
2017-03-15 13:47:03 +01:00
parent 1a920de862
commit 897f4715ab
@@ -21,6 +21,8 @@ import com.intellij.ide.actions.CreateDesktopEntryAction;
import com.intellij.jna.JnaLoader;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.AtomicNotNullLazyValue;
import com.intellij.openapi.util.NotNullLazyValue;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.io.FileUtilRt;
import com.intellij.openapi.util.text.StringUtil;
@@ -43,24 +45,62 @@ public class Restarter {
private Restarter() { }
public static boolean isSupported() {
if (SystemInfo.isWindows) {
return JnaLoader.isLoaded() &&
new File(PathManager.getBinPath(), "restarter.exe").exists();
}
return ourRestartSupported.getValue();
}
if (SystemInfo.isMac) {
return getMacOsAppDir() != null &&
new File(PathManager.getBinPath(), "restarter").canExecute();
}
private static final NotNullLazyValue<Boolean> ourRestartSupported = new AtomicNotNullLazyValue<Boolean>() {
@NotNull
@Override
protected Boolean compute() {
String problem;
if (SystemInfo.isUnix) {
return UnixProcessManager.getCurrentProcessId() > 0 &&
CreateDesktopEntryAction.getLauncherScript() != null &&
new File(PathManager.getBinPath(), "restart.py").canExecute() &&
PathEnvironmentVariableUtil.findInPath("python") != null;
}
if (SystemInfo.isWindows) {
if (!JnaLoader.isLoaded()) {
problem = "JNA not loaded";
}
else {
problem = checkRestarter("restarter.exe");
}
}
else if (SystemInfo.isMac) {
if (getMacOsAppDir() == null) {
problem = "not a bundle: " + PathManager.getHomePath();
}
else {
problem = checkRestarter("restarter");
}
}
else if (SystemInfo.isUnix) {
if (UnixProcessManager.getCurrentProcessId() <= 0) {
problem = "cannot detect process ID";
}
else if (CreateDesktopEntryAction.getLauncherScript() == null) {
problem = "cannot find launcher script in " + PathManager.getBinPath();
}
else if (PathEnvironmentVariableUtil.findInPath("python") == null) {
problem = "cannot find 'python' in PATH";
}
else {
problem = checkRestarter("restart.py");
}
}
else {
problem = "unknown platform: " + SystemInfo.OS_NAME;
}
return false;
if (problem == null) {
return true;
}
else {
Logger.getInstance(Restarter.class).info("not supported: " + problem);
return false;
}
}
};
private static String checkRestarter(String restarterName) {
File restarter = new File(PathManager.getBinPath(), restarterName);
return restarter.isFile() && restarter.canExecute() ? null : "not an executable file: " + restarter;
}
public static void scheduleRestart(@NotNull String... beforeRestart) throws IOException {