FilesNode.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.Collections.Specialized;
  25. using System.Xml;
  26. using Prebuild.Core.Attributes;
  27. using Prebuild.Core.Interfaces;
  28. using System.IO;
  29. namespace Prebuild.Core.Nodes
  30. {
  31. /// <summary>
  32. ///
  33. /// </summary>
  34. [DataNode("Files")]
  35. public class FilesNode : DataNode
  36. {
  37. #region Fields
  38. private readonly List<string> m_Files = new List<string>();
  39. private readonly Dictionary<string,BuildAction> m_BuildActions = new Dictionary<string, BuildAction>();
  40. private readonly Dictionary<string, SubType> m_SubTypes = new Dictionary<string, SubType>();
  41. private readonly Dictionary<string, string> m_ResourceNames = new Dictionary<string, string>();
  42. private readonly Dictionary<string, CopyToOutput> m_CopyToOutputs = new Dictionary<string, CopyToOutput>();
  43. private readonly Dictionary<string, bool> m_Links = new Dictionary<string, bool>();
  44. private readonly Dictionary<string, string> m_LinkPaths = new Dictionary<string, string>();
  45. private readonly Dictionary<string, bool> m_PreservePaths = new Dictionary<string, bool>();
  46. private readonly Dictionary<string, string> m_DestinationPath = new Dictionary<string, string>();
  47. private readonly NameValueCollection m_CopyFiles = new NameValueCollection();
  48. #endregion
  49. #region Properties
  50. public int Count
  51. {
  52. get
  53. {
  54. return m_Files.Count;
  55. }
  56. }
  57. public string[] Destinations
  58. {
  59. get { return m_CopyFiles.AllKeys; }
  60. }
  61. public int CopyFiles
  62. {
  63. get { return m_CopyFiles.Count; }
  64. }
  65. #endregion
  66. #region Public Methods
  67. public BuildAction GetBuildAction(string file)
  68. {
  69. if(!m_BuildActions.ContainsKey(file))
  70. {
  71. return BuildAction.Compile;
  72. }
  73. return m_BuildActions[file];
  74. }
  75. public string GetDestinationPath(string file)
  76. {
  77. if( !m_DestinationPath.ContainsKey(file))
  78. {
  79. return null;
  80. }
  81. return m_DestinationPath[file];
  82. }
  83. public string[] SourceFiles(string dest)
  84. {
  85. return m_CopyFiles.GetValues(dest);
  86. }
  87. public CopyToOutput GetCopyToOutput(string file)
  88. {
  89. if (!m_CopyToOutputs.ContainsKey(file))
  90. {
  91. return CopyToOutput.Never;
  92. }
  93. return m_CopyToOutputs[file];
  94. }
  95. public bool GetIsLink(string file)
  96. {
  97. if (!m_Links.ContainsKey(file))
  98. {
  99. return false;
  100. }
  101. return m_Links[file];
  102. }
  103. public bool Contains(string file)
  104. {
  105. return m_Files.Contains(file);
  106. }
  107. public string GetLinkPath( string file )
  108. {
  109. if ( !m_LinkPaths.ContainsKey( file ) )
  110. {
  111. return string.Empty;
  112. }
  113. return m_LinkPaths[ file ];
  114. }
  115. public SubType GetSubType(string file)
  116. {
  117. if(!m_SubTypes.ContainsKey(file))
  118. {
  119. return SubType.Code;
  120. }
  121. return m_SubTypes[file];
  122. }
  123. public string GetResourceName(string file)
  124. {
  125. if(!m_ResourceNames.ContainsKey(file))
  126. {
  127. return string.Empty;
  128. }
  129. return m_ResourceNames[file];
  130. }
  131. public bool GetPreservePath( string file )
  132. {
  133. if ( !m_PreservePaths.ContainsKey( file ) )
  134. {
  135. return false;
  136. }
  137. return m_PreservePaths[ file ];
  138. }
  139. public override void Parse(XmlNode node)
  140. {
  141. if( node == null )
  142. {
  143. throw new ArgumentNullException("node");
  144. }
  145. foreach(XmlNode child in node.ChildNodes)
  146. {
  147. IDataNode dataNode = Kernel.Instance.ParseNode(child, this);
  148. if(dataNode is FileNode)
  149. {
  150. FileNode fileNode = (FileNode)dataNode;
  151. if(fileNode.IsValid)
  152. {
  153. if (!m_Files.Contains(fileNode.Path))
  154. {
  155. m_Files.Add(fileNode.Path);
  156. m_BuildActions[fileNode.Path] = fileNode.BuildAction;
  157. m_SubTypes[fileNode.Path] = fileNode.SubType;
  158. m_ResourceNames[fileNode.Path] = fileNode.ResourceName;
  159. m_PreservePaths[ fileNode.Path ] = fileNode.PreservePath;
  160. m_Links[ fileNode.Path ] = fileNode.IsLink;
  161. m_LinkPaths[ fileNode.Path ] = fileNode.LinkPath;
  162. m_CopyToOutputs[ fileNode.Path ] = fileNode.CopyToOutput;
  163. }
  164. }
  165. }
  166. else if(dataNode is MatchNode)
  167. {
  168. foreach(string file in ((MatchNode)dataNode).Files)
  169. {
  170. MatchNode matchNode = (MatchNode)dataNode;
  171. if (!m_Files.Contains(file))
  172. {
  173. m_Files.Add(file);
  174. if (matchNode.BuildAction == null)
  175. m_BuildActions[file] = GetBuildActionByFileName(file);
  176. else
  177. m_BuildActions[file] = matchNode.BuildAction.Value;
  178. if (matchNode.BuildAction == BuildAction.Copy)
  179. {
  180. m_CopyFiles.Add(matchNode.DestinationPath, file);
  181. m_DestinationPath[file] = matchNode.DestinationPath;
  182. }
  183. m_SubTypes[file] = matchNode.SubType == null ? GetSubTypeByFileName(file) : matchNode.SubType.Value;
  184. m_ResourceNames[ file ] = matchNode.ResourceName;
  185. m_PreservePaths[ file ] = matchNode.PreservePath;
  186. m_Links[ file ] = matchNode.IsLink;
  187. m_LinkPaths[ file ] = matchNode.LinkPath;
  188. m_CopyToOutputs[ file ] = matchNode.CopyToOutput;
  189. }
  190. }
  191. }
  192. }
  193. }
  194. // TODO: Check in to why StringCollection's enumerator doesn't implement
  195. // IEnumerator?
  196. public IEnumerator<string> GetEnumerator()
  197. {
  198. return m_Files.GetEnumerator();
  199. }
  200. #endregion
  201. }
  202. }