Removed most of the 'extra' logging I had put in, but kept the
consistency between .Exists() checks and removed extra code being called
twice within logical a logical flow.  It compiles fine on Mono so I'd
imagine it's fine on MS.NET.  

If someone has a suggestion, let me know and I'll fix it.

~ Matthew
Index: DeleteTask.cs
===================================================================
RCS file: /cvsroot/nant/nant/src/NAnt.Core/Tasks/DeleteTask.cs,v
retrieving revision 1.17
diff -u -r1.17 DeleteTask.cs
--- DeleteTask.cs	23 Aug 2003 13:31:34 -0000	1.17
+++ DeleteTask.cs	12 Oct 2003 15:38:27 -0000
@@ -16,6 +16,7 @@
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 //
 // Gerry Shaw ([EMAIL PROTECTED])
+// Matthew Metnetsky ([EMAIL PROTECTED])
 
 using System;
 using System.IO;
@@ -118,9 +119,11 @@
         #region Override implementation of Task
 
         protected override void ExecuteTask() {
-            // limit task to deleting either a file or a directory or a file set
+            // limit task to deleting either a file, directory, or file set
             if (FileName != null && DirectoryName != null) {
-                throw new BuildException("Cannot specify 'file' and 'dir' in the same delete task.", Location);
+                string msg = "Cannot specify 'file' and 'dir' in the same " +
+                             "delete task.";
+                throw new BuildException(msg, Location);
             }
 
             if (FileName != null) {
@@ -129,44 +132,49 @@
                 try {
                     path = Project.GetFullPath(FileName);
                 } catch (Exception e) {
-                    string msg = String.Format(CultureInfo.InvariantCulture, "Could not determine path from {0}.", FileName);
+                    string msg = 
+                        String.Format(CultureInfo.InvariantCulture, 
+                                      "Could not determine path from '{0}'.", 
+                                      FileName);
                     throw new BuildException(msg, Location, e);
                 }
-                DeleteFile(path, true);
-
+                DeleteFile(path);
             } else if (DirectoryName != null) {
                 // try to delete specified directory
                 string path = null;
                 try {
                     path = Project.GetFullPath(DirectoryName);
                 } catch (Exception e) {
-                    string msg = String.Format(CultureInfo.InvariantCulture, "Could not determine path from {0}.", DirectoryName);
+                    string msg = 
+                        String.Format(CultureInfo.InvariantCulture, 
+                                      "Could not determine path from '{0}'.", 
+                                      DirectoryName);
                     throw new BuildException(msg, Location, e);
                 }
-                if (!Directory.Exists(path)) {
-                    string msg = String.Format(CultureInfo.InvariantCulture, "Cannot delete directory {0}. The directory does not exist.", path);
-                    throw new BuildException(msg, Location);
-                }
-                
-                Log(Level.Info, LogPrefix + "Deleting directory {0}.", path);
                 RecursiveDeleteDirectory(path);
             } else {
-                // delete files in fileset
+                // dBelete files in fileset
                 if (DeleteFileSet.DirectoryNames.Count == 0) {
-                    Log(Level.Info, LogPrefix + "Deleting {0} files.", DeleteFileSet.FileNames.Count);
+                    Log(Level.Info, 
+                        LogPrefix + "Deleting '{0}' files.", 
+                        DeleteFileSet.FileNames.Count);
                 } else if (DeleteFileSet.FileNames.Count == 0) {
-                    Log(Level.Info, LogPrefix + "Deleting {0} directories.", DeleteFileSet.DirectoryNames.Count);
+                    Log(Level.Info, 
+                        LogPrefix + "Deleting '{0}' directories.", 
+                        DeleteFileSet.DirectoryNames.Count);
                 } else {
-                    Log(Level.Info, LogPrefix + "Deleting {0} files and {1} directories.", DeleteFileSet.FileNames.Count, DeleteFileSet.DirectoryNames.Count);
+                    Log(Level.Info, 
+                        LogPrefix + "Deleting '{0}' files and '{1}' " + 
+                        "directories.",
+                        DeleteFileSet.FileNames.Count, 
+                        DeleteFileSet.DirectoryNames.Count);
                 }
 
                 foreach (string path in DeleteFileSet.FileNames) {
-                    DeleteFile(path, Verbose);
+                    DeleteFile(path);
                 }
                 foreach (string path in DeleteFileSet.DirectoryNames) {
-                    if (Directory.Exists(path)) {
-                        RecursiveDeleteDirectory(path);
-                    }
+                    RecursiveDeleteDirectory(path);
                 }
             }
         }
@@ -178,34 +186,42 @@
         private void RecursiveDeleteDirectory(string path) {
             try {
                 // First, recursively delete all directories in the directory
-                string[] dirs = Directory.GetDirectories(path);
-                foreach (string dir in dirs)
-                    RecursiveDeleteDirectory(dir);
-
-                // Next, delete all files in the directory
-                string[] files = Directory.GetFiles(path);
-                foreach (string file in files) {
-                    try {
-                        File.SetAttributes(file, FileAttributes.Normal);
-                        Log(Level.Verbose, LogPrefix + "Deleting file {0}.", file);
-                        File.Delete(file);
-                    } catch (Exception e) {
-                        string msg = String.Format(CultureInfo.InvariantCulture, "Cannot delete file {0}.", file);
-                        if (FailOnError) {
-                            throw new BuildException(msg, Location, e);
-                        }
-                        Log(Level.Verbose, LogPrefix + msg);
-                    }
+                if (Directory.Exists(path)) {
+		    string[] dirs = Directory.GetDirectories(path);
+		    foreach (string dir in dirs) {
+			RecursiveDeleteDirectory(dir);
+		    }
+
+		    // Next, delete all files in the directory
+		    string[] files = Directory.GetFiles(path);
+		    foreach (string file in files) {
+			try {
+			    DeleteFile(file);
+			} catch (Exception e) {
+			    string msg = 
+				String.Format(CultureInfo.InvariantCulture, 
+					      "Cannot delete file '{0}'.", 
+					      file);
+			    Log(Level.Verbose, LogPrefix + msg);
+			    if (FailOnError) {
+				throw new BuildException(msg, Location, e);
+			    }
+			}
+		    }
+
+		    // Finally, delete the directory
+		    File.SetAttributes(path, FileAttributes.Normal);
+                    Log(Level.Info, 
+                        LogPrefix + "Deleting directory '{0}'.", 
+                        path);
+		    Directory.Delete(path);
                 }
-
-                // Finally, delete the directory
-                File.SetAttributes(path, FileAttributes.Normal);
-                Log(Level.Verbose, LogPrefix + "Deleting directory {0}.", path);
-                Directory.Delete(path);
             } catch (BuildException e) {
                 throw e;
             } catch (Exception e) {
-                string msg = String.Format(CultureInfo.InvariantCulture, "Cannot delete directory {0}.", path);
+                string msg = String.Format(CultureInfo.InvariantCulture, 
+                                           "Cannot delete directory '{0}'.", 
+                                           path);
                 if (FailOnError) {
                     throw new BuildException(msg, Location, e);
                 }
@@ -213,22 +229,31 @@
             }
         }
 
-        private void DeleteFile(string path, bool verbose) {
+        private void DeleteFile(string path) {
             try {
                 FileInfo deleteInfo = new FileInfo(path);
                 if (deleteInfo.Exists) {
-                    if (verbose) {
-                        Log(Level.Info, LogPrefix + "Deleting file {0}.", path);
-                    }
+                    Log(Level.Verbose,  
+                        LogPrefix + "Deleting file '{0}'.", 
+                        path);
                     if (deleteInfo.Attributes != FileAttributes.Normal) {
-                        File.SetAttributes(deleteInfo.FullName, FileAttributes.Normal);
+                        File.SetAttributes(deleteInfo.FullName, 
+                                           FileAttributes.Normal);
                     }
                     File.Delete(path);
                 } else {
-                    throw new FileNotFoundException();
+                    string msg = 
+                        String.Format(CultureInfo.InvariantCulture, 
+                                      "Cannot delete file '{0}' because it " +
+                                      "does not exist.",
+                                      path);
+                    Log(Level.Verbose, LogPrefix + msg);
+                    throw new BuildException();
                 }
             } catch (Exception e) {
-                string msg = String.Format(CultureInfo.InvariantCulture, "Cannot delete file {0}.", path);
+                string msg = String.Format(CultureInfo.InvariantCulture, 
+                                           "Cannot delete file '{0}'.", 
+                                           path);
                 if (FailOnError) {
                     throw new BuildException(msg, Location, e);
                 }

Reply via email to