PageRenderTime 28ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/CKS Developer Edition-VS2012/CKS.Dev.WCT/Mappers/HiveFileMapper.cs

#
C# | 333 lines | 186 code | 51 blank | 96 comment | 22 complexity | cbe783529260dacde14b75e192ccb6df MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using CKS.Dev.WCT.Framework.Extensions;
  6. using CKS.Dev.WCT.Common;
  7. using CKS.Dev.WCT.Resources;
  8. using CKS.Dev.WCT.SolutionModel;
  9. using CKS.Dev.WCT.ModelCreators;
  10. using Microsoft.VisualStudio.SharePoint;
  11. namespace CKS.Dev.WCT.Mappers
  12. {
  13. public class HiveFileMapper
  14. {
  15. #region Properties
  16. private IMappedFolder _tempateFolder = null;
  17. public IMappedFolder TemplateFolder
  18. {
  19. get
  20. {
  21. if (_tempateFolder == null)
  22. {
  23. _tempateFolder = GetORCreateMappedFolder(MappedFolderType.Template);
  24. }
  25. return _tempateFolder;
  26. }
  27. set { _tempateFolder = value; }
  28. }
  29. private IMappedFolder _imagesFolder = null;
  30. public IMappedFolder ImagesFolder
  31. {
  32. get
  33. {
  34. if (_imagesFolder == null)
  35. {
  36. _imagesFolder = GetORCreateMappedFolder(MappedFolderType.Images);
  37. }
  38. return _imagesFolder;
  39. }
  40. set { _imagesFolder = value; }
  41. }
  42. private IMappedFolder _controlTemplatesFolder = null;
  43. public IMappedFolder ControlTemplatesFolder
  44. {
  45. get
  46. {
  47. if (_controlTemplatesFolder == null)
  48. {
  49. _controlTemplatesFolder = GetORCreateMappedFolder(MappedFolderType.ControlTemplates);
  50. }
  51. return _controlTemplatesFolder;
  52. }
  53. set { _controlTemplatesFolder = value; }
  54. }
  55. private IMappedFolder _layoutsFolder = null;
  56. public IMappedFolder LayoutsFolder
  57. {
  58. get
  59. {
  60. if (_layoutsFolder == null)
  61. {
  62. _layoutsFolder = GetORCreateMappedFolder(MappedFolderType.Layouts);
  63. }
  64. return _layoutsFolder;
  65. }
  66. set { _layoutsFolder = value; }
  67. }
  68. private IMappedFolder _rootFolder = null;
  69. public IMappedFolder RootFolder
  70. {
  71. get
  72. {
  73. if (_rootFolder == null)
  74. {
  75. _rootFolder = GetORCreateMappedFolder(MappedFolderType.SharePointRoot);
  76. }
  77. return _rootFolder;
  78. }
  79. set { _rootFolder = value; }
  80. }
  81. public WCTContext WCTContext { get; set; }
  82. #endregion
  83. public HiveFileMapper(WCTContext context)
  84. {
  85. this.WCTContext = context;
  86. }
  87. public void Map()
  88. {
  89. try
  90. {
  91. this.MapTemplateFiles();
  92. }
  93. catch (Exception ex)
  94. {
  95. Logger.LogError(String.Format(StringResources.Strings_Errors_MapTemplateFiles, ex.ToString()));
  96. }
  97. try
  98. {
  99. this.MapRootFiles();
  100. }
  101. catch (Exception ex)
  102. {
  103. Logger.LogError(String.Format(StringResources.Strings_Errors_MapRootFiles, ex.ToString()));
  104. }
  105. }
  106. private void MapTemplateFiles()
  107. {
  108. if (WCTContext.Solution.TemplateFileList.Count > 0)
  109. {
  110. foreach (TemplateFileReference template in WCTContext.Solution.TemplateFileList)
  111. {
  112. string location = template.Location.Substring("Template/".Length);
  113. if(location.StartsWithIgnoreCase("Images"))
  114. {
  115. location = location.Substring("Images/".Length);
  116. CopyFile(template, this.ImagesFolder, location);
  117. continue;
  118. }
  119. if (location.StartsWithIgnoreCase("ControlTemplates"))
  120. {
  121. location = location.Substring("ControlTemplates/".Length);
  122. CopyFile(template, this.ControlTemplatesFolder, location);
  123. continue;
  124. }
  125. if (location.StartsWithIgnoreCase("Layouts"))
  126. {
  127. location = location.Substring("Layouts/".Length);
  128. CopyFile(template, this.LayoutsFolder, location);
  129. continue;
  130. }
  131. if (location.StartsWithIgnoreCase("Features"))
  132. {
  133. continue;
  134. }
  135. CopyFile(template, this.TemplateFolder, location);
  136. }
  137. }
  138. }
  139. private void MapRootFiles()
  140. {
  141. if (this.WCTContext.Solution.RootFileList.Count > 0)
  142. {
  143. foreach (RootFileReference templateFile in WCTContext.Solution.RootFileList)
  144. {
  145. string dest = Path.Combine(this.RootFolder.FullPath, templateFile.Location);
  146. EnsureDirectory(dest);
  147. File.Copy(templateFile.SourceFileInfo.FullName, dest);
  148. ISharePointProjectItemFile spiFile = this.RootFolder.Files.AddFromFile(dest);
  149. //spiFile.DeploymentType = DeploymentType.RootFile;
  150. }
  151. }
  152. }
  153. private void CopyFile(TemplateFileReference templateFile, IMappedFolder folder, string location)
  154. {
  155. string dest = Path.Combine(folder.FullPath, location);
  156. EnsureDirectory(dest);
  157. File.Copy(templateFile.SourceFileInfo.FullName, dest);
  158. ISharePointProjectItemFile spiFile = folder.Files.AddFromFile(dest);
  159. //spiFile.DeploymentType = DeploymentType.TemplateFile;
  160. }
  161. private void EnsureDirectory(string path)
  162. {
  163. string targetFolderPath = Path.GetDirectoryName(path);
  164. if (!Directory.Exists(targetFolderPath))
  165. {
  166. Directory.CreateDirectory(targetFolderPath);
  167. }
  168. }
  169. private IMappedFolder GetORCreateMappedFolder(MappedFolderType folderType)
  170. {
  171. IMappedFolder result = WCTContext.SharePointProject.MappedFolders.OfType<IMappedFolder>().FirstOrDefault(p => p.FolderType == folderType);
  172. if(result == null)
  173. {
  174. result = WCTContext.SharePointProject.MappedFolders.Add(folderType);
  175. }
  176. return result;
  177. }
  178. //private void UpdateSolutionPaths(string oldTemplateFolder, string newTemplateFolder)
  179. //{
  180. // oldTemplateFolder = oldTemplateFolder.Substring(this.WCTContext.TargetRootFolder.Length + 1);
  181. // newTemplateFolder = newTemplateFolder.Substring(this.WCTContext.TargetRootFolder.Length + 1);
  182. // if (!oldTemplateFolder.EndsWith(@"\"))
  183. // {
  184. // oldTemplateFolder += @"\";
  185. // }
  186. // if (!newTemplateFolder.EndsWith(@"\"))
  187. // {
  188. // newTemplateFolder += @"\";
  189. // }
  190. // for (int i = 0; i < this.WCTContext.SolutionToMap.AspxAscxFiles.Count; ++i)
  191. // {
  192. // if (this.WCTContext.SolutionToMap.AspxAscxFiles[i].ToLower().StartsWith(oldTemplateFolder.ToLower()))
  193. // {
  194. // this.WCTContext.SolutionToMap.AspxAscxFiles[i] = newTemplateFolder + this.WCTContext.SolutionToMap.AspxAscxFiles[i].Substring(oldTemplateFolder.Length);
  195. // }
  196. // }
  197. //}
  198. //private string CreateMappedFolder(bool isTemplate)
  199. //{
  200. // MappedFolderCreator folderCreator = new MappedFolderCreator(this.WCTContext.DteProject);
  201. // if (isTemplate)
  202. // {
  203. // folderCreator.FolderType = MappedFolderCreator.MappedFolderCreatorType.Template;
  204. // }
  205. // else
  206. // {
  207. // folderCreator.FolderType = MappedFolderCreator.MappedFolderCreatorType.Root;
  208. // }
  209. // return folderCreator.Create();
  210. //}
  211. //private void MoveAndReferenceFiles(string sourcePath, string targetPath)
  212. //{
  213. // ProjectFileUpdater fileUpdater = new ProjectFileUpdater(this.WCTContext.DteProject);
  214. // IList<string> files = fileUpdater.MoveFolderContentsLocation(
  215. // sourcePath,
  216. // targetPath);
  217. // fileUpdater.AddProjectFiles(files);
  218. // this.LogThemeTemplates(files, targetPath);
  219. //}
  220. //private void LogThemeTemplates(IList<string> filesToCheck, string templatePath)
  221. //{
  222. // string themesFolder = @"THEMES\";
  223. // string layoutsFolder = @"LAYOUTS\";
  224. // if (!templatePath.EndsWith(@"\"))
  225. // {
  226. // templatePath += @"\";
  227. // }
  228. // foreach (string file in filesToCheck)
  229. // {
  230. // if (file.StartsWith(templatePath))
  231. // {
  232. // string fileToCheck = file.Substring(templatePath.Length);
  233. // if (fileToCheck.ToLower().StartsWith(themesFolder.ToLower())
  234. // || fileToCheck.ToLower().StartsWith(layoutsFolder.ToLower()))
  235. // {
  236. // Logger.LogInformation(StringResources.String_LogMessages_ThemeCodeUpdate);
  237. // break;
  238. // }
  239. // }
  240. // }
  241. //}
  242. //private void CorrectFieldControlClassReferences()
  243. //{
  244. // foreach (TemplateFile file in this.WCTContext.SolutionToMap.Templates)
  245. // {
  246. // string filename = Path.GetFileName(file.ProjectRelativePath);
  247. // if (filename.StartsWith("fldTypes", StringComparison.InvariantCultureIgnoreCase))
  248. // {
  249. // string fullPath = Path.Combine(this.WCTContext.TargetRootFolder, file.ProjectRelativePath);
  250. // try
  251. // {
  252. // FieldTypeUpdater updater = new FieldTypeUpdater(fullPath);
  253. // IList<Guid> classes = updater.GetTypeGuids();
  254. // foreach (Guid classGuid in classes)
  255. // {
  256. // if (this.WCTContext.SolutionToMap.GuidMarkedClasses.ContainsKey(classGuid))
  257. // {
  258. // string name = this.GetFullClassName(this.WCTContext.SolutionToMap.GuidMarkedClasses[classGuid].FullClassNameWithoutAssembly);
  259. // string tokenizedName = name + ", " + Constants.TargetAssemblyFullnameToken;
  260. // updater.UpdateTypeGuid(classGuid, tokenizedName);
  261. // }
  262. // }
  263. // updater.SaveChanges();
  264. // }
  265. // catch (Exception ex)
  266. // {
  267. // Logger.LogError(String.Format(StringResources.Strings_Errors_SetFieldControlClassReference, fullPath, ex.ToString()));
  268. // }
  269. // }
  270. // }
  271. //}
  272. private string GetFullClassName(string fullClassName)
  273. {
  274. if (!this.WCTContext.SourceProject.IsCSharp
  275. && !string.IsNullOrEmpty(this.WCTContext.SourceProject.RootNamespace))
  276. {
  277. fullClassName = String.Format("{0}.{1}", this.WCTContext.SourceProject.RootNamespace, fullClassName);
  278. }
  279. return fullClassName;
  280. }
  281. }
  282. }