Program.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.IO;
  5. using System.Net.Http;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Xml;
  9. using System.Xml.Linq;
  10. using Microsoft.Extensions.CommandLineUtils;
  11. using NuGet.Packaging;
  12. using NuGet.Packaging.Core;
  13. namespace PackageBaselineGenerator
  14. {
  15. /// <summary>
  16. /// This generates Baseline.props with information about the last RTM release.
  17. /// </summary>
  18. class Program : CommandLineApplication
  19. {
  20. static void Main(string[] args)
  21. {
  22. new Program().Execute(args);
  23. }
  24. private readonly CommandOption _source;
  25. private readonly CommandOption _output;
  26. private readonly CommandOption _feedv3;
  27. public Program()
  28. {
  29. _source = Option("-s|--source <SOURCE>", "The NuGet v2 source of the package to fetch", CommandOptionType.SingleValue);
  30. _output = Option("-o|--output <OUT>", "The generated file output path", CommandOptionType.SingleValue);
  31. _feedv3 = Option("--v3", "Sources is nuget v3", CommandOptionType.NoValue);
  32. Invoke = () => Run().GetAwaiter().GetResult();
  33. }
  34. private async Task<int> Run()
  35. {
  36. var source = _source.HasValue()
  37. ? _source.Value()
  38. : "https://www.nuget.org/api/v2/package";
  39. var packageCache = Environment.GetEnvironmentVariable("NUGET_PACKAGES") != null
  40. ? Environment.GetEnvironmentVariable("NUGET_PACKAGES")
  41. : Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".nuget", "packages");
  42. var tempDir = Path.Combine(Directory.GetCurrentDirectory(), "obj", "tmp");
  43. Directory.CreateDirectory(tempDir);
  44. var input = XDocument.Load(Path.Combine(Directory.GetCurrentDirectory(), "baseline.xml"));
  45. var output = _output.HasValue()
  46. ? _output.Value()
  47. : Path.Combine(Directory.GetCurrentDirectory(), "Baseline.props");
  48. var baselineVersion = input.Root.Attribute("Version").Value;
  49. var doc = new XDocument(
  50. new XComment(" Auto generated. Do not edit manually, use eng/tools/BaselineGenerator/ to recreate. "),
  51. new XElement("Project",
  52. new XElement("PropertyGroup",
  53. new XElement("MSBuildAllProjects", "$(MSBuildAllProjects);$(MSBuildThisFileFullPath)"),
  54. new XElement("AspNetCoreBaselineVersion", baselineVersion))));
  55. var client = new HttpClient();
  56. foreach (var pkg in input.Root.Descendants("Package"))
  57. {
  58. var id = pkg.Attribute("Id").Value;
  59. var version = pkg.Attribute("Version").Value;
  60. var packageFileName = $"{id}.{version}.nupkg";
  61. var nupkgPath = Path.Combine(packageCache, id.ToLowerInvariant(), version, packageFileName);
  62. if (!File.Exists(nupkgPath))
  63. {
  64. nupkgPath = Path.Combine(tempDir, packageFileName);
  65. }
  66. if (!File.Exists(nupkgPath))
  67. {
  68. var url = _feedv3.HasValue()
  69. ? $"{source}/{id.ToLowerInvariant()}/{version}/{id.ToLowerInvariant()}.{version}.nupkg"
  70. : $"{source}/{id}/{version}";
  71. Console.WriteLine($"Downloading {url}");
  72. var response = await client.GetStreamAsync(url);
  73. using (var file = File.Create(nupkgPath))
  74. {
  75. await response.CopyToAsync(file);
  76. }
  77. }
  78. using (var reader = new PackageArchiveReader(nupkgPath))
  79. {
  80. var first = true;
  81. foreach (var group in reader.NuspecReader.GetDependencyGroups())
  82. {
  83. if (first)
  84. {
  85. first = false;
  86. doc.Root.Add(new XComment($" Package: {id}"));
  87. var propertyGroup = new XElement("PropertyGroup",
  88. new XAttribute("Condition", $" '$(PackageId)' == '{id}' "),
  89. new XElement("BaselinePackageVersion", version));
  90. doc.Root.Add(propertyGroup);
  91. }
  92. var itemGroup = new XElement("ItemGroup", new XAttribute("Condition", $" '$(PackageId)' == '{id}' AND '$(TargetFramework)' == '{group.TargetFramework.GetShortFolderName()}' "));
  93. doc.Root.Add(itemGroup);
  94. foreach (var dependency in group.Packages)
  95. {
  96. itemGroup.Add(new XElement("BaselinePackageReference", new XAttribute("Include", dependency.Id), new XAttribute("Version", dependency.VersionRange.ToString())));
  97. }
  98. }
  99. }
  100. }
  101. var settings = new XmlWriterSettings
  102. {
  103. OmitXmlDeclaration = true,
  104. Encoding = Encoding.UTF8,
  105. Indent = true,
  106. };
  107. using (var writer = XmlWriter.Create(output, settings))
  108. {
  109. doc.Save(writer);
  110. }
  111. return 0;
  112. }
  113. }
  114. }