Fix bug #565849.
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / DirectoryScanner.cs
1 //
2 // DirectoryScanner.cs: Class used by BuildItem.
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 // 
7 // (C) 2005 Marek Sieradzki
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 #if NET_2_0
29
30 using System;
31 using System.Collections.Generic;
32 using System.IO;
33
34 using Microsoft.Build.Framework;
35 using Microsoft.Build.Utilities;
36
37 namespace Microsoft.Build.BuildEngine {
38         internal class DirectoryScanner {
39                 
40                 DirectoryInfo   baseDirectory;
41                 ITaskItem[]     includes, excludes;
42                 ITaskItem[]     matchedItems;
43
44                 static bool _runningOnWindows;
45                 
46                 static DirectoryScanner ()
47                 {
48                         PlatformID pid = Environment.OSVersion.Platform;
49                         _runningOnWindows =((int) pid != 128 && (int) pid != 4 && (int) pid != 6);
50                 }
51
52                 public DirectoryScanner ()
53                 {
54                 }
55                 
56                 public void Scan ()
57                 {
58                         Dictionary <string, bool> excludedItems;
59                         List <ITaskItem> includedItems;
60                         string[] splitExclude;
61                         
62                         if (includes == null)
63                                 throw new ArgumentNullException ("Includes");
64                         if (baseDirectory == null)
65                                 throw new ArgumentNullException ("BaseDirectory");
66                         
67                         excludedItems = new Dictionary <string, bool> ();
68                         includedItems = new List <ITaskItem> ();
69                         
70                         if (excludes != null)
71                                 foreach (ITaskItem excl in excludes)
72                                         ProcessExclude (excl.ItemSpec, excludedItems);
73
74                         foreach (ITaskItem include_item in includes)
75                                 ProcessInclude (include_item, excludedItems, includedItems);
76
77                         matchedItems = includedItems.ToArray ();
78                 }
79                 
80                 private void ProcessInclude (ITaskItem include_item, Dictionary <string, bool> excludedItems,
81                                 List <ITaskItem> includedItems)
82                 {
83                         string[] separatedPath;
84                         FileInfo[] fileInfo;
85
86                         string name = include_item.ItemSpec;
87                         if (name.IndexOf ('?') == -1 && name.IndexOf ('*') == -1) {
88                                 if (!excludedItems.ContainsKey (Path.GetFullPath(name)))
89                                         includedItems.Add (include_item);
90                         } else {
91                                 if (name.Split (Path.DirectorySeparatorChar).Length > name.Split (Path.AltDirectorySeparatorChar).Length) {
92                                         separatedPath = name.Split (new char [] {Path.DirectorySeparatorChar},
93                                                         StringSplitOptions.RemoveEmptyEntries);
94                                 } else {
95                                         separatedPath = name.Split (new char [] {Path.AltDirectorySeparatorChar},
96                                                         StringSplitOptions.RemoveEmptyEntries);
97                                 }
98                                 if (separatedPath.Length == 1 && separatedPath [0] == String.Empty)
99                                         return;
100
101                                 int offset = 0;
102                                 if (Path.IsPathRooted (name)) {
103                                         baseDirectory = new DirectoryInfo (Path.GetPathRoot (name));
104                                         if (IsRunningOnWindows)
105                                                 // skip the "drive:"
106                                                 offset = 1;
107                                 }
108
109                                 fileInfo = ParseIncludeExclude (separatedPath, offset, baseDirectory);
110
111                                 foreach (FileInfo fi in fileInfo) {
112                                         if (!excludedItems.ContainsKey (fi.FullName)) {
113                                                 TaskItem item = new TaskItem (include_item);
114                                                 item.ItemSpec = fi.FullName;
115                                                 includedItems.Add (item);
116                                         }
117                                 }
118                         }
119                 }
120                 
121                 private void ProcessExclude (string name, Dictionary <string, bool> excludedItems)
122                 {
123                         string[] separatedPath;
124                         FileInfo[] fileInfo;
125                         
126                         if (name.IndexOf ('?') == -1 && name.IndexOf ('*') == -1) {
127                                 if (!excludedItems.ContainsKey (Path.GetFullPath (name)))
128                                         excludedItems.Add (Path.GetFullPath (name), true);
129                         } else {
130                                 if (name.Split (Path.DirectorySeparatorChar).Length > name.Split (Path.AltDirectorySeparatorChar).Length) {
131                                         separatedPath = name.Split (new char [] {Path.DirectorySeparatorChar},
132                                                                         StringSplitOptions.RemoveEmptyEntries);
133                                 } else {
134                                         separatedPath = name.Split (new char [] {Path.AltDirectorySeparatorChar},
135                                                                         StringSplitOptions.RemoveEmptyEntries);
136                                 }
137                                 if (separatedPath.Length == 1 && separatedPath [0] == String.Empty)
138                                         return;
139
140                                 int offset = 0;
141                                 if (Path.IsPathRooted (name)) {
142                                         baseDirectory = new DirectoryInfo (Path.GetPathRoot (name));
143                                         if (IsRunningOnWindows)
144                                                 // skip the "drive:"
145                                                 offset = 1;
146                                 }
147                                 fileInfo = ParseIncludeExclude (separatedPath, offset, baseDirectory);
148                                 foreach (FileInfo fi in fileInfo)
149                                         if (!excludedItems.ContainsKey (fi.FullName))
150                                                 excludedItems.Add (fi.FullName, true);
151                         }
152                 }
153                 
154                 private FileInfo[] ParseIncludeExclude (string[] input, int ptr, DirectoryInfo directory)
155                 {
156                         if (input.Length > 1 && ptr == 0 && input [0] == String.Empty)
157                                 ptr++;
158                         if (input.Length == ptr + 1) {
159                                 FileInfo[] fi;
160                                 fi = directory.GetFiles (input [ptr]);
161                                 return fi;
162                         } else {
163                                 DirectoryInfo[] di;
164                                 FileInfo[] fi;
165                                 List <FileInfo> fileInfos = new List <FileInfo> ();
166                                 if (input [ptr] == ".") {
167                                         di = new DirectoryInfo [1];
168                                         di [0] = directory;
169                                 } else if (input [ptr] == "..") {
170                                         di = new DirectoryInfo [1];
171                                         di [0] = directory.Parent;
172                                 } else if (input[ptr] == "**")
173                                 {
174                                         // Read this directory and all subdirectories recursive
175                                         Stack<DirectoryInfo> currentDirectories = new Stack<DirectoryInfo>();                                   
176                                         currentDirectories.Push(directory);
177                                         List<DirectoryInfo> allDirectories = new List<DirectoryInfo>();
178                                         
179                                         while (currentDirectories.Count > 0)
180                                         {
181                                                 DirectoryInfo current = currentDirectories.Pop();
182                                                 allDirectories.Insert (0, current);
183                                                 foreach (DirectoryInfo dir in current.GetDirectories())
184                                                 {
185                                                         currentDirectories.Push(dir);
186                                                 }                                               
187                                         }
188                                         
189                                         // No further directories shall be read
190                                         di = allDirectories.ToArray();                                  
191                                 } else {
192                                         di = directory.GetDirectories (input [ptr]);
193                                 }
194                                 foreach (DirectoryInfo info in di) {
195                                         fi = ParseIncludeExclude (input, ptr + 1, info);
196                                         foreach (FileInfo file in fi)
197                                                 fileInfos.Add (file);
198                                 }
199                                 fi = new FileInfo [fileInfos.Count];
200                                 int i = 0;
201                                 foreach (FileInfo file in fileInfos)
202                                         fi [i++] = file;
203                                 return fi;
204                         }
205                 }
206                 
207                 public DirectoryInfo BaseDirectory {
208                         get { return baseDirectory; }
209                         set { baseDirectory = value; }
210                 }
211                 
212                 public ITaskItem[] Includes {
213                         get { return includes; }
214                         set { includes = value; }
215                 }
216                 
217                 public ITaskItem[] Excludes {
218                         get { return excludes; }
219                         set { excludes = value; }
220                 }
221                 
222                 public ITaskItem[] MatchedItems {
223                         get { return matchedItems; }
224                 }
225                 
226                 static bool IsRunningOnWindows {
227                         get { return _runningOnWindows; }
228                 }
229         }
230 }
231
232 #endif