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.
29 using System.Collections.Generic;
32 using Microsoft.Build.Framework;
33 using Microsoft.Build.Utilities;
35 namespace Microsoft.Build.BuildEngine {
36 internal class DirectoryScanner {
38 DirectoryInfo baseDirectory;
39 ITaskItem[] includes, excludes;
40 ITaskItem[] matchedItems;
42 static bool _runningOnWindows;
44 static DirectoryScanner ()
46 PlatformID pid = Environment.OSVersion.Platform;
47 _runningOnWindows =((int) pid != 128 && (int) pid != 4 && (int) pid != 6);
50 public DirectoryScanner ()
56 Dictionary <string, bool> excludedItems;
57 List <ITaskItem> includedItems;
60 throw new ArgumentNullException ("Includes");
61 if (baseDirectory == null)
62 throw new ArgumentNullException ("BaseDirectory");
64 excludedItems = new Dictionary <string, bool> ();
65 includedItems = new List <ITaskItem> ();
68 foreach (ITaskItem excl in excludes)
69 ProcessExclude (excl.ItemSpec, excludedItems);
71 foreach (ITaskItem include_item in includes)
72 ProcessInclude (include_item, excludedItems, includedItems);
74 matchedItems = includedItems.ToArray ();
77 private void ProcessInclude (ITaskItem include_item, Dictionary <string, bool> excludedItems,
78 List <ITaskItem> includedItems)
80 string[] separatedPath;
83 string name = include_item.ItemSpec;
84 if (!HasWildcard (name)) {
85 if (!excludedItems.ContainsKey (Path.GetFullPath(name)))
86 includedItems.Add (include_item);
88 if (name.Split (Path.DirectorySeparatorChar).Length > name.Split (Path.AltDirectorySeparatorChar).Length) {
89 separatedPath = name.Split (new char [] {Path.DirectorySeparatorChar},
90 StringSplitOptions.RemoveEmptyEntries);
92 separatedPath = name.Split (new char [] {Path.AltDirectorySeparatorChar},
93 StringSplitOptions.RemoveEmptyEntries);
95 if (separatedPath.Length == 1 && separatedPath [0] == String.Empty)
99 if (Path.IsPathRooted (name)) {
100 baseDirectory = new DirectoryInfo (Path.GetPathRoot (name));
101 if (IsRunningOnWindows)
106 string full_path = Path.GetFullPath (Path.Combine (Environment.CurrentDirectory, include_item.ItemSpec));
107 fileInfo = ParseIncludeExclude (separatedPath, offset, baseDirectory);
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);
115 if (!excludedItems.ContainsKey (itemName) && !excludedItems.ContainsKey (Path.GetFullPath (itemName))) {
116 TaskItem item = new TaskItem (include_item);
117 item.ItemSpec = itemName;
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);
125 includedItems.Add (item);
131 private void ProcessExclude (string name, Dictionary <string, bool> excludedItems)
133 string[] separatedPath;
136 if (name.IndexOf ('?') == -1 && name.IndexOf ('*') == -1) {
137 if (!excludedItems.ContainsKey (Path.GetFullPath (name)))
138 excludedItems.Add (Path.GetFullPath (name), true);
140 if (name.Split (Path.DirectorySeparatorChar).Length > name.Split (Path.AltDirectorySeparatorChar).Length) {
141 separatedPath = name.Split (new char [] {Path.DirectorySeparatorChar},
142 StringSplitOptions.RemoveEmptyEntries);
144 separatedPath = name.Split (new char [] {Path.AltDirectorySeparatorChar},
145 StringSplitOptions.RemoveEmptyEntries);
147 if (separatedPath.Length == 1 && separatedPath [0] == String.Empty)
151 if (Path.IsPathRooted (name)) {
152 baseDirectory = new DirectoryInfo (Path.GetPathRoot (name));
153 if (IsRunningOnWindows)
158 fileInfo = ParseIncludeExclude (separatedPath, offset, baseDirectory);
159 foreach (FileInfo fi in fileInfo)
160 if (!excludedItems.ContainsKey (fi.FullName))
161 excludedItems.Add (fi.FullName, true);
165 private FileInfo[] ParseIncludeExclude (string[] input, int ptr, DirectoryInfo directory)
167 return ParseIncludeExclude (input, ptr, directory, false);
170 private FileInfo[] ParseIncludeExclude (string[] input, int ptr, DirectoryInfo directory, bool recursive)
173 List <FileInfo> fileInfos = new List<FileInfo> ();
175 if (input.Length > 1 && ptr == 0 && input [0] == String.Empty)
178 string cur = input.Length > ptr ? input[ptr] : input[input.Length-1];
179 bool dot = cur == ".";
180 recursive = recursive || cur == "**";
181 bool parent = cur == "..";
183 if (input.Length <= ptr + 1) {
185 directory = directory.Parent;
186 if ((input.Length == ptr + 1 && !recursive) || input.Length <= ptr)
187 return directory.GetFiles (cur);
191 di = new DirectoryInfo [1];
194 di = new DirectoryInfo [1];
195 di [0] = directory.Parent;
196 } else if (recursive)
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>();
203 while (currentDirectories.Count > 0)
205 DirectoryInfo current = currentDirectories.Pop();
206 allDirectories.Insert (0, current);
207 foreach (DirectoryInfo dir in current.GetDirectories())
209 currentDirectories.Push(dir);
213 // No further directories shall be read
214 di = allDirectories.ToArray();
216 di = directory.GetDirectories (cur);
218 foreach (DirectoryInfo info in di)
219 fileInfos.AddRange (ParseIncludeExclude (input, ptr + 1, info, recursive));
221 return fileInfos.ToArray ();
224 public static bool HasWildcard (string expression)
226 return expression.IndexOf ('?') >= 0 || expression.IndexOf ('*') >= 0;
229 public DirectoryInfo BaseDirectory {
230 get { return baseDirectory; }
231 set { baseDirectory = value; }
234 public ITaskItem[] Includes {
235 get { return includes; }
236 set { includes = value; }
239 public ITaskItem[] Excludes {
240 get { return excludes; }
241 set { excludes = value; }
244 public ITaskItem[] MatchedItems {
245 get { return matchedItems; }
248 static bool IsRunningOnWindows {
249 get { return _runningOnWindows; }