CheckVersionOverrides.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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.Linq;
  5. using Microsoft.Build.Construction;
  6. using Microsoft.Build.Framework;
  7. using Microsoft.Build.Utilities;
  8. namespace RepoTasks
  9. {
  10. public class CheckVersionOverrides : Task
  11. {
  12. [Required]
  13. public string DotNetPackageVersionPropsPath { get; set; }
  14. [Required]
  15. public string DependenciesFile { get; set; }
  16. public override bool Execute()
  17. {
  18. Log.LogMessage($"Verifying versions set in {DotNetPackageVersionPropsPath} match expected versions set in {DependenciesFile}");
  19. var versionOverrides = ProjectRootElement.Open(DotNetPackageVersionPropsPath);
  20. var dependencies = ProjectRootElement.Open(DependenciesFile);
  21. var pinnedVersions = dependencies.PropertyGroups
  22. .Where(p => !string.Equals("Package Versions: Auto", p.Label))
  23. .SelectMany(p => p.Properties)
  24. .ToDictionary(p => p.Name, p => p.Value, StringComparer.OrdinalIgnoreCase);
  25. foreach (var prop in versionOverrides.Properties)
  26. {
  27. if (pinnedVersions.TryGetValue(prop.Name, out var pinnedVersion))
  28. {
  29. if (!string.Equals(pinnedVersion, prop.Value, StringComparison.OrdinalIgnoreCase))
  30. {
  31. Log.LogError($"The imported package version props file conflicts with a pinned version variable {prop.Name}. Imported value: {prop.Value}, Pinned value: {pinnedVersion}");
  32. }
  33. }
  34. }
  35. return !Log.HasLoggedErrors;
  36. }
  37. }
  38. }