PageRenderTime 53ms CodeModel.GetById 14ms app.highlight 29ms RepoModel.GetById 2ms app.codeStats 0ms

/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
 19using System;
 20using System.IO;
 21
 22using ICSharpCode.Core;
 23using ICSharpCode.NRefactory.TypeSystem;
 24using ICSharpCode.SharpDevelop;
 25using ICSharpCode.SharpDevelop.Project;
 26using ICSharpCode.SharpDevelop.Workbench;
 27using SD = ICSharpCode.SharpDevelop.Project;
 28
 29namespace ICSharpCode.PackageManagement.EnvDTE
 30{
 31	public class ProjectItem : global::EnvDTE.ProjectItemBase, global::EnvDTE.ProjectItem
 32	{
 33		SD.FileProjectItem projectItem;
 34		Project containingProject;
 35		
 36		public const string CopyToOutputDirectoryPropertyName = "CopyToOutputDirectory";
 37		public const string CustomToolPropertyName = "CustomTool";
 38		public const string FullPathPropertyName = "FullPath";
 39		public const string LocalPathPropertyName = "LocalPath";
 40		
 41		public ProjectItem(Project project, FileProjectItem projectItem)
 42		{
 43			this.projectItem = projectItem;
 44			this.containingProject = project;
 45			this.ProjectItems = CreateProjectItems(projectItem);
 46			CreateProperties();
 47			Kind = GetKindFromFileProjectItemType();
 48		}
 49		
 50		global::EnvDTE.ProjectItems CreateProjectItems(FileProjectItem projectItem)
 51		{
 52			if (projectItem.ItemType == ItemType.Folder) {
 53				return new DirectoryProjectItems(this);
 54			}
 55			return new FileProjectItems(this);
 56		}
 57		
 58		internal static ProjectItem FindByEntity(IProject project, IEntity entity)
 59		{
 60			if (entity.Region.FileName != null) {
 61				return FindByFileName(project, entity.Region.FileName);
 62			}
 63			return null;
 64		}
 65		
 66		internal static ProjectItem FindByFileName(IProject project, string fileName)
 67		{
 68			SD.FileProjectItem item = project.FindFile(new FileName(fileName));
 69			if (item != null) {
 70				return new ProjectItem(new Project(project as MSBuildBasedProject), item);
 71			}
 72			return null;
 73		}
 74		
 75		string GetKindFromFileProjectItemType()
 76		{
 77			if (IsDirectory) {
 78				return global::EnvDTE.Constants.vsProjectItemKindPhysicalFolder;
 79			}
 80			return global::EnvDTE.Constants.vsProjectItemKindPhysicalFile;
 81		}
 82		
 83		bool IsDirectory {
 84			get { return projectItem.ItemType == ItemType.Folder; }
 85		}
 86		
 87		public ProjectItem()
 88		{
 89		}
 90		
 91		void CreateProperties()
 92		{
 93			var propertyFactory = new ProjectItemPropertyFactory(this);
 94			Properties = new Properties(propertyFactory);
 95		}
 96		
 97		public virtual string Name {
 98			get { return Path.GetFileName(projectItem.Include); }
 99		}
100		
101		public virtual string Kind { get; set; }
102		
103		public global::EnvDTE.Project SubProject {
104			get { return null; }
105		}
106		
107		public virtual global::EnvDTE.Properties Properties { get; private set; }
108		public virtual global::EnvDTE.Project ContainingProject {
109			get { return this.containingProject; }
110		}
111		public virtual global::EnvDTE.ProjectItems ProjectItems { get; private set; }
112		
113		internal virtual object GetProperty(string name)
114		{
115			if (name == CopyToOutputDirectoryPropertyName) {
116				return GetCopyToOutputDirectory();
117			} else if (name == CustomToolPropertyName) {
118				return projectItem.CustomTool;
119			} else if ((name == FullPathPropertyName) || (name == LocalPathPropertyName)) {
120				return projectItem.FileName.ToString();
121			}
122			return String.Empty;
123		}
124		
125		UInt32 GetCopyToOutputDirectory()
126		{
127			return (UInt32)projectItem.CopyToOutputDirectory;
128		}
129		
130		internal virtual void SetProperty(string name, object value)
131		{
132			if (name == CopyToOutputDirectoryPropertyName) {
133				SetCopyToOutputDirectory(value);
134			} else if (name == CustomToolPropertyName) {
135				projectItem.CustomTool = value as string;
136			}
137		}
138		
139		void SetCopyToOutputDirectory(object value)
140		{
141			CopyToOutputDirectory copyToOutputDirectory = ConvertToCopyToOutputDirectory(value);
142			projectItem.CopyToOutputDirectory = copyToOutputDirectory;
143		}
144		
145		CopyToOutputDirectory ConvertToCopyToOutputDirectory(object value)
146		{
147			string valueAsString = value.ToString();
148			return (CopyToOutputDirectory)Enum.Parse(typeof(CopyToOutputDirectory), valueAsString);
149		}
150		
151		internal virtual bool IsMatchByName(string name)
152		{
153			return String.Equals(this.Name, name, StringComparison.InvariantCultureIgnoreCase);
154		}
155		
156		internal virtual bool IsChildItem(SD.ProjectItem msbuildProjectItem)
157		{
158			string directory = Path.GetDirectoryName(msbuildProjectItem.Include);
159			return IsMatchByName(directory);
160		}
161		
162		internal virtual ProjectItemRelationship GetRelationship(SD.ProjectItem msbuildProjectItem)
163		{
164			return new ProjectItemRelationship(this, msbuildProjectItem);
165		}
166		
167		public void Delete()
168		{
169			containingProject.DeleteFile(projectItem.FileName);
170			containingProject.Save();
171		}
172		
173		public global::EnvDTE.FileCodeModel2 FileCodeModel {
174			get {
175				if (!IsDirectory) {
176					return new FileCodeModel2(CreateModelContext(), containingProject);
177				}
178				return null;
179			}
180		}
181		
182		CodeModelContext CreateModelContext()
183		{
184			return new CodeModelContext {
185				CurrentProject = containingProject.MSBuildProject,
186				FilteredFileName = projectItem.FileName
187			};
188		}
189		
190		internal string GetIncludePath(string fileName)
191		{
192			string relativeDirectory = GetProjectItemRelativePathToProject();
193			return Path.Combine(relativeDirectory, fileName);
194		}
195		
196		string GetProjectItemRelativePathToProject()
197		{
198			return containingProject.GetRelativePath(projectItem.FileName);
199		}
200		
201		internal string GetIncludePath()
202		{
203			return projectItem.Include;
204		}
205		
206		public virtual void Remove()
207		{
208			containingProject.RemoveProjectItem(this);
209			containingProject.Save();
210		}
211		
212		internal FileProjectItem MSBuildProjectItem {
213			get { return projectItem; }
214		}
215		
216		protected override string GetFileNames(short index)
217		{
218			return FileName;
219		}
220		
221		string FileName {
222			get { return projectItem.FileName; }
223		}
224		
225		public virtual global::EnvDTE.Document Document {
226			get { return GetOpenDocument(); }
227		}
228		
229		Document GetOpenDocument()
230		{
231			IViewContent view = containingProject.GetOpenFile(FileName);
232			if (view != null) {
233				return new Document(FileName, view);
234			}
235			return null;
236		}
237		
238		public virtual global::EnvDTE.Window Open(string viewKind)
239		{
240			containingProject.OpenFile(FileName);
241			return null;
242		}
243		
244		public virtual short FileCount {
245			get { return 1; }
246		}
247		
248		public global::EnvDTE.ProjectItems Collection {
249			get {
250				string relativePath = GetProjectItemRelativeDirectoryToProject();
251				if (String.IsNullOrEmpty(relativePath)) {
252					return containingProject.ProjectItems;
253				}
254				var directoryProjectItem = new DirectoryProjectItem(containingProject, relativePath);
255				return directoryProjectItem.ProjectItems;
256			}
257		}
258		
259		string GetProjectItemRelativeDirectoryToProject()
260		{
261			return Path.GetDirectoryName(GetProjectItemRelativePathToProject());
262		}
263		
264		public void Save(string fileName = null)
265		{
266			IViewContent view = containingProject.GetOpenFile(FileName);
267			if (view != null) {
268				containingProject.SaveFile(view);
269			}
270		}
271	}
272}