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