Example #1
0
        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));
        }
Example #2
0
        private static MsbuildRunnerResult InitAndExecute(string msbuildVersion, string projectPath, string[] arguments, bool x64)
        {
            lock (InitLock)
            {
                if (!Initialized)
                {
                    Initialized = true;
                    var vswhereResult = VswhereRunner.Execute(VswhereFindArguments, true);
                    if (vswhereResult.ExitCode != 0)
                    {
                        throw new InvalidOperationException($"Failed to execute vswhere.exe, exit code: {vswhereResult.ExitCode}. Output:\r\n{String.Join("\r\n", vswhereResult.StandardOutput)}");
                    }

                    string msbuild15Path        = null;
                    string msbuild15Path64      = null;
                    string msbuildCurrentPath   = null;
                    string msbuildCurrentPath64 = null;

                    foreach (var installPath in vswhereResult.StandardOutput)
                    {
                        if (msbuildCurrentPath == null)
                        {
                            var path = Path.Combine(installPath, MsbuildCurrentRelativePath);
                            if (File.Exists(path))
                            {
                                msbuildCurrentPath = path;
                            }
                        }

                        if (msbuildCurrentPath64 == null)
                        {
                            var path = Path.Combine(installPath, MsbuildCurrentRelativePath64);
                            if (File.Exists(path))
                            {
                                msbuildCurrentPath64 = path;
                            }
                        }

                        if (msbuild15Path == null)
                        {
                            var path = Path.Combine(installPath, Msbuild15RelativePath);
                            if (File.Exists(path))
                            {
                                msbuild15Path = path;
                            }
                        }

                        if (msbuild15Path64 == null)
                        {
                            var path = Path.Combine(installPath, Msbuild15RelativePath64);
                            if (File.Exists(path))
                            {
                                msbuild15Path64 = path;
                            }
                        }
                    }

                    if (msbuildCurrentPath != null)
                    {
                        MsbuildCurrentRunner = new MsbuildRunner(msbuildCurrentPath);
                    }

                    if (msbuildCurrentPath64 != null)
                    {
                        MsbuildCurrentRunner64 = new MsbuildRunner(msbuildCurrentPath64);
                    }

                    if (msbuild15Path != null)
                    {
                        Msbuild15Runner = new MsbuildRunner(msbuild15Path);
                    }

                    if (msbuild15Path64 != null)
                    {
                        Msbuild15Runner64 = new MsbuildRunner(msbuild15Path64);
                    }
                }
            }

            MsbuildRunner runner;

            switch (msbuildVersion)
            {
            case "15":
            {
                runner = x64 ? Msbuild15Runner64 : Msbuild15Runner;
                break;
            }

            case "Current":
            {
                runner = x64 ? MsbuildCurrentRunner64 : MsbuildCurrentRunner;
                break;
            }

            default:
            {
                runner = x64 ? MsbuildCurrentRunner64 ?? Msbuild15Runner64
                                     : MsbuildCurrentRunner ?? Msbuild15Runner;
                break;
            }
            }

            if (runner == null)
            {
                throw new InvalidOperationException($"Failed to find an installed{(x64 ? " 64-bit" : String.Empty)} MSBuild{msbuildVersion}");
            }

            return(runner.ExecuteCore(projectPath, arguments));
        }