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

https://github.com/ajadex/SharpDevelop · C# · 188 lines · 138 code · 26 blank · 24 comment · 7 complexity · 58cfdd1ab5c7b81176d7ca3b2ed5d3eb 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.Collections;
  20. using System.Collections.Generic;
  21. using System.IO;
  22. using System.Linq;
  23. using ICSharpCode.SharpDevelop.Project;
  24. namespace ICSharpCode.PackageManagement.EnvDTE
  25. {
  26. public class ProjectItems : MarshalByRefObject, IEnumerable, global::EnvDTE.ProjectItems
  27. {
  28. IPackageManagementFileService fileService;
  29. object parent;
  30. public ProjectItems(Project project, object parent)
  31. : this(project, parent, project.FileService)
  32. {
  33. }
  34. ProjectItems(Project project, object parent, IPackageManagementFileService fileService)
  35. {
  36. this.Project = project;
  37. this.fileService = fileService;
  38. this.parent = parent;
  39. }
  40. public ProjectItems()
  41. {
  42. }
  43. protected Project Project { get; private set; }
  44. public virtual object Parent {
  45. get { return parent; }
  46. }
  47. public virtual void AddFromFileCopy(string filePath)
  48. {
  49. string fileAdded = filePath;
  50. if (IsFileInsideProjectFolder(filePath)) {
  51. ThrowExceptionIfFileDoesNotExist(filePath);
  52. } else {
  53. fileAdded = CopyFileIntoProject(filePath);
  54. }
  55. using (IProjectBrowserUpdater updater = Project.CreateProjectBrowserUpdater()) {
  56. AddFileProjectItemToProject(fileAdded);
  57. Project.Save();
  58. }
  59. }
  60. /// <summary>
  61. /// The file will be copied inside the folder for the parent containing
  62. /// these project items.
  63. /// </summary>
  64. string GetIncludePathForFileCopy(string filePath)
  65. {
  66. string fileNameWithoutAnyPath = Path.GetFileName(filePath);
  67. if (Parent is Project) {
  68. return fileNameWithoutAnyPath;
  69. }
  70. var item = Parent as ProjectItem;
  71. return item.GetIncludePath(fileNameWithoutAnyPath);
  72. }
  73. bool IsFileInsideProjectFolder(string filePath)
  74. {
  75. return Project.IsFileFileInsideProjectFolder(filePath);
  76. }
  77. void ThrowExceptionIfFileDoesNotExist(string filePath)
  78. {
  79. if (!fileService.FileExists(filePath)) {
  80. throw new FileNotFoundException("Cannot find file", filePath);
  81. }
  82. }
  83. void ThrowExceptionIfFileExists(string filePath)
  84. {
  85. if (fileService.FileExists(filePath)) {
  86. throw new FileExistsException(filePath);
  87. }
  88. }
  89. string CopyFileIntoProject(string fileName)
  90. {
  91. string projectItemInclude = GetIncludePathForFileCopy(fileName);
  92. string newFileName = GetFileNameInProjectFromProjectItemInclude(projectItemInclude);
  93. ThrowExceptionIfFileExists(newFileName);
  94. fileService.CopyFile(fileName, newFileName);
  95. return newFileName;
  96. }
  97. string GetFileNameInProjectFromProjectItemInclude(string projectItemInclude)
  98. {
  99. return Path.Combine(Project.MSBuildProject.Directory, projectItemInclude);
  100. }
  101. public virtual IEnumerator GetEnumerator()
  102. {
  103. return GetProjectItems().GetEnumerator();
  104. }
  105. protected virtual IEnumerable<global::EnvDTE.ProjectItem> GetProjectItems()
  106. {
  107. return new ProjectItemsInsideProject(Project, fileService);
  108. }
  109. internal virtual ProjectItem Item(string name)
  110. {
  111. foreach (ProjectItem item in this) {
  112. if (item.IsMatchByName(name)) {
  113. return item;
  114. }
  115. }
  116. throw new ArgumentException("Unable to find item: " + name, "name");
  117. }
  118. internal virtual ProjectItem Item(int index)
  119. {
  120. return GetProjectItems()
  121. .Skip(index - 1)
  122. .First() as ProjectItem;
  123. }
  124. public virtual global::EnvDTE.ProjectItem Item(object index)
  125. {
  126. if (index is int) {
  127. return Item((int)index);
  128. }
  129. return Item(index as string);
  130. }
  131. public virtual global::EnvDTE.ProjectItem AddFromDirectory(string directory)
  132. {
  133. using (IProjectBrowserUpdater updater = Project.CreateProjectBrowserUpdater()) {
  134. ProjectItem directoryItem = Project.AddDirectoryProjectItemUsingFullPath(directory);
  135. Project.Save();
  136. return directoryItem;
  137. }
  138. }
  139. public virtual global::EnvDTE.ProjectItem AddFromFile(string fileName)
  140. {
  141. using (IProjectBrowserUpdater updater = Project.CreateProjectBrowserUpdater()) {
  142. ProjectItem projectItem = AddFileProjectItemToProject(fileName);
  143. Project.Save();
  144. fileService.ParseFile(fileName);
  145. return projectItem;
  146. }
  147. }
  148. /// <summary>
  149. /// Adds a file to the project with this ProjectItems as its parent.
  150. /// </summary>
  151. protected virtual ProjectItem AddFileProjectItemToProject(string fileName)
  152. {
  153. return Project.AddFileProjectItemUsingFullPath(fileName);
  154. }
  155. public virtual int Count {
  156. get { return GetProjectItems().Count(); }
  157. }
  158. public virtual string Kind {
  159. get { return global::EnvDTE.Constants.vsProjectItemKindPhysicalFolder; }
  160. }
  161. }
  162. }