PageRenderTime 58ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/Code/Main/RefAssistant.10.0/ShellGateway.cs

#
C# | 289 lines | 172 code | 40 blank | 77 comment | 27 complexity | cd3e71662fb8aa54adbb0a35b76daa1f MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //
  2. // Copyright Š 2011 Lardite.
  3. //
  4. // Author: Belikov Sergey (sbelikov@lardite.com)
  5. // Chistov Victor (vchistov@lardite.com)
  6. //
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using Lardite.RefAssistant.ObjectModel;
  12. using Lardite.RefAssistant.UI;
  13. using Lardite.RefAssistant.Utils;
  14. namespace Lardite.RefAssistant
  15. {
  16. /// <summary>
  17. /// Shell gateway.
  18. /// </summary>
  19. public sealed class ShellGateway : IShellGateway
  20. {
  21. #region Fields
  22. private readonly IServiceProvider _serviceProvider;
  23. private readonly IExtensionOptions _options;
  24. private readonly ProjectsCache _projectsCache;
  25. #endregion // Fields
  26. #region .ctor
  27. /// <summary>
  28. /// Constructor.
  29. /// </summary>
  30. /// <param name="serviceProvider">Service provider.</param>
  31. /// <param name="options">Extension options.</param>
  32. public ShellGateway(IServiceProvider serviceProvider, IExtensionOptions options)
  33. {
  34. _serviceProvider = serviceProvider;
  35. _options = options;
  36. _projectsCache = new ProjectsCache(_serviceProvider);
  37. }
  38. #endregion // .ctor
  39. #region Public methods
  40. /// <summary>
  41. /// Builds project.
  42. /// </summary>
  43. /// <param name="projectInfo">The project information. If null then builds active project.</param>
  44. /// <returns>Returns true if success; otherwise false.</returns>
  45. public CompilationInfo BuildProject(ProjectInfo projectInfo)
  46. {
  47. var project = GetProjectWrapper(projectInfo);
  48. var buildResult = project.Build();
  49. if (buildResult != 0)
  50. {
  51. DTEHelper.ShowErrorList(_serviceProvider);
  52. }
  53. return new CompilationInfo(buildResult == 0, project.HasAssembly);
  54. }
  55. /// <summary>
  56. /// Builds current solution.
  57. /// </summary>
  58. /// <returns>Returns true if success; otherwise false.</returns>
  59. public bool BuildSolution()
  60. {
  61. var buildResult = DTEHelper.BuildSolution(_serviceProvider);
  62. if (buildResult != 0)
  63. {
  64. DTEHelper.ShowErrorList(_serviceProvider);
  65. }
  66. return buildResult == 0;
  67. }
  68. /// <summary>
  69. /// Gets active project info.
  70. /// </summary>
  71. /// <returns>Active project info.</returns>
  72. public ProjectInfo GetActiveProjectInfo()
  73. {
  74. return GetProjectWrapper(null).GetProjectInfo();
  75. }
  76. /// <summary>
  77. /// Gets solution's projects.
  78. /// </summary>
  79. /// <returns>The projects list.</returns>
  80. public IEnumerable<ProjectInfo> GetSolutionProjectsInfo()
  81. {
  82. throw Error.NotImplemented();
  83. }
  84. /// <summary>
  85. /// Can show unused references window.
  86. /// </summary>
  87. /// <returns>If true, then can.</returns>
  88. public bool CanShowUnusedReferencesWindow()
  89. {
  90. return _options.IsShowUnusedReferencesWindow.HasValue
  91. && _options.IsShowUnusedReferencesWindow.Value;
  92. }
  93. /// <summary>
  94. /// Shows unused references window.
  95. /// </summary>
  96. /// <param name="unusedProjectReferences">Unused project references.</param>
  97. /// <returns>If true, then continue.</returns>
  98. public bool ShowUnusedReferencesWindow(ref IEnumerable<ProjectReference> unusedProjectReferences)
  99. {
  100. var window = new UnusedReferencesWindow();
  101. window.SetProjectReferences(unusedProjectReferences);
  102. window.IsShowThisWindowAgain = _options.IsShowUnusedReferencesWindow.Value;
  103. var result = window.ShowModal();
  104. if (result.HasValue && result.Value)
  105. {
  106. unusedProjectReferences = window.GetUnusedReferences();
  107. _options.IsShowUnusedReferencesWindow = window.IsShowThisWindowAgain;
  108. return true;
  109. }
  110. return false;
  111. }
  112. /// <summary>
  113. /// Can remove unused references.
  114. /// </summary>
  115. /// <param name="projectInfo">The project information. If null then gets active project.</param>
  116. public bool CanRemoveUnusedReferences(ProjectInfo projectInfo)
  117. {
  118. try
  119. {
  120. var project = GetProjectWrapper(projectInfo);
  121. var vcpp = project as VisualCppCliProjectWrapper;
  122. if (vcpp != null && !vcpp.IsManaged)
  123. {
  124. return false;
  125. }
  126. return (project.Kind != ProjectKinds.Modeling
  127. && project.Kind != ProjectKinds.Database
  128. && !project.IsBuildInProgress);
  129. }
  130. catch
  131. {
  132. return false;
  133. }
  134. }
  135. /// <summary>
  136. /// Removes unused references.
  137. /// </summary>
  138. /// <param name="projectInfo">The project information. If null then gets active project.</param>
  139. /// <param name="unusedProjectReferences">Unused project references.</param>
  140. /// <returns>Removed references count.</returns>
  141. public int RemoveUnusedReferences(ProjectInfo projectInfo, IEnumerable<ProjectReference> unusedProjectReferences)
  142. {
  143. var project = GetProjectWrapper(projectInfo);
  144. StringBuilder builder = new StringBuilder();
  145. IEnumerable<ProjectReference> unusedReferences = project.RemoveUnusedReferences(unusedProjectReferences);
  146. foreach (var unusedReference in unusedReferences)
  147. {
  148. builder.Append(" ").AppendLine(unusedReference.FullName);
  149. }
  150. LogManager.OutputLog.Information(builder.ToString());
  151. return unusedReferences.Count();
  152. }
  153. /// <summary>
  154. /// Can remove unused references.
  155. /// </summary>
  156. /// <param name="projectInfo">The project information. If null then gets active project.</param>
  157. /// <returns>If true, then can.</returns>
  158. public bool CanRemoveUnusedUsings(ProjectInfo projectInfo)
  159. {
  160. try
  161. {
  162. var project = GetProjectWrapper(projectInfo);
  163. return (project.Kind == ProjectKinds.CSharp
  164. && !project.IsBuildInProgress
  165. && _options.IsRemoveUsingsAfterRemoving.HasValue
  166. && _options.IsRemoveUsingsAfterRemoving.Value);
  167. }
  168. catch
  169. {
  170. return false;
  171. }
  172. }
  173. /// <summary>
  174. /// Removes unused usings.
  175. /// </summary>
  176. /// <param name="projectInfo">The project information. If null then gets active project.</param>
  177. public void RemoveUnusedUsings(ProjectInfo projectInfo)
  178. {
  179. var project = GetProjectWrapper(projectInfo);
  180. LogManager.OutputLog.Information(" " + Resources.RefAssistantPackage_RemoveUnusedUsings);
  181. project.RemoveUnusedUsings(_serviceProvider);
  182. }
  183. #endregion // Public methods
  184. #region Private methods
  185. /// <summary>
  186. /// Gets cached project wrapper.
  187. /// </summary>
  188. /// <param name="projectInfo">The project information. If null then gets active project.</param>
  189. /// <returns>Returns the project wrapper.</returns>
  190. /// <exception cref="System.InvalidOperationException"/>
  191. private BaseProjectWrapper GetProjectWrapper(ProjectInfo projectInfo)
  192. {
  193. if (projectInfo == null)
  194. {
  195. // gets active project
  196. var project = DTEHelper.GetActiveProject(_serviceProvider);
  197. if (project == null)
  198. {
  199. throw Error.InvalidOperation(Resources.ShellGateway_CannotGetActiveProject);
  200. }
  201. return GetProjectWrapperByName(project.Name);
  202. }
  203. // gets project by name
  204. return GetProjectWrapperByName(projectInfo.Name);
  205. }
  206. /// <summary>
  207. /// Gets cached project wrapper by project name.
  208. /// </summary>
  209. /// <param name="projectName">The project name.</param>
  210. /// <returns>Returns the project wrapper.</returns>
  211. /// <exception cref="System.ArgumentNullException"/>
  212. /// <exception cref="System.InvalidOperationException"/>
  213. private BaseProjectWrapper GetProjectWrapperByName(string projectName)
  214. {
  215. if (string.IsNullOrWhiteSpace(projectName))
  216. {
  217. throw Error.ArgumentNull("projectName", Resources.ShellGateway_ProjectNameIsNull);
  218. }
  219. var wrapper = _projectsCache[projectName];
  220. if (wrapper == null)
  221. {
  222. throw Error.InvalidOperation(string.Format(Resources.ShellGateway_ProjectNotFound, projectName));
  223. }
  224. return wrapper;
  225. }
  226. #endregion // Private methods
  227. #region Nested class
  228. /// <summary>
  229. /// Cache for solution's projects.
  230. /// </summary>
  231. private class ProjectsCache : SimpleCache<string, BaseProjectWrapper>
  232. {
  233. #region .ctor
  234. private readonly IServiceProvider _serviceProvider;
  235. public ProjectsCache(IServiceProvider serviceProvider)
  236. {
  237. _serviceProvider = serviceProvider;
  238. }
  239. #endregion // .ctor
  240. #region Overriding
  241. protected override BaseProjectWrapper GetValue(string key)
  242. {
  243. return DTEHelper.CreateProjectWrapper(DTEHelper.GetProjectByName(_serviceProvider, key));
  244. }
  245. #endregion // Overriding
  246. }
  247. #endregion // Nested class
  248. }
  249. }