1 package net.sourceforge.phpdt.internal.launching;
4 import java.io.IOException;
5 import java.util.Iterator;
7 import net.sourceforge.phpdt.internal.core.JavaProject;
9 import org.eclipse.core.resources.IProject;
10 import org.eclipse.core.runtime.Platform;
11 import org.eclipse.debug.core.DebugPlugin;
12 import org.eclipse.debug.core.ILaunch;
13 import org.eclipse.debug.core.model.IProcess;
15 public class InterpreterRunner {
17 public InterpreterRunner() {
20 public IProcess run(InterpreterRunnerConfiguration configuration, ILaunch launch, String[] env) {
21 String commandLine = renderCommandLine(configuration);
22 File workingDirectory = configuration.getAbsoluteWorkingDirectory();
24 Process nativePHPProcess = null;
26 nativePHPProcess = configuration.getInterpreter().exec(commandLine, workingDirectory, env);
27 } catch (IOException e) {
28 throw new RuntimeException("Unable to execute interpreter: " + commandLine + workingDirectory);
31 IProcess process = DebugPlugin.newProcess(launch, nativePHPProcess, renderLabel(configuration));
32 process.setAttribute(PHPLaunchingPlugin.PLUGIN_ID + ".launcher.cmdline", commandLine);
37 protected String renderLabel(InterpreterRunnerConfiguration configuration) {
38 StringBuffer buffer = new StringBuffer();
40 PHPInterpreter interpreter = configuration.getInterpreter();
41 buffer.append("PHP ");
42 buffer.append(interpreter.getCommand());
44 buffer.append(configuration.getFileName());
46 return buffer.toString();
49 protected String renderCommandLine(InterpreterRunnerConfiguration configuration) {
50 PHPInterpreter interpreter = configuration.getInterpreter();
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());
60 return buffer.toString();
63 protected String renderLoadPath(InterpreterRunnerConfiguration configuration) {
64 StringBuffer loadPath = new StringBuffer();
66 JavaProject project = configuration.getProject();
67 addToLoadPath(loadPath, project.getProject());
69 Iterator referencedProjects = project.getReferencedProjects().iterator();
70 while (referencedProjects.hasNext())
71 addToLoadPath(loadPath, (IProject) referencedProjects.next());
73 return loadPath.toString();
76 protected void addToLoadPath(StringBuffer loadPath, IProject project) {
77 loadPath.append(" -I " + osDependentPath(project.getLocation().toOSString()));
80 protected String osDependentPath(String aPath) {
81 if (Platform.getOS().equals(Platform.OS_WIN32))
82 aPath = "\"" + aPath + "\"";
87 protected String getDebugCommandLineArgument() {