|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using Microsoft.DotNet.ApiCompatibility; |
| 8 | +using Microsoft.DotNet.ApiCompatibility.Abstractions; |
| 9 | +using NuGet.Common; |
| 10 | +using NuGet.ContentModel; |
| 11 | +using NuGet.Frameworks; |
| 12 | + |
| 13 | +namespace Microsoft.DotNet.PackageValidation |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// Validates that no target framework / rid support is dropped in the latest package. |
| 17 | + /// Reports all the breaking changes in the latest package. |
| 18 | + /// </summary> |
| 19 | + public class BaselinePackageValidator |
| 20 | + { |
| 21 | + private static HashSet<string> s_diagList = new HashSet<string>{ DiagnosticIds.TargetFrameworkDropped, DiagnosticIds.TargetFrameworkAndRidPairDropped }; |
| 22 | + |
| 23 | + private readonly Package _baselinePackage; |
| 24 | + private readonly bool _runApiCompat; |
| 25 | + private readonly ApiCompatRunner _apiCompatRunner; |
| 26 | + private readonly ILogger _log; |
| 27 | + private readonly DiagnosticBag<IDiagnostic> _diagnosticBag; |
| 28 | + |
| 29 | + public BaselinePackageValidator(Package baselinePackage, string noWarn, (string, string)[] ignoredDifferences, bool runApiCompat, ILogger log) |
| 30 | + { |
| 31 | + _baselinePackage = baselinePackage; |
| 32 | + _runApiCompat = runApiCompat; |
| 33 | + _log = log; |
| 34 | + _apiCompatRunner = new(noWarn, ignoredDifferences, _log); |
| 35 | + _diagnosticBag = new(noWarn?.Split(';')?.Where(t => s_diagList.Contains(t)), ignoredDifferences); |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Validates the latest nuget package doesnot drop any target framework/rid and does not introduce any breaking changes. |
| 40 | + /// </summary> |
| 41 | + /// <param name="package">Nuget Package that needs to be validated.</param> |
| 42 | + public void Validate(Package package) |
| 43 | + { |
| 44 | + foreach (ContentItem baselineCompileTimeAsset in _baselinePackage.CompileAssets) |
| 45 | + { |
| 46 | + NuGetFramework baselineTargetFramework = (NuGetFramework)baselineCompileTimeAsset.Properties["tfm"]; |
| 47 | + ContentItem latestCompileTimeAsset = package.FindBestCompileAssetForFramework(baselineTargetFramework); |
| 48 | + if (latestCompileTimeAsset == null) |
| 49 | + { |
| 50 | + if (!_diagnosticBag.Filter(DiagnosticIds.TargetFrameworkDropped, baselineTargetFramework.ToString())) |
| 51 | + { |
| 52 | + string message = string.Format(Resources.MissingTargetFramework, baselineTargetFramework.ToString()); |
| 53 | + _log.LogError(DiagnosticIds.TargetFrameworkDropped + " " + message); |
| 54 | + } |
| 55 | + } |
| 56 | + else if (_runApiCompat) |
| 57 | + { |
| 58 | + _apiCompatRunner.QueueApiCompat(_baselinePackage.PackagePath, |
| 59 | + baselineCompileTimeAsset.Path, |
| 60 | + package.PackagePath, |
| 61 | + latestCompileTimeAsset.Path, |
| 62 | + Path.GetFileName(package.PackagePath), |
| 63 | + Resources.BaselineVersionValidatorHeader, |
| 64 | + string.Format(Resources.ApiCompatibilityHeader, baselineCompileTimeAsset.Path, latestCompileTimeAsset.Path)); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + foreach (ContentItem baselineRuntimeAsset in _baselinePackage.RuntimeAssets) |
| 69 | + { |
| 70 | + NuGetFramework baselineTargetFramework = (NuGetFramework)baselineRuntimeAsset.Properties["tfm"]; |
| 71 | + ContentItem latestRuntimeAsset = package.FindBestRuntimeAssetForFramework(baselineTargetFramework); |
| 72 | + if (latestRuntimeAsset == null) |
| 73 | + { |
| 74 | + if (!_diagnosticBag.Filter(DiagnosticIds.TargetFrameworkDropped, baselineTargetFramework.ToString())) |
| 75 | + { |
| 76 | + string message = string.Format(Resources.MissingTargetFramework, baselineTargetFramework.ToString()); |
| 77 | + _log.LogError(DiagnosticIds.TargetFrameworkDropped + " " + message); |
| 78 | + } |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + if (_runApiCompat) |
| 83 | + { |
| 84 | + _apiCompatRunner.QueueApiCompat(_baselinePackage.PackagePath, |
| 85 | + baselineRuntimeAsset.Path, |
| 86 | + package.PackagePath, |
| 87 | + latestRuntimeAsset.Path, |
| 88 | + Path.GetFileName(package.PackagePath), |
| 89 | + Resources.BaselineVersionValidatorHeader, |
| 90 | + string.Format(Resources.ApiCompatibilityHeader, baselineRuntimeAsset.Path, latestRuntimeAsset.Path)); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + foreach (ContentItem baselineRuntimeSpecificAsset in _baselinePackage.RuntimeSpecificAssets) |
| 96 | + { |
| 97 | + NuGetFramework baselineTargetFramework = (NuGetFramework)baselineRuntimeSpecificAsset.Properties["tfm"]; |
| 98 | + string baselineRid = (string)baselineRuntimeSpecificAsset.Properties["rid"]; |
| 99 | + ContentItem latestRuntimeSpecificAsset = package.FindBestRuntimeAssetForFrameworkAndRuntime(baselineTargetFramework, baselineRid); |
| 100 | + if (latestRuntimeSpecificAsset == null) |
| 101 | + { |
| 102 | + if (!_diagnosticBag.Filter(DiagnosticIds.TargetFrameworkDropped, baselineTargetFramework.ToString() + "-" + baselineRid)) |
| 103 | + { |
| 104 | + string message = string.Format(Resources.MissingTargetFrameworkAndRid, baselineTargetFramework.ToString(), baselineRid); |
| 105 | + _log.LogError(DiagnosticIds.TargetFrameworkAndRidPairDropped + " " + message); |
| 106 | + } |
| 107 | + } |
| 108 | + else |
| 109 | + { |
| 110 | + if (_runApiCompat) |
| 111 | + { |
| 112 | + _apiCompatRunner.QueueApiCompat(_baselinePackage.PackagePath, |
| 113 | + baselineRuntimeSpecificAsset.Path, |
| 114 | + package.PackagePath, |
| 115 | + latestRuntimeSpecificAsset.Path, |
| 116 | + Path.GetFileName(package.PackagePath), |
| 117 | + Resources.BaselineVersionValidatorHeader, |
| 118 | + string.Format(Resources.ApiCompatibilityHeader, baselineRuntimeSpecificAsset.Path, latestRuntimeSpecificAsset.Path)); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + _apiCompatRunner.RunApiCompat(); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments