From 55c357b714c2d9ea9288c054e3e606835ee67126 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Fri, 17 Dec 2010 13:55:29 +0300 Subject: [PATCH] work in progress for "create launcher script" action --- .../actions/CreateLauncherScriptAction.java | 126 ++++++++++++++++++ .../actions/CreateLauncherScriptDialog.form | 62 +++++++++ .../src/messages/ActionsBundle.properties | 2 + .../src/idea/PlatformActions.xml | 1 + 4 files changed, 191 insertions(+) create mode 100644 platform/platform-impl/src/com/intellij/ide/actions/CreateLauncherScriptAction.java create mode 100644 platform/platform-impl/src/com/intellij/ide/actions/CreateLauncherScriptDialog.form diff --git a/platform/platform-impl/src/com/intellij/ide/actions/CreateLauncherScriptAction.java b/platform/platform-impl/src/com/intellij/ide/actions/CreateLauncherScriptAction.java new file mode 100644 index 000000000000..4b4a1ae3bec2 --- /dev/null +++ b/platform/platform-impl/src/com/intellij/ide/actions/CreateLauncherScriptAction.java @@ -0,0 +1,126 @@ +/* + * Copyright 2000-2010 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.ide.actions; + +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.PlatformDataKeys; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.application.ApplicationNamesInfo; +import com.intellij.openapi.application.PathManager; +import com.intellij.openapi.application.ex.ApplicationEx; +import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.ui.DialogWrapper; +import com.intellij.openapi.ui.Messages; +import com.intellij.openapi.util.SystemInfo; +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.vfs.CharsetToolkit; + +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.swing.*; +import java.io.File; +import java.io.IOException; + +/** + * @author yole + */ +public class CreateLauncherScriptAction extends AnAction { + private static final Logger LOG = Logger.getInstance("#com.intellij.ide.actions.CreateLauncherScriptAction"); + + @Override + public void actionPerformed(AnActionEvent e) { + Project project = e.getData(PlatformDataKeys.PROJECT); + CreateLauncherScriptDialog dialog = new CreateLauncherScriptDialog(project); + dialog.show(); + if (dialog.getExitCode() != DialogWrapper.OK_EXIT_CODE) { + return; + } + createLauncherScript(project, dialog.myNameField.getText(), dialog.myPathField.getText()); + } + + public static void createLauncherScript(Project project, String name, String path) { + try { + final File scriptFile = createLauncherScriptFile(); + + 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 \"mv " + scriptFile + " " + path + "/" + name + "\" with administrator privileges"); + } + else if (SystemInfo.isUnix) { + + } + } + catch (Exception e) { + LOG.info(e); + Messages.showErrorDialog(project, "Failed to create launcher script: " + e.getMessage(), "Create Launcher Script"); + } + } + + private static File createLauncherScriptFile() throws IOException { + final File tempFile = FileUtil.createTempFile("launcher", ""); + StringBuilder textBuilder = new StringBuilder("#!/bin/bash\n"); + String homePath = PathManager.getHomePath().replace(" ", "\\ "); + String productName = ApplicationNamesInfo.getInstance().getProductName().toLowerCase(); + if (SystemInfo.isMac) { + textBuilder.append("cd ").append(homePath).append("/Contents/MacOS/\n"); + textBuilder.append("./").append(productName); + } + else { + textBuilder.append("cd ").append(homePath).append("/bin\n"); + textBuilder.append("./").append(productName).append(".sh"); + } + textBuilder.append(" $@"); + FileUtil.writeToFile(tempFile, textBuilder.toString().getBytes(CharsetToolkit.UTF8_CHARSET)); + return tempFile; + } + + @Override + public void update(AnActionEvent e) { + boolean canCreateScript = /*SystemInfo.isUnix || SystemInfo.isMac */ ((ApplicationEx) ApplicationManager.getApplication()).isInternal(); + e.getPresentation().setVisible(canCreateScript); + } + + public static class CreateLauncherScriptDialog extends DialogWrapper { + private JPanel myMainPanel; + private JTextField myNameField; + private JTextField myPathField; + private JLabel myTitle; + + protected CreateLauncherScriptDialog(Project project) { + super(project); + init(); + setTitle("Create Launcher Script"); + final String productName = ApplicationNamesInfo.getInstance().getProductName(); + myTitle.setText(myTitle.getText().replace("$APPNAME", productName)); + if (productName.equalsIgnoreCase("RubyMine")) { + myNameField.setText("mine"); + } + else if (productName.equalsIgnoreCase("PyCharm")) { + myNameField.setText("charm"); + } + } + + @Override + protected JComponent createCenterPanel() { + return myMainPanel; + } + } +} diff --git a/platform/platform-impl/src/com/intellij/ide/actions/CreateLauncherScriptDialog.form b/platform/platform-impl/src/com/intellij/ide/actions/CreateLauncherScriptDialog.form new file mode 100644 index 000000000000..15f5158a038f --- /dev/null +++ b/platform/platform-impl/src/com/intellij/ide/actions/CreateLauncherScriptDialog.form @@ -0,0 +1,62 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/platform/platform-resources-en/src/messages/ActionsBundle.properties b/platform/platform-resources-en/src/messages/ActionsBundle.properties index f4d6a6a4dabe..09089df9d3dd 100644 --- a/platform/platform-resources-en/src/messages/ActionsBundle.properties +++ b/platform/platform-resources-en/src/messages/ActionsBundle.properties @@ -1202,3 +1202,5 @@ action.MarkTestSourceRoot.text=Test Source Root action.MarkExcludeRoot.text=Excluded action.UnmarkRoot.text=Unmark Root group.MarkRootGroup.text=Mark Directory As +action.CreateLauncherScript.text=Create Launcher Script +action.CreateLauncherScript.description=Create script for opening files and projects from the command line diff --git a/platform/platform-resources/src/idea/PlatformActions.xml b/platform/platform-resources/src/idea/PlatformActions.xml index e35175a28399..15056410b941 100644 --- a/platform/platform-resources/src/idea/PlatformActions.xml +++ b/platform/platform-resources/src/idea/PlatformActions.xml @@ -124,6 +124,7 @@ +