summaryrefslogtreecommitdiff
path: root/src/internal/WixBuildTools.TestSupport/VswhereRunner.cs
blob: 0197e12523644e11a6068004efdddb37254a07dc (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
// 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.IO;

    public class VswhereRunner : ExternalExecutable
    {
        private static readonly string VswhereRelativePath = @"Microsoft Visual Studio\Installer\vswhere.exe";

        private static readonly object InitLock = new object();
        private static bool Initialized;
        private static VswhereRunner Instance;

        public static ExternalExecutableResult Execute(string args, bool mergeErrorIntoOutput = false) =>
            InitAndExecute(args, mergeErrorIntoOutput);

        private static ExternalExecutableResult InitAndExecute(string args, bool mergeErrorIntoOutput)
        {
            lock (InitLock)
            {
                if (!Initialized)
                {
                    Initialized = true;
                    var vswherePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), VswhereRelativePath);
                    if (!File.Exists(vswherePath))
                    {
                        throw new InvalidOperationException($"Failed to find vswhere at: {vswherePath}");
                    }

                    Instance = new VswhereRunner(vswherePath);
                }
            }

            return Instance.Run(args, mergeErrorIntoOutput);
        }

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