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