/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

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