CheckExpectedPackagesExist.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using Microsoft.Build.Framework;
  9. using NuGet.Packaging;
  10. using NuGet.Packaging.Core;
  11. using RepoTasks.Utilities;
  12. namespace RepoTasks
  13. {
  14. public class CheckExpectedPackagesExist : Microsoft.Build.Utilities.Task
  15. {
  16. /// <summary>
  17. /// The item group containing the nuget packages to split in different folders.
  18. /// </summary>
  19. [Required]
  20. public ITaskItem[] Packages { get; set; }
  21. [Required]
  22. public ITaskItem[] Files { get; set; }
  23. public override bool Execute()
  24. {
  25. if (Files?.Length == 0)
  26. {
  27. Log.LogError("No packages were found.");
  28. return false;
  29. }
  30. var expectedPackages = new HashSet<string>(Packages.Select(i => i.ItemSpec), StringComparer.OrdinalIgnoreCase);
  31. foreach (var file in Files)
  32. {
  33. PackageIdentity identity;
  34. using (var reader = new PackageArchiveReader(file.ItemSpec))
  35. {
  36. identity = reader.GetIdentity();
  37. }
  38. if (!expectedPackages.Contains(identity.Id))
  39. {
  40. Log.LogError($"Unexpected package artifact with id: {identity.Id}");
  41. continue;
  42. }
  43. expectedPackages.Remove(identity.Id);
  44. }
  45. if (expectedPackages.Count != 0)
  46. {
  47. var error = new StringBuilder();
  48. foreach (var id in expectedPackages)
  49. {
  50. error.Append(" - ").AppendLine(id);
  51. }
  52. Log.LogError($"Expected the following packages, but they were not found:" + error.ToString());
  53. return false;
  54. }
  55. return !Log.HasLoggedErrors;
  56. }
  57. }
  58. }