summaryrefslogtreecommitdiff
path: root/src/internal/WixBuildTools.TestSupport/DotnetRunner.cs
blob: 8239117869a91a2395832e34be973b7b550e4ade (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
// 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 WixBuildTools.TestSupport
{
    using System;
    using System.Collections.Generic;
    using System.IO;

    public class DotnetRunner : ExternalExecutable
    {
        private static readonly object InitLock = new object();
        private static bool Initialized;
        private static DotnetRunner Instance;

        public static ExternalExecutableResult Execute(string command, string[] arguments = null) =>
            InitAndExecute(command, arguments);

        private static ExternalExecutableResult InitAndExecute(string command, string[] arguments)
        {
            lock (InitLock)
            {
                if (!Initialized)
                {
                    Initialized = true;
                    var dotnetPath = Environment.GetEnvironmentVariable("DOTNET_HOST_PATH");
                    if (String.IsNullOrEmpty(dotnetPath) || !File.Exists(dotnetPath))
                    {
                        dotnetPath = "dotnet";
                    }

                    Instance = new DotnetRunner(dotnetPath);
                }
            }

            return Instance.ExecuteCore(command, arguments);
        }

        private DotnetRunner(string exePath) : base(exePath) { }

        private ExternalExecutableResult ExecuteCore(string command, string[] arguments)
        {
            var total = new List<string>
            {
                command,
            };

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

            var args = CombineArguments(total);
            var mergeErrorIntoOutput = true;
            return this.Run(args, mergeErrorIntoOutput);
        }
    }
}