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 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);
122 includedItems.Add (item);
128 private void ProcessExclude (string name, Dictionary <string, bool> excludedItems)
130 string[] separatedPath;
133 if (name.IndexOf ('?') == -1 && name.IndexOf ('*') == -1) {
134 if (!excludedItems.ContainsKey (Path.GetFullPath (name)))
135 excludedItems.Add (Path.GetFullPath (name), true);
137 if (name.Split (Path.DirectorySeparatorChar).Length > name.Split (Path.AltDirectorySeparatorChar).Length) {
138 separatedPath = name.Split (new char [] {Path.DirectorySeparatorChar},
139 StringSplitOptions.RemoveEmptyEntries);
141 separatedPath = name.Split (new char [] {Path.AltDirectorySeparatorChar},
142 StringSplitOptions.RemoveEmptyEntries);
144 if (separatedPath.Length == 1 && separatedPath [0] == String.Empty)
148 if (Path.IsPathRooted (name)) {
149 baseDirectory = new DirectoryInfo (Path.GetPathRoot (name));
150 if (IsRunningOnWindows)
154 fileInfo = ParseIncludeExclude (separatedPath, offset, baseDirectory);
155 foreach (FileInfo fi in fileInfo)
156 if (!excludedItems.ContainsKey (fi.FullName))
157 excludedItems.Add (fi.FullName, true);
161 private FileInfo[] ParseIncludeExclude (string[] input, int ptr, DirectoryInfo directory)
163 if (input.Length > 1 && ptr == 0 && input [0] == String.Empty)
165 if (input.Length == ptr + 1) {
167 fi = directory.GetFiles (input [ptr]);
172 List <FileInfo> fileInfos = new List <FileInfo> ();
173 if (input [ptr] == ".") {
174 di = new DirectoryInfo [1];
176 } else if (input [ptr] == "..") {
177 di = new DirectoryInfo [1];
178 di [0] = directory.Parent;
179 } else if (input[ptr] == "**")
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>();
186 while (currentDirectories.Count > 0)
188 DirectoryInfo current = currentDirectories.Pop();
189 allDirectories.Insert (0, current);
190 foreach (DirectoryInfo dir in current.GetDirectories())
192 currentDirectories.Push(dir);
196 // No further directories shall be read
197 di = allDirectories.ToArray();
199 di = directory.GetDirectories (input [ptr]);
201 foreach (DirectoryInfo info in di) {
202 fi = ParseIncludeExclude (input, ptr + 1, info);
203 foreach (FileInfo file in fi)
204 fileInfos.Add (file);
206 fi = new FileInfo [fileInfos.Count];
208 foreach (FileInfo file in fileInfos)
214 public static bool HasWildcard (string expression)
216 return expression.IndexOf ('?') >= 0 || expression.IndexOf ('*') >= 0;
219 public DirectoryInfo BaseDirectory {
220 get { return baseDirectory; }
221 set { baseDirectory = value; }
224 public ITaskItem[] Includes {
225 get { return includes; }
226 set { includes = value; }
229 public ITaskItem[] Excludes {
230 get { return excludes; }
231 set { excludes = value; }
234 public ITaskItem[] MatchedItems {
235 get { return matchedItems; }
238 static bool IsRunningOnWindows {
239 get { return _runningOnWindows; }