3535/**
3636 * launch() implements starting Node and passing all parameters.
3737 * Node is launched as node, coffee, coffee -c, tsc or node-dev(or other monitors)
38- *
38+ *
3939 * @author Lamb, Tomoyuki, Pushkar, Paul Verest
4040 */
4141public class LaunchConfigurationDelegate implements
4242 ILaunchConfigurationDelegate {
4343 private static RuntimeProcess nodeProcess = null ; //since 0.7 it should be debuggable instance
4444 //@since 0.7. contain all running Node thread, including under debug. Non Thread-safe, as it should be only in GUI thread
4545 //private static List<RuntimeProcess> nodeRunningProcesses = new LinkedList<RuntimeProcess>();
46-
46+
4747 private boolean warned = false ;
48-
48+
4949 /*
5050 * (non-Javadoc)
51- *
51+ *
5252 * @see
5353 * org.eclipse.debug.core.model.ILaunchConfigurationDelegate#launch(org.
5454 * eclipse.debug.core.ILaunchConfiguration, java.lang.String,
@@ -58,30 +58,30 @@ public class LaunchConfigurationDelegate implements
5858 @ Override
5959 public void launch (ILaunchConfiguration configuration , String mode ,
6060 ILaunch launch , IProgressMonitor monitor ) throws CoreException {
61-
61+
6262 IPreferenceStore preferenceStore = Activator .getDefault ().getPreferenceStore ();
6363 boolean allowedMany = preferenceStore .getBoolean (PreferenceConstants .NODE_ALLOW_MANY );//@since 0.7
6464 boolean isDebugMode = mode .equals (ILaunchManager .DEBUG_MODE );
6565
6666 if (allowedMany ){//@since 0.7
67- if ( isDebugMode
67+ if ( isDebugMode
6868 && (nodeProcess != null && !nodeProcess .isTerminated ()) ) {
6969 showErrorDialog ("Only 1 node process can be debugged in 1 Eclipse instance!\n \n " +
7070 "Open other Eclipse/Enide Studio with different node debug port configurred. " );
7171 return ;
7272 }
73-
74- }else {
73+
74+ }else {
7575 if (nodeProcess != null && !nodeProcess .isTerminated ()) {
7676 //throw new CoreException(new Status(IStatus.OK, ChromiumDebugPlugin.PLUGIN_ID, null, null));
7777 showErrorDialog ("Other node process is running!" );
7878 return ;
7979 //TODO suggest to terminate and start new
80- }
80+ }
8181 }
8282
83-
84- // Using configuration to build command line
83+
84+ // Using configuration to build command line
8585 List <String > cmdLine = new ArrayList <String >();
8686
8787 if (preferenceStore .getBoolean (PreferenceConstants .NODE_JUST_NODE )){
@@ -96,10 +96,10 @@ public void launch(ILaunchConfiguration configuration, String mode,
9696 Dialogs .showPreferencesDialog ("Node.js runtime is not correctly configured.\n \n "
9797 + "Please goto Window -> Prefrences -> Nodeclipse and configure the correct location" );
9898 return ;
99- }
99+ }
100100 cmdLine .add (nodePath );
101101 }
102-
102+
103103 if (isDebugMode ) {
104104 // -brk says to Node runtime wait until Chromium Debugger starts and connects
105105 // that is causing "stop on first line" behavior,
@@ -113,24 +113,24 @@ public void launch(ILaunchConfiguration configuration, String mode,
113113 if (nodeDebugPort ==0 ) { nodeDebugPort =5858 ;};
114114 cmdLine .add ("--debug" +brk +"=" +nodeDebugPort ); //--debug-brk=5858
115115 }
116-
116+
117117 //@since 0.9 from Preferences
118118 String nodeOptions = preferenceStore .getString (PreferenceConstants .NODE_OPTIONS );
119119 if (!nodeOptions .equals ("" )) {
120120 String [] sa = nodeOptions .split (" " );
121121 for (String s : sa ) {
122122 cmdLine .add (s );
123- }
123+ }
124124 }
125-
125+
126126 String nodeArgs = configuration .getAttribute (Constants .ATTR_NODE_ARGUMENTS , "" );
127127 if (!nodeArgs .equals ("" )) {
128128 String [] sa = nodeArgs .split (" " );
129129 for (String s : sa ) {
130130 cmdLine .add (s );
131131 }
132132 }
133-
133+
134134 String file = configuration .getAttribute (Constants .KEY_FILE_PATH , Constants .BLANK_STRING );
135135 String extension = null ;
136136 int i = file .lastIndexOf ('.' );
@@ -142,15 +142,15 @@ public void launch(ILaunchConfiguration configuration, String mode,
142142 // by default assume
143143 extension = "js" ;
144144 }
145-
145+
146146 // #57 running app.js with node-dev, forever, supervisor, nodemon etc
147147 // https://github.com/Nodeclipse/nodeclipse-1/issues/57
148148 String nodeMonitor = configuration .getAttribute (Constants .ATTR_NODE_MONITOR , "" );
149149 if (!nodeMonitor .equals ("" )) { // any value
150150 //TODO support selection, now only one
151-
151+
152152 String nodeMonitorPath = preferenceStore .getString (PreferenceConstants .NODE_MONITOR_PATH );
153-
153+
154154 // Check if the node monitor location is correctly configured
155155 File nodeMonitorFile = new File (nodeMonitorPath );
156156 if (!nodeMonitorFile .exists ()){
@@ -181,7 +181,7 @@ public void launch(ILaunchConfiguration configuration, String mode,
181181 //String typescriptCompiler = configuration.getAttribute(Constants.ATTR_TYPESCRIPT_COMPILER, "");
182182 cmdLine .add (preferenceStore .getString (PreferenceConstants .TYPESCRIPT_COMPILER_PATH ));
183183 }
184-
184+
185185 String filePath = ResourcesPlugin .getWorkspace ().getRoot ().findMember (file ).getLocation ().toOSString ();
186186 // path is relative, so can not found it.
187187 cmdLine .add (filePath );
@@ -194,7 +194,7 @@ public void launch(ILaunchConfiguration configuration, String mode,
194194 cmdLine .add (s );
195195 }
196196 }
197-
197+
198198 String programArgs = configuration .getAttribute (Constants .ATTR_PROGRAM_ARGUMENTS , "" );
199199 if (!programArgs .equals ("" )) {
200200 String [] sa = programArgs .split (" " );
@@ -216,75 +216,74 @@ public void launch(ILaunchConfiguration configuration, String mode,
216216 if (workingPath == null ){
217217 workingPath = (new File (filePath )).getParentFile ();
218218 }
219-
219+
220220 //env
221- String [] envp = getEnvironmentVariables (configuration );
222-
221+ String [] envp = getEnvironmentVariables (configuration );
222+
223223 for (String s : cmdLine ) NodeclipseConsole .write (s +" " );
224224 NodeclipseConsole .write ("\n " );
225-
225+
226226 String [] cmds = {};
227227 cmds = cmdLine .toArray (cmds );
228228 // Launch a process to run/debug. See also #71 (output is less or no output)
229229 Process p = DebugPlugin .exec (cmds , workingPath , envp );
230230 // no way to get private p.handle from java.lang.ProcessImpl
231- RuntimeProcess process = (RuntimeProcess )DebugPlugin .newProcess (launch , p , Constants .PROCESS_MESSAGE );
231+ RuntimeProcess process = (RuntimeProcess )DebugPlugin .newProcess (launch , p , Constants .PROCESS_MESSAGE );
232232 if (isDebugMode ) {
233- if (!process .isTerminated ()) {
233+ if (!process .isTerminated ()) {
234234 int nodeDebugPort = preferenceStore .getInt (PreferenceConstants .NODE_DEBUG_PORT );
235235 NodeDebugUtil .launch (mode , launch , monitor , nodeDebugPort );
236236 }
237237 }
238-
238+
239239 if (allowedMany ){ //@since 0.7
240240 if (isDebugMode ){
241- nodeProcess = process ;
241+ nodeProcess = process ;
242242 }
243243 //nodeRunningProcesses.add(process);
244244 }else {
245- nodeProcess = process ;
245+ nodeProcess = process ;
246246 }
247247 }
248-
248+
249+ public static String [] NODE_ENV_VAR_SET = new String []{"APPDATA" ,"PATH" ,"TEMP" ,"TMP" ,"SystemDrive" }; // #81, #197
250+
249251 private String [] getEnvironmentVariables (ILaunchConfiguration configuration ) throws CoreException {
250252 Map <String , String > envm = new HashMap <String , String >();
251253 envm = configuration .getAttribute (Constants .ATTR_ENVIRONMENT_VARIABLES , envm );
252-
253- int envmSizeDelta = 4 ;
254+
255+ int envmSizeDelta = NODE_ENV_VAR_SET . length ;
254256 Map <String ,String > all = null ;
255257 IPreferenceStore preferenceStore = Activator .getDefault ().getPreferenceStore ();
256258 boolean passAllEnvVars = preferenceStore .getBoolean (PreferenceConstants .NODE_PASS_ALL_ENVIRONMENT_VARIABLES );//@since 0.12
257259 if (passAllEnvVars ){
258260 all = System .getenv ();
259261 envmSizeDelta = all .size ();
260262 }
261-
263+
262264 String [] envp = new String [envm .size ()+envmSizeDelta ]; // see below
263265 int idx = 0 ;
264266 for (String key : envm .keySet ()) {
265267 String value = envm .get (key );
266268 envp [idx ++] = key + "=" + value ;
267269 }
268-
270+
269271 if (passAllEnvVars ){
270272 for (Map .Entry <String , String > entry : all .entrySet ())
271273 {
272274 //System.out.println(entry.getKey() + "/" + entry.getValue());
273275 envp [idx ++] = entry .getKey () + "=" + entry .getValue ();
274276 }
275277 }else {
276- envp [idx ++] = getEnvVariableEqualsString ("APPDATA" ); //#197
277- //+ #81
278- envp [idx ++] = getEnvVariableEqualsString ("PATH" );
279- envp [idx ++] = getEnvVariableEqualsString ("TEMP" );
280- envp [idx ++] = getEnvVariableEqualsString ("TMP" );
281- envp [idx ++] = getEnvVariableEqualsString ("SystemDrive" );
278+ for (String envVarName : NODE_ENV_VAR_SET ){
279+ envp [idx ++] = getEnvVariableEqualsString (envVarName );
280+ }
282281 }
283282 if (!warned ){
284283 NodeclipseConsole .write (" These environment variables will be applied automatically to every `node` launch.\n " );
285284 StringBuilder sb = new StringBuilder (100 );
286285 for (int i =0 ; i <envp .length ; i ++){
287- sb .append (" " ).append (envp [i ]).append ('\n' );
286+ sb .append (" " ).append (envp [i ]).append ('\n' );
288287 }
289288 NodeclipseConsole .write (sb .toString ());
290289 warned = true ;
@@ -295,21 +294,21 @@ private String[] getEnvironmentVariables(ILaunchConfiguration configuration) thr
295294 protected String getEnvVariableEqualsString (String envvarName ){
296295 String envvarValue = System .getenv (envvarName );
297296 if (envvarValue ==null ) envvarValue = "" ;
298- return envvarName + "=" + envvarValue ;
297+ return envvarName + "=" + envvarValue ;
299298 }
300-
299+
301300 private void showErrorDialog (final String message ) {
302301 Display .getDefault ().syncExec (new Runnable () {
303302 public void run () {
304303 Shell shell = PlatformUI .getWorkbench ().getActiveWorkbenchWindow ().getShell ();
305304
306- MessageDialog dialog = new MessageDialog (shell , "Nodeclipse" , null , message ,
305+ MessageDialog dialog = new MessageDialog (shell , "Nodeclipse" , null , message ,
307306 MessageDialog .ERROR , new String [] { "OK" }, 0 );
308307 dialog .open ();
309308 }
310309 });
311310 }
312-
311+
313312 public static void terminateNodeProcess () {
314313 if (nodeProcess != null ) {
315314 try {
0 commit comments