1 18 24 26 package org.apache.tools.ant.taskdefs.optional.dotnet; 27 28 30 import java.io.File ; 31 import java.io.IOException ; 32 import java.io.FileOutputStream ; 33 import java.io.PrintWriter ; 34 import java.io.BufferedOutputStream ; 35 import java.util.Hashtable ; 36 37 import org.apache.tools.ant.BuildException; 38 import org.apache.tools.ant.Project; 39 import org.apache.tools.ant.Task; 40 import org.apache.tools.ant.DirectoryScanner; 41 import org.apache.tools.ant.util.FileUtils; 42 import org.apache.tools.ant.taskdefs.Execute; 43 import org.apache.tools.ant.taskdefs.ExecuteStreamHandler; 44 import org.apache.tools.ant.taskdefs.LogStreamHandler; 45 import org.apache.tools.ant.types.Commandline; 46 47 56 57 public class NetCommand { 58 59 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 60 62 65 protected Task owner; 66 67 70 protected Execute executable; 71 72 75 protected Commandline commandLine; 76 77 80 protected String title; 81 82 85 protected String program; 86 87 90 protected boolean traceCommandLine = false; 91 92 95 protected boolean failOnError; 96 97 99 103 private File directory; 104 105 108 private boolean useResponseFile = false; 109 110 113 private File temporaryCommandFile; 114 115 118 private int automaticResponseFileThreshold = 64; 119 120 127 128 public NetCommand(Task owner, String title, String program) { 129 this.owner = owner; 130 this.title = title; 131 this.program = program; 132 commandLine = new Commandline(); 133 commandLine.setExecutable(program); 134 } 135 136 137 142 public void setTraceCommandLine(boolean b) { 143 traceCommandLine = b; 144 } 145 146 147 153 public void setFailOnError(boolean b) { 154 failOnError = b; 155 } 156 157 158 163 public boolean getFailFailOnError() { 164 return failOnError; 165 } 166 167 168 172 public void setDirectory(File directory) { 173 this.directory = directory; 174 } 175 176 181 protected void logVerbose(String msg) { 182 owner.getProject().log(msg, Project.MSG_VERBOSE); 183 } 184 185 186 191 protected void logError(String msg) { 192 owner.getProject().log(msg, Project.MSG_ERR); 193 } 194 195 196 202 public void addArgument(String argument) { 203 if (argument != null && argument.length() != 0) { 204 commandLine.createArgument().setValue(argument); 205 } 206 } 207 208 214 public void addArguments(String [] arguments) { 215 if (arguments != null && arguments.length != 0) { 216 for (int i = 0; i < arguments.length; i++) { 217 addArgument(arguments[i]); 218 } 219 } 220 } 221 222 229 public void addArgument(String argument1, String argument2) { 230 if (argument2 != null && argument2.length() != 0) { 231 commandLine.createArgument().setValue(argument1 + argument2); 232 } 233 } 234 235 239 public boolean isUseResponseFile() { 240 return useResponseFile; 241 } 242 243 247 public void setUseResponseFile(boolean useResponseFile) { 248 this.useResponseFile = useResponseFile; 249 } 250 251 255 public int getAutomaticResponseFileThreshold() { 256 return automaticResponseFileThreshold; 257 } 258 259 263 public void setAutomaticResponseFileThreshold(int automaticResponseFileThreshold) { 264 this.automaticResponseFileThreshold = automaticResponseFileThreshold; 265 } 266 267 270 protected void prepareExecutor() { 271 if (owner == null) { 273 throw new RuntimeException ("no owner"); 274 } 275 if (owner.getProject() == null) { 276 throw new RuntimeException ("Owner has no project"); 277 } 278 File dir = owner.getProject().getBaseDir(); 279 if (directory != null) { 280 dir = directory; 281 } 282 283 ExecuteStreamHandler handler = new LogStreamHandler(owner, 284 Project.MSG_INFO, Project.MSG_WARN); 285 executable = new Execute(handler, null); 286 executable.setAntRun(owner.getProject()); 287 executable.setWorkingDirectory(dir); 288 } 289 290 291 297 public void runCommand() 298 throws BuildException { 299 prepareExecutor(); 300 int err = -1; 301 try { 303 if (traceCommandLine) { 304 owner.log("In directory " + executable.getWorkingDirectory()); 305 owner.log(commandLine.describeCommand()); 306 } else { 307 logVerbose("In directory " + executable.getWorkingDirectory()); 309 logVerbose(commandLine.describeCommand()); 310 } 311 setExecutableCommandLine(); 312 err = executable.execute(); 313 if (Execute.isFailure(err)) { 314 if (failOnError) { 315 throw new BuildException(title + " returned: " + err, owner.getLocation()); 316 } else { 317 owner.log(title + " Result: " + err, Project.MSG_ERR); 318 } 319 } 320 } catch (IOException e) { 321 throw new BuildException(title + " failed: " + e, e, owner.getLocation()); 322 } finally { 323 if (temporaryCommandFile != null) { 324 temporaryCommandFile.delete(); 325 } 326 } 327 } 328 329 332 private void setExecutableCommandLine() { 333 334 String [] commands = commandLine.getCommandline(); 335 if (automaticResponseFileThreshold > 0 337 && commands.length > automaticResponseFileThreshold) { 338 useResponseFile = true; 339 } 340 if (!useResponseFile || commands.length <= 1) { 341 executable.setCommandline(commands); 343 } else { 344 FileOutputStream fos = null; 348 349 temporaryCommandFile = FILE_UTILS.createTempFile("cmd", ".txt", null); 350 owner.log("Using response file " + temporaryCommandFile, Project.MSG_VERBOSE); 351 352 try { 353 fos = new FileOutputStream (temporaryCommandFile); 354 PrintWriter out = new PrintWriter (new BufferedOutputStream (fos)); 355 for (int i = 1; i < commands.length; ++i) { 357 out.println(commands[i]); 358 } 359 out.flush(); 360 out.close(); 361 } catch (IOException ex) { 362 throw new BuildException("saving command stream to " + temporaryCommandFile, ex); 363 } 364 365 String [] newCommandLine = new String [2]; 366 newCommandLine[0] = commands[0]; 367 newCommandLine[1] = "@" + temporaryCommandFile.getAbsolutePath(); 368 logVerbose(Commandline.describeCommand(newCommandLine)); 369 executable.setCommandline(newCommandLine); 370 } 371 } 372 373 374 382 public int scanOneFileset(DirectoryScanner scanner, Hashtable filesToBuild, 383 long outputTimestamp) { 384 int filesOutOfDate = 0; 385 String [] dependencies = scanner.getIncludedFiles(); 386 File base = scanner.getBasedir(); 387 for (int i = 0; i < dependencies.length; i++) { 389 File targetFile = new File (base, dependencies[i]); 390 if (filesToBuild.get(targetFile) == null) { 391 filesToBuild.put(targetFile, targetFile); 392 if (targetFile.lastModified() > outputTimestamp) { 393 filesOutOfDate++; 394 owner.log(targetFile.toString() + " is out of date", 395 Project.MSG_VERBOSE); 396 } else { 397 owner.log(targetFile.toString(), 398 Project.MSG_VERBOSE); 399 } 400 } 401 } 402 return filesOutOfDate; 403 } 404 } 405 | Popular Tags |