work in progress for "create launcher script" action

This commit is contained in:
Dmitry Jemerov
2010-12-17 13:58:51 +03:00
parent 6be3ff5e63
commit 55c357b714
4 changed files with 191 additions and 0 deletions
@@ -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;
}
}
}
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.intellij.ide.actions.CreateLauncherScriptAction.CreateLauncherScriptDialog">
<grid id="27dc6" binding="myMainPanel" layout-manager="GridLayoutManager" row-count="4" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="521" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="4f7c4" class="javax.swing.JLabel" binding="myTitle">
<constraints>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="&lt;html&gt;You can create a launcher script to enable opening files and projects in $APPNAME from the command line.&lt;br&gt;&#10;Please specify the name of the script and the path where it should be created:&lt;/html&gt;"/>
</properties>
</component>
<vspacer id="fef28">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="16f26" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Name:"/>
</properties>
</component>
<component id="7e2e2" class="javax.swing.JTextField" binding="myNameField">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<text value="idea"/>
</properties>
</component>
<component id="b9fbf" class="javax.swing.JLabel">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Path:"/>
</properties>
</component>
<component id="f6940" class="javax.swing.JTextField" binding="myPathField">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<text value="/usr/local/bin"/>
</properties>
</component>
</children>
</grid>
</form>
@@ -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
@@ -124,6 +124,7 @@
<group id="FileOtherSettingsGroup" popup="true">
<group id="FileSettingsGroup"/>
</group>
<action id="CreateLauncherScript" class="com.intellij.ide.actions.CreateLauncherScriptAction"/>
</group>
<separator/>
<action id="SaveAll" class="com.intellij.ide.actions.SaveAllAction" icon="/actions/menu-saveall.png"/>