Skip to content

Commit 1f9a8e5

Browse files
committed
#66 add support for setting working directory before executing a command
- new store format - refactoring of store migration Signed-off-by: Andre Bossert <anb0s@anbos.de>
1 parent 8a1880d commit 1f9a8e5

24 files changed

+411
-282
lines changed

plugin/src/de/anbos/eclipse/easyshell/plugin/Activator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import de.anbos.eclipse.easyshell.plugin.types.CommandType;
3535
import de.anbos.eclipse.easyshell.plugin.types.Debug;
36+
import de.anbos.eclipse.easyshell.plugin.types.Version;
3637

3738
/**
3839
* The activator class controls the plug-in life cycle
@@ -225,7 +226,7 @@ public static void logDebug(String string) {
225226
public IPreferenceStore getPreferenceStore() {
226227
// Create the preference store lazily.
227228
if (myPreferenceStore == null) {
228-
myPreferenceStore = getNewPreferenceStoreByVersion(Constants.PREF_VERSIONS[0]);
229+
myPreferenceStore = getNewPreferenceStoreByVersion(Version.actual.name());
229230
}
230231
return myPreferenceStore;
231232
}

plugin/src/de/anbos/eclipse/easyshell/plugin/Constants.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@ public interface Constants {
2626
public static final String IMAGE_OTHER = "editor.gif";
2727

2828
// Preferences
29-
// version with index = 0 is the actual one
30-
// do not delete old entries !!!
31-
public static final String[] PREF_VERSIONS = {
32-
"v2_0_002",
33-
"v2_0_001",
34-
"v1_4"
35-
};
3629
public static final String PREF_COMMANDS_PRESET = "COMMANDS_PRESET";
3730
public static final String PREF_COMMANDS = "COMMANDS";
3831
public static final String PREF_MENU = "MENU";

plugin/src/de/anbos/eclipse/easyshell/plugin/UIMessages.properties

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,16 @@ easyshell.command.editor.dialog.title.group1=Command
6464
easyshell.command.editor.dialog.tooltip.group1=Add your command here
6565
easyshell.command.editor.dialog.title.group2=Usable variables
6666
easyshell.command.editor.dialog.tooltip.group2=Double click at variable copies it to clipboard
67-
easyshell.command.editor.dialog.error.title.incompletedata=Missing or wrong command detected
67+
easyshell.command.editor.dialog.error.title.incompletedata=Missing or wrong parameters
6868
easyshell.command.editor.dialog.error.text.resource=Please choose a valid resource type
6969
easyshell.command.editor.dialog.error.text.type=Please choose a valid command type
7070
easyshell.command.editor.dialog.error.text.name=Please enter a valid name
71+
easyshell.command.editor.dialog.error.text.workingdir=Please enter a valid working directory
7172
easyshell.command.editor.dialog.error.text.value=Please enter a valid command
7273
easyshell.command.editor.dialog.label.combo1=type:
7374
easyshell.command.editor.dialog.label.combo2=resource:
7475
easyshell.command.editor.dialog.label.name=name:
76+
easyshell.command.editor.dialog.label.useworkdir=working directory:
77+
easyshell.command.editor.dialog.button.tooltip.useworkdir=Set working directory prior command execution?\n\nNeeded for commands that does not support working directory as parameter.
78+
easyshell.command.editor.dialog.label.workdir=
7579
easyshell.command.editor.dialog.label.value=command:

plugin/src/de/anbos/eclipse/easyshell/plugin/actions/ActionDelegate.java

Lines changed: 104 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
package de.anbos.eclipse.easyshell.plugin.actions;
1313

14+
import java.io.File;
15+
import java.io.IOException;
1416
import java.util.StringTokenizer;
1517

1618
import org.eclipse.core.runtime.CoreException;
@@ -36,6 +38,7 @@ public class ActionDelegate implements IObjectActionDelegate {
3638
private Resource[] resource = null;
3739
private IStructuredSelection currentSelection;
3840
private String commandValue = null;
41+
private String commandWorkingDir = null;
3942
private CommandType commandType = CommandType.commandTypeUnknown;
4043

4144
public CommandType getCommandType() {
@@ -54,6 +57,10 @@ public void setCommandValue(String commandValue) {
5457
this.commandValue = commandValue;
5558
}
5659

60+
public void setCommandWorkingDir(String commandWorkingDir) {
61+
this.commandWorkingDir = commandWorkingDir;
62+
}
63+
5764
public ActionDelegate() {
5865
super();
5966
}
@@ -66,68 +73,26 @@ public void run(IAction action) {
6673
Activator.logError("Wrong Selection", null);
6774
return;
6875
}
69-
70-
/* TODO: remove
71-
// get the ID
72-
String ActionIDStr = action.getId();
73-
Activator.logDebug("Action ID: >" + ActionIDStr + "<");
74-
*/
75-
7676
// String for all commands in case of clipboard
7777
String cmdAll = null;
7878
if (commandType == CommandType.commandTypeClipboard) {
7979
cmdAll = new String();
8080
}
81-
81+
// get the manager for variables expansion
8282
IStringVariableManager variableManager = VariablesPlugin.getDefault().getStringVariableManager();
83-
83+
// iterate over the reources
8484
for (int i=0;i<resource.length;i++) {
85-
8685
if (resource[i] == null)
8786
continue;
88-
89-
String drive = null;
90-
String full_path = null;
91-
String parent_path = null;
92-
String file_name = null;
93-
94-
full_path = resource[i].getFile().toString();
95-
if (resource[i].getFile().isDirectory()) {
96-
parent_path = resource[i].getFile().getPath();
97-
//file_name = "dir"; // dummy cmd
98-
}else
99-
{
100-
parent_path = resource[i].getFile().getParent();
101-
file_name = resource[i].getFile().getName();
102-
}
103-
104-
if (full_path != null) {
105-
106-
Activator.logDebug("full_path : >" + full_path + "<");
107-
Activator.logDebug("parent_path: >" + parent_path + "<");
108-
Activator.logDebug("file_name : >" + file_name + "<");
109-
110-
// Try to extract drive on Win32
111-
if (full_path.indexOf(":") != -1) {
112-
drive = full_path.substring(0, full_path.indexOf(":"));
113-
}
114-
87+
// TODO: get from preferences store
88+
//Quotes quotes = Activator.getQuotes(InstanceIDNum);
89+
Quotes quotes = Quotes.quotesNo;
90+
String[] args = fillArguments(resource[i], variableManager, quotes);
91+
if (args != null) {
11592
try {
116-
// TODO: get from preferences store
117-
//Quotes quotes = Activator.getQuotes(InstanceIDNum);
118-
Quotes quotes = Quotes.quotesNo;
119-
String[] args = new String[6];
120-
// args format
121-
args[0] = drive; // {0} == ${easyshell:drive}
122-
args[1] = autoQuotes(parent_path, quotes); // {1} == ${easyshell:container_loc}
123-
args[2] = autoQuotes(full_path, quotes); // {2} == ${easyshell:resource_loc}
124-
args[3] = autoQuotes(file_name, quotes); // {3} == ${easyshell:resource_name}
125-
args[4] = resource[i].getProjectName(); // {4} == ${easyshell:project_name}
126-
if (args[4] == null)
127-
args[4] = Activator.getResourceString("easyshell.plugin.name");
128-
args[5] = System.getProperty("line.separator"); // {5} == ${easyshell:line_separator}
129-
// variable format
93+
// set arguments for resolving
13094
DynamicVariableResolver.setArgs(args);
95+
// validate the command
13196
variableManager.validateStringVariables(commandValue);
13297
Activator.logDebug(commandValue);
13398
// handling copy to clipboard
@@ -140,53 +105,111 @@ public void run(IAction action) {
140105
}
141106
// handling command line
142107
else {
143-
// string tokenizer enabled ?
144-
// TODO: get from preferences store
145-
//Tokenizer tokenizer = Activator.isTokenizer(InstanceIDNum);
146-
Tokenizer tokenizer = Tokenizer.tokenizerYes;
147-
if (tokenizer == Tokenizer.tokenizerYes)
148-
{
149-
StringTokenizer st = new StringTokenizer(commandValue);
150-
String[] cmds = new String[st.countTokens()];
151-
int c = 0;
152-
Activator.logDebug("--- cmd: >");
153-
while (st.hasMoreElements()) {
154-
cmds[c] = fixQuotes(variableManager.performStringSubstitution(st.nextToken(), false), quotes);
155-
Activator.logDebug(cmds[c]);
156-
c++;
157-
}
158-
Activator.logDebug("--- cmd: <");
159-
//Utils.showToolTip(Display.getDefault().getActiveShell(), "EasyShell: executed", commandValue);
160-
// ---------- RUN --------------
161-
Runtime.getRuntime().exec(cmds);
162-
}
163-
// the old command line passing without string tokenizer
164-
else {
165-
String cmd = fixQuotes(variableManager.performStringSubstitution(commandValue, false), quotes);
166-
Activator.logDebug("--- cmd: >");
167-
Activator.logDebug(cmd);
168-
Activator.logDebug("--- cmd: <");
169-
Runtime.getRuntime().exec(cmd);
170-
}
108+
handleExec(variableManager, quotes);
171109
}
172110
} catch (CoreException e) {
173111
Activator.logError(Activator.getResourceString("easyshell.message.error.validation"), commandValue, e, true);
174112
} catch (Exception e) {
175113
Activator.logError(Activator.getResourceString("easyshell.message.error.execution"), commandValue, e, true);
176114
}
177-
178115
} else {
179116
Activator.logError(Activator.getResourceString("easyshell.message.error.internal"), commandValue, null, true);
180117
}
181118
}
182-
183119
// handling copy to clipboard
184120
if ((commandType == CommandType.commandTypeClipboard) && (cmdAll != null) && (cmdAll.length() != 0)) {
185121
Utils.copyToClipboard(cmdAll);
186122
Activator.tooltipInfo(Activator.getResourceString("easyshell.message.copytoclipboard"), cmdAll);
187123
}
188124
}
189125

126+
private String[] fillArguments(Resource res, IStringVariableManager variableManager, Quotes quotes) {
127+
String[] args = null;
128+
String drive = null;
129+
String full_path = null;
130+
String parent_path = null;
131+
String file_name = null;
132+
full_path = res.getFile().toString();
133+
if (res.getFile().isDirectory()) {
134+
parent_path = res.getFile().getPath();
135+
//file_name = "dir"; // dummy cmd
136+
} else {
137+
parent_path = res.getFile().getParent();
138+
file_name = res.getFile().getName();
139+
}
140+
if (full_path != null) {
141+
Activator.logDebug("full_path : >" + full_path + "<");
142+
Activator.logDebug("parent_path: >" + parent_path + "<");
143+
Activator.logDebug("file_name : >" + file_name + "<");
144+
// Try to extract drive on Win32
145+
if (full_path.indexOf(":") != -1) {
146+
drive = full_path.substring(0, full_path.indexOf(":"));
147+
}
148+
String project_name = res.getProjectName();
149+
if (project_name == null || project_name.isEmpty()) {
150+
project_name = Activator.getResourceString("easyshell.plugin.name");
151+
}
152+
String line_separator = System.getProperty("line.separator");
153+
// args format
154+
args = new String[6];
155+
args[0] = drive; // {0} == ${easyshell:drive}
156+
args[1] = autoQuotes(parent_path, quotes); // {1} == ${easyshell:container_loc}
157+
args[2] = autoQuotes(full_path, quotes); // {2} == ${easyshell:resource_loc}
158+
args[3] = autoQuotes(file_name, quotes); // {3} == ${easyshell:resource_name}
159+
args[4] = project_name; // {4} == ${easyshell:project_name}
160+
args[5] = line_separator; // {5} == ${easyshell:line_separator}
161+
}
162+
return args;
163+
}
164+
165+
private void handleExec(IStringVariableManager variableManager, Quotes quotes) throws CoreException, IOException {
166+
String[] cmds = null;
167+
// working directory
168+
if (commandWorkingDir != null && !commandWorkingDir.isEmpty()) {
169+
variableManager.validateStringVariables(commandWorkingDir);
170+
}
171+
Activator.logDebug(commandWorkingDir);
172+
// string tokenizer enabled ?
173+
// TODO: get from preferences store
174+
//Tokenizer tokenizer = Activator.isTokenizer(InstanceIDNum);
175+
Tokenizer tokenizer = Tokenizer.tokenizerYes;
176+
if (tokenizer == Tokenizer.tokenizerYes)
177+
{
178+
StringTokenizer st = new StringTokenizer(commandValue);
179+
cmds = new String[st.countTokens()];
180+
int i = 0;
181+
while (st.hasMoreElements()) {
182+
cmds[i] = fixQuotes(variableManager.performStringSubstitution(st.nextToken(), false), quotes);
183+
i++;
184+
}
185+
}
186+
// the old command line passing without string tokenizer
187+
else {
188+
cmds = new String[1];
189+
cmds[0] = fixQuotes(variableManager.performStringSubstitution(commandValue, false), quotes);
190+
}
191+
// log out
192+
for (int i=0;i<cmds.length;i++) {
193+
Activator.logDebug("--- cmd: >");
194+
Activator.logDebug(cmds[i]);
195+
Activator.logDebug("--- cmd: <");
196+
}
197+
// execute
198+
//Utils.showToolTip(Display.getDefault().getActiveShell(), "EasyShell: executed", commandValue);
199+
// ---------- RUN --------------
200+
//Runtime.getRuntime().exec(cmds);
201+
// create process builder with commands and
202+
ProcessBuilder pb = new ProcessBuilder(cmds);
203+
// set working directory and redirect error stream
204+
if (commandWorkingDir != null && !commandWorkingDir.isEmpty()) {
205+
pb.directory(new File(variableManager.performStringSubstitution(commandWorkingDir, false)));
206+
}
207+
// get passed system environment
208+
//Map<String, String> env = pb.environment();
209+
// add own variables
210+
pb.start();
211+
}
212+
190213
public void selectionChanged(IAction action, ISelection selection) {
191214
currentSelection = selection instanceof IStructuredSelection ? (IStructuredSelection)selection : null;
192215
}

plugin/src/de/anbos/eclipse/easyshell/plugin/commands/DefineCommands.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,25 @@ public void createContributionItems(IServiceLocator serviceLocator,
4444
item.getNameExpanded(),
4545
"de.anbos.eclipse.easyshell.plugin.commands.execute",
4646
"de.anbos.eclipse.easyshell.plugin.commands.parameter.type",
47-
item.getCommandData().getTypeAction(),
47+
item.getCommandData().getCommandType().getAction(),
4848
"de.anbos.eclipse.easyshell.plugin.commands.parameter.value",
4949
item.getCommandData().getCommand(),
50-
item.getCommandData().getTypeIcon());
50+
"de.anbos.eclipse.easyshell.plugin.commands.parameter.workingdir",
51+
item.getCommandData().isUseWorkingDirectory() ? item.getCommandData().getWorkingDirectory() : "",
52+
item.getCommandData().getCommandType().getIcon());
5153
}
5254
}
5355

5456
private void addItem(IServiceLocator serviceLocator, IContributionRoot additions,
5557
String commandLabel, String commandId,
56-
String paramId1, String paramValue1, String paramId2, String paramValue2, String commandImageId) {
58+
String paramId1, String paramValue1, String paramId2, String paramValue2, String paramId3, String paramValue3, String commandImageId) {
5759
CommandContributionItemParameter param = new CommandContributionItemParameter(serviceLocator, "", commandId, SWT.PUSH);
5860
param.label = commandLabel;
5961
param.icon = Activator.getImageDescriptor(commandImageId);
6062
Map<String, Object> commandParamametersMap = new HashMap<String, Object>();
6163
commandParamametersMap.put(paramId1, paramValue1);
6264
commandParamametersMap.put(paramId2, paramValue2);
65+
commandParamametersMap.put(paramId3, paramValue3);
6366
param.parameters = commandParamametersMap;
6467
CommandContributionItem item = new CommandContributionItem(param);
6568
item.setVisible(true);

plugin/src/de/anbos/eclipse/easyshell/plugin/handlers/CommandHandler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
3131
if (action != null) {
3232
String commandID = event.getCommand().getId();
3333
String commandType = event.getParameter("de.anbos.eclipse.easyshell.plugin.commands.parameter.type");
34-
String commanValue = event.getParameter("de.anbos.eclipse.easyshell.plugin.commands.parameter.value");
34+
String commandValue = event.getParameter("de.anbos.eclipse.easyshell.plugin.commands.parameter.value");
35+
String commandWorkingDir = event.getParameter("de.anbos.eclipse.easyshell.plugin.commands.parameter.workingdir");
3536
action.setCommandType(CommandType.getFromAction(commandType));
36-
action.setCommandValue(commanValue);
37+
action.setCommandValue(commandValue);
38+
action.setCommandWorkingDir(commandWorkingDir);
3739
Action act = new Action(commandID);
3840
action.run((IAction)act);
3941
}

0 commit comments

Comments
 (0)