public static DevelopmentEnvironment LoadCurrentDevelopmentEnvironment()

in src/Shared/DevelopmentEnvironment.cs [75:147]


        public static DevelopmentEnvironment LoadCurrentDevelopmentEnvironment()
        {
            string msbuildToolset = Environment.GetEnvironmentVariable("MSBuildToolset")?.Trim();

            if (!msbuildToolset.IsNullOrWhiteSpace())
            {
                string msbuildToolsPath = Environment.GetEnvironmentVariable($"MSBuildToolsPath_{msbuildToolset}")?.Trim();

                if (!msbuildToolsPath.IsNullOrWhiteSpace())
                {
#if NETFRAMEWORK
                    if (!Version.TryParse(Environment.GetEnvironmentVariable("VisualStudioVersion") ?? string.Empty, out Version visualStudioVersion))
                    {
                        return new DevelopmentEnvironment("The VisualStudioVersion environment variable must be set in CoreXT");
                    }

                    if (visualStudioVersion.Major <= 14)
                    {
                        return new DevelopmentEnvironment("MSBuild.Corext version 15.0 or greater is required");
                    }

                    return new DevelopmentEnvironment
                    {
                        MSBuildExe = new FileInfo(Path.Combine(msbuildToolsPath!, "MSBuild.exe")),
                        IsCorext = true,
                        VisualStudio = VisualStudioConfiguration.GetLaunchableInstances()
                            .Where(i => !i.IsBuildTools && i.HasMSBuild && i.InstallationVersion.Major == visualStudioVersion.Major)
                            .OrderByDescending(i => i.InstallationVersion)
                            .FirstOrDefault(),
                    };
#else
                    return new DevelopmentEnvironment("The .NET Core version of SlnGen is not supported in CoreXT.  You must use the .NET Framework version via the SlnGen.Corext package");
#endif
                }
            }
#if NETFRAMEWORK
            if (Utility.TryFindOnPath("MSBuild.exe", IsMSBuildExeCompatible, out FileInfo msbuildExeFileInfo))
            {
                return new DevelopmentEnvironment
                {
                    MSBuildExe = GetPathToMSBuildExe(msbuildExeFileInfo),
                    VisualStudio = VisualStudioConfiguration.GetInstanceForPath(msbuildExeFileInfo.FullName),
                };
            }

            return new DevelopmentEnvironment("SlnGen must be run from a command-line window where MSBuild.exe is on the PATH.");
#else
            if (!Utility.TryFindOnPath(Utility.RunningOnWindows ? "dotnet.exe" : "dotnet", null, out FileInfo dotnetFileInfo))
            {
                return new DevelopmentEnvironment("SlnGen must be run from a command-line window where dotnet.exe is on the PATH.");
            }

            if (!DotNetCoreSdkResolver.TryResolveDotNetCoreSdk(dotnetFileInfo, out DirectoryInfo dotnetCoreSdkDirectoryInfo))
            {
                return new DevelopmentEnvironment(string.Empty);
            }

            DevelopmentEnvironment developmentEnvironment = new DevelopmentEnvironment
            {
                DotNetSdkVersion = dotnetCoreSdkDirectoryInfo.Name,
                DotNetSdkMajorVersion = dotnetCoreSdkDirectoryInfo.Name.Substring(0, dotnetCoreSdkDirectoryInfo.Name.IndexOf(".", StringComparison.OrdinalIgnoreCase)),
                MSBuildDll = new FileInfo(Path.Combine(dotnetCoreSdkDirectoryInfo.FullName, "MSBuild.dll")),
            };

            if (Utility.RunningOnWindows && Utility.TryFindOnPath("MSBuild.exe", IsMSBuildExeCompatible, out FileInfo msbuildExeFileInfo))
            {
                developmentEnvironment.MSBuildExe = GetPathToMSBuildExe(msbuildExeFileInfo);
                developmentEnvironment.VisualStudio = VisualStudioConfiguration.GetInstanceForPath(msbuildExeFileInfo.FullName);
            }

            return developmentEnvironment;
#endif
        }