/src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/ProjectItem.cs

https://github.com/ajadex/SharpDevelop · C# · 272 lines · 214 code · 41 blank · 17 comment · 29 complexity · 4a6316741a34a9d896580ace9066681b MD5 · raw file

  1. // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.IO;
  20. using ICSharpCode.Core;
  21. using ICSharpCode.NRefactory.TypeSystem;
  22. using ICSharpCode.SharpDevelop;
  23. using ICSharpCode.SharpDevelop.Project;
  24. using ICSharpCode.SharpDevelop.Workbench;
  25. using SD = ICSharpCode.SharpDevelop.Project;
  26. namespace ICSharpCode.PackageManagement.EnvDTE
  27. {
  28. public class ProjectItem : global::EnvDTE.ProjectItemBase, global::EnvDTE.ProjectItem
  29. {
  30. SD.FileProjectItem projectItem;
  31. Project containingProject;
  32. public const string CopyToOutputDirectoryPropertyName = "CopyToOutputDirectory";
  33. public const string CustomToolPropertyName = "CustomTool";
  34. public const string FullPathPropertyName = "FullPath";
  35. public const string LocalPathPropertyName = "LocalPath";
  36. public ProjectItem(Project project, FileProjectItem projectItem)
  37. {
  38. this.projectItem = projectItem;
  39. this.containingProject = project;
  40. this.ProjectItems = CreateProjectItems(projectItem);
  41. CreateProperties();
  42. Kind = GetKindFromFileProjectItemType();
  43. }
  44. global::EnvDTE.ProjectItems CreateProjectItems(FileProjectItem projectItem)
  45. {
  46. if (projectItem.ItemType == ItemType.Folder) {
  47. return new DirectoryProjectItems(this);
  48. }
  49. return new FileProjectItems(this);
  50. }
  51. internal static ProjectItem FindByEntity(IProject project, IEntity entity)
  52. {
  53. if (entity.Region.FileName != null) {
  54. return FindByFileName(project, entity.Region.FileName);
  55. }
  56. return null;
  57. }
  58. internal static ProjectItem FindByFileName(IProject project, string fileName)
  59. {
  60. SD.FileProjectItem item = project.FindFile(new FileName(fileName));
  61. if (item != null) {
  62. return new ProjectItem(new Project(project as MSBuildBasedProject), item);
  63. }
  64. return null;
  65. }
  66. string GetKindFromFileProjectItemType()
  67. {
  68. if (IsDirectory) {
  69. return global::EnvDTE.Constants.vsProjectItemKindPhysicalFolder;
  70. }
  71. return global::EnvDTE.Constants.vsProjectItemKindPhysicalFile;
  72. }
  73. bool IsDirectory {
  74. get { return projectItem.ItemType == ItemType.Folder; }
  75. }
  76. public ProjectItem()
  77. {
  78. }
  79. void CreateProperties()
  80. {
  81. var propertyFactory = new ProjectItemPropertyFactory(this);
  82. Properties = new Properties(propertyFactory);
  83. }
  84. public virtual string Name {
  85. get { return Path.GetFileName(projectItem.Include); }
  86. }
  87. public virtual string Kind { get; set; }
  88. public global::EnvDTE.Project SubProject {
  89. get { return null; }
  90. }
  91. public virtual global::EnvDTE.Properties Properties { get; private set; }
  92. public virtual global::EnvDTE.Project ContainingProject {
  93. get { return this.containingProject; }
  94. }
  95. public virtual global::EnvDTE.ProjectItems ProjectItems { get; private set; }
  96. internal virtual object GetProperty(string name)
  97. {
  98. if (name == CopyToOutputDirectoryPropertyName) {
  99. return GetCopyToOutputDirectory();
  100. } else if (name == CustomToolPropertyName) {
  101. return projectItem.CustomTool;
  102. } else if ((name == FullPathPropertyName) || (name == LocalPathPropertyName)) {
  103. return projectItem.FileName.ToString();
  104. }
  105. return String.Empty;
  106. }
  107. UInt32 GetCopyToOutputDirectory()
  108. {
  109. return (UInt32)projectItem.CopyToOutputDirectory;
  110. }
  111. internal virtual void SetProperty(string name, object value)
  112. {
  113. if (name == CopyToOutputDirectoryPropertyName) {
  114. SetCopyToOutputDirectory(value);
  115. } else if (name == CustomToolPropertyName) {
  116. projectItem.CustomTool = value as string;
  117. }
  118. }
  119. void SetCopyToOutputDirectory(object value)
  120. {
  121. CopyToOutputDirectory copyToOutputDirectory = ConvertToCopyToOutputDirectory(value);
  122. projectItem.CopyToOutputDirectory = copyToOutputDirectory;
  123. }
  124. CopyToOutputDirectory ConvertToCopyToOutputDirectory(object value)
  125. {
  126. string valueAsString = value.ToString();
  127. return (CopyToOutputDirectory)Enum.Parse(typeof(CopyToOutputDirectory), valueAsString);
  128. }
  129. internal virtual bool IsMatchByName(string name)
  130. {
  131. return String.Equals(this.Name, name, StringComparison.InvariantCultureIgnoreCase);
  132. }
  133. internal virtual bool IsChildItem(SD.ProjectItem msbuildProjectItem)
  134. {
  135. string directory = Path.GetDirectoryName(msbuildProjectItem.Include);
  136. return IsMatchByName(directory);
  137. }
  138. internal virtual ProjectItemRelationship GetRelationship(SD.ProjectItem msbuildProjectItem)
  139. {
  140. return new ProjectItemRelationship(this, msbuildProjectItem);
  141. }
  142. public void Delete()
  143. {
  144. containingProject.DeleteFile(projectItem.FileName);
  145. containingProject.Save();
  146. }
  147. public global::EnvDTE.FileCodeModel2 FileCodeModel {
  148. get {
  149. if (!IsDirectory) {
  150. return new FileCodeModel2(CreateModelContext(), containingProject);
  151. }
  152. return null;
  153. }
  154. }
  155. CodeModelContext CreateModelContext()
  156. {
  157. return new CodeModelContext {
  158. CurrentProject = containingProject.MSBuildProject,
  159. FilteredFileName = projectItem.FileName
  160. };
  161. }
  162. internal string GetIncludePath(string fileName)
  163. {
  164. string relativeDirectory = GetProjectItemRelativePathToProject();
  165. return Path.Combine(relativeDirectory, fileName);
  166. }
  167. string GetProjectItemRelativePathToProject()
  168. {
  169. return containingProject.GetRelativePath(projectItem.FileName);
  170. }
  171. internal string GetIncludePath()
  172. {
  173. return projectItem.Include;
  174. }
  175. public virtual void Remove()
  176. {
  177. containingProject.RemoveProjectItem(this);
  178. containingProject.Save();
  179. }
  180. internal FileProjectItem MSBuildProjectItem {
  181. get { return projectItem; }
  182. }
  183. protected override string GetFileNames(short index)
  184. {
  185. return FileName;
  186. }
  187. string FileName {
  188. get { return projectItem.FileName; }
  189. }
  190. public virtual global::EnvDTE.Document Document {
  191. get { return GetOpenDocument(); }
  192. }
  193. Document GetOpenDocument()
  194. {
  195. IViewContent view = containingProject.GetOpenFile(FileName);
  196. if (view != null) {
  197. return new Document(FileName, view);
  198. }
  199. return null;
  200. }
  201. public virtual global::EnvDTE.Window Open(string viewKind)
  202. {
  203. containingProject.OpenFile(FileName);
  204. return null;
  205. }
  206. public virtual short FileCount {
  207. get { return 1; }
  208. }
  209. public global::EnvDTE.ProjectItems Collection {
  210. get {
  211. string relativePath = GetProjectItemRelativeDirectoryToProject();
  212. if (String.IsNullOrEmpty(relativePath)) {
  213. return containingProject.ProjectItems;
  214. }
  215. var directoryProjectItem = new DirectoryProjectItem(containingProject, relativePath);
  216. return directoryProjectItem.ProjectItems;
  217. }
  218. }
  219. string GetProjectItemRelativeDirectoryToProject()
  220. {
  221. return Path.GetDirectoryName(GetProjectItemRelativePathToProject());
  222. }
  223. public void Save(string fileName = null)
  224. {
  225. IViewContent view = containingProject.GetOpenFile(FileName);
  226. if (view != null) {
  227. containingProject.SaveFile(view);
  228. }
  229. }
  230. }
  231. }