1 18 package org.apache.tools.ant.taskdefs.optional.dotnet; 19 20 import org.apache.tools.ant.types.EnumeratedAttribute; 21 import org.apache.tools.ant.BuildException; 22 import org.apache.tools.ant.Task; 23 import org.apache.tools.ant.Project; 24 import org.apache.tools.ant.util.FileUtils; 25 26 import java.io.File ; 27 28 60 public class Ildasm extends Task { 61 62 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 63 64 67 private File sourceFile; 68 69 72 private File destFile; 73 76 private boolean progressBar = false; 77 78 81 private String encoding; 82 83 86 87 private boolean bytes = false; 88 89 92 private boolean linenumbers = false; 93 94 97 private boolean rawExceptionHandling = false; 98 99 102 private boolean showSource = false; 103 104 107 private boolean quoteallnames = false; 108 109 112 private boolean header = false; 113 114 118 private boolean assembler = true; 119 120 124 125 private boolean metadata = false; 126 127 131 private String visibility; 132 133 136 137 private String item; 138 139 142 private String executable = "ildasm"; 143 144 149 private File resourceDir; 150 151 152 158 public void setResourceDir(File resourceDir) { 159 this.resourceDir = resourceDir; 160 } 161 162 171 public void setExecutable(String executable) { 172 this.executable = executable; 173 } 174 175 179 public void setEncoding(EncodingTypes encoding) { 180 this.encoding = encoding.getValue(); 181 } 182 183 187 public void setAssembler(boolean assembler) { 188 this.assembler = assembler; 189 } 190 191 195 public void setBytes(boolean bytes) { 196 this.bytes = bytes; 197 } 198 199 203 public void setDestFile(File destFile) { 204 this.destFile = destFile; 205 } 206 207 211 public void setHeader(boolean header) { 212 this.header = header; 213 } 214 215 220 public void setItem(String item) { 221 this.item = item; 222 } 223 224 228 public void setLinenumbers(boolean linenumbers) { 229 this.linenumbers = linenumbers; 230 } 231 232 236 public void setMetadata(boolean metadata) { 237 this.metadata = metadata; 238 } 239 240 244 public void setProgressBar(boolean progressBar) { 245 this.progressBar = progressBar; 246 } 247 248 252 public void setQuoteallnames(boolean quoteallnames) { 253 this.quoteallnames = quoteallnames; 254 } 255 256 260 public void setRawExceptionHandling(boolean rawExceptionHandling) { 261 this.rawExceptionHandling = rawExceptionHandling; 262 } 263 264 268 public void setShowSource(boolean showSource) { 269 this.showSource = showSource; 270 } 271 272 276 public void setSourceFile(File sourceFile) { 277 this.sourceFile = sourceFile; 278 } 279 280 284 public void setSrcFile(File sourceFile) { 285 setSourceFile(sourceFile); 286 } 287 303 public void setVisibility(String visibility) { 304 this.visibility = visibility; 305 } 306 307 310 private void validate() { 311 if (sourceFile == null || !sourceFile.exists() || !sourceFile.isFile()) { 312 throw new BuildException("invalid source"); 313 } 314 if (destFile == null || destFile.isDirectory()) { 315 throw new BuildException("invalid dest"); 316 } 317 if (resourceDir != null 318 && (!resourceDir.exists() || !resourceDir.isDirectory())) { 319 throw new BuildException("invalid resource directory"); 320 } 321 } 322 323 328 private boolean isDisassemblyNeeded() { 329 if (!destFile.exists()) { 330 log("Destination file does not exist: a build is required", 331 Project.MSG_VERBOSE); 332 return true; 333 } 334 long sourceTime = sourceFile.lastModified(); 335 long destTime = destFile.lastModified(); 336 if (sourceTime > (destTime + FILE_UTILS.getFileTimestampGranularity())) { 337 log("Source file is newer than the dest file: a rebuild is required", 338 Project.MSG_VERBOSE); 339 return true; 340 } else { 341 log("The .il file is up to date", Project.MSG_VERBOSE); 342 return false; 343 } 344 345 } 346 350 public void execute() throws BuildException { 351 log("This task is deprecated and will be removed in a future version\n" 352 + "of Ant. It is now part of the .NET Antlib:\n" 353 + "http://ant.apache.org/antlibs/dotnet/index.html", 354 Project.MSG_WARN); 355 validate(); 356 if (!isDisassemblyNeeded()) { 357 return; 358 } 359 NetCommand command = new NetCommand(this, "ildasm", executable); 360 command.setFailOnError(true); 361 command.addArgument("/text"); 363 command.addArgument("/out=" + destFile.toString()); 364 if (!progressBar) { 365 command.addArgument("/nobar"); 366 } 367 if (linenumbers) { 368 command.addArgument("/linenum"); 369 } 370 if (showSource) { 371 command.addArgument("/source"); 372 } 373 if (quoteallnames) { 374 command.addArgument("/quoteallnames"); 375 } 376 if (header) { 377 command.addArgument("/header"); 378 } 379 if (!assembler) { 380 command.addArgument("/noil"); 381 } 382 if (metadata) { 383 command.addArgument("/tokens"); 384 } 385 command.addArgument("/item:", item); 386 if (rawExceptionHandling) { 387 command.addArgument("/raweh"); 388 } 389 command.addArgument(EncodingTypes.getEncodingOption(encoding)); 390 if (bytes) { 391 command.addArgument("/bytes"); 392 } 393 command.addArgument("/vis:", visibility); 394 395 command.addArgument(sourceFile.getAbsolutePath()); 397 398 File execDir = resourceDir; 401 if (execDir == null) { 402 execDir = destFile.getParentFile(); 403 } 404 command.setDirectory(execDir); 405 406 try { 408 command.runCommand(); 409 } catch (BuildException e) { 410 if (destFile.exists()) { 412 log("Deleting destination file as it may be corrupt"); 413 destFile.delete(); 414 } 415 throw e; 417 } 418 419 } 420 421 424 public static class EncodingTypes extends EnumeratedAttribute { 425 426 public static final String UNICODE = "unicode"; 427 428 public static final String UTF8 = "utf8"; 429 430 public static final String ASCII = "ascii"; 431 432 public String [] getValues() { 433 return new String []{ 434 ASCII, 435 UTF8, 436 UNICODE, 437 }; 438 } 439 440 445 public static String getEncodingOption(String enumValue) { 446 if (UNICODE.equals(enumValue)) { 447 return "/unicode"; 448 } 449 if (UTF8.equals(enumValue)) { 450 return "/utf8"; 451 } 452 return null; 453 } 454 } 455 456 459 public static class VisibilityOptions extends EnumeratedAttribute { 460 461 public String [] getValues() { 462 return new String []{ 463 "pub", "pri", "fam", "asm", "faa", "foa", "psc", }; 471 } 472 473 } 474 } 475 | Popular Tags |