PageRenderTime 61ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/CKS Developer Edition-VS2012/CKS.Dev11/DTEManager.cs

#
C# | 404 lines | 254 code | 29 blank | 121 comment | 43 complexity | 9ee4c3a624859d14d4b4f6d19bcc804d MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Linq;
  3. using Microsoft.VisualStudio.Shell;
  4. using EnvDTE;
  5. using System.Runtime.InteropServices;
  6. using Microsoft.VisualStudio;
  7. using Microsoft.VisualStudio.Shell.Interop;
  8. using System.Collections.Generic;
  9. using CKS.Dev11.VisualStudio.SharePoint.Environment;
  10. using Microsoft.VisualStudio.SharePoint;
  11. using IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
  12. namespace CKS.Dev11.VisualStudio.SharePoint
  13. {
  14. /// <summary>
  15. /// The DTE manager to provide functionality for the DTE.
  16. /// </summary>
  17. public static class DTEManager
  18. {
  19. /// <summary>
  20. /// The SharePoint Project type ID
  21. /// </summary>
  22. static readonly Guid SharePointProjectTypeID = new Guid("BB1F664B-9266-4FD6-B973-E1E44974B511");
  23. static IOleServiceProvider _globalOleServiceProvider;
  24. static IServiceProvider _globalServiceProvider;
  25. static ISharePointProjectService _projectService;
  26. /// <summary>
  27. /// Gets or sets the DTE.
  28. /// </summary>
  29. /// <value>The DTE.</value>
  30. public static DTE DTE { get; private set; }
  31. /// <summary>
  32. /// Gets the active solution.
  33. /// </summary>
  34. /// <value>The active solution.</value>
  35. public static Solution ActiveSolution
  36. {
  37. get { return DTE.Solution; }
  38. }
  39. /// <summary>
  40. /// Gets the active project.
  41. /// </summary>
  42. /// <returns></returns>
  43. public static EnvDTE.Project ActiveProject
  44. {
  45. get
  46. {
  47. EnvDTE.Project project = null;
  48. object[] projects = DTE.ActiveSolutionProjects;
  49. if ((projects != null) && (projects.Length > 0))
  50. {
  51. project = projects[0] as EnvDTE.Project;
  52. }
  53. return project;
  54. }
  55. }
  56. /// <summary>
  57. /// Gets the active SharePoint project.
  58. /// </summary>
  59. /// <returns></returns>
  60. public static ISharePointProject ActiveSharePointProject
  61. {
  62. get
  63. {
  64. EnvDTE.Project project = null;
  65. object[] projects = DTE.ActiveSolutionProjects;
  66. if ((projects != null) && (projects.Length > 0))
  67. {
  68. project = projects[0] as EnvDTE.Project;
  69. return ProjectService.Convert<EnvDTE.Project, Microsoft.VisualStudio.SharePoint.ISharePointProject>(project);
  70. }
  71. return null;
  72. }
  73. }
  74. /// <summary>
  75. /// Gets the all SharePoint projects.
  76. /// </summary>
  77. /// <returns></returns>
  78. public static List<ISharePointProject> SharePointProjects
  79. {
  80. get
  81. {
  82. List<ISharePointProject> projects = new List<ISharePointProject>();
  83. foreach (ISharePointProject project in ProjectService.Projects)
  84. {
  85. projects.Add(project);
  86. }
  87. return projects;
  88. }
  89. }
  90. /// <summary>
  91. /// Gets the selected item.
  92. /// </summary>
  93. /// <value>The selected item.</value>
  94. public static SelectedItem SelectedItem
  95. {
  96. get
  97. {
  98. return DTE.SelectedItems.Item(1);
  99. }
  100. }
  101. /// <summary>
  102. /// Gets the project service.
  103. /// </summary>
  104. /// <value>The project service.</value>
  105. public static ISharePointProjectService ProjectService
  106. {
  107. get
  108. {
  109. if (_projectService == null)
  110. {
  111. _projectService = (ISharePointProjectService)_globalServiceProvider.GetService(typeof(ISharePointProjectService));
  112. }
  113. return _projectService;
  114. }
  115. }
  116. /// <summary>
  117. /// Initializes the <see cref="DTEManager"/> class.
  118. /// </summary>
  119. static DTEManager()
  120. {
  121. DTE = (DTE)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(EnvDTE.DTE));
  122. _globalOleServiceProvider = (IOleServiceProvider)DTE;
  123. _globalServiceProvider = new ServiceProvider(_globalOleServiceProvider);
  124. }
  125. /// <summary>
  126. /// Gets the language file extension.
  127. /// </summary>
  128. /// <param name="projectItem">The project item.</param>
  129. /// <returns></returns>
  130. public static string GetLanguageFileExtension(ProjectItem projectItem)
  131. {
  132. switch (projectItem.ContainingProject.CodeModel.Language)
  133. {
  134. case "{B5E9BD34-6D3E-4B5D-925E-8A43B79820B4}":
  135. return ".cs";
  136. case "{B5E9BD33-6D3E-4B5D-925E-8A43B79820B4}":
  137. return ".vb";
  138. }
  139. throw new NotSupportedException();
  140. }
  141. /// <summary>
  142. /// Gets the default root name space.
  143. /// </summary>
  144. /// <param name="project">The project.</param>
  145. /// <returns></returns>
  146. public static string GetDefaultRootNameSpace(Project project)
  147. {
  148. return (string)project.Properties.Item("RootNameSpace").Value;
  149. }
  150. /// <summary>
  151. /// Gets the selected projects.
  152. /// </summary>
  153. /// <returns></returns>
  154. public static IEnumerable<IVsHierarchy> GetSelectedProjects()
  155. {
  156. List<IVsHierarchy> list = new List<IVsHierarchy>();
  157. IntPtr hierarchyPointer = IntPtr.Zero;
  158. uint itemCount = 0;
  159. IntPtr ppSC = IntPtr.Zero;
  160. int singleItemPointer = 0;
  161. IVsMultiItemSelect multiSelect = null;
  162. try
  163. {
  164. IVsMonitorSelection monitorSelection = (IVsMonitorSelection)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SVsShellMonitorSelection));
  165. ErrorHandler.ThrowOnFailure(monitorSelection.GetCurrentSelection(
  166. out hierarchyPointer, out itemCount, out multiSelect, out ppSC));
  167. if (hierarchyPointer != IntPtr.Zero)
  168. {
  169. IVsHierarchy hierarchy = (IVsHierarchy)Marshal.GetObjectForIUnknown(hierarchyPointer);
  170. list.Add(hierarchy);
  171. }
  172. else if (multiSelect != null)
  173. {
  174. ErrorHandler.ThrowOnFailure(multiSelect.GetSelectionInfo(out itemCount, out singleItemPointer));
  175. VSITEMSELECTION[] items = new VSITEMSELECTION[itemCount];
  176. ErrorHandler.ThrowOnFailure(multiSelect.GetSelectedItems(0, itemCount, items));
  177. list.AddRange(items.Select(item => item.pHier));
  178. }
  179. return list;
  180. }
  181. finally
  182. {
  183. if (hierarchyPointer != IntPtr.Zero)
  184. {
  185. Marshal.Release(hierarchyPointer);
  186. }
  187. if (ppSC != IntPtr.Zero)
  188. {
  189. Marshal.Release(ppSC);
  190. }
  191. }
  192. }
  193. /// <summary>
  194. /// Determines whether [is share point project] [the specified project].
  195. /// </summary>
  196. /// <param name="project">The project.</param>
  197. /// <returns>
  198. /// <c>true</c> if [is share point project] [the specified project]; otherwise, <c>false</c>.
  199. /// </returns>
  200. public static bool IsSharePointProject(EnvDTE.Project project)
  201. {
  202. return IsSharePointProject(project, false);
  203. }
  204. /// <summary>
  205. /// Determines whether [is share point project] [the specified project].
  206. /// </summary>
  207. /// <param name="project">The project.</param>
  208. /// <param name="requireFarm">if set to <c>true</c> [require farm].</param>
  209. /// <returns>
  210. /// <c>true</c> if [is share point project] [the specified project]; otherwise, <c>false</c>.
  211. /// </returns>
  212. public static bool IsSharePointProject(EnvDTE.Project project, bool requireFarm)
  213. {
  214. // Convert the DTE project into a SharePoint project. If the conversion fails, this is not a SP project.
  215. ISharePointProject p = ProjectService.Convert<EnvDTE.Project, Microsoft.VisualStudio.SharePoint.ISharePointProject>(project);
  216. if (p != null && requireFarm)
  217. {
  218. return !p.IsSandboxedSolution;
  219. }
  220. else
  221. {
  222. return p != null;
  223. }
  224. }
  225. /// <summary>
  226. /// Determines whether [is share point project] [the specified h].
  227. /// </summary>
  228. /// <param name="h">The h.</param>
  229. /// <returns>
  230. /// <c>true</c> if [is share point project] [the specified h]; otherwise, <c>false</c>.
  231. /// </returns>
  232. public static bool IsSharePointProject(IVsHierarchy h)
  233. {
  234. IVsAggregatableProject project = h as IVsAggregatableProject;
  235. if (project != null)
  236. {
  237. string guidString = null;
  238. project.GetAggregateProjectTypeGuids(out guidString);
  239. IEnumerable<Guid> guids = guidString.Split(';').Select(s => new Guid(s));
  240. return guids.Contains(SharePointProjectTypeID);
  241. }
  242. return false;
  243. }
  244. /// <summary>
  245. /// Creates the new file.
  246. /// </summary>
  247. /// <param name="fileType">Type of the file.</param>
  248. /// <param name="title">The title.</param>
  249. /// <param name="fileContents">The file contents.</param>
  250. public static void CreateNewFile(
  251. string fileType, string title, string fileContents)
  252. {
  253. Document file = DTE.ItemOperations.NewFile(fileType, title).Document;
  254. TextSelection selection = file.Selection;
  255. selection.SelectAll();
  256. selection.Text = "";
  257. selection.Insert(fileContents);
  258. selection.StartOfDocument();
  259. }
  260. /// <summary>
  261. /// Creates the new text file.
  262. /// </summary>
  263. /// <param name="title">The title.</param>
  264. /// <param name="fileContents">The file contents.</param>
  265. public static void CreateNewTextFile(string title, string fileContents)
  266. {
  267. CreateNewFile(@"General\Text File", title, fileContents);
  268. }
  269. /// <summary>
  270. /// Finds the name of the item by.
  271. /// </summary>
  272. /// <param name="collection">The collection.</param>
  273. /// <param name="name">The name.</param>
  274. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  275. /// <returns>The project item.</returns>
  276. public static ProjectItem FindItemByName(ProjectItems collection, string name, bool recursive)
  277. {
  278. if (collection != null)
  279. {
  280. foreach (ProjectItem item in collection)
  281. {
  282. if (item.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase))
  283. {
  284. return item;
  285. }
  286. if (recursive)
  287. {
  288. ProjectItem item2 = FindItemByName(item.ProjectItems, name, recursive);
  289. if (item2 != null)
  290. {
  291. return item2;
  292. }
  293. }
  294. }
  295. }
  296. return null;
  297. }
  298. /// <summary>
  299. /// Gets the name of the project by.
  300. /// </summary>
  301. /// <param name="projectName">Name of the project.</param>
  302. /// <param name="isUniqueName">if set to <c>true</c> [is unique name].</param>
  303. /// <returns></returns>
  304. public static Project GetProjectByName(String projectName, bool isUniqueName = true)
  305. {
  306. Project result = null;
  307. foreach (Project project in DTEManager.DTE.Solution.Projects)
  308. {
  309. if (((isUniqueName) && (project.UniqueName == projectName)) || ((!isUniqueName) && (project.Name == projectName)))
  310. {
  311. result = project;
  312. break;
  313. }
  314. }
  315. return result;
  316. }
  317. /// <summary>
  318. /// Safes the delete project item.
  319. /// </summary>
  320. /// <param name="projectItem">The project item.</param>
  321. public static void SafeDeleteProjectItem(ProjectItem projectItem)
  322. {
  323. if (projectItem != null)
  324. {
  325. projectItem.Delete();
  326. }
  327. }
  328. /// <summary>
  329. /// Finds the name of the SPI item by.
  330. /// </summary>
  331. /// <param name="spiName">Name of the spi.</param>
  332. /// <param name="project">The project.</param>
  333. /// <returns></returns>
  334. public static ISharePointProjectItem FindSPIItemByName(string spiName, ISharePointProject project)
  335. {
  336. return (from ISharePointProjectItem spi
  337. in project.ProjectItems
  338. where spi.Name == spiName
  339. select spi).FirstOrDefault();
  340. }
  341. /// <summary>
  342. /// Tries the get setting.
  343. /// </summary>
  344. /// <typeparam name="T"></typeparam>
  345. /// <param name="settingName">Name of the setting.</param>
  346. /// <param name="category">The category.</param>
  347. /// <param name="page">The page.</param>
  348. /// <param name="value">The value.</param>
  349. /// <returns></returns>
  350. public static bool TryGetSetting<T>(string settingName, string category, string page, out T value)
  351. {
  352. bool result = false;
  353. value = default(T);
  354. DTE dte = DTEManager.DTE;
  355. if (dte != null)
  356. {
  357. EnvDTE.Properties properties = dte.get_Properties(category, page);
  358. if (properties != null)
  359. {
  360. value = (T)properties.Item(settingName).Value;
  361. result = true;
  362. }
  363. }
  364. return result;
  365. }
  366. /// <summary>
  367. /// Set the status of the status bar.
  368. /// </summary>
  369. /// <param name="message">The message to display.</param>
  370. public static void SetStatus(string message)
  371. {
  372. DTEManager.DTE.StatusBar.Text = message;
  373. }
  374. }
  375. }