1 18 19 package org.apache.tools.ant.taskdefs.optional.dotnet; 20 21 import org.apache.tools.ant.Task; 22 import org.apache.tools.ant.BuildException; 23 import org.apache.tools.ant.Project; 24 import org.apache.tools.ant.util.FileUtils; 25 26 import java.io.File ; 27 28 46 public class ImportTypelib extends Task { 47 48 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 49 50 53 private File srcFile; 54 55 58 private File destFile; 59 60 63 private String namespace; 64 65 68 private boolean useSysArray = false; 69 70 73 private boolean unsafe = false; 74 75 78 private String extraOptions = null; 79 80 86 public void setDestFile(File destFile) { 87 this.destFile = destFile; 88 } 89 90 95 public void setNamespace(String namespace) { 96 this.namespace = namespace; 97 } 98 99 104 public void setSrcFile(File srcFile) { 105 this.srcFile = srcFile; 106 } 107 108 112 public void setUnsafe(boolean unsafe) { 113 this.unsafe = unsafe; 114 } 115 116 120 public void setUseSysArray(boolean useSysArray) { 121 this.useSysArray = useSysArray; 122 } 123 124 128 public void setExtraOptions(String extraOptions) { 129 this.extraOptions = extraOptions; 130 } 131 132 136 protected void validate() 137 throws BuildException { 138 if (destFile == null) { 139 throw new BuildException("destination file must be specified"); 140 } 141 if (destFile.isDirectory()) { 142 throw new BuildException( 143 "destination file is a directory"); 144 } 145 if (srcFile == null || !srcFile.exists()) { 146 throw new BuildException( 147 "source file does not exist"); 148 } 149 if (srcFile.isDirectory()) { 150 throw new BuildException( 151 "source file is a directory"); 152 } 153 if (namespace == null) { 154 throw new BuildException("No namespace"); 155 } 156 } 157 158 163 private boolean isExecuteNeeded() { 164 if (!destFile.exists()) { 165 log("Destination file does not exist: a build is required", 166 Project.MSG_VERBOSE); 167 return true; 168 } 169 long sourceTime = srcFile.lastModified(); 170 long destTime = destFile.lastModified(); 171 if (sourceTime > (destTime + FILE_UTILS.getFileTimestampGranularity())) { 172 log("Source file is newer than the dest file: a rebuild is required", 173 Project.MSG_VERBOSE); 174 return true; 175 } else { 176 log("The output file is up to date", Project.MSG_VERBOSE); 177 return false; 178 } 179 180 } 181 182 183 187 public void execute() throws BuildException { 188 log("This task is deprecated and will be removed in a future version\n" 189 + "of Ant. It is now part of the .NET Antlib:\n" 190 + "http://ant.apache.org/antlibs/dotnet/index.html", 191 Project.MSG_WARN); 192 validate(); 193 log("Importing typelib " + srcFile 194 + " to assembly " + destFile 195 + " in namespace " + namespace, Project.MSG_VERBOSE); 196 if (!isExecuteNeeded()) { 198 return; 199 } 200 201 NetCommand command = new NetCommand(this, "ImportTypelib", "tlbimp"); 202 command.setFailOnError(true); 203 command.addArgument(srcFile.toString()); 204 command.addArgument("/nologo"); 206 command.addArgument("/out:" + destFile); 207 command.addArgument("/namespace:", namespace); 208 if (useSysArray) { 209 command.addArgument("/sysarray"); 210 } 211 if (unsafe) { 212 command.addArgument("/unsafe"); 213 } 214 command.addArgument(extraOptions); 215 command.runCommand(); 216 } 217 } 218 | Popular Tags |