MatchNode.cs 11 KB

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