(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / nant / src / Tasks / DeleteTask.cs
blob4d4abf25149dba3bf46af83164c1c43a71e8b5a3
1 // NAnt - A .NET build tool
2 // Copyright (C) 2001 Gerry Shaw
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // Gerry Shaw (gerry_shaw@yahoo.com)
21 // TODO: move this into the task documentation (once we figure out how tasks
22 // should be documented - xml??
25 verbose: Show name of each deleted file ("true"/"false"). Default is "false"
26 when omitted.
28 quiet: If the file does not exist, do not display a diagnostic message or
29 modify the exit status to reflect an error (unless Ant has been invoked with
30 the -verbose or -debug switches). This means that if a file or directory cannot
31 be deleted, then no error is reported. This setting emulates the -f option to
32 the Unix "rm" command. ("true"/"false"). Default is "false" meaning things are
33 "noisy". Setting this to true, implies setting failonerror to false.
35 failonerror: This flag (which is only of relevance if 'quiet' is false),
36 controls whether an error -such as a failure to delete a file- stops the build
37 task, or is merely reported to the screen. The default is "true"
41 namespace SourceForge.NAnt {
43 using System;
44 using System.IO;
46 [TaskName("delete")]
47 public class DeleteTask : Task {
49 [TaskAttribute("file")]
50 string _file = null;
52 [TaskAttribute("dir")]
53 string _dir = null;
55 [TaskAttribute("verbose")]
56 [BooleanValidator()]
57 string _verbose = Boolean.FalseString;
59 [TaskAttribute("failonerror")]
60 [BooleanValidator()]
61 string _failOnError = Boolean.TrueString;
63 /// <summary>If true then delete empty directories when using filesets.</summary>
64 [TaskAttribute("includeEmptyDirs")]
65 [BooleanValidator()]
66 string _includeEmptyDirs = Boolean.FalseString;
68 [TaskFileSet("fileset")]
69 FileSet _fileset = new FileSet(false);
71 public string FileName { get { return _file; } }
72 public string DirectoryName { get { return _dir; } }
73 public bool FailOnError { get { return Convert.ToBoolean(_failOnError); } }
74 public bool IncludeEmptyDirectories { get { return Convert.ToBoolean(_includeEmptyDirs); } }
75 public FileSet DeleteFileSet { get { return _fileset; } }
77 public bool Verbose {
78 get {
79 return (Project.Verbose || Convert.ToBoolean(_verbose));
83 protected override void ExecuteTask() {
85 // limit task to deleting either a file or a directory or a file set
86 if (FileName != null && DirectoryName != null) {
87 throw new BuildException("Cannot specify 'file' and 'dir' in the same delete task", Location);
90 // try to delete specified file
91 if (FileName != null) {
92 string path = null;
93 try {
94 path = Project.GetFullPath(FileName);
95 } catch (Exception e) {
96 string msg = String.Format("Could not determine path from {0}", FileName);
97 throw new BuildException(msg, Location, e);
99 DeleteFile(path);
101 // try to delete specified directory
102 } else if (DirectoryName != null) {
103 string path = null;
104 try {
105 path = Project.GetFullPath(DirectoryName);
106 } catch (Exception e) {
107 string msg = String.Format("Could not determine path from {0}", DirectoryName);
108 throw new BuildException(msg, Location, e);
110 DeleteDirectory(path);
112 // delete files/directories in fileset
113 } else {
114 // only use the file set if file and dir attributes have NOT been set
115 foreach (string path in DeleteFileSet.FileNames) {
116 DeleteFile(path);
119 if (IncludeEmptyDirectories) {
120 foreach (string path in DeleteFileSet.DirectoryNames) {
121 // only delete EMPTY directories (no files, no directories)
122 DirectoryInfo dirInfo = new DirectoryInfo(path);
124 if ((dirInfo.GetFiles().Length == 0) && (dirInfo.GetDirectories().Length == 0)) {
125 DeleteDirectory(path);
132 void DeleteDirectory(string path) {
133 try {
134 if (Directory.Exists(path)) {
135 if (Verbose) {
136 Log.WriteLine(LogPrefix + "Deleting directory {0}", path);
138 if (path.Length > 10) {
139 Directory.Delete(path, true);
140 } else {
141 // TODO: remove this once this task is fully tested and NAnt is at 1.0
142 Console.WriteLine(LogPrefix + "Path {0} is too close to root to delete this early in development", path);
144 } else {
145 throw new DirectoryNotFoundException();
147 } catch (Exception e) {
148 if (FailOnError) {
149 string msg = String.Format("Cannot delete directory {0}", path);
150 throw new BuildException(msg, Location, e);
155 void DeleteFile(string path) {
156 try {
157 if (File.Exists(path)) {
158 if (Verbose) {
159 Log.WriteLine(LogPrefix + "Deleting file {0}", path);
161 File.Delete(path);
162 } else {
163 throw new FileNotFoundException();
165 } catch (Exception e) {
166 if (FailOnError) {
167 string msg = String.Format("Cannot delete file {0}", path);
168 throw new BuildException(msg, Location, e);