aboutsummaryrefslogtreecommitdiff
path: root/src/test/WixToolsetTest.Sdk/MsbuildUtilities.cs
blob: cfb421b2242bb282edeaa01f288e4bd84bc305ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// 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.

namespace WixToolsetTest.Sdk
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using WixBuildTools.TestSupport;

    public enum BuildSystem
    {
        DotNetCoreSdk,
        MSBuild,
        MSBuild64,
    }

    public static class MsbuildUtilities
    {
        public static readonly string WixMsbuildPath = Path.Combine(new Uri(typeof(MsbuildUtilities).Assembly.CodeBase).AbsolutePath, "..", "..", "publish", "WixToolset.Sdk");
        public static readonly string WixPropsPath = Path.Combine(WixMsbuildPath, "build", "WixToolset.Sdk.props");

        public static MsbuildRunnerResult BuildProject(BuildSystem buildSystem, string projectPath, string[] arguments = null, string configuration = "Release", bool? outOfProc = null, string verbosityLevel = "normal")
        {
            var allArgs = new List<string>
            {
                $"-verbosity:{verbosityLevel}",
                $"-p:Configuration={configuration}",
                GetQuotedPropertySwitch(buildSystem, "WixMSBuildProps", MsbuildUtilities.WixPropsPath),
                // Node reuse means that child msbuild processes can stay around after the build completes.
                // Under that scenario, the root msbuild does not reliably close its streams which causes us to hang.
                "-nr:false",
            };

            if (outOfProc.HasValue)
            {
                allArgs.Add($"-p:RunWixToolsOutOfProc={outOfProc.Value}");
            }

            if (arguments != null)
            {
                allArgs.AddRange(arguments);
            }

            switch (buildSystem)
            {
                case BuildSystem.DotNetCoreSdk:
                    {
                        allArgs.Add(projectPath);
                        var result = DotnetRunner.Execute("msbuild", allArgs.ToArray());
                        return new MsbuildRunnerResult
                        {
                            ExitCode = result.ExitCode,
                            Output = result.StandardOutput,
                        };
                    }
                case BuildSystem.MSBuild:
                case BuildSystem.MSBuild64:
                    {
                        return MsbuildRunner.Execute(projectPath, allArgs.ToArray(), buildSystem == BuildSystem.MSBuild64);
                    }
                default:
                    {
                        throw new NotImplementedException();
                    }
            }
        }

        public static string GetQuotedPropertySwitch(BuildSystem buildSystem, string propertyName, string valueToQuote)
        {
            switch (buildSystem)
            {
                case BuildSystem.DotNetCoreSdk:
                    {
                        return $"-p:{propertyName}=\\\"{valueToQuote}\\\"";
                    }
                case BuildSystem.MSBuild:
                case BuildSystem.MSBuild64:
                    {
                        return $"-p:{propertyName}=\"{valueToQuote}\"";
                    }
                default:
                    {
                        throw new NotImplementedException();
                    }
            }
        }

        public static IEnumerable<string> GetToolCommandLines(MsbuildRunnerResult result, string toolName, string operation, BuildSystem buildSystem, bool? outOfProc = null)
        {
            var expectedOutOfProc = buildSystem == BuildSystem.DotNetCoreSdk || outOfProc.HasValue && outOfProc.Value;
            var expectedToolExe = !expectedOutOfProc ? $"({toolName}.exe)" :
                                  buildSystem == BuildSystem.DotNetCoreSdk ? $"{toolName}.dll\"" : $"{toolName}.exe";
            var expectedToolCommand = $"{expectedToolExe} {operation}";
            return result.Output.Where(line => line.Contains(expectedToolCommand));
        }
    }
}