[xbuild] Fix warnings.
[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 (name.IndexOf ('?') == -1 && name.IndexOf ('*') == -1) {
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                                         if (!excludedItems.ContainsKey (fi.FullName)) {
114                                                 TaskItem item = new TaskItem (include_item);
115                                                 item.ItemSpec = fi.FullName;
116                                                 if (wildcard_offset >= 0) {
117                                                         string rec_dir = Path.GetDirectoryName (fi.FullName.Substring (wildcard_offset));
118                                                         if (rec_dir.Length > 0)
119                                                                 rec_dir += Path.DirectorySeparatorChar;
120                                                         item.SetMetadata ("RecursiveDir", rec_dir);
121                                                 }
122                                                 includedItems.Add (item);
123                                         }
124                                 }
125                         }
126                 }
127                 
128                 private void ProcessExclude (string name, Dictionary <string, bool> excludedItems)
129                 {
130                         string[] separatedPath;
131                         FileInfo[] fileInfo;
132                         
133                         if (name.IndexOf ('?') == -1 && name.IndexOf ('*') == -1) {
134                                 if (!excludedItems.ContainsKey (Path.GetFullPath (name)))
135                                         excludedItems.Add (Path.GetFullPath (name), true);
136                         } else {
137                                 if (name.Split (Path.DirectorySeparatorChar).Length > name.Split (Path.AltDirectorySeparatorChar).Length) {
138                                         separatedPath = name.Split (new char [] {Path.DirectorySeparatorChar},
139                                                                         StringSplitOptions.RemoveEmptyEntries);
140                                 } else {
141                                         separatedPath = name.Split (new char [] {Path.AltDirectorySeparatorChar},
142                                                                         StringSplitOptions.RemoveEmptyEntries);
143                                 }
144                                 if (separatedPath.Length == 1 && separatedPath [0] == String.Empty)
145                                         return;
146
147                                 int offset = 0;
148                                 if (Path.IsPathRooted (name)) {
149                                         baseDirectory = new DirectoryInfo (Path.GetPathRoot (name));
150                                         if (IsRunningOnWindows)
151                                                 // skip the "drive:"
152                                                 offset = 1;
153                                 }
154                                 fileInfo = ParseIncludeExclude (separatedPath, offset, baseDirectory);
155                                 foreach (FileInfo fi in fileInfo)
156                                         if (!excludedItems.ContainsKey (fi.FullName))
157                                                 excludedItems.Add (fi.FullName, true);
158                         }
159                 }
160                 
161                 private FileInfo[] ParseIncludeExclude (string[] input, int ptr, DirectoryInfo directory)
162                 {
163                         if (input.Length > 1 && ptr == 0 && input [0] == String.Empty)
164                                 ptr++;
165                         if (input.Length == ptr + 1) {
166                                 FileInfo[] fi;
167                                 fi = directory.GetFiles (input [ptr]);
168                                 return fi;
169                         } else {
170                                 DirectoryInfo[] di;
171                                 FileInfo[] fi;
172                                 List <FileInfo> fileInfos = new List <FileInfo> ();
173                                 if (input [ptr] == ".") {
174                                         di = new DirectoryInfo [1];
175                                         di [0] = directory;
176                                 } else if (input [ptr] == "..") {
177                                         di = new DirectoryInfo [1];
178                                         di [0] = directory.Parent;
179                                 } else if (input[ptr] == "**")
180                                 {
181                                         // Read this directory and all subdirectories recursive
182                                         Stack<DirectoryInfo> currentDirectories = new Stack<DirectoryInfo>();                                   
183                                         currentDirectories.Push(directory);
184                                         List<DirectoryInfo> allDirectories = new List<DirectoryInfo>();
185                                         
186                                         while (currentDirectories.Count > 0)
187                                         {
188                                                 DirectoryInfo current = currentDirectories.Pop();
189                                                 allDirectories.Insert (0, current);
190                                                 foreach (DirectoryInfo dir in current.GetDirectories())
191                                                 {
192                                                         currentDirectories.Push(dir);
193                                                 }                                               
194                                         }
195                                         
196                                         // No further directories shall be read
197                                         di = allDirectories.ToArray();                                  
198                                 } else {
199                                         di = directory.GetDirectories (input [ptr]);
200                                 }
201                                 foreach (DirectoryInfo info in di) {
202                                         fi = ParseIncludeExclude (input, ptr + 1, info);
203                                         foreach (FileInfo file in fi)
204                                                 fileInfos.Add (file);
205                                 }
206                                 fi = new FileInfo [fileInfos.Count];
207                                 int i = 0;
208                                 foreach (FileInfo file in fileInfos)
209                                         fi [i++] = file;
210                                 return fi;
211                         }
212                 }
213                 
214                 public DirectoryInfo BaseDirectory {
215                         get { return baseDirectory; }
216                         set { baseDirectory = value; }
217                 }
218                 
219                 public ITaskItem[] Includes {
220                         get { return includes; }
221                         set { includes = value; }
222                 }
223                 
224                 public ITaskItem[] Excludes {
225                         get { return excludes; }
226                         set { excludes = value; }
227                 }
228                 
229                 public ITaskItem[] MatchedItems {
230                         get { return matchedItems; }
231                 }
232                 
233                 static bool IsRunningOnWindows {
234                         get { return _runningOnWindows; }
235                 }
236         }
237 }
238
239 #endif