From b0b4c0abf0724dae6b0791de20c7c643b4077eb6 Mon Sep 17 00:00:00 2001 From: Viktor Hofer Date: Sun, 1 Aug 2021 13:40:15 +0200 Subject: [PATCH] Move VerifyClosure tasks into M.D.PackageTesting As we plan to deprecate the M.D.B.Tasks.Packaging project in the future, moving the tasks into the PackageTesting project. Fixes https://github.com/dotnet/arcade/issues/7474 --- ...rosoft.DotNet.Build.Tasks.Packaging.csproj | 4 +-- .../src/build/Packaging.common.targets | 2 -- ...crosoft.DotNet.PackageTesting.Tests.csproj | 3 +- .../GetCompatiblePackageTargetFrameworks.cs | 26 +++++++++----- .../Microsoft.DotNet.PackageTesting.csproj | 1 - .../NupkgParser.cs | 13 ++++--- .../Package.cs | 10 +++--- .../VerifyClosure.cs | 7 ++-- .../VerifyTypes.cs | 7 ++-- .../Microsoft.DotNet.PackageTesting.props | 4 +++ ...icrosoft.DotNet.SharedFramework.Sdk.csproj | 4 +-- .../src/Packaging/Extensions.cs | 36 ------------------- 12 files changed, 48 insertions(+), 69 deletions(-) rename src/{Microsoft.DotNet.Build.Tasks.Packaging/src => Microsoft.DotNet.PackageTesting}/VerifyClosure.cs (98%) rename src/{Microsoft.DotNet.Build.Tasks.Packaging/src => Microsoft.DotNet.PackageTesting}/VerifyTypes.cs (97%) delete mode 100644 src/Microsoft.DotNet.SharedFramework.Sdk/src/Packaging/Extensions.cs diff --git a/src/Microsoft.DotNet.Build.Tasks.Packaging/src/Microsoft.DotNet.Build.Tasks.Packaging.csproj b/src/Microsoft.DotNet.Build.Tasks.Packaging/src/Microsoft.DotNet.Build.Tasks.Packaging.csproj index 31562d593436..7b4feb8805a7 100644 --- a/src/Microsoft.DotNet.Build.Tasks.Packaging/src/Microsoft.DotNet.Build.Tasks.Packaging.csproj +++ b/src/Microsoft.DotNet.Build.Tasks.Packaging/src/Microsoft.DotNet.Build.Tasks.Packaging.csproj @@ -38,9 +38,7 @@ - SplitDependenciesBySupport - SplitReferences - UpdatePackageIndex -- ValidationTask -- VerifyClosure -- VerifyTypes +- ValidationTask **/*.Desktop.* $(BeforePack);AddRuntimeJson diff --git a/src/Microsoft.DotNet.Build.Tasks.Packaging/src/build/Packaging.common.targets b/src/Microsoft.DotNet.Build.Tasks.Packaging/src/build/Packaging.common.targets index 71ea85006096..c9a95a0ad95c 100644 --- a/src/Microsoft.DotNet.Build.Tasks.Packaging/src/build/Packaging.common.targets +++ b/src/Microsoft.DotNet.Build.Tasks.Packaging/src/build/Packaging.common.targets @@ -44,7 +44,5 @@ - - diff --git a/src/Microsoft.DotNet.PackageTesting.Tests/Microsoft.DotNet.PackageTesting.Tests.csproj b/src/Microsoft.DotNet.PackageTesting.Tests/Microsoft.DotNet.PackageTesting.Tests.csproj index fb54391f1509..ff97a84d42f0 100644 --- a/src/Microsoft.DotNet.PackageTesting.Tests/Microsoft.DotNet.PackageTesting.Tests.csproj +++ b/src/Microsoft.DotNet.PackageTesting.Tests/Microsoft.DotNet.PackageTesting.Tests.csproj @@ -6,9 +6,10 @@ - + + diff --git a/src/Microsoft.DotNet.PackageTesting/GetCompatiblePackageTargetFrameworks.cs b/src/Microsoft.DotNet.PackageTesting/GetCompatiblePackageTargetFrameworks.cs index ff00124037e3..4fa94bfe4c80 100644 --- a/src/Microsoft.DotNet.PackageTesting/GetCompatiblePackageTargetFrameworks.cs +++ b/src/Microsoft.DotNet.PackageTesting/GetCompatiblePackageTargetFrameworks.cs @@ -13,7 +13,7 @@ namespace Microsoft.DotNet.PackageTesting { public class GetCompatiblePackageTargetFrameworks : BuildTask { - private static List allTargetFrameworks = allTargetFrameworks = new(); + private static List allTargetFrameworks = new(); private static Dictionary> packageTfmMapping = new(); [Required] @@ -27,12 +27,15 @@ public class GetCompatiblePackageTargetFrameworks : BuildTask public override bool Execute() { - bool result = true; - List testProjects = new List(); + List testProjects = new(); + try { Initialize(SupportedTestFrameworks); - string minDotnetTargetFramework = allTargetFrameworks.Where(t => t.Framework == ".NETCoreApp").OrderBy(t => t.Version).FirstOrDefault()?.GetShortFolderName(); + string minDotnetTargetFramework = allTargetFrameworks.Where(t => t.Framework == ".NETCoreApp") + .OrderBy(t => t.Version) + .FirstOrDefault()? + .GetShortFolderName(); foreach (var packagePath in PackagePaths) { @@ -50,25 +53,30 @@ public override bool Execute() Log.LogErrorFromException(e, showStackTrace: false); } - return result && !Log.HasLoggedErrors; + return !Log.HasLoggedErrors; } public static IEnumerable GetTestFrameworks(Package package, string minDotnetTargetFramework) { - List frameworksToTest= new List(); + List frameworksToTest= new(); IEnumerable packageTargetFrameworks = package.FrameworksInPackage; // Testing the package installation on all tfms linked with package targetframeworks. foreach (var item in packageTargetFrameworks) { if (packageTfmMapping.ContainsKey(item)) + { frameworksToTest.AddRange(packageTfmMapping[item]); + } + // Adding the frameworks in the packages to the test matrix. frameworksToTest.Add(item); } if (!string.IsNullOrEmpty(minDotnetTargetFramework) && frameworksToTest.Any(t => t.Framework == ".NETStandard")) + { frameworksToTest.Add(NuGetFramework.Parse(minDotnetTargetFramework)); + } return frameworksToTest.Where(tfm => allTargetFrameworks.Contains(tfm)).Distinct(); } @@ -97,12 +105,12 @@ public static void Initialize(string targetFrameworks) } } - public List CreateItemFromTestFramework(string packageId, string version, IEnumerable testFrameworks) + private static List CreateItemFromTestFramework(string packageId, string version, IEnumerable testFrameworks) { - List testprojects = new List(); + List testprojects = new(); foreach (var framework in testFrameworks) { - var supportedPackage = new TaskItem(packageId); + TaskItem supportedPackage = new(packageId); supportedPackage.SetMetadata("Version", version); supportedPackage.SetMetadata("TargetFramework", framework.ToString()); supportedPackage.SetMetadata("TargetFrameworkShort", framework.GetShortFolderName()); diff --git a/src/Microsoft.DotNet.PackageTesting/Microsoft.DotNet.PackageTesting.csproj b/src/Microsoft.DotNet.PackageTesting/Microsoft.DotNet.PackageTesting.csproj index 2f35e4aa96f8..ef713a0f1541 100644 --- a/src/Microsoft.DotNet.PackageTesting/Microsoft.DotNet.PackageTesting.csproj +++ b/src/Microsoft.DotNet.PackageTesting/Microsoft.DotNet.PackageTesting.csproj @@ -2,7 +2,6 @@ netcoreapp3.1;net472 - false MSBuildSdk false true diff --git a/src/Microsoft.DotNet.PackageTesting/NupkgParser.cs b/src/Microsoft.DotNet.PackageTesting/NupkgParser.cs index 919041292dce..d2d94dd9790a 100644 --- a/src/Microsoft.DotNet.PackageTesting/NupkgParser.cs +++ b/src/Microsoft.DotNet.PackageTesting/NupkgParser.cs @@ -2,24 +2,29 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Linq; +using System.Collections.Generic; using NuGet.Frameworks; using NuGet.Packaging; namespace Microsoft.DotNet.PackageTesting { - public class NupkgParser + class NupkgParser { public static Package CreatePackageObject(string packagePath) { - PackageArchiveReader nupkgReader = new PackageArchiveReader(packagePath); + PackageArchiveReader nupkgReader = new(packagePath); NuspecReader nuspecReader = nupkgReader.NuspecReader; string packageId = nuspecReader.GetId(); string version = nuspecReader.GetVersion().ToString(); - NuGetFramework[] dependencyFrameworks = nuspecReader.GetDependencyGroups().Select(dg => dg.TargetFramework).Where(tfm => tfm != null).ToArray(); + NuGetFramework[] dependencyFrameworks = nuspecReader.GetDependencyGroups() + .Select(dg => dg.TargetFramework) + .Where(tfm => tfm != null) + .ToArray(); + IEnumerable files = nupkgReader.GetFiles()?.Where(t => t.EndsWith(packageId + ".dll")); - return new Package(packageId, version, nupkgReader.GetFiles()?.Where(t => t.EndsWith(packageId + ".dll")), dependencyFrameworks); + return new Package(packageId, version, files, dependencyFrameworks); } } } diff --git a/src/Microsoft.DotNet.PackageTesting/Package.cs b/src/Microsoft.DotNet.PackageTesting/Package.cs index 303d4713b76c..3c6b7a1b845d 100644 --- a/src/Microsoft.DotNet.PackageTesting/Package.cs +++ b/src/Microsoft.DotNet.PackageTesting/Package.cs @@ -11,6 +11,10 @@ namespace Microsoft.DotNet.PackageTesting { public class Package { + public IEnumerable FrameworksInPackage { get; } + public string PackageId { get; } + public string Version { get; } + public Package(string packageId, string version, IEnumerable packageAssetPaths, IEnumerable dependencyFrameworks) { PackageId = packageId; @@ -18,7 +22,7 @@ public Package(string packageId, string version, IEnumerable packageAsse ContentItemCollection packageAssets = new(); packageAssets.Load(packageAssetPaths); - ManagedCodeConventions conventions = new ManagedCodeConventions(null); + ManagedCodeConventions conventions = new(null); IEnumerable RefAssets = packageAssets.FindItems(conventions.Patterns.CompileRefAssemblies); IEnumerable LibAssets = packageAssets.FindItems(conventions.Patterns.CompileLibAssemblies); @@ -30,9 +34,5 @@ public Package(string packageId, string version, IEnumerable packageAsse FrameworksInPackageList.AddRange(dependencyFrameworks); FrameworksInPackage = FrameworksInPackageList.Distinct(); } - - public string PackageId { get; set; } - public string Version { get; set; } - public IEnumerable FrameworksInPackage { get; private set; } } } diff --git a/src/Microsoft.DotNet.Build.Tasks.Packaging/src/VerifyClosure.cs b/src/Microsoft.DotNet.PackageTesting/VerifyClosure.cs similarity index 98% rename from src/Microsoft.DotNet.Build.Tasks.Packaging/src/VerifyClosure.cs rename to src/Microsoft.DotNet.PackageTesting/VerifyClosure.cs index 8182b27a9853..5b088259db17 100644 --- a/src/Microsoft.DotNet.Build.Tasks.Packaging/src/VerifyClosure.cs +++ b/src/Microsoft.DotNet.PackageTesting/VerifyClosure.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using Microsoft.Build.Framework; +using Microsoft.DotNet.Build.Tasks; using System; using System.Collections.Generic; using System.IO; @@ -12,7 +13,7 @@ using System.Text; using System.Xml.Linq; -namespace Microsoft.DotNet.Build.Tasks.Packaging +namespace Microsoft.DotNet.PackageTesting { /// /// Verifies the closure of a set of DLLs, making sure all files are present and no cycles exist @@ -125,7 +126,7 @@ private void AddSourceFile(string file) private void LoadIgnoredReferences() { - foreach (var ignoredReference in IgnoredReferences.NullAsEmpty()) + foreach (var ignoredReference in IgnoredReferences.DefaultIfEmpty()) { var name = ignoredReference.ItemSpec; var versionString = ignoredReference.GetMetadata("Version"); @@ -175,7 +176,7 @@ void CheckDependencies(Stack depStack) // check module references if (assm.State == CheckState.Unchecked && CheckModuleReferences) { - foreach(var moduleReference in assm.ModuleReferences.NullAsEmpty()) + foreach(var moduleReference in assm.ModuleReferences.DefaultIfEmpty()) { if (ShouldIgnore(moduleReference)) { diff --git a/src/Microsoft.DotNet.Build.Tasks.Packaging/src/VerifyTypes.cs b/src/Microsoft.DotNet.PackageTesting/VerifyTypes.cs similarity index 97% rename from src/Microsoft.DotNet.Build.Tasks.Packaging/src/VerifyTypes.cs rename to src/Microsoft.DotNet.PackageTesting/VerifyTypes.cs index 02e8b16a1605..7fd15a1815e3 100644 --- a/src/Microsoft.DotNet.Build.Tasks.Packaging/src/VerifyTypes.cs +++ b/src/Microsoft.DotNet.PackageTesting/VerifyTypes.cs @@ -2,21 +2,22 @@ // The .NET Foundation licenses this file to you under the MIT license. using Microsoft.Build.Framework; +using Microsoft.DotNet.Build.Tasks; using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Reflection; using System.Reflection.Metadata; using System.Reflection.PortableExecutable; -namespace Microsoft.DotNet.Build.Tasks.Packaging +namespace Microsoft.DotNet.PackageTesting { /// /// Verifies no type overlap in a set of DLLs /// public class VerifyTypes : BuildTask { - /// /// Sources to scan. Items can be directories or files. /// @@ -124,7 +125,7 @@ private void AddSourceFile(string file) private void LoadIgnoredTypes() { - foreach(var ignoredType in IgnoredTypes.NullAsEmpty()) + foreach(var ignoredType in IgnoredTypes.DefaultIfEmpty()) { ignoredTypes.Add(ignoredType.ItemSpec); } diff --git a/src/Microsoft.DotNet.PackageTesting/build/Microsoft.DotNet.PackageTesting.props b/src/Microsoft.DotNet.PackageTesting/build/Microsoft.DotNet.PackageTesting.props index 5ecea08caeb5..f568f711c5e0 100644 --- a/src/Microsoft.DotNet.PackageTesting/build/Microsoft.DotNet.PackageTesting.props +++ b/src/Microsoft.DotNet.PackageTesting/build/Microsoft.DotNet.PackageTesting.props @@ -4,6 +4,10 @@ $(MSBuildThisFileDirectory)..\tools\netcoreapp3.1\Microsoft.DotNet.PackageTesting.dll + + + + diff --git a/src/Microsoft.DotNet.SharedFramework.Sdk/Microsoft.DotNet.SharedFramework.Sdk.csproj b/src/Microsoft.DotNet.SharedFramework.Sdk/Microsoft.DotNet.SharedFramework.Sdk.csproj index 31df0418e14f..e1d14cb98c5b 100644 --- a/src/Microsoft.DotNet.SharedFramework.Sdk/Microsoft.DotNet.SharedFramework.Sdk.csproj +++ b/src/Microsoft.DotNet.SharedFramework.Sdk/Microsoft.DotNet.SharedFramework.Sdk.csproj @@ -41,8 +41,8 @@ - - + + diff --git a/src/Microsoft.DotNet.SharedFramework.Sdk/src/Packaging/Extensions.cs b/src/Microsoft.DotNet.SharedFramework.Sdk/src/Packaging/Extensions.cs deleted file mode 100644 index be63e1cb5a5d..000000000000 --- a/src/Microsoft.DotNet.SharedFramework.Sdk/src/Packaging/Extensions.cs +++ /dev/null @@ -1,36 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using Microsoft.Build.Framework; -using NuGet; -using NuGet.Frameworks; -using NuGet.Versioning; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Linq.Expressions; -using System.Reflection; -using System.Runtime.Versioning; - -// This file implements a subset of the Extensions class in the -// Microsoft.DotNet.Build.Tasks.Packaging project to support -// the Shared Framework SDK's usage of the VerifyClosure and -// VerifyTypes tasks used in shared framework validation. - -namespace Microsoft.DotNet.Build.Tasks.Packaging -{ - public static class Extensions - { - public static IEnumerable NullAsEmpty(this IEnumerable source) - { - if (source == null) - { - return Enumerable.Empty(); - } - - return source; - } - } -} -