Lazily evaluate system properties which require file access

This commit is contained in:
Roman Shevchenko
2012-03-28 12:30:29 +02:00
parent 34bfff2e11
commit 11e5b89329
8 changed files with 71 additions and 37 deletions
@@ -27,9 +27,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.script.ScriptException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@@ -103,12 +101,15 @@ public class ExecUtil {
}
@Nullable
public static String execAndReadLine(@NotNull final List<String> command) {
public static String execAndReadLine(final String... command) {
try {
final ProcessOutput output = execAndGetOutput(command, null);
final List<String> lines = output.getStdoutLines();
if (lines.size() > 0) {
return lines.get(0);
final Process process = new GeneralCommandLine(command).createProcess();
final BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
try {
return reader.readLine();
}
finally {
reader.close();
}
}
catch (Exception ignored) { }
@@ -133,7 +133,7 @@ public class BrowserUtil {
if (SystemInfo.isMac || SystemInfo.isWindows) {
return true;
}
else if (SystemInfo.isUnix && SystemInfo.hasXdgOpen) {
else if (SystemInfo.isUnix && SystemInfo.hasXdgOpen()) {
return true;
}
else if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
@@ -152,7 +152,7 @@ public class BrowserUtil {
else if (SystemInfo.isMac) {
return new String[]{ExecUtil.getOpenCommandPath()};
}
else if (SystemInfo.isUnix && SystemInfo.hasXdgOpen) {
else if (SystemInfo.isUnix && SystemInfo.hasXdgOpen()) {
return new String[]{"xdg-open"};
}
@@ -41,7 +41,7 @@ public class RevealFileAction extends DumbAwareAction {
@NotNull
public static String getActionName() {
return SystemInfo.isMac ? "Reveal in Finder" : "Show in " + SystemInfo.nativeFileManagerName;
return SystemInfo.isMac ? "Reveal in Finder" : "Show in " + SystemInfo.getFileManagerName();
}
@Override
@@ -35,6 +35,7 @@ import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.ui.popup.ListPopup;
import com.intellij.openapi.ui.popup.PopupStep;
import com.intellij.openapi.ui.popup.util.BaseListPopupStep;
import com.intellij.openapi.util.NotNullLazyValue;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.vfs.JarFileSystem;
@@ -51,11 +52,19 @@ import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
public class ShowFilePathAction extends AnAction {
private static final Logger LOG = Logger.getInstance("#com.intellij.ide.actions.ShowFilePathAction");
private static NotNullLazyValue<Boolean> hasNautilusV3 = new NotNullLazyValue<Boolean>() {
@NotNull
@Override
protected Boolean compute() {
final String version = ExecUtil.execAndReadLine("nautilus", "--version");
return version != null && version.startsWith("GNOME nautilus 3");
}
};
@Override
public void update(final AnActionEvent e) {
if (SystemInfo.isMac || !isSupported()) {
@@ -166,7 +175,7 @@ public class ShowFilePathAction extends AnAction {
public static boolean isSupported() {
return SystemInfo.isWindows ||
Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.OPEN) ||
SystemInfo.hasXdgOpen || SystemInfo.hasNautilus;
SystemInfo.hasXdgOpen() || SystemInfo.hasNautilus();
}
/** @deprecated use {@linkplain #openFile(java.io.File)} (to remove in IDEA 13) */
@@ -231,7 +240,7 @@ public class ShowFilePathAction extends AnAction {
return;
}
if (Registry.is("ide.use.nautilus3") && SystemInfo.hasNautilus && hasNautilusV3()) {
if (Registry.is("ide.use.nautilus3") && SystemInfo.hasNautilus() && hasNautilusV3.getValue()) {
if (toSelect != null) {
new GeneralCommandLine("nautilus", toSelect.getCanonicalPath()).createProcess();
}
@@ -242,10 +251,10 @@ public class ShowFilePathAction extends AnAction {
}
final String path = dir.getCanonicalPath();
if (SystemInfo.hasXdgOpen) {
if (SystemInfo.hasXdgOpen()) {
new GeneralCommandLine("/usr/bin/xdg-open", path).createProcess();
}
else if (SystemInfo.hasNautilus) {
else if (SystemInfo.hasNautilus()) {
new GeneralCommandLine("nautilus", path).createProcess();
}
else if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) {
@@ -256,16 +265,6 @@ public class ShowFilePathAction extends AnAction {
}
}
private static Boolean hasNautilusV3 = null;
private static boolean hasNautilusV3() {
if (hasNautilusV3 == null) {
final String version = ExecUtil.execAndReadLine(Arrays.asList("nautilus", "--version"));
hasNautilusV3 = version != null && version.startsWith("GNOME nautilus 3");
}
return hasNautilusV3;
}
@Nullable
private static VirtualFile getFile(final AnActionEvent e) {
return PlatformDataKeys.VIRTUAL_FILE.getData(e.getDataContext());
@@ -44,6 +44,6 @@ public class ShowLogAction extends AnAction implements DumbAware {
@NotNull
public static String getActionName() {
return "Reveal Log in " + SystemInfo.nativeFileManagerName;
return "Reveal Log in " + SystemInfo.getFileManagerName();
}
}
@@ -166,7 +166,7 @@ public class EditorsSplitters extends JPanel {
painter.appendLine("Open a file by name with " + getActionShortcutText("GotoFile")).smaller().withBullet()
.appendLine("Open Recent files with " + getActionShortcutText("RecentFiles")).smaller().withBullet()
.appendLine("Open Navigation Bar with " + getActionShortcutText("ShowNavBar")).smaller().withBullet()
.appendLine("Drag'n'Drop file(s) here from " + SystemInfo.nativeFileManagerName).smaller().withBullet()
.appendLine("Drag'n'Drop file(s) here from " + SystemInfo.getFileManagerName()).smaller().withBullet()
.draw(g, new PairFunction<Integer, Integer, Pair<Integer, Integer>>() {
@Override
public Pair<Integer, Integer> fun(Integer width, Integer height) {
@@ -51,9 +51,6 @@ public class SystemInfo extends SystemInfoRt {
public static final boolean isKDE = _SUN_DESKTOP.contains("kde");
public static final boolean isGnome = _SUN_DESKTOP.contains("gnome");
public static final boolean hasNautilus = isUnix && new File("/usr/bin/nautilus").canExecute();
public static final boolean hasXdgOpen = isUnix && new File("/usr/bin/xdg-open").canExecute();
public static final boolean isMacSystemMenu = isMac && "true".equals(System.getProperty("apple.laf.useScreenMenuBar"));
public static final boolean isFileSystemCaseSensitive = SystemInfoRt.isFileSystemCaseSensitive;
@@ -65,10 +62,46 @@ public class SystemInfo extends SystemInfoRt {
public static final boolean isAMD64 = "amd64".equals(OS_ARCH);
public static final boolean isMacIntel64 = isMac && "x86_64".equals(OS_ARCH);
public static final String nativeFileManagerName = isMac ? "Finder" :
isWindows ? "Explorer" :
hasNautilus ? "Nautilus" :
"File Manager";
/** @deprecated use {@linkplain #hasXdgOpen()} (to remove in IDEA 13) */
public static final boolean hasXdgOpen = false;
private static final NotNullLazyValue<Boolean> ourHasXdgOpen = new AtomicNotNullLazyValue<Boolean>() {
@NotNull
@Override
protected Boolean compute() {
return isUnix && new File("/usr/bin/xdg-open").canExecute();
}
};
public static boolean hasXdgOpen() {
return ourHasXdgOpen.getValue();
}
private static final NotNullLazyValue<Boolean> hasNautilus = new AtomicNotNullLazyValue<Boolean>() {
@NotNull
@Override
protected Boolean compute() {
return isUnix && new File("/usr/bin/nautilus").canExecute();
}
};
public static boolean hasNautilus() {
return hasNautilus.getValue();
}
/** @deprecated use {@linkplain #getFileManagerName()} (to remove in IDEA 13) */
public static final String nativeFileManagerName = "File Manager";
private static final NotNullLazyValue<String> ourFileManagerName = new AtomicNotNullLazyValue<String>() {
@NotNull
@Override
protected String compute() {
return isMac ? "Finder" :
isWindows ? "Explorer" :
hasNautilus() ? "Nautilus" :
"File Manager";
}
};
@NotNull
public static String getFileManagerName() {
return ourFileManagerName.getValue();
}
/**
* Whether IDEA is running under MacOS X version 10.4 or later.
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -95,7 +95,8 @@ public class VcsGeneralConfigurationPanel implements SearchableConfigurable {
}
myPromptsPanel.setSize(myPromptsPanel.getPreferredSize());
myOnPatchCreation.setName((SystemInfo.isMac ? "Reveal patch in" : "Show patch in ") + SystemInfo.nativeFileManagerName + " after creation:");
myOnPatchCreation.setName((SystemInfo.isMac ? "Reveal patch in" : "Show patch in ") +
SystemInfo.getFileManagerName() + " after creation:");
}
public void apply() throws ConfigurationException {