Merge remote-tracking branch 'joncham/sgen-msvc2'
[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 using System;
29 using System.Collections.Generic;
30 using System.IO;
31
32 using Microsoft.Build.Framework;
33 using Microsoft.Build.Utilities;
34
35 namespace Microsoft.Build.BuildEngine {
36         internal class DirectoryScanner {
37                 
38                 DirectoryInfo   baseDirectory;
39                 ITaskItem[]     includes, excludes;
40                 ITaskItem[]     matchedItems;
41
42                 static bool _runningOnWindows;
43                 
44                 static DirectoryScanner ()
45                 {
46                         PlatformID pid = Environment.OSVersion.Platform;
47                         _runningOnWindows =((int) pid != 128 && (int) pid != 4 && (int) pid != 6);
48                 }
49
50                 public DirectoryScanner ()
51                 {
52                 }
53                 
54                 public void Scan ()
55                 {
56                         Dictionary <string, bool> excludedItems;
57                         List <ITaskItem> includedItems;
58                         
59                         if (includes == null)
60                                 throw new ArgumentNullException ("Includes");
61                         if (baseDirectory == null)
62                                 throw new ArgumentNullException ("BaseDirectory");
63                         
64                         excludedItems = new Dictionary <string, bool> ();
65                         includedItems = new List <ITaskItem> ();
66                         
67                         if (excludes != null)
68                                 foreach (ITaskItem excl in excludes)
69                                         ProcessExclude (excl.ItemSpec, excludedItems);
70
71                         foreach (ITaskItem include_item in includes)
72                                 ProcessInclude (include_item, excludedItems, includedItems);
73
74                         matchedItems = includedItems.ToArray ();
75                 }
76                 
77                 private void ProcessInclude (ITaskItem include_item, Dictionary <string, bool> excludedItems,
78                                 List <ITaskItem> includedItems)
79                 {
80                         string[] separatedPath;
81                         FileInfo[] fileInfo;
82
83                         string name = include_item.ItemSpec;
84                         if (!HasWildcard (name)) {
85                                 if (!excludedItems.ContainsKey (Path.GetFullPath(name)))
86                                         includedItems.Add (include_item);
87                         } else {
88                                 if (name.Split (Path.DirectorySeparatorChar).Length > name.Split (Path.AltDirectorySeparatorChar).Length) {
89                                         separatedPath = name.Split (new char [] {Path.DirectorySeparatorChar},
90                                                         StringSplitOptions.RemoveEmptyEntries);
91                                 } else {
92                                         separatedPath = name.Split (new char [] {Path.AltDirectorySeparatorChar},
93                                                         StringSplitOptions.RemoveEmptyEntries);
94                                 }
95                                 if (separatedPath.Length == 1 && separatedPath [0] == String.Empty)
96                                         return;
97
98                                 int offset = 0;
99                                 if (Path.IsPathRooted (name)) {
100                                         baseDirectory = new DirectoryInfo (Path.GetPathRoot (name));
101                                         if (IsRunningOnWindows)
102                                                 // skip the "drive:"
103                                                 offset = 1;
104                                 }
105
106                                 string full_path = Path.GetFullPath (Path.Combine (Environment.CurrentDirectory, include_item.ItemSpec));
107                                 fileInfo = ParseIncludeExclude (separatedPath, offset, baseDirectory);
108
109                                 int wildcard_offset = full_path.IndexOf ("**");
110                                 foreach (FileInfo fi in fileInfo) {
111                                         string itemName = fi.FullName;
112                                         if (!Path.IsPathRooted (name) && itemName.Length > baseDirectory.FullName.Length && itemName.StartsWith (baseDirectory.FullName))
113                                                 itemName = itemName.Substring (baseDirectory.FullName.Length + 1);
114
115                                         if (!excludedItems.ContainsKey (itemName) &&  !excludedItems.ContainsKey (Path.GetFullPath (itemName))) {
116                                                 TaskItem item = new TaskItem (include_item);
117                                                 item.ItemSpec = itemName;
118
119                                                 if (wildcard_offset >= 0) {
120                                                         string rec_dir = Path.GetDirectoryName (fi.FullName.Substring (wildcard_offset));
121                                                         if (rec_dir.Length > 0)
122                                                                 rec_dir += Path.DirectorySeparatorChar;
123                                                         item.SetMetadata ("RecursiveDir", rec_dir);
124                                                 }
125                                                 includedItems.Add (item);
126                                         }
127                                 }
128                         }
129                 }
130                 
131                 private void ProcessExclude (string name, Dictionary <string, bool> excludedItems)
132                 {
133                         string[] separatedPath;
134                         FileInfo[] fileInfo;
135                         
136                         if (name.IndexOf ('?') == -1 && name.IndexOf ('*') == -1) {
137                                 if (!excludedItems.ContainsKey (Path.GetFullPath (name)))
138                                         excludedItems.Add (Path.GetFullPath (name), true);
139                         } else {
140                                 if (name.Split (Path.DirectorySeparatorChar).Length > name.Split (Path.AltDirectorySeparatorChar).Length) {
141                                         separatedPath = name.Split (new char [] {Path.DirectorySeparatorChar},
142                                                                         StringSplitOptions.RemoveEmptyEntries);
143                                 } else {
144                                         separatedPath = name.Split (new char [] {Path.AltDirectorySeparatorChar},
145                                                                         StringSplitOptions.RemoveEmptyEntries);
146                                 }
147                                 if (separatedPath.Length == 1 && separatedPath [0] == String.Empty)
148                                         return;
149
150                                 int offset = 0;
151                                 if (Path.IsPathRooted (name)) {
152                                         baseDirectory = new DirectoryInfo (Path.GetPathRoot (name));
153                                         if (IsRunningOnWindows)
154                                                 // skip the "drive:"
155                                                 offset = 1;
156                                 }
157
158                                 fileInfo = ParseIncludeExclude (separatedPath, offset, baseDirectory);
159                                 foreach (FileInfo fi in fileInfo)
160                                         if (!excludedItems.ContainsKey (fi.FullName))
161                                                 excludedItems.Add (fi.FullName, true);
162                         }
163                 }
164                 
165                 private FileInfo[] ParseIncludeExclude (string[] input, int ptr, DirectoryInfo directory)
166                 {
167                         return ParseIncludeExclude (input, ptr, directory, false);
168                 }
169
170                 private FileInfo[] ParseIncludeExclude (string[] input, int ptr, DirectoryInfo directory, bool recursive)
171                 {
172                         DirectoryInfo[] di;
173                         List <FileInfo> fileInfos = new List<FileInfo> ();
174
175                         if (input.Length > 1 && ptr == 0 && input [0] == String.Empty)
176                                 ptr++;
177
178                         string cur = input.Length > ptr ? input[ptr] : input[input.Length-1];
179                         bool dot = cur == ".";
180                         recursive = recursive || cur == "**";
181                         bool parent = cur == "..";
182
183                         if (input.Length <= ptr + 1) {
184                                 if (parent)
185                                         directory = directory.Parent;
186                                 if ((input.Length == ptr + 1 && !recursive) || input.Length <= ptr)
187                                         return directory.GetFiles (cur);
188                         }
189
190                         if (dot) {
191                                 di = new DirectoryInfo [1];
192                                 di [0] = directory;
193                         } else if (parent) {
194                                 di = new DirectoryInfo [1];
195                                 di [0] = directory.Parent;
196                         } else if (recursive)
197                         {
198                                 // Read this directory and all subdirectories recursive
199                                 Stack<DirectoryInfo> currentDirectories = new Stack<DirectoryInfo>();
200                                 currentDirectories.Push(directory);
201                                 List<DirectoryInfo> allDirectories = new List<DirectoryInfo>();
202
203                                 while (currentDirectories.Count > 0)
204                                 {
205                                         DirectoryInfo current = currentDirectories.Pop();
206                                         allDirectories.Insert (0, current);
207                                         foreach (DirectoryInfo dir in current.GetDirectories())
208                                         {
209                                                 currentDirectories.Push(dir);
210                                         }
211                                 }
212
213                                 // No further directories shall be read
214                                 di = allDirectories.ToArray();
215                         } else
216                                 di = directory.GetDirectories (cur);
217
218                         foreach (DirectoryInfo info in di)
219                                 fileInfos.AddRange (ParseIncludeExclude (input, ptr + 1, info, recursive));
220
221                         return fileInfos.ToArray ();
222                 }
223
224                 public static bool HasWildcard (string expression)
225                 {
226                         return expression.IndexOf ('?') >= 0 || expression.IndexOf ('*') >= 0;
227                 }
228                 
229                 public DirectoryInfo BaseDirectory {
230                         get { return baseDirectory; }
231                         set { baseDirectory = value; }
232                 }
233                 
234                 public ITaskItem[] Includes {
235                         get { return includes; }
236                         set { includes = value; }
237                 }
238                 
239                 public ITaskItem[] Excludes {
240                         get { return excludes; }
241                         set { excludes = value; }
242                 }
243                 
244                 public ITaskItem[] MatchedItems {
245                         get { return matchedItems; }
246                 }
247                 
248                 static bool IsRunningOnWindows {
249                         get { return _runningOnWindows; }
250                 }
251         }
252 }