PageRenderTime 38ms CodeModel.GetById 11ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/ajadex/SharpDevelop
C# | 356 lines | 282 code | 57 blank | 17 comment | 10 complexity | 96d18b864c38ee0c0dc819a1e8fdbe15 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.Generic;
 21using System.IO;
 22using System.Linq;
 23
 24using ICSharpCode.Core;
 25using ICSharpCode.NRefactory.TypeSystem;
 26using ICSharpCode.SharpDevelop;
 27using ICSharpCode.SharpDevelop.Dom;
 28using ICSharpCode.SharpDevelop.Gui;
 29using ICSharpCode.SharpDevelop.Project;
 30using ICSharpCode.SharpDevelop.Workbench;
 31using Microsoft.Build.Construction;
 32using SD = ICSharpCode.SharpDevelop.Project;
 33
 34namespace ICSharpCode.PackageManagement.EnvDTE
 35{
 36	public class Project : MarshalByRefObject, global::EnvDTE.Project
 37	{
 38		IPackageManagementProjectService projectService;
 39		IPackageManagementFileService fileService;
 40		DTE dte;
 41		CodeModelContext context;
 42		
 43		public Project(MSBuildBasedProject project)
 44			: this(
 45				project,
 46				new PackageManagementProjectService(),
 47				new PackageManagementFileService())
 48		{
 49		}
 50		
 51		public Project(
 52			MSBuildBasedProject project,
 53			IPackageManagementProjectService projectService,
 54			IPackageManagementFileService fileService)
 55		{
 56			this.MSBuildProject = project;
 57			this.projectService = projectService;
 58			this.fileService = fileService;
 59			
 60			context = new CodeModelContext {
 61				CurrentProject = project,
 62				DteProject = this
 63			};
 64			
 65			CreateProperties();
 66			Object = new ProjectObject(this);
 67			ProjectItems = new ProjectItems(this, this);
 68		}
 69		
 70		public Project()
 71		{
 72		}
 73		
 74		internal IPackageManagementFileService FileService {
 75			get { return fileService; }
 76		}
 77		
 78		void CreateProperties()
 79		{
 80			var propertyFactory = new ProjectPropertyFactory(this);
 81			Properties = new Properties(propertyFactory);
 82		}
 83		
 84		public virtual string Name {
 85			get { return MSBuildProject.Name; }
 86		}
 87	
 88		public virtual string UniqueName {
 89			get { return GetUniqueName(); }
 90		}
 91		
 92		string GetUniqueName()
 93		{
 94			return FileUtility.GetRelativePath(MSBuildProject.ParentSolution.Directory, FileName);
 95		}
 96		
 97		public virtual string FileName {
 98			get { return MSBuildProject.FileName; }
 99		}
100		
101		public virtual string FullName {
102			get { return FileName; }
103		}
104		
105		public virtual object Object { get; private set; }
106		public virtual global::EnvDTE.Properties Properties { get; private set; }
107		public virtual global::EnvDTE.ProjectItems ProjectItems { get; private set; }
108		
109		public virtual global::EnvDTE.DTE DTE {
110			get {
111				if (dte == null) {
112					dte = new DTE(projectService, fileService);
113				}
114				return dte;
115			}
116		}
117		
118		public virtual string Type {
119			get { return GetProjectType(); }
120		}
121		
122		string GetProjectType()
123		{
124			return ProjectType.GetProjectType(MSBuildProject);
125		}
126		
127		public virtual string Kind {
128			get { return GetProjectKind(); }
129		}
130		
131		string GetProjectKind()
132		{
133			return new ProjectKind(this).Kind;
134		}
135		
136		internal MSBuildBasedProject MSBuildProject { get; private set; }
137		
138		public virtual void Save()
139		{
140			projectService.Save(MSBuildProject);
141		}
142		
143		internal virtual void AddReference(string path)
144		{
145			if (!HasReference(path)) {
146				var referenceItem = new ReferenceProjectItem(MSBuildProject, path);
147				AddProjectItemToMSBuildProject(referenceItem);
148			}
149		}
150		
151		bool HasReference(string include)
152		{
153			foreach (ReferenceProjectItem reference in GetReferences()) {
154				if (IsReferenceMatch(reference, include)) {
155					return true;
156				}
157			}
158			return false;
159		}
160		
161		void AddProjectItemToMSBuildProject(SD.ProjectItem projectItem)
162		{
163			projectService.AddProjectItem(MSBuildProject, projectItem);
164		}
165		
166		internal IEnumerable<SD.ProjectItem> GetReferences()
167		{
168			return MSBuildProject.GetItemsOfType(ItemType.Reference);
169		}
170		
171		bool IsReferenceMatch(ReferenceProjectItem reference, string include)
172		{
173			return String.Equals(reference.Include, include, StringComparison.InvariantCultureIgnoreCase);
174		}
175		
176		internal void RemoveReference(ReferenceProjectItem referenceItem)
177		{
178			projectService.RemoveProjectItem(MSBuildProject, referenceItem);
179		}
180		
181		internal ProjectItem AddFileProjectItemUsingFullPath(string path)
182		{
183			string dependentUpon = GetDependentUpon(path);
184			return AddFileProjectItemWithDependentUsingFullPath(path, dependentUpon);
185		}
186		
187		string GetDependentUpon(string path)
188		{
189			var dependentFile = new DependentFile(MSBuildProject);
190			FileProjectItem projectItem = dependentFile.GetParentFileProjectItem(path);
191			if (projectItem != null) {
192				return Path.GetFileName(projectItem.Include);
193			}
194			return null;
195		}
196		
197		internal ProjectItem AddFileProjectItemWithDependentUsingFullPath(string path, string dependentUpon)
198		{
199			FileProjectItem fileProjectItem = CreateFileProjectItemUsingFullPath(path);
200			fileProjectItem.FileName = ICSharpCode.Core.FileName.Create(path);
201			fileProjectItem.DependentUpon = dependentUpon;
202			AddProjectItemToMSBuildProject(fileProjectItem);
203			return new ProjectItem(this, fileProjectItem);
204		}
205		
206		FileProjectItem CreateFileProjectItemUsingPathRelativeToProject(string include)
207		{
208			ItemType itemType = GetDefaultItemType(include);
209			return CreateFileProjectItemUsingPathRelativeToProject(itemType, include);
210		}
211		
212		ItemType GetDefaultItemType(string include)
213		{
214			return MSBuildProject.GetDefaultItemType(Path.GetFileName(include));
215		}
216		
217		FileProjectItem CreateFileProjectItemUsingPathRelativeToProject(ItemType itemType, string include)
218		{
219			var fileItem = new FileProjectItem(MSBuildProject, itemType) {
220				Include = include
221			};
222			if (IsLink(include)) {
223				fileItem.SetEvaluatedMetadata("Link", Path.GetFileName(include));
224			}
225			return fileItem;
226		}
227		
228		bool IsLink(string include)
229		{
230			return include.StartsWith("..");
231		}
232		
233		FileProjectItem CreateFileProjectItemUsingFullPath(string path)
234		{
235			string relativePath = GetRelativePath(path);
236			return CreateFileProjectItemUsingPathRelativeToProject(relativePath);
237		}
238		
239		internal IList<string> GetAllPropertyNames()
240		{
241			var names = new List<string>();
242			lock (MSBuildProject.SyncRoot) {
243				foreach (ProjectPropertyElement propertyElement in MSBuildProject.MSBuildProjectFile.Properties) {
244					names.Add(propertyElement.Name);
245				}
246				names.Add("OutputFileName");
247				names.Add("LocalPath");
248			}
249			return names;
250		}
251		
252		public virtual global::EnvDTE.CodeModel CodeModel {
253			get { return new CodeModel(context, this); }
254		}
255		
256		public virtual global::EnvDTE.ConfigurationManager ConfigurationManager {
257			get { return new ConfigurationManager(this); }
258		}
259		
260		internal virtual string GetLowercaseFileExtension()
261		{
262			return Path.GetExtension(FileName).ToLowerInvariant();
263		}
264		
265		internal virtual void DeleteFile(string fileName)
266		{
267			fileService.RemoveFile(fileName);
268		}
269		
270		internal ProjectItem AddDirectoryProjectItemUsingFullPath(string directory)
271		{
272			AddDirectoryProjectItemsRecursively(directory);
273			return DirectoryProjectItem.CreateDirectoryProjectItemFromFullPath(this, directory);
274		}
275		
276		void AddDirectoryProjectItemsRecursively(string directory)
277		{
278			string[] files = fileService.GetFiles(directory);
279			string[] childDirectories = fileService.GetDirectories(directory);
280			if (files.Any()) {
281				foreach (string file in files) {
282					AddFileProjectItemUsingFullPath(file);
283				}
284			} else if (!childDirectories.Any()) {
285				AddDirectoryProjectItemToMSBuildProject(directory);
286			}
287			
288			foreach (string childDirectory in childDirectories) {
289				AddDirectoryProjectItemsRecursively(childDirectory);
290			}
291		}
292		
293		void AddDirectoryProjectItemToMSBuildProject(string directory)
294		{
295			FileProjectItem projectItem = CreateMSBuildProjectItemForDirectory(directory);
296			AddProjectItemToMSBuildProject(projectItem);
297		}
298		
299		FileProjectItem CreateMSBuildProjectItemForDirectory(string directory)
300		{
301			return new FileProjectItem(MSBuildProject, ItemType.Folder) {
302				FileName = ICSharpCode.Core.FileName.Create(directory)
303			};
304		}
305		
306		internal string GetRelativePath(string path)
307		{
308			return FileUtility.GetRelativePath(MSBuildProject.Directory, path);
309		}
310		
311		internal IProjectBrowserUpdater CreateProjectBrowserUpdater()
312		{
313			return projectService.CreateProjectBrowserUpdater();
314		}
315		
316		internal ICompilation GetCompilationUnit(string fileName)
317		{
318			return fileService.GetCompilationUnit(fileName);
319		}
320		
321		internal ICompilation GetCompilationUnit()
322		{
323			return fileService.GetCompilationUnit(MSBuildProject);
324		}
325		
326		internal void RemoveProjectItem(ProjectItem projectItem)
327		{
328			projectService.RemoveProjectItem(MSBuildProject, projectItem.MSBuildProjectItem);
329		}
330		
331		internal ProjectItem FindProjectItem(string fileName)
332		{
333			return ProjectItem.FindByFileName(MSBuildProject, fileName);
334		}
335		
336		internal IViewContent GetOpenFile(string fileName)
337		{
338			return fileService.GetOpenFile(fileName);
339		}
340		
341		internal void OpenFile(string fileName)
342		{
343			fileService.OpenFile(fileName);
344		}
345		
346		internal bool IsFileFileInsideProjectFolder(string filePath)
347		{
348			return FileUtility.IsBaseDirectory(MSBuildProject.Directory, filePath);
349		}
350		
351		internal void SaveFile(IViewContent view)
352		{
353			fileService.SaveFile(view);
354		}
355	}
356}