1. /*
  2. * Copyright 2000-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs.optional.dotnet;
  18. import org.apache.tools.ant.taskdefs.MatchingTask;
  19. import org.apache.tools.ant.types.FileSet;
  20. import org.apache.tools.ant.Project;
  21. import org.apache.tools.ant.DirectoryScanner;
  22. import java.io.File;
  23. import java.util.Vector;
  24. import java.util.Hashtable;
  25. import java.util.Enumeration;
  26. /**
  27. * refactoring of some stuff so that different things (like ILASM)
  28. * can use shared code.
  29. */
  30. public class DotnetBaseMatchingTask extends MatchingTask {
  31. /**
  32. * output file. If not supplied this is derived from the source file
  33. */
  34. protected File outputFile;
  35. /**
  36. * filesets of file to compile
  37. */
  38. protected Vector filesets = new Vector();
  39. /**
  40. * source directory upon which the search pattern is applied
  41. */
  42. protected File srcDir;
  43. /**
  44. * Overridden because we need to be able to set the srcDir.
  45. */
  46. public File getSrcDir() {
  47. return this.srcDir;
  48. }
  49. /**
  50. * Set the source directory of the files to be compiled.
  51. *
  52. *@param srcDirName The new SrcDir value
  53. */
  54. public void setSrcDir(File srcDirName) {
  55. this.srcDir = srcDirName;
  56. }
  57. /**
  58. * Set the name of exe/library to create.
  59. *
  60. *@param file The new outputFile value
  61. */
  62. public void setDestFile(File file) {
  63. outputFile = file;
  64. }
  65. /**
  66. * add a new source directory to the compile
  67. * @param src
  68. */
  69. public void addSrc(FileSet src) {
  70. filesets.add(src);
  71. }
  72. /**
  73. * get the destination file
  74. * @return the dest file or null for not assigned
  75. */
  76. public File getDestFile() {
  77. return outputFile;
  78. }
  79. /**
  80. * create the list of files
  81. * @param filesToBuild vector to add files to
  82. * @param outputTimestamp timestamp to compare against
  83. * @return number of files out of date
  84. */
  85. protected int buildFileList(NetCommand command, Hashtable filesToBuild, long outputTimestamp) {
  86. int filesOutOfDate = 0;
  87. boolean scanImplicitFileset
  88. = getSrcDir() != null || filesets.size() == 0;
  89. if (scanImplicitFileset) {
  90. //scan for an implicit fileset if there was a srcdir set
  91. //or there was no srcDir set but there was no contained classes
  92. if (getSrcDir() == null) {
  93. //if there is no src dir here, set it
  94. setSrcDir(getProject().resolveFile("."));
  95. }
  96. log("working from source directory " + getSrcDir(),
  97. Project.MSG_VERBOSE);
  98. //get dependencies list.
  99. DirectoryScanner scanner = getDirectoryScanner(getSrcDir());
  100. filesOutOfDate = command.scanOneFileset(scanner,
  101. filesToBuild, outputTimestamp);
  102. }
  103. //get any included source directories
  104. for (int i = 0; i < filesets.size(); i++) {
  105. FileSet fs = (FileSet) filesets.elementAt(i);
  106. filesOutOfDate += command.scanOneFileset(
  107. fs.getDirectoryScanner(getProject()),
  108. filesToBuild,
  109. outputTimestamp);
  110. }
  111. return filesOutOfDate;
  112. }
  113. /**
  114. * add the list of files to a command
  115. * @param filesToBuild vector of files
  116. * @param command the command to append to
  117. */
  118. protected void addFilesToCommand(Hashtable filesToBuild, NetCommand command) {
  119. int count = filesToBuild.size();
  120. log("compiling " + count + " file" + ((count == 1) ? "" : "s"));
  121. Enumeration files = filesToBuild.elements();
  122. while (files.hasMoreElements()) {
  123. File file = (File) files.nextElement();
  124. command.addArgument(file.toString());
  125. }
  126. }
  127. /**
  128. * determine the timestamp of the output file
  129. * @return a timestamp or 0 for no output file known/exists
  130. */
  131. protected long getOutputFileTimestamp() {
  132. long outputTimestamp;
  133. if (getDestFile() != null && getDestFile().exists()) {
  134. outputTimestamp = getDestFile().lastModified();
  135. } else {
  136. outputTimestamp = 0;
  137. }
  138. return outputTimestamp;
  139. }
  140. /**
  141. * finish off the command by adding all dependent files, execute
  142. * @param command
  143. */
  144. protected void addFilesAndExecute(NetCommand command, boolean ignoreTimestamps) {
  145. long outputTimestamp = getOutputFileTimestamp();
  146. Hashtable filesToBuild = new Hashtable();
  147. int filesOutOfDate = buildFileList(command, filesToBuild, outputTimestamp);
  148. //add the files to the command
  149. addFilesToCommand(filesToBuild, command);
  150. //now run the command of exe + settings + files
  151. if (filesOutOfDate > 0) {
  152. command.runCommand();
  153. } else {
  154. log("output file is up to date", Project.MSG_VERBOSE);
  155. }
  156. }
  157. }