*** empty log message ***
[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                 return process ;
34         }
35
36         protected String renderLabel(InterpreterRunnerConfiguration configuration) {
37                 StringBuffer buffer = new StringBuffer();
38
39                 PHPInterpreter interpreter = configuration.getInterpreter();
40                 buffer.append("PHP ");
41                 buffer.append(interpreter.getCommand());
42                 buffer.append(" : ");
43                 buffer.append(configuration.getFileName());
44
45                 return buffer.toString();
46         }
47
48         protected String renderCommandLine(InterpreterRunnerConfiguration configuration) {
49                 PHPInterpreter interpreter = configuration.getInterpreter();
50
51                 StringBuffer buffer = new StringBuffer();
52                 buffer.append(this.getDebugCommandLineArgument());
53         //      buffer.append(renderLoadPath(configuration));
54                 buffer.append(" " + configuration.getInterpreterArguments());
55         //      buffer.append(interpreter.endOfOptionsDelimeter);
56                 buffer.append(" " + osDependentPath(configuration.getAbsoluteFileName()));
57                 buffer.append(" " + configuration.getProgramArguments());
58
59                 return buffer.toString();
60         }
61
62         protected String renderLoadPath(InterpreterRunnerConfiguration configuration) {
63                 StringBuffer loadPath = new StringBuffer();
64
65                 PHPProject project = configuration.getProject();
66                 addToLoadPath(loadPath, project.getProject());
67
68                 Iterator referencedProjects = project.getReferencedProjects().iterator();
69                 while (referencedProjects.hasNext())
70                         addToLoadPath(loadPath, (IProject) referencedProjects.next());
71
72                 return loadPath.toString();
73         }
74
75         protected void addToLoadPath(StringBuffer loadPath, IProject project) {
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 }