[platform] reusing the launcher script lookup method (IDEA-CR-14046)

This commit is contained in:
Roman Shevchenko
2016-10-04 12:36:37 +02:00
parent 50e727cbf4
commit 2c38274a24
3 changed files with 42 additions and 39 deletions
@@ -23,7 +23,9 @@ import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.*;
import com.intellij.openapi.application.ApplicationBundle;
import com.intellij.openapi.application.ApplicationNamesInfo;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
@@ -31,6 +33,8 @@ import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.util.AtomicNullableLazyValue;
import com.intellij.openapi.util.NullableLazyValue;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
@@ -52,6 +56,26 @@ import static com.intellij.util.containers.ContainerUtil.newHashMap;
public class CreateDesktopEntryAction extends DumbAwareAction {
private static final Logger LOG = Logger.getInstance("#com.intellij.ide.actions.CreateDesktopEntryAction");
private static final NullableLazyValue<String> ourScript = new AtomicNullableLazyValue<String>() {
@Nullable
@Override
protected String compute() {
String binPath = PathManager.getBinPath();
ApplicationNamesInfo names = ApplicationNamesInfo.getInstance();
String execPath = binPath + '/' + names.getProductName() + ".sh";
if (new File(execPath).canExecute()) return execPath;
execPath = binPath + '/' + names.getProductName().toLowerCase(Locale.US) + ".sh";
if (new File(execPath).canExecute()) return execPath;
execPath = binPath + '/' + names.getScriptName() + ".sh";
if (new File(execPath).canExecute()) return execPath;
return null;
}
};
public static boolean isAvailable() {
return SystemInfo.isUnix && SystemInfo.hasXdgOpen();
}
@@ -123,24 +147,21 @@ public class CreateDesktopEntryAction extends DumbAwareAction {
}
private static File prepare() throws IOException {
String homePath = PathManager.getHomePath();
assert new File(homePath).isDirectory() : "Invalid home path: '" + homePath + "'";
String binPath = homePath + "/bin";
String binPath = PathManager.getBinPath();
assert new File(binPath).isDirectory() : "Invalid bin path: '" + binPath + "'";
String name = ApplicationNamesInfo.getInstance().getFullProductName();
if (PlatformUtils.isIdeaCommunity()) name += " Community Edition";
String iconPath = AppUIUtil.findIcon(binPath);
if (iconPath == null) {
throw new RuntimeException(ApplicationBundle.message("desktop.entry.icon.missing", binPath));
}
String execPath = findScript(binPath);
String execPath = ourScript.getValue();
if (execPath == null) {
throw new RuntimeException(ApplicationBundle.message("desktop.entry.script.missing", binPath));
}
String name = ApplicationNamesInfo.getInstance().getFullProductName();
if (PlatformUtils.isIdeaCommunity()) name += " Community Edition";
String wmClass = AppUIUtil.getFrameClass();
Map<String, String> vars = newHashMap(pair("$NAME$", name), pair("$SCRIPT$", execPath), pair("$ICON$", iconPath), pair("$WM_CLASS$", wmClass));
String content = ExecUtil.loadTemplate(CreateDesktopEntryAction.class.getClassLoader(), "entry.desktop", vars);
@@ -149,22 +170,6 @@ public class CreateDesktopEntryAction extends DumbAwareAction {
return entryFile;
}
@Nullable
private static String findScript(String binPath) {
ApplicationNamesInfo names = ApplicationNamesInfo.getInstance();
String execPath = binPath + '/' + names.getProductName() + ".sh";
if (new File(execPath).canExecute()) return execPath;
execPath = binPath + '/' + names.getProductName().toLowerCase(Locale.US) + ".sh";
if (new File(execPath).canExecute()) return execPath;
execPath = binPath + '/' + names.getScriptName() + ".sh";
if (new File(execPath).canExecute()) return execPath;
return null;
}
private static void install(File entryFile, boolean globalEntry) throws IOException, ExecutionException {
if (globalEntry) {
File script = ExecUtil.createTempExecutableScript(
@@ -213,4 +218,9 @@ public class CreateDesktopEntryAction extends DumbAwareAction {
return myContentPane;
}
}
@Nullable
public static String getLauncherScript() {
return ourScript.getValue();
}
}
@@ -149,9 +149,8 @@ public class CreateLauncherScriptAction extends DumbAwareAction {
}
private static File createLauncherScriptFile() throws IOException, ExecutionException {
String runPath;
if (SystemInfo.isMac) runPath = StringUtil.trimEnd(PathManager.getHomePath(), CONTENTS);
else runPath = PathManager.getBinPath() + "/" + ApplicationNamesInfo.getInstance().getProductName().toLowerCase(Locale.US) + ".sh";
String runPath = SystemInfo.isMac ? StringUtil.trimEnd(PathManager.getHomePath(), CONTENTS) : CreateDesktopEntryAction.getLauncherScript();
if (runPath == null) throw new IOException(ApplicationBundle.message("desktop.entry.script.missing", PathManager.getBinPath()));
ClassLoader loader = CreateLauncherScriptAction.class.getClassLoader();
assert loader != null;
@@ -166,7 +165,7 @@ public class CreateLauncherScriptAction extends DumbAwareAction {
public static String defaultScriptPath() {
String scriptName = ApplicationNamesInfo.getInstance().getScriptName();
if (StringUtil.isEmptyOrSpaces(scriptName)) scriptName = "idea";
if (StringUtil.isEmptyOrSpaces(scriptName)) scriptName = ApplicationNamesInfo.getInstance().getProductName().toLowerCase(Locale.US);
return "/usr/local/bin/" + scriptName;
}
}
@@ -15,8 +15,8 @@
*/
package com.intellij.util;
import com.intellij.ide.actions.CreateDesktopEntryAction;
import com.intellij.jna.JnaLoader;
import com.intellij.openapi.application.ApplicationNamesInfo;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.io.FileUtilRt;
@@ -34,16 +34,10 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
public class Restarter {
private Restarter() { }
private static File getLauncherScript() {
String name = ApplicationNamesInfo.getInstance().getProductName().toLowerCase(Locale.US);
return new File(PathManager.getBinPath(), name + ".sh");
}
public static boolean isSupported() {
if (SystemInfo.isWindows) {
return JnaLoader.isLoaded() && new File(PathManager.getBinPath(), "restarter.exe").exists();
@@ -52,7 +46,7 @@ public class Restarter {
return PathManager.getHomePath().contains(".app") && new File(PathManager.getBinPath(), "restarter").canExecute();
}
if (SystemInfo.isUnix) {
return getLauncherScript().canExecute() && new File(PathManager.getBinPath(), "restart.py").canExecute();
return CreateDesktopEntryAction.getLauncherScript() != null && new File(PathManager.getBinPath(), "restart.py").canExecute();
}
return false;
@@ -139,10 +133,10 @@ public class Restarter {
}
private static void restartOnUnix(String... beforeRestart) throws IOException {
File launcherScript = getLauncherScript();
if (!launcherScript.exists()) throw new IOException("Launcher script not found: " + launcherScript);
String launcherScript = CreateDesktopEntryAction.getLauncherScript();
if (launcherScript == null) throw new IOException("Launcher script not found in " + PathManager.getBinPath());
doScheduleRestart(new File(PathManager.getBinPath(), "restart.py"), commands -> {
commands.add(launcherScript.getPath());
commands.add(launcherScript);
Collections.addAll(commands, beforeRestart);
});
}