PageRenderTime 12ms CodeModel.GetById 2ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

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