cmdline lancher script fixed
authorRoman Chernyatchik <roman.chernyatchik@jetbrains.com>
Thu, 13 Jan 2011 06:29:05 +0000 (09:29 +0300)
committerRoman Chernyatchik <roman.chernyatchik@jetbrains.com>
Thu, 13 Jan 2011 09:48:01 +0000 (12:48 +0300)
platform/platform-impl/src/com/intellij/ide/actions/CreateLauncherScriptAction.java

index 1a3a2472ab0926fd2d92b610797b0beca1191e86..c4091286e937fbb2b0afb3bb439499b3f29b925b 100644 (file)
@@ -71,9 +71,23 @@ public class CreateLauncherScriptAction extends AnAction {
           return;
         }
       }
+
       if (!scriptFile.renameTo(scriptTarget)) {
-        final String mvCommand = "mv -f " + scriptFile + " " + pathname;
-        sudo(mvCommand, "In order to create a launcher script in " + scriptTarget.getParent() + ", please enter your administrator password:");
+        if (SystemInfo.isUnix) {
+          final String launcherScriptContainingDirPath = scriptTarget.getParent();
+          final String installationScriptSrc =
+            // create all intermediate folders
+            "mkdir -p " + launcherScriptContainingDirPath + "\n" +
+            // Copy file & change owner to root
+            // uid 0 = root
+            // gid 0 = root || wheel (MacOS)
+            "install -g 0 -o 0 " + scriptFile.getCanonicalPath() + " " + pathname;
+          final File installationScript = createTempExecutableScript("launcher_installer", installationScriptSrc);
+
+          sudo(installationScript.getCanonicalPath(),
+               installationScriptSrc,
+               "In order to create a launcher script in " + launcherScriptContainingDirPath + ", please enter your administrator password:");
+        }
       }
     }
     catch (Exception e) {
@@ -82,13 +96,15 @@ public class CreateLauncherScriptAction extends AnAction {
     }
   }
 
-  private static boolean sudo(String mvCommand, final String prompt) throws IOException, ScriptException, ExecutionException {
+  private static boolean sudo(String installationScriptPath,
+                              String installScriptSrc,
+                              final String prompt) throws IOException, ScriptException, ExecutionException {
     if (SystemInfo.isMac) {
       final ScriptEngine engine = new ScriptEngineManager(null).getEngineByName("AppleScript");
       if (engine == null) {
         throw new IOException("Could not find AppleScript engine");
       }
-      engine.eval("do shell script \"" + mvCommand + "\" with administrator privileges");
+      engine.eval("do shell script \"" + installationScriptPath + "\" with administrator privileges");
     }
     else if (SystemInfo.isUnix) {
       GeneralCommandLine cmdLine = new GeneralCommandLine();
@@ -100,12 +116,12 @@ public class CreateLauncherScriptAction extends AnAction {
         cmdLine.setExePath("kdesudo");
       }
       else {
-        Messages.showMessageDialog("Unsupported graphical environment. Please execute the following command from the shell:\n" + mvCommand,
+        Messages.showMessageDialog("Unsupported graphical environment. Please execute the following command from the shell:\n" + installScriptSrc,
                                    "Create Launcher Script",
                                    Messages.getInformationIcon());
         return true;
       }
-      cmdLine.addParameter(mvCommand);
+      cmdLine.addParameter(installationScriptPath);
       cmdLine.createProcess();
     }
     return false;
@@ -133,6 +149,16 @@ public class CreateLauncherScriptAction extends AnAction {
     return tempFile;
   }
 
+  private static File createTempExecutableScript(final String fileNamePrefix,
+                                                 final String source) throws IOException {
+    final File tempFile = FileUtil.createTempFile(fileNamePrefix, "");
+    FileUtil.writeToFile(tempFile, source.getBytes(CharsetToolkit.UTF8_CHARSET));
+    if (!tempFile.setExecutable(true)) {
+      throw new IOException("Failed to mark the launcher installation script as executable: script path " + tempFile.getCanonicalPath());
+    }
+    return tempFile;
+  }
+
   public static String defaultScriptName() {
     String productName = ApplicationNamesInfo.getInstance().getProductName();
     if (productName.equalsIgnoreCase("RubyMine")) {