misc changes
[phpeclipse.git] / net.sourceforge.phpeclipse.launching / src / net / sourceforge / phpdt / internal / launching / InterpreterRunner.java
1 package net.sourceforge.phpdt.internal.launching;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.Iterator;
6
7 import org.eclipse.core.boot.BootLoader;
8 import org.eclipse.core.resources.IProject;
9 import org.eclipse.debug.core.DebugPlugin;
10 import org.eclipse.debug.core.ILaunch;
11 import org.eclipse.debug.core.model.IProcess;
12
13 import net.sourceforge.phpeclipse.resourcesview.PHPProject;
14
15 public class InterpreterRunner {
16
17         public InterpreterRunner() {
18         }
19
20         public IProcess run(InterpreterRunnerConfiguration configuration, ILaunch launch, String[] env) {               
21                 String commandLine = renderCommandLine(configuration);          
22                 File workingDirectory = configuration.getAbsoluteWorkingDirectory();
23
24                 Process nativePHPProcess = null;
25                 try {
26                         nativePHPProcess = configuration.getInterpreter().exec(commandLine, workingDirectory, env);
27                 } catch (IOException e) {
28                         throw new RuntimeException("Unable to execute interpreter: " + commandLine + workingDirectory);
29                 }
30
31                 IProcess process = DebugPlugin.newProcess(launch, nativePHPProcess, renderLabel(configuration));
32                 process.setAttribute(PHPLaunchingPlugin.PLUGIN_ID + ".launcher.cmdline", commandLine);
33
34                 return process ;
35         }
36
37         protected String renderLabel(InterpreterRunnerConfiguration configuration) {
38                 StringBuffer buffer = new StringBuffer();
39
40                 PHPInterpreter interpreter = configuration.getInterpreter();
41                 buffer.append("PHP ");
42                 buffer.append(interpreter.getCommand());
43                 buffer.append(" : ");
44                 buffer.append(configuration.getFileName());
45
46                 return buffer.toString();
47         }
48
49         protected String renderCommandLine(InterpreterRunnerConfiguration configuration) {
50                 PHPInterpreter interpreter = configuration.getInterpreter();
51
52                 StringBuffer buffer = new StringBuffer();
53                 buffer.append(this.getDebugCommandLineArgument());
54         //      buffer.append(renderLoadPath(configuration));
55                 buffer.append(" " + configuration.getInterpreterArguments());
56         //      buffer.append(interpreter.endOfOptionsDelimeter);
57                 buffer.append(" " + osDependentPath(configuration.getAbsoluteFileName()));
58                 buffer.append(" " + configuration.getProgramArguments());
59
60                 return buffer.toString();
61         }
62
63         protected String renderLoadPath(InterpreterRunnerConfiguration configuration) {
64                 StringBuffer loadPath = new StringBuffer();
65
66                 PHPProject project = configuration.getProject();
67                 addToLoadPath(loadPath, project.getProject());
68
69                 Iterator referencedProjects = project.getReferencedProjects().iterator();
70                 while (referencedProjects.hasNext())
71                         addToLoadPath(loadPath, (IProject) referencedProjects.next());
72
73                 return loadPath.toString();
74         }
75
76         protected void addToLoadPath(StringBuffer loadPath, IProject project) {
77                 loadPath.append(" -I " + osDependentPath(project.getLocation().toOSString()));
78         }
79
80         protected String osDependentPath(String aPath) {
81                 if (BootLoader.getOS().equals(BootLoader.OS_WIN32))
82                         aPath = "\"" + aPath + "\"";
83
84                 return aPath;
85         }
86         
87         protected String getDebugCommandLineArgument() {
88                 return "" ;     
89         }       
90 }