PageRenderTime 3328ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Tests.VisualStudio/contexts/visual_studio.cs

http://github.com/openrasta/openwrap
C# | 294 lines | 248 code | 43 blank | 3 comment | 6 complexity | 1b54748db8ba5f7e18539c4d55f5e707 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using EnvDTE80;
  8. using Microsoft.Win32;
  9. using OpenFileSystem.IO;
  10. using OpenFileSystem.IO.FileSystems.Local;
  11. using OpenRasta.Client;
  12. using OpenWrap.Build;
  13. using OpenWrap.Build.Tasks;
  14. using OpenWrap.Commands;
  15. using OpenWrap.Commands.Cli;
  16. using OpenWrap.Commands.Cli.Locators;
  17. using OpenWrap.Commands.Core;
  18. using OpenWrap.Commands.Wrap;
  19. using OpenWrap.Configuration;
  20. using OpenWrap.Configuration.Remotes;
  21. using OpenWrap.IO.Packaging;
  22. using OpenWrap.ProjectModel.Drivers.File;
  23. using OpenWrap.Services;
  24. using OpenWrap.Testing;
  25. using OpenWrap.VisualStudio;
  26. using OpenWrap.VisualStudio.Hooks;
  27. using OpenWrap.VisualStudio.SolutionAddIn;
  28. using Tests.ProjectModel.drivers.file;
  29. using Tests.VisualStudio.Artifacts;
  30. namespace Tests.VisualStudio.contexts
  31. {
  32. public class visual_studio : context, IDisposable
  33. {
  34. protected DTE2 Dte;
  35. protected List<VisualStudioError> Errors = new List<VisualStudioError>();
  36. protected int ExitCode;
  37. protected IDirectory OutDir;
  38. protected string Output;
  39. protected IDirectory RootDir;
  40. protected IFile SlnFile;
  41. protected bool Timeout;
  42. readonly IDirectory ConfigDir;
  43. readonly LocalFileSystem FileSystem;
  44. readonly List<Func<PackageContent>> OpenWrapAssemblies = new List<Func<PackageContent>>();
  45. readonly IDirectory SourceDir;
  46. readonly IDirectory SysRepoDir;
  47. readonly List<string> _commands = new List<string>();
  48. readonly ITemporaryDirectory _tempDir;
  49. string Vs2008Path;
  50. string Vs2010Path;
  51. Action<DTE2>[] _dteActions;
  52. ClassLibraryProject _project;
  53. bool _solutionHasAddin;
  54. string _solutionName;
  55. string _packageVersion = "99.99";
  56. public visual_studio()
  57. {
  58. ServiceLocator.Clear();
  59. Vs2010Path = (string)Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\VisualStudio\10.0\Setup\VS\").GetValue("EnvironmentDirectory") + "devenv.com";
  60. Vs2008Path = (string)Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\VisualStudio\9.0\Setup\VS\").GetValue("EnvironmentDirectory") + "devenv.com";
  61. FileSystem = LocalFileSystem.Instance;
  62. _tempDir = FileSystem.CreateTempDirectory();
  63. Console.WriteLine("Temp path: " + _tempDir.Path);
  64. RootDir = _tempDir.GetDirectory("root").MustExist();
  65. OutDir = _tempDir.GetDirectory("outdir").MustExist();
  66. ConfigDir = _tempDir.GetDirectory("config").MustExist();
  67. SysRepoDir = _tempDir.GetDirectory("sys").MustExist();
  68. SourceDir = RootDir.GetDirectory("src");
  69. }
  70. public void Dispose()
  71. {
  72. if (Dte.Solution.IsOpen) Dte.Solution.Close(CloseOptions.Save);
  73. Dte.Application.Quit();
  74. Dte = null;
  75. MessageFilter.Revoke();
  76. }
  77. protected void given_command(string command)
  78. {
  79. _commands.Add(command);
  80. }
  81. protected void given_empty_solution_addin_com_reg()
  82. {
  83. AddInInstaller.Uninstall();
  84. }
  85. protected void given_file(string fileName, string content)
  86. {
  87. _project.AddCompile(fileName, content);
  88. }
  89. protected void given_openwrap_assemblyOf<T>(string destination)
  90. {
  91. OpenWrapAssemblies.Add(() => AssemblyOf<T>(destination));
  92. }
  93. protected void given_project_2010(string projectName)
  94. {
  95. _project = new ClassLibraryProject(projectName, projectName, projectName, OutDir.Path);
  96. }
  97. protected void given_solution_addin_com_reg()
  98. {
  99. AddInInstaller.Install();
  100. }
  101. protected void given_solution_file(string solutionName, bool withAddin = false)
  102. {
  103. _solutionName = solutionName;
  104. _solutionHasAddin = withAddin;
  105. }
  106. protected void given_vs_action(params Action<DTE2>[] actions)
  107. {
  108. _dteActions = actions;
  109. }
  110. protected void when_executing_vs2010(bool waitOnPlugins = false)
  111. {
  112. CreateProjectFiles();
  113. BuildOpenWrapPackage();
  114. CopyPackageDependencies();
  115. InitializeServices();
  116. ExecuteCommands();
  117. Type t = Type.GetTypeFromProgID("VisualStudio.DTE.10.0", true);
  118. object app = Activator.CreateInstance(t, true);
  119. Dte = (DTE2)app;
  120. MessageFilter.Register();
  121. Dte.Solution.Open(SlnFile.Path);
  122. while (Dte.Solution.IsOpen == false) Thread.Sleep(TimeSpan.FromSeconds(1));
  123. foreach (var action in _dteActions)
  124. action(Dte);
  125. if (waitOnPlugins)
  126. Dte.Windows.Output("OpenWrap").WaitForMessage(StartSolutionPlugin.SOLUTION_PLUGINS_STARTED);
  127. ReadErrors();
  128. ReadOpenWrapOutput();
  129. foreach (var error in Errors) Console.WriteLine("Error: " + error.Description);
  130. }
  131. protected void when_loading_solution_with_plugins()
  132. {
  133. when_executing_vs2010(true);
  134. }
  135. PackageContent AssemblyOf<T>(string relativePath = "bin-net35")
  136. {
  137. var assembly = typeof(T).Assembly;
  138. return new PackageContent { FileName = assembly.GetName().Name + ".dll", RelativePath = relativePath, Stream = () => File.OpenRead(assembly.Location) };
  139. }
  140. void BuildOpenWrapPackage()
  141. {
  142. Packager.NewFromFiles(
  143. SysRepoDir.GetFile("openwrap-" + _packageVersion + ".wrap"),
  144. GetOpenWrapPackageContent());
  145. }
  146. void CopyPackageDependencies()
  147. {
  148. foreach (var file in FileSystem.GetFile(GetType().Assembly.Location).Parent.GetDirectory("Artifacts").Files("*.wrap"))
  149. file.CopyTo(SysRepoDir.GetFile(file.Name));
  150. }
  151. void CreateProjectFiles()
  152. {
  153. SlnFile = SourceDir.GetFile(_solutionName);
  154. var sol = new SolutionFile(SlnFile, SolutionConstants.VisualStudio2010Version);
  155. if (_solutionHasAddin)
  156. sol.OpenWrapAddInEnabled = true;
  157. var projectFile = SourceDir.GetDirectory(_project.Name).GetFile(_project.Name + ".csproj");
  158. _project.Write(projectFile);
  159. sol.AddProject(projectFile);
  160. sol.Save();
  161. }
  162. void ExecuteCommands()
  163. {
  164. var executor = new ConsoleCommandExecutor(
  165. ServiceLocator.GetService<IEnumerable<ICommandLocator>>(),
  166. ServiceLocator.GetService<IEventHub>(),
  167. ServiceLocator.GetService<ICommandOutputFormatter>());
  168. foreach (var command in _commands)
  169. executor.Execute(command, Enumerable.Empty<string>());
  170. }
  171. IEnumerable<PackageContent> GetOpenWrapPackageContent()
  172. {
  173. yield return Static("openwrap.wrapdesc", ".", VsFiles.openwrap_wrapdesc);
  174. yield return Static("version", ".", Encoding.UTF8.GetBytes(_packageVersion));
  175. // bin-net35
  176. yield return AssemblyOf<ICommand>(); // openwrap.dll
  177. yield return AssemblyOf<IHttpClient>(); // openrasta.client.dll
  178. yield return AssemblyOf<SolutionAddInEnabler>(); // openwrap.visualstudio.shared.dll
  179. // build
  180. yield return Static("OpenWrap.CSharp.targets", "build", VsFiles.OpenWrap_CSharp_targets);
  181. yield return Static("OpenWrap.tasks", "build", VsFiles.OpenWrap_tasks);
  182. yield return AssemblyOf<InitializeOpenWrap>("build"); // openwrap.build.bootstrap.dll
  183. yield return AssemblyOf<RunCommand>("build"); // openwrap.build.tasks.dll
  184. yield return AssemblyOf<OpenWrapVisualStudioAddIn>(); // openwrap.visualstudio.comshim.dll
  185. // commands
  186. yield return AssemblyOf<BuildWrapCommand>("commands-net35");
  187. foreach (var added in OpenWrapAssemblies)
  188. yield return added();
  189. }
  190. void InitializeServices()
  191. {
  192. new ServiceRegistry()
  193. .CurrentDirectory(RootDir.Path)
  194. .SystemRepositoryDirectory(SysRepoDir.Path)
  195. .ConfigurationDirectory(ConfigDir.Path)
  196. .Initialize();
  197. ServiceLocator.GetService<IConfigurationManager>().Save(new RemoteRepositories());
  198. }
  199. void ReadErrors()
  200. {
  201. for (int i = 1; i <= Dte.ToolWindows.ErrorList.ErrorItems.Count; i++)
  202. {
  203. var error = Dte.ToolWindows.ErrorList.ErrorItems.Item(i);
  204. Errors.Add(new VisualStudioError
  205. {
  206. Description = TryGet(() => error.Description),
  207. Project = TryGet(() => error.Project),
  208. FileName = TryGet(() => error.FileName)
  209. });
  210. }
  211. }
  212. void ReadOpenWrapOutput()
  213. {
  214. Output = Dte.Windows.Output("OpenWrap").Read();
  215. }
  216. PackageContent Static(string fileName, string relativePath, byte[] content)
  217. {
  218. return new PackageContent { FileName = fileName, RelativePath = relativePath, Stream = () => new MemoryStream(content) };
  219. }
  220. string TryGet(Func<string> project)
  221. {
  222. try
  223. {
  224. return project();
  225. }
  226. catch
  227. {
  228. return null;
  229. }
  230. }
  231. protected string resharper_assert(string assertionName)
  232. {
  233. var resharperTestWindow = Dte.Windows.Output("OpenWrap-Tests", true);
  234. resharperTestWindow.OutputString("?ReSharper" + assertionName + "\r\n");
  235. var response = "!ReSharper" + assertionName + ":";
  236. resharperTestWindow.WaitForMessage(response, waitFor: TimeSpan.FromMinutes(5));
  237. var returnValue = resharperTestWindow.Read()
  238. .Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries)
  239. .Where(x => x.StartsWith(response))
  240. .Select(x => x.Substring(response.Length))
  241. .Last();
  242. resharperTestWindow.Clear();
  243. return returnValue;
  244. }
  245. protected void BumpPackageVersion()
  246. {
  247. _packageVersion = "99.100";
  248. BuildOpenWrapPackage();
  249. }
  250. }
  251. }