/// <summary> /// Walks up the directory tree to find the first global.json and reads the msbuild-sdks section. /// </summary> /// <returns>A <see cref="Dictionary{String,String}"/> of MSBuild SDK versions from a global.json if found, otherwise <code>null</code>.</returns> public static Dictionary <string, string> GetMSBuildSdkVersions(SdkResolverContextBase context) { DirectoryInfo projectDirectory = Directory.GetParent(context.ProjectFilePath); string globalJsonPath; if (projectDirectory == null || !projectDirectory.Exists || !FileUtilities.TryGetPathOfFileAbove(GlobalJsonFileName, projectDirectory.FullName, out globalJsonPath)) { return(null); } string contents = File.ReadAllText(globalJsonPath); // Look ahead in the contents to see if there is an msbuild-sdks section. Deserializing the file requires us to load // Newtonsoft.Json which is 500 KB while a global.json is usually ~100 bytes of text. if (contents.IndexOf(MSBuildSdksPropertyName, StringComparison.Ordinal) == -1) { return(null); } try { return(Deserialize(contents)); } catch (Exception e) { // Failed to parse "{0}". {1} string message = ResourceUtilities.FormatResourceString("FailedToParseGlobalJson", globalJsonPath, e.Message); context.Logger.LogMessage(message); return(null); } }
public override SdkResultBase Resolve(SdkReference sdk, SdkResolverContextBase context, SdkResultFactoryBase factory) { // Escape hatch to disable this resolver if (Traits.Instance.EscapeHatches.DisableNuGetSdkResolver) { return(null); } #if FEATURE_APPDOMAIN AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolve; #else AssemblyLoadContext.Default.Resolving += AssemblyResolve; #endif try { return(ResolveSdk(sdk, context, factory)); } finally { #if FEATURE_APPDOMAIN AppDomain.CurrentDomain.AssemblyResolve -= AssemblyResolve; #else AssemblyLoadContext.Default.Resolving -= AssemblyResolve; #endif } }
public abstract Microsoft.Build.Framework.SdkResult Resolve(Microsoft.Build.Framework.SdkReference sdkReference, Microsoft.Build.Framework.SdkResolverContext resolverContext, Microsoft.Build.Framework.SdkResultFactory factory);
private void VerifyTryGetNuGetVersionForSdk(string version, NuGetVersion expectedVersion, SdkResolverContextBase context = null) { var result = NuGetSdkResolver.TryGetNuGetVersionForSdk("foo", version, context, out var parsedVersion); if (expectedVersion != null) { result.Should().BeTrue(); parsedVersion.Should().NotBeNull(); parsedVersion.Should().Be(expectedVersion); } else { result.Should().BeFalse(); parsedVersion.Should().BeNull(); } }
protected abstract SdkResultBase ResolveSdk(SdkReference sdk, SdkResolverContextBase context, SdkResultFactoryBase factory);