PageRenderTime 134ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/Xpand.Addins/PlugIn1.cs

https://github.com/krazana/eXpand
C# | 205 lines | 191 code | 14 blank | 0 comment | 26 complexity | eaadf5efc7c14b13033add4dddb8a0fa MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Data.SqlClient;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text.RegularExpressions;
  10. using System.Windows.Forms;
  11. using DevExpress.CodeRush.Core;
  12. using DevExpress.CodeRush.Diagnostics.Commands;
  13. using DevExpress.CodeRush.PlugInCore;
  14. using DevExpress.CodeRush.StructuralParser;
  15. using DevExpress.DXCore.Controls.Xpo;
  16. using DevExpress.DXCore.Controls.Xpo.DB;
  17. using DevExpress.DXCore.Controls.Xpo.DB.Exceptions;
  18. using EnvDTE;
  19. using XpandAddIns.Enums;
  20. using XpandAddIns.Extensions;
  21. using Configuration = System.Configuration.Configuration;
  22. using ConfigurationManager = System.Configuration.ConfigurationManager;
  23. using Process = System.Diagnostics.Process;
  24. using Project = EnvDTE.Project;
  25. using Property = EnvDTE.Property;
  26. namespace XpandAddIns {
  27. public partial class PlugIn1 : StandardPlugIn {
  28. private void convertProject_Execute(ExecuteEventArgs ea) {
  29. string path = Options.ReadString(Options.ProjectConverterPath);
  30. string token = Options.ReadString(Options.Token);
  31. if (!string.IsNullOrEmpty(path) && !string.IsNullOrEmpty(token)) {
  32. var directoryName = Path.GetDirectoryName(CodeRush.Solution.Active.FileName);
  33. var userName = string.Format("/s /k:{0} \"{1}\"", token, directoryName);
  34. Process.Start(path, userName);
  35. actionHint1.Text = "Project Converter Started !!!";
  36. Rectangle rectangle = Screen.PrimaryScreen.Bounds;
  37. actionHint1.PointTo(new Point(rectangle.Width / 2, rectangle.Height / 2));
  38. }
  39. }
  40. private void collapseAllItemsInSolutionExplorer_Execute(ExecuteEventArgs ea) {
  41. CodeRush.ApplicationObject.Solution.CollapseAllFolders();
  42. }
  43. private void exploreXafErrors_Execute(ExecuteEventArgs ea) {
  44. Project startUpProject = CodeRush.ApplicationObject.Solution.FindStartUpProject();
  45. Property outPut = startUpProject.ConfigurationManager.ActiveConfiguration.FindProperty(Enums.ConfigurationProperty.OutputPath);
  46. bool isWeb = IsWeb(startUpProject);
  47. string fullPath = startUpProject.FindProperty(ProjectProperty.FullPath).Value + "";
  48. string path = Path.Combine(fullPath, outPut.Value.ToString())+"";
  49. if (isWeb)
  50. path = Path.GetDirectoryName(startUpProject.FullName);
  51. Func<Stream> streamSource = () => {
  52. var path1 = path+"";
  53. File.Copy(Path.Combine(path1, "expressAppFrameWork.log"), Path.Combine(path1, "expressAppFrameWork.locked"), true);
  54. return File.Open(Path.Combine(path1, "expressAppFrameWork.locked"), FileMode.Open, FileAccess.Read, FileShare.Read);
  55. };
  56. var reader = new ReverseLineReader(streamSource);
  57. var stackTrace = new List<string>();
  58. foreach (var readline in reader) {
  59. stackTrace.Add(readline);
  60. if (readline.Trim().StartsWith("The error occured:") || readline.Trim().StartsWith("The error occurred:")) {
  61. stackTrace.Reverse();
  62. string errorMessage = "";
  63. foreach (string trace in stackTrace) {
  64. errorMessage += trace + Environment.NewLine;
  65. if (trace.Trim().StartsWith("----------------------------------------------------"))
  66. break;
  67. }
  68. Clipboard.SetText(errorMessage);
  69. break;
  70. }
  71. }
  72. }
  73. bool IsWeb(Project startUpProject) {
  74. return startUpProject.ProjectItems.OfType<ProjectItem>().Where(item => item.Name.ToLower() == "web.config").Count() > 0;
  75. }
  76. private void SpAtDesignTime_Execute(ExecuteEventArgs ea) {
  77. IEnumerable<ProjectItem> enumerable = CodeRush.ApplicationObject.Solution.FindStartUpProject().ProjectItems.Cast<ProjectItem>();
  78. Trace.Listeners.Add(new DefaultTraceListener { LogFileName = "log.txt" });
  79. foreach (ProjectItem item in enumerable) {
  80. if (item.Name.ToLower() == "app.config" || item.Name.ToLower() == "web.config") {
  81. foreach (var connectionString in Options.GetConnectionStrings()) {
  82. if (!string.IsNullOrEmpty(connectionString.Name)) {
  83. ConnectionStringSettings connectionStringSettings = GetConnectionStringSettings(item, connectionString.Name);
  84. if (connectionStringSettings != null) {
  85. DropDatabase(connectionStringSettings.ConnectionString, connectionString.Name);
  86. }
  87. }
  88. }
  89. }
  90. }
  91. }
  92. private ConnectionStringSettings GetConnectionStringSettings(ProjectItem item, string name) {
  93. Property property = item.FindProperty(ProjectItemProperty.FullPath);
  94. var exeConfigurationFileMap = new ExeConfigurationFileMap { ExeConfigFilename = property.Value.ToString() };
  95. Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(exeConfigurationFileMap, ConfigurationUserLevel.None);
  96. ConnectionStringsSection strings = configuration.ConnectionStrings;
  97. return strings.ConnectionStrings[name];
  98. }
  99. private void DropDatabase(string connectionString, string name) {
  100. string error = null;
  101. string database = name;
  102. try {
  103. IDataStore provider = XpoDefault.GetConnectionProvider(connectionString, AutoCreateOption.None);
  104. if (provider is MSSqlConnectionProvider)
  105. database = DropSqlServerDatabase(connectionString);
  106. else if (provider is AccessConnectionProvider) {
  107. database = ((AccessConnectionProvider)provider).Connection.Database;
  108. File.Delete(database);
  109. } else {
  110. throw new NotImplementedException(provider.GetType().FullName);
  111. }
  112. }
  113. catch (UnableToOpenDatabaseException) {
  114. error = "UnableToOpenDatabase " + database;
  115. } catch (Exception e) {
  116. Trace.WriteLine(e.ToString());
  117. error = database + " Error check log";
  118. }
  119. actionHint1.Text = error ?? database + " DataBase Dropped !!!";
  120. Rectangle rectangle = Screen.PrimaryScreen.Bounds;
  121. actionHint1.PointTo(new Point(rectangle.Width / 2, rectangle.Height / 2));
  122. }
  123. private string DropSqlServerDatabase(string connectionString) {
  124. using (var connection = new SqlConnection(connectionString)) {
  125. using (var sqlConnection = new SqlConnection(connectionString.Replace(connection.Database, "master") + ";Pooling=false")) {
  126. sqlConnection.Open();
  127. SqlCommand sqlCommand = sqlConnection.CreateCommand();
  128. sqlCommand.CommandText = string.Format("ALTER DATABASE {0} SET SINGLE_USER WITH ROLLBACK IMMEDIATE", connection.Database);
  129. sqlCommand.ExecuteNonQuery();
  130. sqlCommand.CommandText = string.Format("DROP DATABASE {0}", connection.Database);
  131. sqlCommand.ExecuteNonQuery();
  132. }
  133. return connection.Database;
  134. }
  135. }
  136. private void loadProjects_Execute(ExecuteEventArgs ea) {
  137. string constants = Constants.vsext_wk_SProjectWindow;
  138. if (ea.Action.ParentMenu=="Object Browser Objects Pane")
  139. constants = Constants.vsWindowKindObjectBrowser;
  140. ProjectElement activeProject = CodeRush.Source.ActiveProject;
  141. if (activeProject != null) {
  142. var projectLoader = new ProjectLoader();
  143. var selectedAssemblyReferences = activeProject.GetSelectedAssemblyReferences(constants);
  144. projectLoader.Load(selectedAssemblyReferences.ToList(), constants);
  145. }
  146. else {
  147. actionHint1.Text = "Active project not found. Please open a code file";
  148. Rectangle rectangle = Screen.PrimaryScreen.Bounds;
  149. actionHint1.PointTo(new Point(rectangle.Width / 2, rectangle.Height / 2));
  150. }
  151. }
  152. private void events_ProjectBuildDone(string project, string projectConfiguration, string platform, string solutionConfiguration, bool succeeded) {
  153. if (succeeded) {
  154. string gacUtilPath = Options.Storage.ReadString(Options.GetPageName(), Options.GacUtilPath);
  155. if (File.Exists(gacUtilPath)) {
  156. Project dteProject = CodeRush.Solution.Active.FindProjectFromUniqueName(project);
  157. if (ProjectExists(dteProject)) {
  158. Environment.CurrentDirectory = Path.GetDirectoryName(gacUtilPath) + "";
  159. string outputPath = dteProject.FindOutputPath();
  160. if (File.Exists(outputPath))
  161. Process.Start("gacutil.exe", "/i " + @"""" + outputPath + @""" /f");
  162. }
  163. else {
  164. Log.Send("GagUtl Project Not Found:",dteProject.FileName);
  165. }
  166. }
  167. }
  168. }
  169. public static bool ProjectExists(Project project) {
  170. IEnumerable<string> allProjectPaths =
  171. Options.Storage.GetGroupedKeys(Options.ProjectPaths).SelectMany(
  172. s => Options.Storage.ReadStrings(Options.ProjectPaths, s));
  173. return allProjectPaths.Where(MatchProjectName(project)).FirstOrDefault() != null;
  174. }
  175. static Func<string, bool> MatchProjectName(Project project) {
  176. string fileName = Path.GetFileName(project.FileName)+"";
  177. string pattern = Options.ReadString(Options.GacUtilRegex);
  178. return s => {
  179. string s1 = s.Split('|')[0];
  180. return s1 == project.FileName && (!string.IsNullOrEmpty(pattern) && !Regex.IsMatch(fileName, pattern));
  181. };
  182. }
  183. private void events_DocumentSaved(DocumentEventArgs ea) {
  184. if (Options.Storage.ReadBoolean(Options.GetPageName(), Options.FormatOnSave))
  185. CodeRush.Command.Execute("Edit.FormatDocument");
  186. }
  187. }
  188. }