/// <summary> /// Attempts to determine what version of an SDK to resolve. A project-specific version is used first and then a version specified in a global.json. /// This method should not consume any NuGet classes directly to avoid loading additional assemblies when they are not needed. This method /// returns an object so that NuGetVersion is not consumed directly. /// </summary> internal bool TryGetNuGetVersionForSdk(string id, string version, SdkResolverContext context, out object parsedVersion) { if (!string.IsNullOrWhiteSpace(version)) { // Use the version specified in the project if it is a NuGet compatible version return(NuGetAbstraction.TryParseNuGetVersion(version, out parsedVersion)); } parsedVersion = null; // Don't try to find versions defined in global.json if the project full path isn't set because an in-memory project is being evaluated and there's no // way to be sure where to look if (string.IsNullOrWhiteSpace(context?.ProjectFilePath)) { return(false); } Dictionary <string, string> msbuildSdkVersions = _globalJsonReader.GetMSBuildSdkVersions(context); // Check if global.json specified a version for this SDK and make sure its a version compatible with NuGet if (msbuildSdkVersions != null && msbuildSdkVersions.TryGetValue(id, out var globalJsonVersion) && !string.IsNullOrWhiteSpace(globalJsonVersion)) { return(NuGetAbstraction.TryParseNuGetVersion(globalJsonVersion, out parsedVersion)); } return(false); }
protected override SdkResultBase ResolveSdk(SdkReference sdk, SdkResolverContextBase context, SdkResultFactoryBase factory) { object parsedSdkVersion; // This resolver only works if the user specifies a version in a project or a global.json. // Ignore invalid versions, there may be another resolver that can handle the version specified if (!TryGetNuGetVersionForSdk(sdk.Name, sdk.Version, context, out parsedSdkVersion)) { return(null); } return(NuGetAbstraction.GetSdkResult(sdk, parsedSdkVersion, context, factory)); }
/// <summary>Resolves the specified SDK reference from NuGet.</summary> /// <param name="sdkReference">A <see cref="T:Microsoft.Build.Framework.SdkReference" /> containing the referenced SDKs be resolved.</param> /// <param name="resolverContext">Context for resolving the SDK.</param> /// <param name="factory">Factory class to create an <see cref="T:Microsoft.Build.Framework.SdkResult" /></param> /// <returns> /// An <see cref="T:Microsoft.Build.Framework.SdkResult" /> containing the resolved SDKs or associated error / reason /// the SDK could not be resolved. Return <code>null</code> if the resolver is not /// applicable for a particular <see cref="T:Microsoft.Build.Framework.SdkReference" />. /// </returns> public override SdkResult Resolve(SdkReference sdkReference, SdkResolverContext resolverContext, SdkResultFactory factory) { // Escape hatch to disable this resolver if (DisableNuGetSdkResolver.Value) { return(null); } // This resolver only works if the user specifies a version in a project or a global.json. // Ignore invalid versions, there may be another resolver that can handle the version specified if (!TryGetNuGetVersionForSdk(sdkReference.Name, sdkReference.Version, resolverContext, out var parsedSdkVersion)) { return(null); } return(NuGetAbstraction.GetSdkResult(sdkReference, parsedSdkVersion, resolverContext, factory)); }
public override SdkResultBase Resolve(SdkReference sdk, SdkResolverContextBase context, SdkResultFactoryBase factory) { // Escape hatch to disable this resolver if (Traits.Instance.EscapeHatches.DisableNuGetSdkResolver) { return(null); } object parsedSdkVersion; // This resolver only works if the user specifies a version in a project or a global.json. // Ignore invalid versions, there may be another resolver that can handle the version specified if (!TryGetNuGetVersionForSdk(sdk.Name, sdk.Version, context, out parsedSdkVersion)) { return(null); } return(NuGetAbstraction.GetSdkResult(sdk, parsedSdkVersion, context, factory)); }
/// <summary> /// Attempts to determine what version of an SDK to resolve. A project-specific version is used first and then a version specified in a global.json. /// This method should not consume any NuGet classes directly to avoid loading additional assemblies when they are not needed. This method /// returns an object so that NuGetVersion is not consumed directly. /// </summary> internal static bool TryGetNuGetVersionForSdk(string id, string version, SdkResolverContext context, out object parsedVersion) { if (!string.IsNullOrWhiteSpace(version)) { // Use the version specified in the project if it is a NuGet compatible version return(NuGetAbstraction.TryParseNuGetVersion(version, out parsedVersion)); } parsedVersion = null; // Don't try to find versions defined in global.json if the project full path isn't set because an in-memory project is being evaluated and there's no // way to be sure where to look if (string.IsNullOrWhiteSpace(context?.ProjectFilePath)) { return(false); } Dictionary <string, string> msbuildSdkVersions; // Get the SDK versions from a previous state, otherwise find and load global.json to get them if (context.State is Dictionary <string, string> dictionary) { msbuildSdkVersions = dictionary; } else { msbuildSdkVersions = GlobalJsonReader.GetMSBuildSdkVersions(context); // Save the SDK versions in case this resolver is called again for another SDK in the same build context.State = msbuildSdkVersions; } // Check if global.json specified a version for this SDK and make sure its a version compatible with NuGet if (msbuildSdkVersions != null && msbuildSdkVersions.TryGetValue(id, out var globalJsonVersion) && !string.IsNullOrWhiteSpace(globalJsonVersion)) { return(NuGetAbstraction.TryParseNuGetVersion(globalJsonVersion, out parsedVersion)); } return(false); }
/// <summary> /// Attempts to determine what version of an SDK to resolve. A project-specific version is used first and then a version specified in a global.json. /// This method should not consume any NuGet classes directly to avoid loading additional assemblies when they are not needed. This method /// returns an object so that NuGetVersion is not consumed directly. /// </summary> internal static bool TryGetNuGetVersionForSdk(string id, string version, SdkResolverContextBase context, out object parsedVersion) { if (!String.IsNullOrWhiteSpace(version)) { // Use the version specified in the project if it is a NuGet compatible version return(NuGetAbstraction.TryParseNuGetVersion(version, out parsedVersion)); } Dictionary <string, string> msbuildSdkVersions; // Get the SDK versions from a previous state, otherwise find and load global.json to get them if (context.State is Dictionary <string, string> ) { msbuildSdkVersions = (Dictionary <string, string>)context.State; } else { msbuildSdkVersions = GlobalJsonReader.GetMSBuildSdkVersions(context); // Save the SDK versions in case this resolver is called again for another SDK in the same build context.State = msbuildSdkVersions; } string globalJsonVersion; // Check if global.json specified a version for this SDK and make sure its a version compatible with NuGet if (msbuildSdkVersions != null && msbuildSdkVersions.TryGetValue(id, out globalJsonVersion) && !String.IsNullOrWhiteSpace(globalJsonVersion)) { return(NuGetAbstraction.TryParseNuGetVersion(globalJsonVersion, out parsedVersion)); } parsedVersion = null; return(false); }