001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs.optional.dotnet;
020:
021: import org.apache.tools.ant.taskdefs.MatchingTask;
022: import org.apache.tools.ant.taskdefs.condition.Os;
023: import org.apache.tools.ant.types.FileSet;
024: import org.apache.tools.ant.Project;
025: import org.apache.tools.ant.DirectoryScanner;
026:
027: import java.io.File;
028: import java.util.Vector;
029: import java.util.Hashtable;
030: import java.util.Enumeration;
031:
032: /**
033: * refactoring of some stuff so that different things (like ILASM)
034: * can use shared code.
035: */
036: public class DotnetBaseMatchingTask extends MatchingTask {
037: // CheckStyle:VisibilityModifier OFF - bc
038: /**
039: * output file. If not supplied this is derived from the source file
040: */
041: protected File outputFile;
042: /**
043: * filesets of file to compile
044: */
045: protected Vector filesets = new Vector();
046:
047: /**
048: * source directory upon which the search pattern is applied
049: */
050: protected File srcDir;
051:
052: /**
053: * Are we running on Windows?
054: *
055: * @since Ant 1.6.3
056: */
057: // CheckStyle:ConstantNameCheck OFF - bc
058: protected static final boolean isWindows = Os.isFamily("windows");
059:
060: // CheckStyle:ConstantNameCheck ON
061: // CheckStyle:VisibilityModifier ON
062:
063: /**
064: * Overridden because we need to be able to set the srcDir.
065: * @return the source directory.
066: */
067: public File getSrcDir() {
068: return this .srcDir;
069: }
070:
071: /**
072: * Set the source directory of the files to be compiled.
073: *
074: *@param srcDirName The new SrcDir value
075: */
076: public void setSrcDir(File srcDirName) {
077: this .srcDir = srcDirName;
078: }
079:
080: /**
081: * Set the name of exe/library to create.
082: *
083: *@param file The new outputFile value
084: */
085: public void setDestFile(File file) {
086: outputFile = file;
087: }
088:
089: /**
090: * add a new source directory to the compile
091: * @param src a fileset.
092: */
093: public void addSrc(FileSet src) {
094: filesets.add(src);
095: }
096:
097: /**
098: * get the destination file
099: * @return the dest file or null for not assigned
100: */
101: public File getDestFile() {
102: return outputFile;
103: }
104:
105: /**
106: * create the list of files
107: * @param command the command to create the files for.
108: * @param filesToBuild vector to add files to
109: * @param outputTimestamp timestamp to compare against
110: * @return number of files out of date
111: */
112: protected int buildFileList(NetCommand command,
113: Hashtable filesToBuild, long outputTimestamp) {
114: int filesOutOfDate = 0;
115: boolean scanImplicitFileset = getSrcDir() != null
116: || filesets.size() == 0;
117: if (scanImplicitFileset) {
118: //scan for an implicit fileset if there was a srcdir set
119: //or there was no srcDir set but there was no contained classes
120: if (getSrcDir() == null) {
121: //if there is no src dir here, set it
122: setSrcDir(getProject().resolveFile("."));
123: }
124: log("working from source directory " + getSrcDir(),
125: Project.MSG_VERBOSE);
126: //get dependencies list.
127: DirectoryScanner scanner = getDirectoryScanner(getSrcDir());
128: filesOutOfDate = command.scanOneFileset(scanner,
129: filesToBuild, outputTimestamp);
130: }
131: //get any included source directories
132: for (int i = 0; i < filesets.size(); i++) {
133: FileSet fs = (FileSet) filesets.elementAt(i);
134: filesOutOfDate += command.scanOneFileset(fs
135: .getDirectoryScanner(getProject()), filesToBuild,
136: outputTimestamp);
137: }
138:
139: return filesOutOfDate;
140: }
141:
142: /**
143: * add the list of files to a command
144: * @param filesToBuild vector of files
145: * @param command the command to append to
146: */
147: protected void addFilesToCommand(Hashtable filesToBuild,
148: NetCommand command) {
149: int count = filesToBuild.size();
150: log("compiling " + count + " file" + ((count == 1) ? "" : "s"),
151: Project.MSG_VERBOSE);
152: Enumeration files = filesToBuild.elements();
153: while (files.hasMoreElements()) {
154: File file = (File) files.nextElement();
155: command.addArgument(file.toString());
156: }
157: }
158:
159: /**
160: * determine the timestamp of the output file
161: * @return a timestamp or 0 for no output file known/exists
162: */
163: protected long getOutputFileTimestamp() {
164: long outputTimestamp;
165: if (getDestFile() != null && getDestFile().exists()) {
166: outputTimestamp = getDestFile().lastModified();
167: } else {
168: outputTimestamp = 0;
169: }
170: return outputTimestamp;
171: }
172:
173: /**
174: * finish off the command by adding all dependent files, execute
175: * @param command the command to update.
176: * @param ignoreTimestamps not used.
177: */
178: protected void addFilesAndExecute(NetCommand command,
179: boolean ignoreTimestamps) {
180: long outputTimestamp = getOutputFileTimestamp();
181: Hashtable filesToBuild = new Hashtable();
182: int filesOutOfDate = buildFileList(command, filesToBuild,
183: outputTimestamp);
184:
185: //now run the command of exe + settings + files
186: if (filesOutOfDate > 0) {
187: //add the files to the command
188: addFilesToCommand(filesToBuild, command);
189: command.runCommand();
190: } else {
191: log("output file is up to date", Project.MSG_VERBOSE);
192: }
193: }
194:
195: }
|