opensim – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 eva 1 #region BSD License
2 /*
3 Copyright (c) 2004-2005 Matthew Holmes (matthew@wildfiregames.com), Dan Moorehead (dan05a@gmail.com)
4  
5 Redistribution and use in source and binary forms, with or without modification, are permitted
6 provided that the following conditions are met:
7  
8 * Redistributions of source code must retain the above copyright notice, this list of conditions
9 and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
11 and the following disclaimer in the documentation and/or other materials provided with the
12 distribution.
13 * The name of the author may not be used to endorse or promote products derived from this software
14 without specific prior written permission.
15  
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
17 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
22 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24 #endregion
25  
26 using System;
27 using System.Collections.Generic;
28 using System.IO;
29 using System.Text.RegularExpressions;
30 using System.Xml;
31  
32 using Prebuild.Core.Attributes;
33 using Prebuild.Core.Interfaces;
34 using Prebuild.Core.Utilities;
35  
36 namespace Prebuild.Core.Nodes
37 {
38 /// <summary>
39 ///
40 /// </summary>
41 [DataNode("Match")]
42 public class MatchNode : DataNode
43 {
44 #region Fields
45  
46 private readonly List<string> m_Files = new List<string>();
47 private Regex m_Regex;
48 private BuildAction? m_BuildAction;
49 private SubType? m_SubType;
50 string m_ResourceName = "";
51 private CopyToOutput m_CopyToOutput;
52 private bool m_Link;
53 private string m_LinkPath;
54 private bool m_PreservePath;
55 private string m_Destination = "";
56 private readonly List<ExcludeNode> m_Exclusions = new List<ExcludeNode>();
57  
58 #endregion
59  
60 #region Properties
61  
62 /// <summary>
63 ///
64 /// </summary>
65 public IEnumerable<string> Files
66 {
67 get
68 {
69 return m_Files;
70 }
71 }
72  
73 /// <summary>
74 ///
75 /// </summary>
76 public BuildAction? BuildAction
77 {
78 get
79 {
80 return m_BuildAction;
81 }
82 }
83  
84 public string DestinationPath
85 {
86 get
87 {
88 return m_Destination;
89 }
90 }
91 /// <summary>
92 ///
93 /// </summary>
94 public SubType? SubType
95 {
96 get
97 {
98 return m_SubType;
99 }
100 }
101  
102 public CopyToOutput CopyToOutput
103 {
104 get
105 {
106 return m_CopyToOutput;
107 }
108 }
109  
110 public bool IsLink
111 {
112 get
113 {
114 return m_Link;
115 }
116 }
117  
118 public string LinkPath
119 {
120 get
121 {
122 return m_LinkPath;
123 }
124 }
125 /// <summary>
126 ///
127 /// </summary>
128 public string ResourceName
129 {
130 get
131 {
132 return m_ResourceName;
133 }
134 }
135  
136 public bool PreservePath
137 {
138 get
139 {
140 return m_PreservePath;
141 }
142 }
143  
144 #endregion
145  
146 #region Private Methods
147  
148 /// <summary>
149 /// Recurses the directories.
150 /// </summary>
151 /// <param name="path">The path.</param>
152 /// <param name="pattern">The pattern.</param>
153 /// <param name="recurse">if set to <c>true</c> [recurse].</param>
154 /// <param name="useRegex">if set to <c>true</c> [use regex].</param>
155 private void RecurseDirectories(string path, string pattern, bool recurse, bool useRegex, List<ExcludeNode> exclusions)
156 {
157 Match match;
158 try
159 {
160 string[] files;
161  
162 Boolean excludeFile;
163 if(!useRegex)
164 {
165 try
166 {
167 files = Directory.GetFiles(path, pattern);
168 }
169 catch (IOException)
170 {
171 // swallow weird IOException error when running in a virtual box
172 // guest OS on a network share when the host OS is not Windows.
173 // This seems to happen on network shares
174 // when no files match, and may be related to this report:
175 // http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=254546
176  
177 files = null;
178 }
179  
180 if(files != null)
181 {
182 foreach (string file in files)
183 {
184 excludeFile = false;
185 string fileTemp;
186 if (file.Substring(0,2) == "./" || file.Substring(0,2) == ".\\")
187 {
188 fileTemp = file.Substring(2);
189 }
190 else
191 {
192 fileTemp = file;
193 }
194  
195 // Check all excludions and set flag if there are any hits.
196 foreach ( ExcludeNode exclude in exclusions )
197 {
198 Regex exRegEx = new Regex( exclude.Pattern );
199 match = exRegEx.Match( file );
200 excludeFile |= match.Success;
201 }
202  
203 if ( !excludeFile )
204 {
205 m_Files.Add( fileTemp );
206 }
207  
208 }
209 }
210  
211 // don't call return here, because we may need to recursively search directories below
212 // this one, even if no matches were found in this directory.
213 }
214 else
215 {
216 try
217 {
218 files = Directory.GetFiles(path);
219 }
220 catch (IOException)
221 {
222 // swallow weird IOException error when running in a virtual box
223 // guest OS on a network share.
224 files = null;
225 }
226  
227 if (files != null)
228 {
229 foreach (string file in files)
230 {
231 excludeFile = false;
232  
233 match = m_Regex.Match(file);
234 if (match.Success)
235 {
236 // Check all excludions and set flag if there are any hits.
237 foreach (ExcludeNode exclude in exclusions)
238 {
239 Regex exRegEx = new Regex(exclude.Pattern);
240 match = exRegEx.Match(file);
241 excludeFile |= !match.Success;
242 }
243  
244 if (!excludeFile)
245 {
246 m_Files.Add(file);
247 }
248 }
249 }
250 }
251 }
252  
253 if(recurse)
254 {
255 string[] dirs = Directory.GetDirectories(path);
256 if(dirs != null && dirs.Length > 0)
257 {
258 foreach (string str in dirs)
259 {
260 // hack to skip subversion folders. Not having this can cause
261 // a significant performance hit when running on a network drive.
262 if (str.EndsWith(".svn"))
263 continue;
264  
265 RecurseDirectories(Helper.NormalizePath(str), pattern, recurse, useRegex, exclusions);
266 }
267 }
268 }
269 }
270 catch(DirectoryNotFoundException)
271 {
272 return;
273 }
274 catch(ArgumentException)
275 {
276 return;
277 }
278 }
279  
280 #endregion
281  
282 #region Public Methods
283  
284 /// <summary>
285 ///
286 /// </summary>
287 /// <param name="node"></param>
288 public override void Parse(XmlNode node)
289 {
290 if( node == null )
291 {
292 throw new ArgumentNullException("node");
293 }
294 string path = Helper.AttributeValue(node, "path", ".");
295 string pattern = Helper.AttributeValue(node, "pattern", "*");
296 string destination = Helper.AttributeValue(node, "destination", string.Empty);
297 bool recurse = (bool)Helper.TranslateValue(typeof(bool), Helper.AttributeValue(node, "recurse", "false"));
298 bool useRegex = (bool)Helper.TranslateValue(typeof(bool), Helper.AttributeValue(node, "useRegex", "false"));
299 string buildAction = Helper.AttributeValue(node, "buildAction", String.Empty);
300 if (buildAction != string.Empty)
301 m_BuildAction = (BuildAction)Enum.Parse(typeof(BuildAction), buildAction);
302  
303  
304 //TODO: Figure out where the subtype node is being assigned
305 //string subType = Helper.AttributeValue(node, "subType", string.Empty);
306 //if (subType != String.Empty)
307 // m_SubType = (SubType)Enum.Parse(typeof(SubType), subType);
308 m_ResourceName = Helper.AttributeValue(node, "resourceName", m_ResourceName);
309 m_CopyToOutput = (CopyToOutput) Enum.Parse(typeof(CopyToOutput), Helper.AttributeValue(node, "copyToOutput", m_CopyToOutput.ToString()));
310 m_Link = bool.Parse(Helper.AttributeValue(node, "link", bool.FalseString));
311 if ( m_Link )
312 {
313 m_LinkPath = Helper.AttributeValue( node, "linkPath", string.Empty );
314 }
315 m_PreservePath = bool.Parse( Helper.AttributeValue( node, "preservePath", bool.FalseString ) );
316  
317 if ( buildAction == "Copy")
318 m_Destination = destination;
319  
320 if(path != null && path.Length == 0)
321 path = ".";//use current directory
322  
323 //throw new WarningException("Match must have a 'path' attribute");
324  
325 if(pattern == null)
326 {
327 throw new WarningException("Match must have a 'pattern' attribute");
328 }
329  
330 path = Helper.NormalizePath(path);
331 if(!Directory.Exists(path))
332 {
333 throw new WarningException("Match path does not exist: {0}", path);
334 }
335  
336 try
337 {
338 if(useRegex)
339 {
340 m_Regex = new Regex(pattern);
341 }
342 }
343 catch(ArgumentException ex)
344 {
345 throw new WarningException("Could not compile regex pattern: {0}", ex.Message);
346 }
347  
348  
349 foreach(XmlNode child in node.ChildNodes)
350 {
351 IDataNode dataNode = Kernel.Instance.ParseNode(child, this);
352 if(dataNode is ExcludeNode)
353 {
354 ExcludeNode excludeNode = (ExcludeNode)dataNode;
355 m_Exclusions.Add( excludeNode );
356 }
357 }
358  
359 RecurseDirectories( path, pattern, recurse, useRegex, m_Exclusions );
360  
361 if (m_Files.Count < 1)
362 {
363 // Include the project name when the match node returns no matches to provide extra
364 // debug info.
365 ProjectNode project = Parent.Parent as ProjectNode;
366 string projectName = "";
367  
368 if (project != null)
369 projectName = " in project " + project.AssemblyName;
370  
371 throw new WarningException("Match" + projectName + " returned no files: {0}{1}", Helper.EndPath(path), pattern);
372 }
373 m_Regex = null;
374 }
375  
376 #endregion
377 }
378 }