aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Prebuild/src/Core/Nodes/MatchNode.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Prebuild/src/Core/Nodes/MatchNode.cs')
-rw-r--r--Prebuild/src/Core/Nodes/MatchNode.cs330
1 files changed, 0 insertions, 330 deletions
diff --git a/Prebuild/src/Core/Nodes/MatchNode.cs b/Prebuild/src/Core/Nodes/MatchNode.cs
deleted file mode 100644
index 656d7d0..0000000
--- a/Prebuild/src/Core/Nodes/MatchNode.cs
+++ /dev/null
@@ -1,330 +0,0 @@
1#region BSD License
2/*
3Copyright (c) 2004-2005 Matthew Holmes (matthew@wildfiregames.com), Dan Moorehead (dan05a@gmail.com)
4
5Redistribution and use in source and binary forms, with or without modification, are permitted
6provided 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
16THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
17BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
22IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23*/
24#endregion
25
26using System;
27using System.Collections.Generic;
28using System.Collections.Specialized;
29using System.IO;
30using System.Text.RegularExpressions;
31using System.Xml;
32
33using Prebuild.Core.Attributes;
34using Prebuild.Core.Interfaces;
35using Prebuild.Core.Utilities;
36using System.Collections;
37
38namespace Prebuild.Core.Nodes
39{
40 /// <summary>
41 ///
42 /// </summary>
43 [DataNode("Match")]
44 public class MatchNode : DataNode
45 {
46 #region Fields
47
48 private readonly StringCollection m_Files = new StringCollection();
49 private Regex m_Regex;
50 private BuildAction? m_BuildAction;
51 private SubType? m_SubType;
52 string m_ResourceName = "";
53 private CopyToOutput m_CopyToOutput;
54 private bool m_Link;
55 private string m_LinkPath;
56 private bool m_PreservePath;
57 private readonly List<ExcludeNode> m_Exclusions = new List<ExcludeNode>();
58
59 #endregion
60
61 #region Properties
62
63 /// <summary>
64 ///
65 /// </summary>
66 public StringCollection Files
67 {
68 get
69 {
70 return m_Files;
71 }
72 }
73
74 /// <summary>
75 ///
76 /// </summary>
77 public BuildAction? BuildAction
78 {
79 get
80 {
81 return m_BuildAction;
82 }
83 }
84
85 /// <summary>
86 ///
87 /// </summary>
88 public SubType? SubType
89 {
90 get
91 {
92 return m_SubType;
93 }
94 }
95
96 public CopyToOutput CopyToOutput
97 {
98 get
99 {
100 return this.m_CopyToOutput;
101 }
102 }
103
104 public bool IsLink
105 {
106 get
107 {
108 return this.m_Link;
109 }
110 }
111
112 public string LinkPath
113 {
114 get
115 {
116 return this.m_LinkPath;
117 }
118 }
119 /// <summary>
120 ///
121 /// </summary>
122 public string ResourceName
123 {
124 get
125 {
126 return m_ResourceName;
127 }
128 }
129
130 public bool PreservePath
131 {
132 get
133 {
134 return m_PreservePath;
135 }
136 }
137
138 #endregion
139
140 #region Private Methods
141
142 /// <summary>
143 /// Recurses the directories.
144 /// </summary>
145 /// <param name="path">The path.</param>
146 /// <param name="pattern">The pattern.</param>
147 /// <param name="recurse">if set to <c>true</c> [recurse].</param>
148 /// <param name="useRegex">if set to <c>true</c> [use regex].</param>
149 private void RecurseDirectories(string path, string pattern, bool recurse, bool useRegex, List<ExcludeNode> exclusions)
150 {
151 Match match;
152 Boolean excludeFile;
153 try
154 {
155 string[] files;
156
157 if(!useRegex)
158 {
159 files = Directory.GetFiles(path, pattern);
160 if(files != null)
161 {
162 string fileTemp;
163 foreach (string file in files)
164 {
165 excludeFile = false;
166 if (file.Substring(0,2) == "./" || file.Substring(0,2) == ".\\")
167 {
168 fileTemp = file.Substring(2);
169 }
170 else
171 {
172 fileTemp = file;
173 }
174
175 // Check all excludions and set flag if there are any hits.
176 foreach ( ExcludeNode exclude in exclusions )
177 {
178 Regex exRegEx = new Regex( exclude.Pattern );
179 match = exRegEx.Match( file );
180 excludeFile |= match.Success;
181 }
182
183 if ( !excludeFile )
184 {
185 m_Files.Add( fileTemp );
186 }
187
188 }
189 }
190 else
191 {
192 return;
193 }
194 }
195 else
196 {
197 files = Directory.GetFiles(path);
198 foreach(string file in files)
199 {
200 excludeFile = false;
201
202 match = m_Regex.Match(file);
203 if(match.Success)
204 {
205 // Check all excludions and set flag if there are any hits.
206 foreach ( ExcludeNode exclude in exclusions )
207 {
208 Regex exRegEx = new Regex( exclude.Pattern );
209 match = exRegEx.Match( file );
210 excludeFile |= !match.Success;
211 }
212
213 if ( !excludeFile )
214 {
215 m_Files.Add( file );
216 }
217 }
218 }
219 }
220
221 if(recurse)
222 {
223 string[] dirs = Directory.GetDirectories(path);
224 if(dirs != null && dirs.Length > 0)
225 {
226 foreach(string str in dirs)
227 {
228 RecurseDirectories(Helper.NormalizePath(str), pattern, recurse, useRegex, exclusions);
229 }
230 }
231 }
232 }
233 catch(DirectoryNotFoundException)
234 {
235 return;
236 }
237 catch(ArgumentException)
238 {
239 return;
240 }
241 }
242
243 #endregion
244
245 #region Public Methods
246
247 /// <summary>
248 ///
249 /// </summary>
250 /// <param name="node"></param>
251 public override void Parse(XmlNode node)
252 {
253 if( node == null )
254 {
255 throw new ArgumentNullException("node");
256 }
257 string path = Helper.AttributeValue(node, "path", ".");
258 string pattern = Helper.AttributeValue(node, "pattern", "*");
259 bool recurse = (bool)Helper.TranslateValue(typeof(bool), Helper.AttributeValue(node, "recurse", "false"));
260 bool useRegex = (bool)Helper.TranslateValue(typeof(bool), Helper.AttributeValue(node, "useRegex", "false"));
261 string buildAction = Helper.AttributeValue(node, "buildAction", String.Empty);
262 if (buildAction != string.Empty)
263 m_BuildAction = (BuildAction)Enum.Parse(typeof(BuildAction), buildAction);
264
265 //TODO: Figure out where the subtype node is being assigned
266 //string subType = Helper.AttributeValue(node, "subType", string.Empty);
267 //if (subType != String.Empty)
268 // m_SubType = (SubType)Enum.Parse(typeof(SubType), subType);
269 m_ResourceName = Helper.AttributeValue(node, "resourceName", m_ResourceName.ToString());
270 this.m_CopyToOutput = (CopyToOutput) Enum.Parse(typeof(CopyToOutput), Helper.AttributeValue(node, "copyToOutput", this.m_CopyToOutput.ToString()));
271 this.m_Link = bool.Parse(Helper.AttributeValue(node, "link", bool.FalseString));
272 if ( this.m_Link == true )
273 {
274 this.m_LinkPath = Helper.AttributeValue( node, "linkPath", string.Empty );
275 }
276 this.m_PreservePath = bool.Parse( Helper.AttributeValue( node, "preservePath", bool.FalseString ) );
277
278
279 if(path != null && path.Length == 0)
280 {
281 path = ".";//use current directory
282 }
283 //throw new WarningException("Match must have a 'path' attribute");
284
285 if(pattern == null)
286 {
287 throw new WarningException("Match must have a 'pattern' attribute");
288 }
289
290 path = Helper.NormalizePath(path);
291 if(!Directory.Exists(path))
292 {
293 throw new WarningException("Match path does not exist: {0}", path);
294 }
295
296 try
297 {
298 if(useRegex)
299 {
300 m_Regex = new Regex(pattern);
301 }
302 }
303 catch(ArgumentException ex)
304 {
305 throw new WarningException("Could not compile regex pattern: {0}", ex.Message);
306 }
307
308
309 foreach(XmlNode child in node.ChildNodes)
310 {
311 IDataNode dataNode = Kernel.Instance.ParseNode(child, this);
312 if(dataNode is ExcludeNode)
313 {
314 ExcludeNode excludeNode = (ExcludeNode)dataNode;
315 m_Exclusions.Add( excludeNode );
316 }
317 }
318
319 RecurseDirectories( path, pattern, recurse, useRegex, m_Exclusions );
320
321 if(m_Files.Count < 1)
322 {
323 throw new WarningException("Match returned no files: {0}{1}", Helper.EndPath(path), pattern);
324 }
325 m_Regex = null;
326 }
327
328 #endregion
329 }
330}