aboutsummaryrefslogtreecommitdiff
path: root/src/WixBuildTools.TestSupport/MsbuildRunner.cs
diff options
context:
space:
mode:
authorRob Mensching <rob@firegiant.com>2018-07-30 01:49:53 -0700
committerRob Mensching <rob@firegiant.com>2018-07-30 01:49:53 -0700
commita7f145fa0669ab988e69e031f346360072306a94 (patch)
tree4aee84be4b40357451798fb236e96d58a479c6f5 /src/WixBuildTools.TestSupport/MsbuildRunner.cs
parentab202648a24f8ec753ccd2e73eb1fc58efc23b15 (diff)
downloadwix-a7f145fa0669ab988e69e031f346360072306a94.tar.gz
wix-a7f145fa0669ab988e69e031f346360072306a94.tar.bz2
wix-a7f145fa0669ab988e69e031f346360072306a94.zip
Add support for testing with MSBuild
Diffstat (limited to 'src/WixBuildTools.TestSupport/MsbuildRunner.cs')
-rw-r--r--src/WixBuildTools.TestSupport/MsbuildRunner.cs117
1 files changed, 117 insertions, 0 deletions
diff --git a/src/WixBuildTools.TestSupport/MsbuildRunner.cs b/src/WixBuildTools.TestSupport/MsbuildRunner.cs
new file mode 100644
index 00000000..dbfc02f1
--- /dev/null
+++ b/src/WixBuildTools.TestSupport/MsbuildRunner.cs
@@ -0,0 +1,117 @@
1// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
3namespace WixBuildTools.TestSupport
4{
5 using System;
6 using System.Collections.Generic;
7 using System.Diagnostics;
8 using System.IO;
9 using System.Text;
10
11 public class MsbuildRunner
12 {
13 private static readonly string VswhereRelativePath = @"Microsoft Visual Studio\Installer\vswhere.exe";
14 private static readonly string[] VswhereFindArguments = new[] { "-property", "installationPath", "-latest" };
15 private static readonly string Msbuild15RelativePath = @"MSBuild\15.0\bin\MSBuild.exe";
16
17 public MsbuildRunner()
18 {
19 var vswherePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), VswhereRelativePath);
20 if (!File.Exists(vswherePath))
21 {
22 throw new InvalidOperationException($"Failed to find vswhere at: {vswherePath}");
23 }
24
25 var result = RunProcessCaptureOutput(vswherePath, VswhereFindArguments);
26 if (result.ExitCode != 0)
27 {
28 throw new InvalidOperationException($"Failed to execute vswhere.exe, exit code: {result.ExitCode}");
29 }
30
31 this.Msbuild15Path = Path.Combine(result.Output[0], Msbuild15RelativePath);
32 if (!File.Exists(this.Msbuild15Path))
33 {
34 throw new InvalidOperationException($"Failed to find MSBuild v15 at: {this.Msbuild15Path}");
35 }
36 }
37
38 private string Msbuild15Path { get; }
39
40 public MsbuildRunnerResult Execute(string projectPath, string[] arguments = null)
41 {
42 var total = new List<string>
43 {
44 projectPath
45 };
46
47 if (arguments != null)
48 {
49 total.AddRange(arguments);
50 }
51
52 var workingFolder = Path.GetDirectoryName(projectPath);
53 return RunProcessCaptureOutput(this.Msbuild15Path, total.ToArray(), workingFolder);
54 }
55
56 private static MsbuildRunnerResult RunProcessCaptureOutput(string executablePath, string[] arguments = null, string workingFolder = null)
57 {
58 var startInfo = new ProcessStartInfo(executablePath)
59 {
60 Arguments = CombineArguments(arguments),
61 CreateNoWindow = true,
62 RedirectStandardError = true,
63 RedirectStandardOutput = true,
64 UseShellExecute = false,
65 WorkingDirectory = workingFolder,
66 };
67
68 var exitCode = 0;
69 var output = new List<string>();
70
71 using (var process = Process.Start(startInfo))
72 {
73 process.OutputDataReceived += (s, e) => { if (e.Data != null) output.Add(e.Data); };
74 process.ErrorDataReceived += (s, e) => { if (e.Data != null) output.Add(e.Data); };
75
76 process.BeginErrorReadLine();
77 process.BeginOutputReadLine();
78
79 process.WaitForExit();
80 exitCode = process.ExitCode;
81 }
82
83 return new MsbuildRunnerResult { ExitCode = exitCode, Output = output.ToArray() };
84 }
85
86 private static string CombineArguments(string[] arguments)
87 {
88 if (arguments == null)
89 {
90 return null;
91 }
92
93 var sb = new StringBuilder();
94
95 foreach (var arg in arguments)
96 {
97 if (sb.Length > 0)
98 {
99 sb.Append(' ');
100 }
101
102 if (arg.IndexOf(' ') > -1)
103 {
104 sb.Append("\"");
105 sb.Append(arg);
106 sb.Append("\"");
107 }
108 else
109 {
110 sb.Append(arg);
111 }
112 }
113
114 return sb.ToString();
115 }
116 }
117}