Пример #1
0
        /// <summary>
        /// Gets the path of MSBuild for the specified version.
        /// </summary>
        public static string GetMSBuildPath(MSBuildSettings?settings)
        {
            if (settings?.MSBuildPath is string settingsPath)
            {
                return(settingsPath);
            }

            if (BuildEnvironment.IsMacOS())
            {
                const string msbuildPath = "/Library/Frameworks/Mono.framework/Versions/Current/Commands/msbuild";
                if (File.Exists(msbuildPath))
                {
                    return(msbuildPath);
                }
            }
            else if (BuildEnvironment.IsLinux())
            {
                const string msbuildPath = "/usr/bin/msbuild";
                if (File.Exists(msbuildPath))
                {
                    return(msbuildPath);
                }
            }
            else
            {
                var version  = settings?.Version ?? MSBuildVersion.VS2017;
                var platform = settings?.Platform ?? (BuildEnvironment.Is64Bit() ? MSBuildPlatform.X64 : MSBuildPlatform.X32);

                (string Year, string Version) getPathParts()
                {
                    if (version == MSBuildVersion.VS2017)
                    {
                        return("2017", "15.0");
                    }
                    else if (version == MSBuildVersion.VS2019)
                    {
                        return("2019", "Current");
                    }
                    else
                    {
                        throw new ArgumentException("Invalid version.", nameof(version));
                    }
                }

                var parts = getPathParts();
                foreach (var edition in new[] { "Enterprise", "Professional", "Community", "BuildTools", "Preview" })
                {
                    var msbuildPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86),
                                                   "Microsoft Visual Studio", parts.Year, edition, "MSBuild", parts.Version, "Bin", platform == MSBuildPlatform.X64 ? "amd64" : "", "MSBuild.exe");
                    if (File.Exists(msbuildPath))
                    {
                        return(msbuildPath);
                    }
                }
            }

            throw new InvalidOperationException("MSBuild not found.");
        }
Пример #2
0
        /// <summary>
        /// Gets the path of MSBuild for the specified version.
        /// </summary>
        public static string GetMSBuildPath(MSBuildSettings?settings)
        {
            if (settings?.MSBuildPath is string settingsPath)
            {
                return(settingsPath);
            }

            if (BuildEnvironment.IsMacOS())
            {
                const string msbuildPath = "/Library/Frameworks/Mono.framework/Versions/Current/Commands/msbuild";
                if (File.Exists(msbuildPath))
                {
                    return(msbuildPath);
                }
            }
            else if (BuildEnvironment.IsLinux())
            {
                const string msbuildPath = "/usr/bin/msbuild";
                if (File.Exists(msbuildPath))
                {
                    return(msbuildPath);
                }
            }
            else
            {
                var version  = settings?.Version ?? MSBuildVersion.VS2017;
                var platform = settings?.Platform ?? (BuildEnvironment.Is64Bit() ? MSBuildPlatform.X64 : MSBuildPlatform.X32);

                var(pathYear, pathVersion) = GetPathParts();
                foreach (var edition in new[] { "Enterprise", "Professional", "Community", "BuildTools", "Preview" })
                {
                    var msbuildPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86),
                                                   "Microsoft Visual Studio", pathYear, edition, "MSBuild", pathVersion, "Bin", platform == MSBuildPlatform.X64 ? "amd64" : "", "MSBuild.exe");
                    if (File.Exists(msbuildPath))
                    {
                        return(msbuildPath);
                    }
                }

                (string Year, string Version) GetPathParts()
                {
                    return(version switch
                    {
                        MSBuildVersion.VS2017 => ("2017", "15.0"),
                        MSBuildVersion.VS2019 => ("2019", "Current"),
                        _ => throw new BuildException($"Unexpected MSBuildVersion: {version}"),
                    });