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.cs299
1 files changed, 299 insertions, 0 deletions
diff --git a/Prebuild/src/Core/Nodes/MatchNode.cs b/Prebuild/src/Core/Nodes/MatchNode.cs
new file mode 100644
index 0000000..e0d2fa8
--- /dev/null
+++ b/Prebuild/src/Core/Nodes/MatchNode.cs
@@ -0,0 +1,299 @@
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
26#region CVS Information
27/*
28 * $Source$
29 * $Author: jendave $
30 * $Date: 2006-09-20 09:42:51 +0200 (on, 20 sep 2006) $
31 * $Revision: 164 $
32 */
33#endregion
34
35using System;
36using System.Collections.Specialized;
37using System.IO;
38using System.Text.RegularExpressions;
39using System.Xml;
40
41using Prebuild.Core.Attributes;
42using Prebuild.Core.Interfaces;
43using Prebuild.Core.Utilities;
44
45namespace Prebuild.Core.Nodes
46{
47 /// <summary>
48 ///
49 /// </summary>
50 [DataNode("Match")]
51 public class MatchNode : DataNode
52 {
53 #region Fields
54
55 private StringCollection m_Files;
56 private Regex m_Regex;
57 private BuildAction m_BuildAction = BuildAction.Compile;
58 private SubType m_SubType = SubType.Code;
59 string m_ResourceName = "";
60 private CopyToOutput m_CopyToOutput;
61 private bool m_Link;
62
63
64 #endregion
65
66 #region Constructors
67
68 /// <summary>
69 ///
70 /// </summary>
71 public MatchNode()
72 {
73 m_Files = new StringCollection();
74 }
75
76 #endregion
77
78 #region Properties
79
80 /// <summary>
81 ///
82 /// </summary>
83 public StringCollection Files
84 {
85 get
86 {
87 return m_Files;
88 }
89 }
90
91 /// <summary>
92 ///
93 /// </summary>
94 public BuildAction BuildAction
95 {
96 get
97 {
98 return m_BuildAction;
99 }
100 }
101
102 /// <summary>
103 ///
104 /// </summary>
105 public SubType SubType
106 {
107 get
108 {
109 return m_SubType;
110 }
111 }
112
113 public CopyToOutput CopyToOutput
114 {
115 get
116 {
117 return this.m_CopyToOutput;
118 }
119 }
120
121 public bool IsLink
122 {
123 get
124 {
125 return this.m_Link;
126 }
127 }
128
129 /// <summary>
130 ///
131 /// </summary>
132 public string ResourceName
133 {
134 get
135 {
136 return m_ResourceName;
137 }
138 }
139
140
141 #endregion
142
143 #region Private Methods
144
145 /// <summary>
146 /// Recurses the directories.
147 /// </summary>
148 /// <param name="path">The path.</param>
149 /// <param name="pattern">The pattern.</param>
150 /// <param name="recurse">if set to <c>true</c> [recurse].</param>
151 /// <param name="useRegex">if set to <c>true</c> [use regex].</param>
152 private void RecurseDirectories(string path, string pattern, bool recurse, bool useRegex)
153 {
154 try
155 {
156 string[] files;
157
158 if(!useRegex)
159 {
160 files = Directory.GetFiles(path, pattern);
161 if(files != null)
162 {
163 string fileTemp;
164 foreach (string file in files)
165 {
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 m_Files.Add(fileTemp);
176 }
177 }
178 else
179 {
180 return;
181 }
182 }
183 else
184 {
185 Match match;
186 files = Directory.GetFiles(path);
187 foreach(string file in files)
188 {
189 match = m_Regex.Match(file);
190 if(match.Success)
191 {
192 m_Files.Add(file);
193 }
194 }
195 }
196
197 if(recurse)
198 {
199 string[] dirs = Directory.GetDirectories(path);
200 if(dirs != null && dirs.Length > 0)
201 {
202 foreach(string str in dirs)
203 {
204 RecurseDirectories(Helper.NormalizePath(str), pattern, recurse, useRegex);
205 }
206 }
207 }
208 }
209 catch(DirectoryNotFoundException)
210 {
211 return;
212 }
213 catch(ArgumentException)
214 {
215 return;
216 }
217 }
218
219 #endregion
220
221 #region Public Methods
222
223 /// <summary>
224 ///
225 /// </summary>
226 /// <param name="node"></param>
227 public override void Parse(XmlNode node)
228 {
229 if( node == null )
230 {
231 throw new ArgumentNullException("node");
232 }
233 string path = Helper.AttributeValue(node, "path", ".");
234 string pattern = Helper.AttributeValue(node, "pattern", "*");
235 bool recurse = (bool)Helper.TranslateValue(typeof(bool), Helper.AttributeValue(node, "recurse", "false"));
236 bool useRegex = (bool)Helper.TranslateValue(typeof(bool), Helper.AttributeValue(node, "useRegex", "false"));
237 m_BuildAction = (BuildAction)Enum.Parse(typeof(BuildAction),
238 Helper.AttributeValue(node, "buildAction", m_BuildAction.ToString()));
239 m_SubType = (SubType)Enum.Parse(typeof(SubType),
240 Helper.AttributeValue(node, "subType", m_SubType.ToString()));
241 m_ResourceName = Helper.AttributeValue(node, "resourceName", m_ResourceName.ToString());
242 this.m_CopyToOutput = (CopyToOutput) Enum.Parse(typeof(CopyToOutput), Helper.AttributeValue(node, "copyToOutput", this.m_CopyToOutput.ToString()));
243 this.m_Link = bool.Parse(Helper.AttributeValue(node, "link", bool.FalseString));
244
245
246 if(path != null && path.Length == 0)
247 {
248 path = ".";//use current directory
249 }
250 //throw new WarningException("Match must have a 'path' attribute");
251
252 if(pattern == null)
253 {
254 throw new WarningException("Match must have a 'pattern' attribute");
255 }
256
257 path = Helper.NormalizePath(path);
258 if(!Directory.Exists(path))
259 {
260 throw new WarningException("Match path does not exist: {0}", path);
261 }
262
263 try
264 {
265 if(useRegex)
266 {
267 m_Regex = new Regex(pattern);
268 }
269 }
270 catch(ArgumentException ex)
271 {
272 throw new WarningException("Could not compile regex pattern: {0}", ex.Message);
273 }
274
275 RecurseDirectories(path, pattern, recurse, useRegex);
276
277 foreach(XmlNode child in node.ChildNodes)
278 {
279 IDataNode dataNode = Kernel.Instance.ParseNode(child, this);
280 if(dataNode is ExcludeNode)
281 {
282 ExcludeNode excludeNode = (ExcludeNode)dataNode;
283 if (m_Files.Contains(Helper.NormalizePath(excludeNode.Name)))
284 {
285 m_Files.Remove(Helper.NormalizePath(excludeNode.Name));
286 }
287 }
288 }
289
290 if(m_Files.Count < 1)
291 {
292 throw new WarningException("Match returned no files: {0}{1}", Helper.EndPath(path), pattern);
293 }
294 m_Regex = null;
295 }
296
297 #endregion
298 }
299}