📄 dotnetcompile.java
字号:
* Set the destination directory of files to be compiled. * *@param dirName The new DestDir value */ public void setDestDir(File dirName) { log("DestDir currently unused", Project.MSG_WARN); } /** * set the target type to one of exe|library|module|winexe * @param targetType the enumerated value. */ public void setTargetType(TargetTypes targetType) { this.targetType = targetType.getValue(); } /** * Set the type of target. * *@param ttype The new TargetType value *@exception BuildException if target is not one of * exe|library|module|winexe */ public void setTargetType(String ttype) throws BuildException { ttype = ttype.toLowerCase(); if (ttype.equals("exe") || ttype.equals("library") || ttype.equals("module") || ttype.equals("winexe")) { targetType = ttype; } else { throw new BuildException("targetType " + ttype + " is not one of 'exe', 'module', 'winexe' or 'library'"); } } /** * Gets the TargetType attribute * *@return The TargetType value */ public String getTargetType() { return targetType; } /** * get the argument or null for no argument needed * *@return The TargetType Parameter to CSC */ protected String getTargetTypeParameter() { if (notEmpty(targetType)) { return "/target:" + targetType; } else { return null; } } /** * Set the filename of icon to include. * *@param fileName path to the file. Can be relative, absolute, whatever. */ public void setWin32Icon(File fileName) { win32icon = fileName; } /** * get the argument or null for no argument needed * *@return The Win32Icon Parameter to CSC */ protected String getWin32IconParameter() { if (win32icon != null) { return "/win32icon:" + win32icon.toString(); } else { return null; } } /** * Sets the filename of a win32 resource (.RES) file to include. * This is not a .NET resource, but what Windows is used to. * *@param fileName path to the file. Can be relative, absolute, whatever. */ public void setWin32Res(File fileName) { win32res = fileName; } /** * Gets the file of the win32 .res file to include. * @return path to the file. */ public File getWin32Res() { return win32res; } /** * get the argument or null for no argument needed * *@return The Win32Res Parameter to CSC */ protected String getWin32ResParameter() { if (win32res != null) { return "/win32res:" + win32res.toString(); } else { return null; } } /** * If true, require all compiler output to be in UTF8 format. * *@param enabled The new utf8Output value */ public void setUtf8Output(boolean enabled) { utf8output = enabled; } /** * Gets the utf8OutpuParameter attribute of the CSharp object * *@return The utf8OutpuParameter value */ protected String getUtf8OutputParameter() { return utf8output ? "/utf8output" : null; } /** * add a define to the list of definitions * @param define the define value. */ public void addDefine(DotnetDefine define) { definitionList.addElement(define); } /** * get a list of definitions or null * @return a string beginning /D: or null for no definitions * @throws BuildException if there is an error. */ protected String getDefinitionsParameter() throws BuildException { StringBuffer defines = new StringBuffer(); Enumeration defEnum = definitionList.elements(); boolean firstDefinition = true; while (defEnum.hasMoreElements()) { //loop through all definitions DotnetDefine define = (DotnetDefine) defEnum.nextElement(); if (define.isSet(this)) { //add those that are set, and a delimiter if (!firstDefinition) { defines.append(getDefinitionsDelimiter()); } defines.append(define.getValue(this)); firstDefinition = false; } } if (defines.length() == 0) { return null; } else { return "/d:" + defines; } } /** * Semicolon separated list of modules to refer to. * *@param params The new additionalModules value */ public void setAdditionalModules(String params) { additionalModules = params; } /** * get the argument or null for no argument needed * *@return The AdditionalModules Parameter to CSC */ protected String getAdditionalModulesParameter() { if (notEmpty(additionalModules)) { return "/addmodule:" + additionalModules; } else { return null; } } /** * get the argument or null for no argument needed * *@return The OutputFile Parameter to CSC */ protected String getDestFileParameter() { if (outputFile != null) { return "/out:" + outputFile.toString(); } else { return null; } } /** * If true, fail on compilation errors. * *@param b The new FailOnError value */ public void setFailOnError(boolean b) { failOnError = b; } /** * query fail on error flag * *@return The FailFailOnError value */ public boolean getFailOnError() { return failOnError; } /** * link or embed a resource * @param resource the resource to use. */ public void addResource(DotnetResource resource) { resources.add(resource); } /** * This method gets the name of the executable. * @return the name of the executable */ protected String getExecutable() { return executable; } /** * set the name of the program, overriding the defaults. * Can be used to set the full path to a program, or to switch * to an alternate implementation of the command, such as the Mono or Rotor * versions -provided they use the same command line arguments as the * .NET framework edition * @param executable the name of the program. */ public void setExecutable(String executable) { this.executable = executable; } /** * test for a string containing something useful * *@param s string in *@return true if the argument is not null or empty */ protected boolean notEmpty(String s) { return s != null && s.length() != 0; } /** * validation code * @throws BuildException if validation failed */ protected void validate() throws BuildException { if (outputFile != null && outputFile.isDirectory()) { throw new BuildException("destFile cannot be a directory"); } if (getExecutable() == null) { throw new BuildException("There is no executable defined for this task"); } } /** * Get the pattern for files to compile. * @return The compilation file pattern. */ public String getFilePattern() { return "**/*." + getFileExtension(); } /** * getter for flag * @return The flag indicating whether the compilation is using a response file. */ public boolean isUseResponseFile() { return useResponseFile; } /** * Flag to turn on response file use; default=false. * When set the command params are saved to a file and * this is passed in with @file. The task automatically switches * to this mode with big commands; this option is here for * testing and emergencies * @param useResponseFile a <code>boolean</code> value. */ public void setUseResponseFile(boolean useResponseFile) { this.useResponseFile = useResponseFile; } /** * do the work by building the command line and then calling it * *@throws BuildException if validation or execution failed */ public void execute() throws BuildException { log("This task is deprecated and will be removed in a future version\n" + "of Ant. It is now part of the .NET Antlib:\n" + "http://ant.apache.org/antlibs/dotnet/index.html", Project.MSG_WARN); validate(); NetCommand command = createNetCommand(); //set up response file options command.setAutomaticResponseFileThreshold(AUTOMATIC_RESPONSE_FILE_THRESHOLD); command.setUseResponseFile(useResponseFile); //fill in args fillInSharedParameters(command); addResources(command); addCompilerSpecificOptions(command); int referencesOutOfDate = addReferenceFilesets(command, getOutputFileTimestamp()); //if the refs are out of date, force a build. boolean forceBuild = referencesOutOfDate > 0; addFilesAndExecute(command, forceBuild); } /** * Get the delimiter that the compiler uses between references. * For example, c# will return ";"; VB.NET will return "," * @return The string delimiter for the reference string. */ public abstract String getReferenceDelimiter(); /** * Get the extension of filenames to compile. * @return The string extension of files to compile. */ public abstract String getFileExtension(); /** * fill in the common information * @param command the net command. */ protected void fillInSharedParameters(NetCommand command) { command.setFailOnError(getFailOnError()); //fill in args command.addArgument("/nologo"); command.addArgument(getAdditionalModulesParameter()); command.addArgument(getDebugParameter()); command.addArgument(getDefinitionsParameter()); command.addArguments(getExtraOptionsParameters()); command.addArgument(getMainClassParameter()); command.addArgument(getOptimizeParameter()); command.addArgument(getDestFileParameter()); command.addArgument(getReferencesParameter()); command.addArgument(getTargetTypeParameter()); command.addArgument(getUtf8OutputParameter()); command.addArgument(getWin32IconParameter()); command.addArgument(getWin32ResParameter()); } /** * for every resource declared, we get the (language specific) * resource setting * @param command the net command. */ protected void addResources(NetCommand command) { Enumeration e = resources.elements(); while (e.hasMoreElements()) { DotnetResource resource = (DotnetResource) e.nextElement(); createResourceParameter(command, resource); } } /** * Build a C# style parameter. * @param command the command. * @param resource the resource. */ protected abstract void createResourceParameter(NetCommand command, DotnetResource resource); /** * run through the list of reference files and add them to the command * @param command the command to use. * @param outputTimestamp timestamp to compare against * @return number of files out of date */ protected int addReferenceFilesets(NetCommand command, long outputTimestamp) { int filesOutOfDate = 0; Hashtable filesToBuild = new Hashtable(); for (int i = 0; i < referenceFilesets.size(); i++) { FileSet fs = (FileSet) referenceFilesets.elementAt(i); filesOutOfDate += command.scanOneFileset( fs.getDirectoryScanner(getProject()), filesToBuild, outputTimestamp); } //bail out early if there were no files if (filesToBuild.size() == 0) { return 0; } //now scan the hashtable and add the files Enumeration files = filesToBuild.elements(); while (files.hasMoreElements()) { File file = (File) files.nextElement(); if (isFileManagedBinary(file)) { if (isWindows) { command.addArgument( '"' + REFERENCE_OPTION + file.toString() + '"'); } else { command.addArgument(REFERENCE_OPTION + file.toString()); } } else { log("ignoring " + file + " as it is not a managed executable", Project.MSG_VERBOSE); } } return filesOutOfDate; } /** * create our helper command * @return a command prefilled with the exe name and task name */ protected NetCommand createNetCommand() { NetCommand command = new NetCommand(this, getTaskName(), getExecutable()); return command; } /** * add any compiler specifics * @param command the command to use. */ protected abstract void addCompilerSpecificOptions(NetCommand command); /** * override point for delimiting definitions. * @return The definitions limiter, i.e., ";" */ public String getDefinitionsDelimiter() { return ";"; } /** * test for a file being managed or not * @param file the file to test. * @return true if we think this is a managed executable, and thus OK * for linking * @todo look at the PE header of the exe and see if it is managed or not. */ protected static boolean isFileManagedBinary(File file) { String filename = file.toString().toLowerCase(); return filename.endsWith(".exe") || filename.endsWith(".dll") || filename.endsWith(".netmodule"); } /** * Target types to build. * valid build types are exe|library|module|winexe */ public static class TargetTypes extends EnumeratedAttribute { /** {@inheritDoc}. */ public String[] getValues() { return new String[] { "exe", "library", "module", "winexe" }; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -