private static ICollection <DiffTool> GetDiffTools()
        {
            var diffTools = new List <DiffTool>();

            // See if a custom diff tool was specified in the configuration file.
            var diffToolCommandOverride   = ConfigurationManager.AppSettings["DiffToolCommand"];
            var diffToolArgumentsOverride = ConfigurationManager.AppSettings["DiffToolArguments"];

            if (!string.IsNullOrEmpty(diffToolCommandOverride))
            {
                diffTools.Add(new DiffTool("Custom", diffToolCommandOverride, diffToolArgumentsOverride));
            }

            // Visual Studio 2017 and above don't store their setup information in the registry but expose it via a COM interface.
            try
            {
                var configuration  = new SetupConfiguration();
                var instancesQuery = configuration.EnumInstances();
                var fetched        = int.MaxValue;
                var instances      = new ISetupInstance[1];
                while (fetched > 0)
                {
                    instancesQuery.Next(1, instances, out fetched);
                    if (fetched > 0)
                    {
                        var instance         = instances[0];
                        var visualStudioName = instance.GetDisplayName();
                        var devenvPath       = instance.ResolvePath("Common7\\IDE\\devenv.exe");
                        if (!string.IsNullOrWhiteSpace(devenvPath) && File.Exists(devenvPath))
                        {
                            diffTools.Add(new DiffTool(visualStudioName, devenvPath, "/diff %1 %2 %6 %7"));
                        }
                    }
                }
            }
            catch (COMException)
            {
                // Ignore COM Exceptions.
            }

            // Visual Studio 2012 and above have a built-in diff tool, call devenv.exe directly.
            TryAddDevenvToolFromRegistry("14.0", "Visual Studio 2015", diffTools);
            TryAddDevenvToolFromRegistry("12.0", "Visual Studio 2013", diffTools);
            TryAddDevenvToolFromRegistry("11.0", "Visual Studio 2012", diffTools);

            // Older versions should have a diffmerge.exe tool in the IDE path.
            TryAddDiffmergeToolFromRegistry("10.0", "Visual Studio 2010", diffTools);
            TryAddDiffmergeToolFromRegistry("9.0", "Visual Studio 2008", diffTools);

            return(diffTools);
        }
Exemplo n.º 2
0
        private static IEnumerable <ISetupInstance> GetSetupInstances()
        {
            ISetupConfiguration setupConfiguration = new SetupConfiguration();
            IEnumSetupInstances enumerator         = setupConfiguration.EnumInstances();

            int count;

            do
            {
                ISetupInstance[] setupInstances = new ISetupInstance[1];
                enumerator.Next(1, setupInstances, out count);
                if (count == 1 &&
                    setupInstances != null &&
                    setupInstances.Length == 1 &&
                    setupInstances[0] != null)
                {
                    yield return(setupInstances[0]);
                }
            }while (count == 1);
        }
        private bool TryGetVCToolsPath(out string batchPath)
        {
            var configuration = new SetupConfiguration();
            var enumInstances = configuration.EnumInstances();
            int fetched;
            var instances = new ISetupInstance[1];

            do
            {
                enumInstances.Next(1, instances, out fetched);

                if (fetched <= 0)
                {
                    continue;
                }

                if (!(instances[0] is ISetupInstance2 instance))
                {
                    continue;
                }

                var packages = instance.GetPackages();
                foreach (var package in packages)
                {
                    if (package.GetId() != "Microsoft.VisualStudio.Component.VC.Tools.x86.x64")
                    {
                        continue;
                    }

                    var rootPath = instance.ResolvePath();
                    batchPath = Directory.GetFiles(rootPath, "VsDevCmd.bat", SearchOption.AllDirectories)
                                .FirstOrDefault();

                    return(true);
                }
            } while (fetched > 0);

            batchPath = null;
            return(false);
        }
        /// <summary>
        /// Returns the path to the specified version of msbuild.exe or
        /// null if it could not be found
        /// </summary>
        /// <param name="msBuildMajorVersion">MSBuild major version number e.g. 15.0</param>
        private static string GetMSBuildPath(string msBuildMajorVersion, TestContext testContext)
        {
            // Note: we're using a Microsoft component that locates instances of VS, and then
            // we're searching for an expected path under VS.
            // A more robust and flexible approach would be to use https://www.nuget.org/packages/vswhere/
            // which would allow us to search for a specific version of MSBuild directly.
            testContext.WriteLine($"Test setup: attempting to locate an MSBuild instance. Version: {msBuildMajorVersion}");
            ISetupConfiguration config = new SetupConfiguration();

            var instances  = new ISetupInstance[100];
            var enumerator = config.EnumInstances();

            enumerator.Next(100, instances, out int fetched);

            if (fetched == 0)
            {
                throw new InvalidOperationException("Test setup error: no instances of Visual Studio could be located on this machine");
            }

            string partialExePath = Path.Combine("MSBuild", msBuildMajorVersion, "Bin", "msbuild.exe");

            for (int i = 0; i < fetched; i++)
            {
                var instance = instances[i];
                testContext.WriteLine($"\t\tVS instance: {instance.GetDisplayName()}, {instance.GetInstallationVersion()}, {instance.GetInstallationPath()}");

                var candidateExePath = instance.ResolvePath(partialExePath);

                if (File.Exists(candidateExePath))
                {
                    testContext.WriteLine($"\tMSBuild exe located: {candidateExePath}");
                    return(candidateExePath);
                }
            }

            testContext.WriteLine($"Test setup: MSBuild exe could not be located");
            return(null);
        }
Exemplo n.º 5
0
        private static List <ISetupInstance2> GetSetupInstances()
        {
            var instances = new List <ISetupInstance2>();

            var setupConfiguration = new SetupConfiguration();
            var instanceEnumerator = setupConfiguration.EnumInstances();

            var setupInstance = new ISetupInstance2[1];
            var count         = 0;

            while (true)
            {
                instanceEnumerator.Next(1, setupInstance, out count);

                if (count == 0)
                {
                    break;
                }

                instances.Add(new VisualStudioSetupInstance(setupInstance[0]));
            }

            return(instances);
        }
Exemplo n.º 6
0
        private string GetVSInstallPath()
        {
            var vsPathOverride = Environment.GetEnvironmentVariable("SHARPGEN_VS_OVERRIDE");

            if (!string.IsNullOrEmpty(vsPathOverride))
            {
                return(vsPathOverride);
            }

            try
            {
                var query         = new SetupConfiguration();
                var enumInstances = query.EnumInstances();

                int fetched;
                var instances = new ISetupInstance[1];
                do
                {
                    enumInstances.Next(1, instances, out fetched);
                    if (fetched <= 0)
                    {
                        continue;
                    }

                    var instance2 = (ISetupInstance2)instances[0];
                    var state     = instance2.GetState();
                    if ((state & InstanceState.Registered) != InstanceState.Registered)
                    {
                        continue;
                    }

                    if (instance2.GetPackages().Any(Predicate))
                    {
                        return(instance2.GetInstallationPath());
                    }
                } while (fetched > 0);
Exemplo n.º 7
0
 public IEnumerable <VisualStudioInstance> GetLaunchableInstances() => EnumInstances(_configuration.EnumInstances());