public override Framework.SdkResult Resolve(SdkReference sdkReference, SdkResolverContext resolverContext, SdkResultFactory factory)
            {
                ResolvedCalls.AddOrUpdate(sdkReference.Name, k => 1, (k, c) => c + 1);

                return(_resultMap.TryGetValue(sdkReference.Name, out var result)
                    ? new SdkResult(sdkReference, result.Path, result.Version, null)
                    : null);
            }
Ejemplo n.º 2
0
        public override SdkResultBase Resolve(SdkReference sdk, SdkResolverContextBase context, SdkResultFactoryBase factory)
        {
            var sdkPath = Path.Combine(BuildEnvironmentHelper.Instance.MSBuildSDKsPath, sdk.Name, "Sdk");

            // Note: On failure MSBuild will log a generic message, no need to indicate a failure reason here.
            return(FileUtilities.DirectoryExistsNoThrow(sdkPath)
                ? factory.IndicateSuccess(sdkPath, string.Empty)
                : factory.IndicateFailure(null));
        }
Ejemplo n.º 3
0
            public override Framework.SdkResult Resolve(SdkReference sdkReference, SdkResolverContext resolverContext, SdkResultFactory factory)
            {
                resolverContext.Logger.LogMessage($"{nameof(resolverContext.ProjectFilePath)} = {resolverContext.ProjectFilePath}", MessageImportance.High);
                resolverContext.Logger.LogMessage($"{nameof(resolverContext.SolutionFilePath)} = {resolverContext.SolutionFilePath}", MessageImportance.High);
                resolverContext.Logger.LogMessage($"{nameof(resolverContext.MSBuildVersion)} = {resolverContext.MSBuildVersion}", MessageImportance.High);

                return(_mapping.ContainsKey(sdkReference.Name)
                    ? factory.IndicateSuccess(_mapping[sdkReference.Name], null)
                    : factory.IndicateFailure(new[] { $"Not in {nameof(_mapping)}" }));
            }
Ejemplo n.º 4
0
 public override SdkResult Resolve(SdkReference sdk, SdkResolverContext resolverContext,
                                   SdkResultFactory factory)
 {
     return(sdk.Name switch
     {
         SdkName when sdk.Version == SdkExpectedVersion =>
         factory.IndicateSuccess(ResolvedPath, SdkExpectedVersion),
         SdkName => factory.IndicateFailure(new[] { ErrorVersion }),
         _ => factory.IndicateFailure(new[] { ErrorName })
     });
Ejemplo n.º 5
0
            public override SdkResultBase Resolve(SdkReference sdk, SdkResolverContextBase resolverContext, SdkResultFactoryBase factory)
            {
                resolverContext.Logger.LogMessage("MockSdkResolver2 running", MessageImportance.Normal);

                if (sdk.Name.StartsWith("2"))
                {
                    return(factory.IndicateSuccess("resolverpath2", "version2", new[] { "WARNING2" }));
                }

                return(factory.IndicateFailure(new[] { "ERROR2" }, new[] { "WARNING2" }));
            }
Ejemplo n.º 6
0
            public override SdkResultBase Resolve(SdkReference sdk, SdkResolverContextBase resolverContext, SdkResultFactoryBase factory)
            {
                resolverContext.Logger.LogMessage("MockSdkResolverWithResolvableSdkPattern1 running", MessageImportance.Normal);

                if (sdk.Name.StartsWith("11"))
                {
                    return(factory.IndicateSuccess("resolverpathwithresolvablesdkpattern1", "version3"));
                }

                return(factory.IndicateFailure(new[] { "ERROR3" }));
            }
Ejemplo n.º 7
0
            public override Framework.SdkResult Resolve(SdkReference sdkReference, SdkResolverContext resolverContext, SdkResultFactory factory)
            {
                if (_resolveFunc != null)
                {
                    return(_resolveFunc(sdkReference, resolverContext, factory));
                }

                ResolvedCalls.AddOrUpdate(sdkReference.Name, k => 1, (k, c) => c + 1);

                return(_resultMap.TryGetValue(sdkReference.Name, out var result)
                    ? CloneSdkResult(result)
                    : null);
            }
Ejemplo n.º 8
0
        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));
        }
Ejemplo n.º 9
0
            public override SdkResultBase Resolve(SdkReference sdkReference, SdkResolverContextBase resolverContext, SdkResultFactoryBase factory)
            {
                if (sdkReference.Name.Equals("notfound"))
                {
                    return(null);
                }
                if (resolverContext.State != null)
                {
                    return(factory.IndicateSuccess((string)resolverContext.State, "1.0"));
                }

                resolverContext.State = Expected;

                return(factory.IndicateSuccess("resolverpath", "1.0"));
            }
Ejemplo n.º 10
0
        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));
        }
Ejemplo n.º 11
0
        /// <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);
        }
Ejemplo n.º 12
0
 public override SdkResultBase Resolve(SdkReference sdkReference, SdkResolverContextBase resolverContext, SdkResultFactoryBase factory)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 13
0
            public static SdkResultBase GetSdkResult(SdkReference sdk, object nuGetVersion, SdkResolverContextBase context, SdkResultFactoryBase factory)
            {
                // Cast the NuGet version since the caller does not want to consume NuGet classes directly
                NuGetVersion parsedSdkVersion = (NuGetVersion)nuGetVersion;

                // Stores errors and warnings for the result
                ICollection <string> errors   = new List <string>();
                ICollection <string> warnings = new List <string>();

                // Load NuGet settings and a path resolver
                ISettings settings = Settings.LoadDefaultSettings(context.ProjectFilePath);

                FallbackPackagePathResolver fallbackPackagePathResolver = new FallbackPackagePathResolver(NuGetPathContext.Create(settings));

                string installedPath, installedVersion;

                // Attempt to find a package if its already installed
                if (!TryGetMSBuildSdkPackageInfo(fallbackPackagePathResolver, sdk.Name, parsedSdkVersion, out installedPath, out installedVersion))
                {
                    try
                    {
                        // Asynchronously run the restore without a commit which find the package on configured feeds, download, and unzip it without generating any other files
                        IReadOnlyList <RestoreResultPair> results = RestoreRunnerEx.RunWithoutCommit(
                            context.ProjectFilePath,
                            sdk.Name,
                            parsedSdkVersion.ToFullString(),
                            settings,
                            new NuGetSdkLogger(context.Logger, warnings, errors))
                                                                    .ConfigureAwait(continueOnCapturedContext: false)
                                                                    .GetAwaiter()
                                                                    .GetResult();

                        fallbackPackagePathResolver = new FallbackPackagePathResolver(NuGetPathContext.Create(settings));

                        // Look for a successful result, any errors are logged by NuGet
                        foreach (RestoreResult result in results.Select(i => i.Result).Where(i => i.Success))
                        {
                            // Find the information about the package that was installed.  In some cases, the version can be different than what was specified (like you specify 1.0 but get 1.0.0)
                            LibraryIdentity installedPackage = result.GetAllInstalled().FirstOrDefault(i => i.Name.Equals(sdk.Name));

                            if (installedPackage != null)
                            {
                                if (!TryGetMSBuildSdkPackageInfo(fallbackPackagePathResolver, installedPackage.Name, installedPackage.Version, out installedPath, out installedVersion))
                                {
                                    // This should never happen because we were told the package was successfully installed.
                                    // If we can't find it, we probably did something wrong with the NuGet API
                                    errors.Add(ResourceUtilities.FormatResourceString("CouldNotFindInstalledPackage", sdk));
                                }
                            }
                            else
                            {
                                // This should never happen because we were told the restore succeeded.
                                // If we can't find the package from GetAllInstalled(), we probably did something wrong with the NuGet API
                                errors.Add(ResourceUtilities.FormatResourceString("PackageWasNotInstalled", sdk, sdk.Name));
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        errors.Add(e.Message);
                    }
                }

                if (errors.Count == 0)
                {
                    return(factory.IndicateSuccess(path: installedPath, version: installedVersion, warnings: warnings));
                }

                return(factory.IndicateFailure(errors, warnings));
            }
Ejemplo n.º 14
0
            public override SdkResultBase Resolve(SdkReference sdk, SdkResolverContextBase resolverContext, SdkResultFactoryBase factory)
            {
                resolverContext.Logger.LogMessage("MockSdkResolverThrows running", MessageImportance.Normal);

                throw new ArithmeticException("EXMESSAGE");
            }
Ejemplo n.º 15
0
 public override SdkResultBase Resolve(SdkReference sdkReference, SdkResolverContextBase resolverContext, SdkResultFactoryBase factory) => null;