11
11
12
12
package de .anbos .eclipse .easyshell .plugin .actions ;
13
13
14
+ import java .io .File ;
15
+ import java .io .IOException ;
14
16
import java .util .StringTokenizer ;
15
17
16
18
import org .eclipse .core .runtime .CoreException ;
@@ -36,6 +38,7 @@ public class ActionDelegate implements IObjectActionDelegate {
36
38
private Resource [] resource = null ;
37
39
private IStructuredSelection currentSelection ;
38
40
private String commandValue = null ;
41
+ private String commandWorkingDir = null ;
39
42
private CommandType commandType = CommandType .commandTypeUnknown ;
40
43
41
44
public CommandType getCommandType () {
@@ -54,6 +57,10 @@ public void setCommandValue(String commandValue) {
54
57
this .commandValue = commandValue ;
55
58
}
56
59
60
+ public void setCommandWorkingDir (String commandWorkingDir ) {
61
+ this .commandWorkingDir = commandWorkingDir ;
62
+ }
63
+
57
64
public ActionDelegate () {
58
65
super ();
59
66
}
@@ -66,68 +73,26 @@ public void run(IAction action) {
66
73
Activator .logError ("Wrong Selection" , null );
67
74
return ;
68
75
}
69
-
70
- /* TODO: remove
71
- // get the ID
72
- String ActionIDStr = action.getId();
73
- Activator.logDebug("Action ID: >" + ActionIDStr + "<");
74
- */
75
-
76
76
// String for all commands in case of clipboard
77
77
String cmdAll = null ;
78
78
if (commandType == CommandType .commandTypeClipboard ) {
79
79
cmdAll = new String ();
80
80
}
81
-
81
+ // get the manager for variables expansion
82
82
IStringVariableManager variableManager = VariablesPlugin .getDefault ().getStringVariableManager ();
83
-
83
+ // iterate over the reources
84
84
for (int i =0 ;i <resource .length ;i ++) {
85
-
86
85
if (resource [i ] == null )
87
86
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 ) {
115
92
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
130
94
DynamicVariableResolver .setArgs (args );
95
+ // validate the command
131
96
variableManager .validateStringVariables (commandValue );
132
97
Activator .logDebug (commandValue );
133
98
// handling copy to clipboard
@@ -140,53 +105,111 @@ public void run(IAction action) {
140
105
}
141
106
// handling command line
142
107
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 );
171
109
}
172
110
} catch (CoreException e ) {
173
111
Activator .logError (Activator .getResourceString ("easyshell.message.error.validation" ), commandValue , e , true );
174
112
} catch (Exception e ) {
175
113
Activator .logError (Activator .getResourceString ("easyshell.message.error.execution" ), commandValue , e , true );
176
114
}
177
-
178
115
} else {
179
116
Activator .logError (Activator .getResourceString ("easyshell.message.error.internal" ), commandValue , null , true );
180
117
}
181
118
}
182
-
183
119
// handling copy to clipboard
184
120
if ((commandType == CommandType .commandTypeClipboard ) && (cmdAll != null ) && (cmdAll .length () != 0 )) {
185
121
Utils .copyToClipboard (cmdAll );
186
122
Activator .tooltipInfo (Activator .getResourceString ("easyshell.message.copytoclipboard" ), cmdAll );
187
123
}
188
124
}
189
125
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
+
190
213
public void selectionChanged (IAction action , ISelection selection ) {
191
214
currentSelection = selection instanceof IStructuredSelection ? (IStructuredSelection )selection : null ;
192
215
}
0 commit comments