PageRenderTime 52ms CodeModel.GetById 30ms app.highlight 5ms RepoModel.GetById 15ms app.codeStats 0ms

/src/org/ooc/frontend/compilers/BaseCompiler.java

http://github.com/nddrylliog/ooc
Java | 66 lines | 55 code | 11 blank | 0 comment | 9 complexity | f589a5796a27ba77d997f6a894c0ad97 MD5 | raw file
 1package org.ooc.frontend.compilers;
 2
 3import java.io.File;
 4import java.io.IOException;
 5import java.util.ArrayList;
 6import java.util.List;
 7
 8import org.ooc.utils.ProcessUtils;
 9import org.ooc.utils.ShellUtils;
10
11public abstract class BaseCompiler implements AbstractCompiler {
12	
13	protected List<String> command = new ArrayList<String>();
14	protected String executablePath;
15	
16	public BaseCompiler(String executableName) {
17		setExecutable(executableName);
18		reset();
19	}
20	
21	@SuppressWarnings("null")
22	public void setExecutable(String executableName) {
23		File execFile = new File(executableName);
24		if(!execFile.exists()) {
25			execFile = ShellUtils.findExecutable(executableName);
26			if(execFile == null) {
27				execFile = ShellUtils.findExecutable(executableName + ".exe");
28				if(execFile == null) {
29					ShellUtils.findExecutable(executableName, true);
30				}
31			}
32		}
33		executablePath = execFile.getAbsolutePath();
34		if(command.size() == 0) {
35			command.add(executablePath);
36		} else {
37			command.set(0, executablePath);
38		}
39	}
40
41	public int launch() throws IOException, InterruptedException {
42		ProcessBuilder builder = new ProcessBuilder();
43		builder.command(command);
44		Process process = builder.start();
45		ProcessUtils.redirectIO(process);
46		return process.waitFor();
47	}
48	
49	public String getCommandLine() {
50		StringBuilder commandLine = new StringBuilder();
51		for(String arg: command) {
52			commandLine.append(arg);
53			commandLine.append(' ');
54		}
55		return commandLine.toString();
56	}
57	
58	public void reset() {
59		command.clear();
60		command.add(executablePath);
61	}
62	
63	@Override
64	public abstract BaseCompiler clone();
65
66}