Ejemplo n.º 1
0
        internal override string ResolveMSBuild1670Dir()
        {
            string        programFiles = SystemEnvironment.GetFolderPath(SystemEnvironment.SpecialFolder.ProgramFilesX86);
            List <string> probPaths    = new List <string>
            {
                Path.Combine(programFiles, "Microsoft Visual Studio", "2019", "Enterprise", "MSBuild", "Current", "Bin"),   // visual studio Enterprise
                Path.Combine(programFiles, "Microsoft Visual Studio", "2019", "Professional", "MSBuild", "Current", "Bin"), // visual studio Professional
                Path.Combine(programFiles, "Microsoft Visual Studio", "2019", "Community", "MSBuild", "Current", "Bin"),    // visual studio Community
                Path.Combine(programFiles, "Microsoft Visual Studio", "2019", "BuildTools", "MSBuild", "Current", "Bin"),   // msbuild tools
                // above is for public kudu, below is for azure
            };

            if (FileSystemHelpers.DirectoryExists(Path.Combine(programFiles, "MSBuilds")))
            {
                // Iterate through MSbuild versions inside Program Files (x86)\MSBuilds and fetch the latest one
                string          latestMsBuildStr = null;
                SemanticVersion latestMsBuild    = null;
                foreach (string msBuild in FileSystemHelpers.GetDirectories(Path.Combine(programFiles, "MSBuilds")))
                {
                    var versionStr = Path.GetFileName(msBuild);
                    if (SemanticVersion.TryParse(versionStr, out SemanticVersion currVersion))
                    {
                        string installedFilePath = Path.Combine(programFiles, "MSBuilds", $"{versionStr}.installed");
                        if (FileSystemHelpers.FileExists(installedFilePath) &&
                            (latestMsBuild == null || currVersion.CompareTo(latestMsBuild) > 0))
                        {
                            latestMsBuildStr = versionStr;
                            latestMsBuild    = currVersion;
                        }
                    }
                }

                var pinnedVersion           = VsHelper.MSBuildVersion;
                var pinnedPath              = string.IsNullOrEmpty(pinnedVersion) ? null : Path.Combine(programFiles, "MSBuilds", pinnedVersion, "MSBuild", "Current", "Bin");
                var pinnedInstalledFilePath = string.IsNullOrEmpty(pinnedVersion) ? null : Path.Combine(programFiles, "MSBuilds", $"{pinnedVersion}.installed");
                if (!string.IsNullOrEmpty(pinnedInstalledFilePath) &&
                    FileSystemHelpers.DirectoryExists(pinnedPath) &&
                    FileSystemHelpers.FileExists(pinnedInstalledFilePath))
                {
                    probPaths.Add(pinnedPath);
                }
                else if (!string.IsNullOrEmpty(latestMsBuildStr))
                {
                    string latestMsBuildPath = Path.Combine(programFiles, "MSBuilds", latestMsBuildStr, "MSBuild", "Current", "Bin");
                    if (FileSystemHelpers.DirectoryExists(latestMsBuildPath))
                    {
                        probPaths.Add(latestMsBuildPath);
                    }
                }
            }

            // TODO: Remove this after ANT93
            // Fallback to 16.8.0 if present
            probPaths.Add(Path.Combine(programFiles, "MSBuild-16.8.0", "MSBuild", "Current", "Bin"));

            return(probPaths.FirstOrDefault(path => FileSystemHelpers.DirectoryExists(path)));
        }