| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package net.sf.antcontrib.platform; |
| 17 |
|
| 18 |
|
| 19 |
import java.io.File; |
| 20 |
import java.io.FileOutputStream; |
| 21 |
|
| 22 |
import org.apache.tools.ant.BuildException; |
| 23 |
import org.apache.tools.ant.taskdefs.ExecTask; |
| 24 |
import org.apache.tools.ant.types.Commandline; |
| 25 |
import org.apache.tools.ant.util.FileUtils; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class ShellScriptTask extends ExecTask { |
| 37 |
|
| 38 |
private StringBuffer script = new StringBuffer(); |
| 39 |
private String shell = null; |
| 40 |
private File tmpFile; |
| 41 |
private String tmpSuffix = null; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public void addText(String s) { |
| 47 |
script.append(getProject().replaceProperties(s)); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public void setInputString(String s) { |
| 54 |
script.append(s); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public void setShell(String shell) { |
| 62 |
this.shell = shell; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public void setExecutable(String shell) { |
| 70 |
this.shell = shell; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public void setCommand(Commandline notUsed) { |
| 80 |
throw new BuildException("Attribute command is not supported"); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public void setTmpSuffix(String tmpSuffix) { |
| 93 |
this.tmpSuffix = tmpSuffix; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public void execute() throws BuildException { |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
if (shell == null) |
| 119 |
throw new BuildException("You must specify a shell to run."); |
| 120 |
|
| 121 |
try { |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
{ |
| 128 |
writeScript(); |
| 129 |
super.createArg().setValue(tmpFile.getAbsolutePath()); |
| 130 |
} |
| 131 |
super.setExecutable(shell); |
| 132 |
super.execute(); |
| 133 |
} |
| 134 |
finally { |
| 135 |
if (tmpFile != null) { |
| 136 |
if (! tmpFile.delete()) { |
| 137 |
log("Non-fatal error: could not delete temporary file " + |
| 138 |
tmpFile.getAbsolutePath()); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
protected void writeScript() throws BuildException { |
| 148 |
FileOutputStream os = null; |
| 149 |
try { |
| 150 |
FileUtils fileUtils = FileUtils.newFileUtils(); |
| 151 |
|
| 152 |
tmpFile = fileUtils.createTempFile("script", tmpSuffix, null); |
| 153 |
os = new java.io.FileOutputStream(tmpFile); |
| 154 |
String string = script.toString(); |
| 155 |
os.write(string.getBytes(), 0, string.length()); |
| 156 |
os.close(); |
| 157 |
} |
| 158 |
catch (Exception e) { |
| 159 |
throw new BuildException(e); |
| 160 |
} |
| 161 |
finally { |
| 162 |
try {os.close();} catch (Throwable t) {} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
} |
| 167 |
|