Fix memory enlarger dialog for new Mac launcher

This commit is contained in:
Maxim Shafirov
2012-12-05 15:38:12 +04:00
parent ecf1fbe68b
commit a48bcfd5e4
3 changed files with 64 additions and 199 deletions
@@ -23,6 +23,7 @@ import com.intellij.ui.components.JBLabel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
public class OutOfMemoryDialog extends DialogWrapper {
public enum MemoryKind {
@@ -60,7 +61,8 @@ public class OutOfMemoryDialog extends DialogWrapper {
memoryKind == MemoryKind.HEAP ? VMOptions.XMX_OPTION_NAME : memoryKind == MemoryKind.PERM_GEN ? VMOptions.PERM_GEN_OPTION_NAME : VMOptions.CODE_CACHE_OPTION_NAME,
ApplicationNamesInfo.getInstance().getProductName()));
final String path = VMOptions.getSettingsFilePath();
File file = VMOptions.getWriteFile();
final String path = file != null ? file.getPath() : null;
if (path != null) {
mySettingsFileHintLabel.setText(DiagnosticBundle.message("diagnostic.out.of.memory.willBeSavedTo", path));
}
@@ -47,18 +47,12 @@ public class VMOptions {
@NonNls private static final Pattern XMX_PATTERN = Pattern.compile(XMX_OPTION + MEM_SIZE_EXPR);
@NonNls private static final Pattern PERM_GEN_PATTERN = Pattern.compile(PERM_GEN_OPTION + MEM_SIZE_EXPR);
@NonNls private static final Pattern CODE_CACHE_PATTERN = Pattern.compile(CODE_CACHE_OPTION + MEM_SIZE_EXPR);
@NonNls private static final Pattern MAC_OS_VM_OPTIONS_PATTERN =
Pattern.compile("(<key>" + MAC_ARCH_VM_OPTIONS + "</key>(?:(?:\\s*)(?:<!--(?:.*)-->(?:\\s*))*)<string>)(.*)(</string>)");
@NonNls private static final String INFO_PLIST = "/Contents/Info.plist";
private static String ourTestPath;
private static boolean ourTestMacOs;
@TestOnly
static void setTestFile(String path, boolean isMacOs) {
static void setTestFile(String path) {
ourTestPath = path;
ourTestMacOs = isMacOs;
}
@TestOnly
@@ -92,15 +86,11 @@ public class VMOptions {
@Nullable
public static String read() {
File file = getFile();
File file = getReadFile();
if (file == null) return null;
try {
String content = FileUtil.loadFile(file);
if (isMacOs()) {
content = extractMacOsVMOptions(content);
}
return content;
return FileUtil.loadFile(file);
}
catch (IOException e) {
LOG.info(e);
@@ -137,32 +127,19 @@ public class VMOptions {
}
private static void writeOption(String option, int value, Pattern pattern) {
File file = getFile();
File file = getWriteFile();
if (file == null) return;
try {
String optionValue = option + value + "m";
String content = FileUtil.loadFile(file);
String vmOptions;
String content = read();
if (isMacOs()) {
vmOptions = extractMacOsVMOptions(content);
if (vmOptions == null) return;
}
else {
vmOptions = content;
content = replace(pattern, content, optionValue, "", "", content + " " + optionValue);
if (file.exists()) {
FileUtil.setReadOnlyAttribute(file.getPath(), false);
}
vmOptions = replace(pattern, vmOptions, optionValue, "", "", vmOptions + " " + optionValue);
if (isMacOs()) {
content = replace(MAC_OS_VM_OPTIONS_PATTERN, content, vmOptions, "$1", "$3", content);
}
else {
content = vmOptions;
}
FileUtil.setReadOnlyAttribute(file.getPath(), false);
FileUtil.writeToFile(file, content.getBytes());
}
catch (IOException e) {
@@ -170,13 +147,6 @@ public class VMOptions {
}
}
@Nullable
private static String extractMacOsVMOptions(String text) {
Matcher m = MAC_OS_VM_OPTIONS_PATTERN.matcher(text);
if (!m.find()) return null;
return m.group(2);
}
private static String replace(Pattern pattern,
String text,
String replacement,
@@ -194,35 +164,76 @@ public class VMOptions {
}
@Nullable
private static File getFile() {
final String path = ourTestPath != null ? ourTestPath : getSettingsFilePath();
return path != null ? new File(path) : null;
private static File getReadFile() {
if (ourTestPath != null) {
return new File(ourTestPath);
}
File custom = getCustomFile(true);
if (custom != null) return custom;
return getDefaultFile();
}
@NonNls
@Nullable
public static String getSettingsFilePath() {
final File f = new File(doGetSettingsFilePath()).getAbsoluteFile();
public static File getWriteFile() {
if (ourTestPath != null) {
return new File(ourTestPath);
}
File custom = getCustomFile(false);
if (custom != null) return custom;
return getDefaultFile();
}
@Nullable
public static File getDefaultFile() {
final File f = new File(doGetSettingsFilePath(false)).getAbsoluteFile();
if (!f.exists()) return null;
try {
return f.getCanonicalPath();
return f.getCanonicalFile();
}
catch (IOException e) {
LOG.debug(e);
return f.getPath();
return f;
}
}
@Nullable
public static File getCustomFile(boolean ifExists) {
if (!SystemInfo.isMac) return null;
final File f = new File(doGetSettingsFilePath(true)).getAbsoluteFile();
if (!f.exists()) {
if (ifExists) return null;
return f;
}
try {
return f.getCanonicalFile();
}
catch (IOException e) {
LOG.debug(e);
return f;
}
}
@NotNull
private static String doGetSettingsFilePath() {
private static String doGetSettingsFilePath(boolean customLocation) {
final String vmOptionsFile = System.getProperty("jb.vmOptionsFile");
if (!StringUtil.isEmptyOrSpaces(vmOptionsFile)) {
return vmOptionsFile;
}
if (SystemInfo.isMac) {
return PathManager.getHomePath() + INFO_PLIST;
if (customLocation) {
return PathManager.getConfigPath() + "/idea.vmoptions";
}
else {
return PathManager.getBinPath() + "/idea.vmoptions";
}
}
final String productName = ApplicationNamesInfo.getInstance().getProductName().toLowerCase();
@@ -230,8 +241,4 @@ public class VMOptions {
final String osSuffix = SystemInfo.isWindows ? ".exe" : "";
return PathManager.getBinPath() + File.separatorChar + productName + platformSuffix + osSuffix + ".vmoptions";
}
private static boolean isMacOs() {
return ourTestPath != null ? ourTestMacOs : SystemInfo.isMac;
}
}
@@ -33,7 +33,7 @@ public class VMOptionsTest extends UsefulTestCase {
myFile = FileUtil.createTempFile("vmoptions.", ".txt");
writeFile("-Xmx512m\n" +
"-XX:MaxPermSize=128m");
VMOptions.setTestFile(myFile.getPath(), false);
VMOptions.setTestFile(myFile.getPath());
}
@Override
@@ -167,150 +167,6 @@ public class VMOptionsTest extends UsefulTestCase {
readFile());
}
public void testReadAndWriteUnderMacOs() throws IOException {
VMOptions.setTestFile(myFile.getPath(), true);
writeFile("<plist version=\"1.0\">\n" +
" <dict>\n" +
" <dict>\n" +
" <key>" + VMOptions.MAC_ARCH_VM_OPTIONS + "</key>\n" +
" <string>-someOption -Xmx512m -XX:MaxPermSize=128m -otherOption</string>\n" +
" </dict>\n" +
" </dict>\n" +
"</plist>");
assertEquals(512, VMOptions.readXmx());
assertEquals(128, VMOptions.readMaxPermGen());
VMOptions.writeXmx(1024);
VMOptions.writeMaxPermGen(256);
assertEquals("<plist version=\"1.0\">\n" +
" <dict>\n" +
" <dict>\n" +
" <key>" + VMOptions.MAC_ARCH_VM_OPTIONS + "</key>\n" +
" <string>-someOption -Xmx1024m -XX:MaxPermSize=256m -otherOption</string>\n" +
" </dict>\n" +
" </dict>\n" +
"</plist>",
readFile());
}
public void testReadAndWriteNewUnderMacOs() throws IOException {
VMOptions.setTestFile(myFile.getPath(), true);
writeFile("<plist version=\"1.0\">\n" +
" <dict>\n" +
" <dict>\n" +
" <key>" + VMOptions.MAC_ARCH_VM_OPTIONS + "</key>\n" +
" <string>-someOption</string>\n" +
" </dict>\n" +
" </dict>\n" +
"</plist>");
assertEquals(-1, VMOptions.readXmx());
assertEquals(-1, VMOptions.readMaxPermGen());
VMOptions.writeXmx(1024);
VMOptions.writeMaxPermGen(256);
assertEquals("<plist version=\"1.0\">\n" +
" <dict>\n" +
" <dict>\n" +
" <key>" + VMOptions.MAC_ARCH_VM_OPTIONS + "</key>\n" +
" <string>-someOption -Xmx1024m -XX:MaxPermSize=256m</string>\n" +
" </dict>\n" +
" </dict>\n" +
"</plist>",
readFile());
}
public void testReadAndWriteUnderMacOsIfThereAreCommentsBetweenTags() throws IOException {
VMOptions.setTestFile(myFile.getPath(), true);
writeFile("<plist version=\"1.0\">\n" +
" <dict>\n" +
" <dict>\n" +
" <key>" + VMOptions.MAC_ARCH_VM_OPTIONS + "</key>\n" +
" <!-- foo -->\n" +
" <!-- bar -->\n" +
" <string>-someOption</string>\n" +
" </dict>\n" +
" </dict>\n" +
"</plist>");
assertEquals(-1, VMOptions.readXmx());
assertEquals(-1, VMOptions.readMaxPermGen());
VMOptions.writeXmx(1024);
VMOptions.writeMaxPermGen(256);
assertEquals("<plist version=\"1.0\">\n" +
" <dict>\n" +
" <dict>\n" +
" <key>" + VMOptions.MAC_ARCH_VM_OPTIONS + "</key>\n" +
" <!-- foo -->\n" +
" <!-- bar -->\n" +
" <string>-someOption -Xmx1024m -XX:MaxPermSize=256m</string>\n" +
" </dict>\n" +
" </dict>\n" +
"</plist>",
readFile());
}
public void testReadAndWriteIntoExactTagUnderMacOs() throws IOException {
VMOptions.setTestFile(myFile.getPath(), true);
writeFile("<plist version=\"1.0\">\n" +
" <dict>\n" +
" <dict>\n" +
" <key>Another section</key>\n" +
" <string>-Xmx111m</string>\n" +
" <key>" + VMOptions.MAC_ARCH_VM_OPTIONS + "</key>\n" +
" <string>-Xmx222m</string>\n" +
" </dict>\n" +
" </dict>\n" +
"</plist>");
assertEquals(222, VMOptions.readXmx());
VMOptions.writeXmx(333);
assertEquals("<plist version=\"1.0\">\n" +
" <dict>\n" +
" <dict>\n" +
" <key>Another section</key>\n" +
" <string>-Xmx111m</string>\n" +
" <key>" + VMOptions.MAC_ARCH_VM_OPTIONS + "</key>\n" +
" <string>-Xmx333m</string>\n" +
" </dict>\n" +
" </dict>\n" +
"</plist>",
readFile());
}
public void testDoNothingIfNoVmOptionsSectionUnderMacOs() throws IOException {
VMOptions.setTestFile(myFile.getPath(), true);
writeFile("<plist version=\"1.0\">\n" +
" <dict>\n" +
" <dict>\n" +
" <key>Another section</key>\n" +
" <string>-Xmx111m</string>\n" +
" </dict>\n" +
" </dict>\n" +
"</plist>");
assertEquals(-1, VMOptions.readXmx());
VMOptions.writeXmx(333);
assertEquals("<plist version=\"1.0\">\n" +
" <dict>\n" +
" <dict>\n" +
" <key>Another section</key>\n" +
" <string>-Xmx111m</string>\n" +
" </dict>\n" +
" </dict>\n" +
"</plist>",
readFile());
}
private String readFile() throws IOException {
return FileUtil.loadFile(myFile);