From 8ebe33c4beee2c2106748791a467c435bad6ac8d Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Tue, 23 Sep 2014 11:40:06 +0200 Subject: [PATCH] Cleanup (formatting, i18n) --- .../actions/CreateLauncherScriptAction.java | 82 ++++++++++--------- .../src/messages/ApplicationBundle.properties | 1 + platform/platform-resources/src/launcher.py | 22 ++--- .../src/com/intellij/openapi/util/Pair.java | 7 ++ 4 files changed, 62 insertions(+), 50 deletions(-) diff --git a/platform/platform-impl/src/com/intellij/ide/actions/CreateLauncherScriptAction.java b/platform/platform-impl/src/com/intellij/ide/actions/CreateLauncherScriptAction.java index 0b696c8f4496..3e8dfd469e2d 100644 --- a/platform/platform-impl/src/com/intellij/ide/actions/CreateLauncherScriptAction.java +++ b/platform/platform-impl/src/com/intellij/ide/actions/CreateLauncherScriptAction.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -32,11 +32,15 @@ import com.intellij.openapi.ui.DialogWrapper; import com.intellij.openapi.ui.Messages; import com.intellij.openapi.util.SystemInfo; import com.intellij.openapi.util.text.StringUtil; +import org.jetbrains.annotations.NotNull; import javax.swing.*; import java.io.File; import java.io.IOException; +import java.util.Locale; +import java.util.Map; +import static com.intellij.openapi.util.Pair.pair; import static com.intellij.util.containers.ContainerUtil.newHashMap; import static java.util.Arrays.asList; @@ -44,7 +48,7 @@ import static java.util.Arrays.asList; * @author yole */ public class CreateLauncherScriptAction extends DumbAwareAction { - private static final Logger LOG = Logger.getInstance("#com.intellij.ide.actions.CreateLauncherScriptAction"); + private static final Logger LOG = Logger.getInstance(CreateLauncherScriptAction.class); private static final String CONTENTS = "/Contents"; public static boolean isAvailable() { @@ -52,15 +56,15 @@ public class CreateLauncherScriptAction extends DumbAwareAction { } @Override - public void update(AnActionEvent e) { - final boolean canCreateScript = isAvailable(); - final Presentation presentation = e.getPresentation(); + public void update(@NotNull AnActionEvent e) { + boolean canCreateScript = isAvailable(); + Presentation presentation = e.getPresentation(); presentation.setVisible(canCreateScript); presentation.setEnabled(canCreateScript); } @Override - public void actionPerformed(AnActionEvent e) { + public void actionPerformed(@NotNull AnActionEvent e) { if (!isAvailable()) return; Project project = e.getProject(); @@ -71,8 +75,9 @@ public class CreateLauncherScriptAction extends DumbAwareAction { } String path = dialog.myPathField.getText(); + assert path != null; if (!path.startsWith("/")) { - final String home = System.getenv("HOME"); + String home = System.getenv("HOME"); if (home != null && new File(home).isDirectory()) { if (path.startsWith("~")) { path = home + path.substring(1); @@ -83,11 +88,13 @@ public class CreateLauncherScriptAction extends DumbAwareAction { } } - final File target = new File(path, dialog.myNameField.getText()); + String name = dialog.myNameField.getText(); + assert name != null; + File target = new File(path, name); if (target.exists()) { - int rc = Messages.showOkCancelDialog(project, ApplicationBundle.message("launcher.script.overwrite", target), - "Create Launcher Script", Messages.getQuestionIcon()); - if (rc != Messages.OK) { + String message = ApplicationBundle.message("launcher.script.overwrite", target); + String title = ApplicationBundle.message("launcher.script.title"); + if (Messages.showOkCancelDialog(project, message, title, Messages.getQuestionIcon()) != Messages.OK) { return; } } @@ -99,32 +106,30 @@ public class CreateLauncherScriptAction extends DumbAwareAction { if (!isAvailable()) return; try { - final File scriptFile = createLauncherScriptFile(); - final File scriptTarget = new File(pathName); + File scriptFile = createLauncherScriptFile(); + File scriptTarget = new File(pathName); - final File launcherScriptContainingDir = scriptTarget.getParentFile(); - if (!(launcherScriptContainingDir.exists() || launcherScriptContainingDir.mkdirs()) || - !scriptFile.renameTo(scriptTarget)) { - final String launcherScriptContainingDirPath = launcherScriptContainingDir.getCanonicalPath(); - final String installationScriptSrc = + File scriptTargetDir = scriptTarget.getParentFile(); + assert scriptTargetDir != null; + if (!(scriptTargetDir.exists() || scriptTargetDir.mkdirs()) || !scriptFile.renameTo(scriptTarget)) { + String scriptTargetDirPath = scriptTargetDir.getCanonicalPath(); + // copy file and change ownership to root (UID 0 = root, GID 0 = root (wheel on Macs)) + String installationScriptSrc = "#!/bin/sh\n" + - // create all intermediate folders - "mkdir -p \"" + launcherScriptContainingDirPath + "\"\n" + - // copy file and change ownership to root (UID 0 = root, GID 0 = root (wheel on Macs)) + "mkdir -p \"" + scriptTargetDirPath + "\"\n" + "install -g 0 -o 0 \"" + scriptFile.getCanonicalPath() + "\" \"" + pathName + "\""; - final File installationScript = ExecUtil.createTempExecutableScript("launcher_installer", ".sh", installationScriptSrc); - final String prompt = ApplicationBundle.message("launcher.script.sudo.prompt", launcherScriptContainingDirPath); + File installationScript = ExecUtil.createTempExecutableScript("launcher_installer", ".sh", installationScriptSrc); + String prompt = ApplicationBundle.message("launcher.script.sudo.prompt", scriptTargetDirPath); ExecUtil.sudoAndGetOutput(asList(installationScript.getPath()), prompt, null); } } catch (Exception e) { - final String message = e.getMessage(); + String message = e.getMessage(); if (!StringUtil.isEmptyOrSpaces(message)) { LOG.warn(e); Notifications.Bus.notify( new Notification(Notifications.SYSTEM_MESSAGES_GROUP_ID, "Failed to create launcher script", message, NotificationType.ERROR), - project - ); + project); } else { LOG.error(e); @@ -134,23 +139,20 @@ public class CreateLauncherScriptAction extends DumbAwareAction { private static File createLauncherScriptFile() throws IOException, ExecutionException { String runPath = PathManager.getHomePath(); - final String productName = ApplicationNamesInfo.getInstance().getProductName().toLowerCase(); - if (!SystemInfo.isMac) { - // for Macs just use "*.app" - runPath += "/bin/" + productName + ".sh"; - } else if (runPath.endsWith(CONTENTS)) { - runPath += "/MacOS/" + productName; - } - String launcherContents = ExecUtil.loadTemplate(CreateLauncherScriptAction.class.getClassLoader(), "launcher.py", - newHashMap(asList("$CONFIG_PATH$", "$RUN_PATH$"), - asList(PathManager.getConfigPath(), runPath))); + String productName = ApplicationNamesInfo.getInstance().getProductName().toLowerCase(Locale.US); + if (!SystemInfo.isMac) runPath += "/bin/" + productName + ".sh"; + else if (runPath.endsWith(CONTENTS)) runPath += "/MacOS/" + productName; + + ClassLoader loader = CreateLauncherScriptAction.class.getClassLoader(); + assert loader != null; + Map variables = newHashMap(pair("$CONFIG_PATH$", PathManager.getConfigPath()), pair("$RUN_PATH$", runPath)); + String launcherContents = StringUtil.convertLineSeparators(ExecUtil.loadTemplate(loader, "launcher.py", variables)); - launcherContents = StringUtil.convertLineSeparators(launcherContents); return ExecUtil.createTempExecutableScript("launcher", "", launcherContents); } public static String defaultScriptName() { - final String scriptName = ApplicationNamesInfo.getInstance().getScriptName(); + String scriptName = ApplicationNamesInfo.getInstance().getScriptName(); return StringUtil.isEmptyOrSpaces(scriptName) ? "idea" : scriptName; } @@ -163,8 +165,8 @@ public class CreateLauncherScriptAction extends DumbAwareAction { protected CreateLauncherScriptDialog(Project project) { super(project); init(); - setTitle("Create Launcher Script"); - final String productName = ApplicationNamesInfo.getInstance().getProductName(); + setTitle(ApplicationBundle.message("launcher.script.title")); + String productName = ApplicationNamesInfo.getInstance().getProductName(); myTitle.setText(myTitle.getText().replace("$APP_NAME$", productName)); myNameField.setText(defaultScriptName()); } diff --git a/platform/platform-resources-en/src/messages/ApplicationBundle.properties b/platform/platform-resources-en/src/messages/ApplicationBundle.properties index abe6291a400e..4f2ea1ed7764 100644 --- a/platform/platform-resources-en/src/messages/ApplicationBundle.properties +++ b/platform/platform-resources-en/src/messages/ApplicationBundle.properties @@ -566,6 +566,7 @@ auto.import=Auto Import checkbox.collapse.suppress.warnings=@SuppressWarnings checkbox.collapse.end.of.line.comments=End of line comments sequence title.other.languages=Other Languages +launcher.script.title=Create Launcher Script launcher.script.overwrite=The file ''{0}'' already exists. Would you like to overwrite it? launcher.script.sudo.prompt=Please enter your password to create a launcher script in a ''{0}'' desktop.entry.success=You may now exit {0} and start it from the system menu
\ diff --git a/platform/platform-resources/src/launcher.py b/platform/platform-resources/src/launcher.py index 440f716f8ebf..4b07414504e3 100644 --- a/platform/platform-resources/src/launcher.py +++ b/platform/platform-resources/src/launcher.py @@ -15,23 +15,24 @@ args = [] skip_next = False for i, arg in enumerate(sys.argv[1:]): if arg == '-h' or arg == '-?' or arg == '--help': - print(('Usage:\n' + \ - ' {0} -h |-? | --help\n' + \ - ' {0} [-l|--line line] file[:line]\n' + \ - ' {0} diff ' + \ + print(('Usage:\n' + + ' {0} -h |-? | --help\n' + + ' {0} [-l|--line line] file[:line]\n' + + ' {0} diff ' + ' {0} merge [base] ').format(sys.argv[0])) exit(0) elif ':' in arg: file_path, line_number = arg.rsplit(':', 1) if line_number.isdigit(): - args.append('-l') - args.append(line_number) - args.append(file_path) + args.append('-l') + args.append(line_number) + args.append(file_path) else: - args.append(arg) + args.append(arg) else: args.append(arg) + def launch_with_port(port): found = False @@ -57,11 +58,12 @@ def launch_with_port(port): cmd = "activate " + os.getcwd() + "\0" + "\0".join(args) encoded = struct.pack(">h", len(cmd)) + cmd s.send(encoded) - time.sleep(0.5) # don't close socket immediately + time.sleep(0.5) # don't close socket immediately return True return False + port = -1 try: f = open(os.path.join(CONFIG_PATH, 'port')) @@ -73,7 +75,7 @@ except Exception: if port == -1: # SocketLock actually allows up to 50 ports, but the checking takes too long - for port in range(6942, 6942+10): + for port in range(6942, 6942 + 10): if launch_with_port(port): exit() else: if launch_with_port(port): exit() diff --git a/platform/util-rt/src/com/intellij/openapi/util/Pair.java b/platform/util-rt/src/com/intellij/openapi/util/Pair.java index ffa8e7cd1398..9e74659bdb8c 100644 --- a/platform/util-rt/src/com/intellij/openapi/util/Pair.java +++ b/platform/util-rt/src/com/intellij/openapi/util/Pair.java @@ -28,6 +28,13 @@ public class Pair { return new Pair(first, second); } + @NotNull + @SuppressWarnings("MethodNamesDifferingOnlyByCase") + public static Pair pair(A first, B second) { + //noinspection DontUsePairConstructor + return new Pair(first, second); + } + @NotNull public static Function> createFunction(final B value) { return new Function>() {