protected override void ExecuteTask()
{
// ensure base directory is set, even if fileset was not initialized
// from XML
if (CopyFileSet.BaseDirectory == null) {
CopyFileSet.BaseDirectory = new DirectoryInfo(Project.BaseDirectory);
}
// Clear previous copied files
_fileCopyMap = new Hashtable();
// copy a single file.
if (SourceFile != null) {
if (SourceFile.Exists) {
FileInfo dstInfo = null;
if (ToFile != null) {
dstInfo = ToFile;
} else {
string dstFilePath = Path.Combine(ToDirectory.FullName,
SourceFile.Name);
dstInfo = new FileInfo(dstFilePath);
}
// do the outdated check
bool outdated = (!dstInfo.Exists) || (SourceFile.LastWriteTime > dstInfo.LastWriteTime);
if (Overwrite || outdated) {
// add to a copy map of absolute verified paths
FileCopyMap.Add(dstInfo.FullName, new FileDateInfo(SourceFile.FullName, SourceFile.LastWriteTime));
if (dstInfo.Exists && dstInfo.Attributes != FileAttributes.Normal) {
File.SetAttributes(dstInfo.FullName, FileAttributes.Normal);
}
}
} else {
throw CreateSourceFileNotFoundException (SourceFile.FullName);
}
} else { // copy file set contents.
// get the complete path of the base directory of the fileset, ie, c:\work\nant\src
DirectoryInfo srcBaseInfo = CopyFileSet.BaseDirectory;
// if source file not specified use fileset
foreach (string pathname in CopyFileSet.FileNames) {
FileInfo srcInfo = new FileInfo(pathname);
if (srcInfo.Exists) {
// will holds the full path to the destination file
string dstFilePath;
if (Flatten) {
dstFilePath = Path.Combine(ToDirectory.FullName,
srcInfo.Name);
} else {
// Gets the relative path and file info from the full
// source filepath
// pathname = C:\f2\f3\file1, srcBaseInfo=C:\f2, then
// dstRelFilePath=f3\file1
string dstRelFilePath = "";
if (srcInfo.FullName.IndexOf(srcBaseInfo.FullName, 0) != -1) {
dstRelFilePath = srcInfo.FullName.Substring(
srcBaseInfo.FullName.Length);
} else {
dstRelFilePath = srcInfo.Name;
}
if (dstRelFilePath[0] == Path.DirectorySeparatorChar) {
dstRelFilePath = dstRelFilePath.Substring(1);
}
// The full filepath to copy to.
dstFilePath = Path.Combine(ToDirectory.FullName,
dstRelFilePath);
}
// do the outdated check
FileInfo dstInfo = new FileInfo(dstFilePath);
bool outdated = (!dstInfo.Exists) || (srcInfo.LastWriteTime > dstInfo.LastWriteTime);
if (Overwrite || outdated) {
// construct FileDateInfo for current file
FileDateInfo newFile = new FileDateInfo(srcInfo.FullName,
srcInfo.LastWriteTime);
// if multiple source files are selected to be copied
// to the same destination file, then only the last
// updated source should actually be copied
FileDateInfo oldFile = (FileDateInfo) FileCopyMap[dstInfo.FullName];
if (oldFile != null) {
// if current file was updated after scheduled file,
// then replace it
if (newFile.LastWriteTime > oldFile.LastWriteTime) {
FileCopyMap[dstInfo.FullName] = newFile;
}
} else {
FileCopyMap.Add(dstInfo.FullName, newFile);
if (dstInfo.Exists && dstInfo.Attributes != FileAttributes.Normal) {
File.SetAttributes(dstInfo.FullName, FileAttributes.Normal);
}
}
}
} else {
throw CreateSourceFileNotFoundException (srcInfo.FullName);
}
}
if (IncludeEmptyDirs && !Flatten) {
// create any specified directories that weren't created during the copy (ie: empty directories)
foreach (string pathname in CopyFileSet.DirectoryNames) {
DirectoryInfo srcInfo = new DirectoryInfo(pathname);
// skip directory if not relative to base dir of fileset
if (srcInfo.FullName.IndexOf(srcBaseInfo.FullName) == -1) {
continue;
}
string dstRelPath = srcInfo.FullName.Substring(srcBaseInfo.FullName.Length);
if (dstRelPath.Length > 0 && dstRelPath[0] == Path.DirectorySeparatorChar) {
dstRelPath = dstRelPath.Substring(1);
}
// The full filepath to copy to.
string destinationDirectory = Path.Combine(ToDirectory.FullName, dstRelPath);
if (!Directory.Exists(destinationDirectory)) {
try {
Directory.CreateDirectory(destinationDirectory);
} catch (Exception ex) {
throw new BuildException(string.Format(CultureInfo.InvariantCulture,
"Failed to create directory '{0}'.", destinationDirectory ),
Location, ex);
}
Log(Level.Verbose, "Created directory '{0}'.", destinationDirectory);
}
}
}
}
// do all the actual copy operations now
DoFileOperations();
}