Program.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Xml.Linq;
  7. using McMaster.Extensions.CommandLineUtils;
  8. using NuGet.Packaging;
  9. namespace NuspecBaselineGenerator
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args) => CommandLineApplication.Execute<Program>(args);
  14. [Required]
  15. [DirectoryExists]
  16. [Argument(0, Description = "Path(s) to directories containing .nupkg files from previous releases.")]
  17. public string[] Directories { get; }
  18. [Required]
  19. [Option(Description = "The path to the artifacts.props file")]
  20. [FileExists]
  21. public string Artifacts { get; }
  22. [Option(Description = "Show verbose output")]
  23. public bool Verbose { get; }
  24. private void OnExecute()
  25. {
  26. var doc = XDocument.Load(Artifacts);
  27. var versions = new List<(string, string)>();
  28. foreach (var dir in Directories)
  29. {
  30. foreach (var nupkg in Directory.EnumerateFiles(dir, "*.nupkg"))
  31. {
  32. using (var reader = new PackageArchiveReader(nupkg))
  33. {
  34. var identity = reader.GetIdentity();
  35. versions.Add((identity.Id, identity.Version.ToNormalizedString()));
  36. LogVerbose($"Found package {identity.Id}/{identity.Version} ({nupkg})");
  37. }
  38. }
  39. }
  40. LogVerbose($"Found {versions.Count} package(s)");
  41. void WriteAttribute(XElement element, string attr)
  42. {
  43. var attribute = element.Attribute(attr);
  44. if (attribute != null)
  45. {
  46. Console.Write($" {attr}=\"{attribute.Value}\"");
  47. }
  48. }
  49. foreach (var item in versions.OrderBy(i => i.Item1))
  50. {
  51. var element = doc
  52. .Descendants("PackageArtifact")
  53. .SingleOrDefault(p => p.Attribute("Include")?.Value == item.Item1);
  54. Console.Write($"<ExternalDependency Include=\"{item.Item1}\" Version=\"{item.Item2}\"");
  55. if (element != null)
  56. {
  57. WriteAttribute(element, "Analyzer");
  58. WriteAttribute(element, "AllMetapackage");
  59. WriteAttribute(element, "AppMetapackage");
  60. WriteAttribute(element, "LZMA");
  61. WriteAttribute(element, "PackageType");
  62. WriteAttribute(element, "Category");
  63. }
  64. Console.WriteLine(" />");
  65. }
  66. }
  67. private void LogVerbose(string message)
  68. {
  69. if (!Verbose)
  70. {
  71. return;
  72. }
  73. Console.WriteLine(message);
  74. }
  75. }
  76. }