2 // DirectoryScanner.cs: Class used by BuildItem.
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2005 Marek Sieradzki
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections.Generic;
34 using Microsoft.Build.Framework;
35 using Microsoft.Build.Utilities;
37 namespace Microsoft.Build.BuildEngine {
38 internal class DirectoryScanner {
40 DirectoryInfo baseDirectory;
41 ITaskItem[] includes, excludes;
42 ITaskItem[] matchedItems;
44 static bool _runningOnWindows;
46 static DirectoryScanner ()
48 PlatformID pid = Environment.OSVersion.Platform;
49 _runningOnWindows =((int) pid != 128 && (int) pid != 4 && (int) pid != 6);
52 public DirectoryScanner ()
58 Dictionary <string, bool> excludedItems;
59 List <ITaskItem> includedItems;
62 throw new ArgumentNullException ("Includes");
63 if (baseDirectory == null)
64 throw new ArgumentNullException ("BaseDirectory");
66 excludedItems = new Dictionary <string, bool> ();
67 includedItems = new List <ITaskItem> ();
70 foreach (ITaskItem excl in excludes)
71 ProcessExclude (excl.ItemSpec, excludedItems);
73 foreach (ITaskItem include_item in includes)
74 ProcessInclude (include_item, excludedItems, includedItems);
76 matchedItems = includedItems.ToArray ();
79 private void ProcessInclude (ITaskItem include_item, Dictionary <string, bool> excludedItems,
80 List <ITaskItem> includedItems)
82 string[] separatedPath;
85 string name = include_item.ItemSpec;
86 if (!HasWildcard (name)) {
87 if (!excludedItems.ContainsKey (Path.GetFullPath(name)))
88 includedItems.Add (include_item);
90 if (name.Split (Path.DirectorySeparatorChar).Length > name.Split (Path.AltDirectorySeparatorChar).Length) {
91 separatedPath = name.Split (new char [] {Path.DirectorySeparatorChar},
92 StringSplitOptions.RemoveEmptyEntries);
94 separatedPath = name.Split (new char [] {Path.AltDirectorySeparatorChar},
95 StringSplitOptions.RemoveEmptyEntries);
97 if (separatedPath.Length == 1 && separatedPath [0] == String.Empty)
101 if (Path.IsPathRooted (name)) {
102 baseDirectory = new DirectoryInfo (Path.GetPathRoot (name));
103 if (IsRunningOnWindows)
108 string full_path = Path.GetFullPath (Path.Combine (Environment.CurrentDirectory, include_item.ItemSpec));
109 fileInfo = ParseIncludeExclude (separatedPath, offset, baseDirectory);
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);
117 if (!excludedItems.ContainsKey (itemName) && !excludedItems.ContainsKey (Path.GetFullPath (itemName))) {
118 TaskItem item = new TaskItem (include_item);
119 item.ItemSpec = itemName;
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);
127 includedItems.Add (item);
133 private void ProcessExclude (string name, Dictionary <string, bool> excludedItems)
135 string[] separatedPath;
138 if (name.IndexOf ('?') == -1 && name.IndexOf ('*') == -1) {
139 if (!excludedItems.ContainsKey (Path.GetFullPath (name)))
140 excludedItems.Add (Path.GetFullPath (name), true);
142 if (name.Split (Path.DirectorySeparatorChar).Length > name.Split (Path.AltDirectorySeparatorChar).Length) {
143 separatedPath = name.Split (new char [] {Path.DirectorySeparatorChar},
144 StringSplitOptions.RemoveEmptyEntries);
146 separatedPath = name.Split (new char [] {Path.AltDirectorySeparatorChar},
147 StringSplitOptions.RemoveEmptyEntries);
149 if (separatedPath.Length == 1 && separatedPath [0] == String.Empty)
153 if (Path.IsPathRooted (name)) {
154 baseDirectory = new DirectoryInfo (Path.GetPathRoot (name));
155 if (IsRunningOnWindows)
160 fileInfo = ParseIncludeExclude (separatedPath, offset, baseDirectory);
161 foreach (FileInfo fi in fileInfo)
162 if (!excludedItems.ContainsKey (fi.FullName))
163 excludedItems.Add (fi.FullName, true);
167 private FileInfo[] ParseIncludeExclude (string[] input, int ptr, DirectoryInfo directory)
169 return ParseIncludeExclude (input, ptr, directory, false);
172 private FileInfo[] ParseIncludeExclude (string[] input, int ptr, DirectoryInfo directory, bool recursive)
175 List <FileInfo> fileInfos = new List<FileInfo> ();
177 if (input.Length > 1 && ptr == 0 && input [0] == String.Empty)
180 string cur = input.Length > ptr ? input[ptr] : input[input.Length-1];
181 bool dot = cur == ".";
182 recursive = recursive || cur == "**";
183 bool parent = cur == "..";
185 if (input.Length <= ptr + 1) {
187 directory = directory.Parent;
188 if ((input.Length == ptr + 1 && !recursive) || input.Length <= ptr)
189 return directory.GetFiles (cur);
193 di = new DirectoryInfo [1];
196 di = new DirectoryInfo [1];
197 di [0] = directory.Parent;
198 } else if (recursive)
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>();
205 while (currentDirectories.Count > 0)
207 DirectoryInfo current = currentDirectories.Pop();
208 allDirectories.Insert (0, current);
209 foreach (DirectoryInfo dir in current.GetDirectories())
211 currentDirectories.Push(dir);
215 // No further directories shall be read
216 di = allDirectories.ToArray();
218 di = directory.GetDirectories (cur);
220 foreach (DirectoryInfo info in di)
221 fileInfos.AddRange (ParseIncludeExclude (input, ptr + 1, info, recursive));
223 return fileInfos.ToArray ();
226 public static bool HasWildcard (string expression)
228 return expression.IndexOf ('?') >= 0 || expression.IndexOf ('*') >= 0;
231 public DirectoryInfo BaseDirectory {
232 get { return baseDirectory; }
233 set { baseDirectory = value; }
236 public ITaskItem[] Includes {
237 get { return includes; }
238 set { includes = value; }
241 public ITaskItem[] Excludes {
242 get { return excludes; }
243 set { excludes = value; }
246 public ITaskItem[] MatchedItems {
247 get { return matchedItems; }
250 static bool IsRunningOnWindows {
251 get { return _runningOnWindows; }