1 package hudson.tasks; 2 3 import hudson.model.Build; 4 import hudson.model.BuildListener; 5 import hudson.model.Project; 6 import hudson.Launcher; 7 import hudson.FilePath; 8 import hudson.Util; 9 10 import java.io.IOException ; 11 12 17 public abstract class CommandInterpreter extends Builder { 18 21 protected final String command; 22 23 public CommandInterpreter(String command) { 24 this.command = command; 25 } 26 27 public final String getCommand() { 28 return command; 29 } 30 31 public boolean perform(Build build, Launcher launcher, BuildListener listener) throws InterruptedException { 32 Project proj = build.getProject(); 33 FilePath ws = proj.getWorkspace(); 34 FilePath script=null; 35 try { 36 try { 37 script = ws.createTextTempFile("hudson", getFileExtension(), getContents(), false); 38 } catch (IOException e) { 39 Util.displayIOException(e,listener); 40 e.printStackTrace( listener.fatalError("Unable to produce a script file") ); 41 return false; 42 } 43 44 String [] cmd = buildCommandLine(script); 45 46 int r; 47 try { 48 r = launcher.launch(cmd,build.getEnvVars(),listener.getLogger(),ws).join(); 49 } catch (IOException e) { 50 Util.displayIOException(e,listener); 51 e.printStackTrace( listener.fatalError("command execution failed") ); 52 r = -1; 53 } 54 return r==0; 55 } finally { 56 try { 57 if(script!=null) 58 script.delete(); 59 } catch (IOException e) { 60 Util.displayIOException(e,listener); 61 e.printStackTrace( listener.fatalError("Unable to delete script file "+script) ); 62 } 63 } 64 } 65 66 protected abstract String [] buildCommandLine(FilePath script); 67 68 protected abstract String getContents(); 69 70 protected abstract String getFileExtension(); 71 } 72 | Popular Tags |