提交 c76f9ea5 编写于 作者: J Jared Parsons 提交者: GitHub

Merge pull request #13055 from jhendrixMSFT/master

Add parsing of NuGet.Config files to RepoUtil and other work.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
namespace RepoUtil
{
internal class ConflictingPackagesException : Exception
{
public ConflictingPackagesException(List<NuGetPackageConflict> conflictingPackages) :
base("Creation failed because of conflicting package versions.")
{
ConflictingPackages = conflictingPackages.ToImmutableArray();
}
public ImmutableArray<NuGetPackageConflict> ConflictingPackages { get; }
}
}
......@@ -31,12 +31,23 @@ public bool Run(TextWriter writer, string[] args)
private JObject GoCore()
{
var obj = new JObject();
obj.Add(GetNuGetFeeds());
obj.Add(GetFixedPackages());
obj.Add(GetBuildPackages());
obj.Add(GetToolsetPackages());
return obj;
}
private JProperty GetNuGetFeeds()
{
var obj = new JObject();
foreach (var nugetFeed in _repoData.NuGetFeeds)
{
obj.Add(GetProperty(nugetFeed));
}
return new JProperty("nugetFeeds", obj);
}
private JProperty GetFixedPackages()
{
var obj = new JObject();
......@@ -68,6 +79,11 @@ private JProperty GetFloatingPackages(string name, IEnumerable<NuGetPackage> pac
return new JProperty(name, obj);
}
private static JProperty GetProperty(NuGetFeed feed)
{
return new JProperty(feed.Name, feed.Url);
}
private static JProperty GetProperty(NuGetPackage package)
{
return new JProperty(package.Name, package.Version);
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Xml.Linq;
namespace RepoUtil
{
internal static class NuGetConfigUtil
{
internal static List<NuGetFeed> GetNuGetFeeds(string nugetConfigFile)
{
var nugetConfig = XElement.Load(nugetConfigFile);
var nugetFeeds =
from el in nugetConfig.Descendants("packageSources").Descendants("add")
select new NuGetFeed(el.Attribute("key").Value, new Uri(el.Attribute("value").Value));
return nugetFeeds.ToList();
}
internal static IEnumerable<string> GetNuGetConfigFiles(string sourcesPath)
{
return Directory.EnumerateFiles(sourcesPath, "nuget*config", SearchOption.AllDirectories);
}
}
}
using System;
namespace RepoUtil
{
internal struct NuGetFeed : IEquatable<NuGetFeed>
{
internal string Name { get; }
internal Uri Url { get; }
internal NuGetFeed(string name, Uri url)
{
Name = name;
Url = url;
}
public static bool operator ==(NuGetFeed left, NuGetFeed right) =>
StringComparer.OrdinalIgnoreCase.Equals(left.Name, right.Name) &&
left.Url == right.Url;
public static bool operator !=(NuGetFeed left, NuGetFeed right) => !(left == right);
public override bool Equals(object obj) => obj is NuGetFeed && Equals((NuGetFeed)obj);
public override int GetHashCode() => Name?.GetHashCode() ?? 0;
public override string ToString() => $"{Name}-{Url}";
public bool Equals(NuGetFeed other) => this == other;
}
}
......@@ -21,7 +21,29 @@ private sealed class ParsedArgs
internal static int Main(string[] args)
{
return Run(args) ? 0 : 1;
int result = 1;
try
{
if (Run(args))
result = 0;
}
catch (ConflictingPackagesException ex)
{
Console.WriteLine(ex.Message);
foreach (var package in ex.ConflictingPackages)
{
Console.WriteLine(package.PackageName);
Console.WriteLine($"\t{package.Conflict.NuGetPackage.Version} - {package.Conflict.FileName}");
Console.WriteLine($"\t{package.Original.NuGetPackage.Version} - {package.Original.FileName}");
}
}
catch (Exception ex)
{
Console.WriteLine("Something unexpected happened.");
Console.WriteLine(ex.ToString());
}
return result;
}
private static bool Run(string[] args)
......
......@@ -41,18 +41,21 @@ internal class RepoConfig
internal ImmutableArray<NuGetPackage> FixedPackages { get; }
internal ImmutableArray<string> ToolsetPackages { get; }
internal ImmutableArray<Regex> NuSpecExcludes { get; }
internal ImmutableArray<Regex> ProjectJsonExcludes { get; }
internal GenerateData? MSBuildGenerateData { get; }
internal RepoConfig(
IEnumerable<NuGetPackage> fixedPackages,
IEnumerable<string> toolsetPackages,
IEnumerable<Regex> nuspecExcludes,
IEnumerable<Regex> projectJsonExcludes,
GenerateData? msbuildGenerateData)
{
Debug.Assert(toolsetPackages.Distinct().Count() == toolsetPackages.Count());
MSBuildGenerateData = msbuildGenerateData;
FixedPackages = fixedPackages.OrderBy(x => x.Name).ToImmutableArray();
NuSpecExcludes = nuspecExcludes.ToImmutableArray();
ProjectJsonExcludes = projectJsonExcludes.ToImmutableArray();
ToolsetPackages = toolsetPackages.OrderBy(x => x).ToImmutableArray();
var map = new Dictionary<string, List<string>>();
......@@ -110,10 +113,18 @@ internal static RepoConfig ReadFrom(string jsonFilePath)
nuspecExcludes.AddRange(((JArray)nuspecExcludesProp.Value).Values<string>().Select(x => new Regex(x)));
}
var projectJsonExcludes = new List<Regex>();
var projectJsonExcludesProp = obj.Property("projectJsonExcludes");
if (projectJsonExcludesProp != null)
{
projectJsonExcludes.AddRange(((JArray)projectJsonExcludesProp.Value).Values<string>().Select(x => new Regex(x)));
}
return new RepoConfig(
fixedPackagesList,
toolsetPackages,
nuspecExcludes,
projectJsonExcludes,
msbuildGenerateData);
}
......
......@@ -14,16 +14,18 @@ internal sealed class RepoData
{
internal string SourcesPath { get; }
internal RepoConfig RepoConfig { get; }
internal ImmutableArray<NuGetFeed> NuGetFeeds { get; }
internal ImmutableArray<NuGetPackage> FloatingBuildPackages { get; }
internal ImmutableArray<NuGetPackage> FloatingToolsetPackages { get; }
internal ImmutableArray<NuGetPackage> FloatingPackages { get; }
internal ImmutableArray<NuGetPackage> FixedPackages => RepoConfig.FixedPackages;
internal ImmutableArray<NuGetPackage> AllPackages { get; }
private RepoData(RepoConfig config, string sourcesPath, IEnumerable<NuGetPackage> floatingPackages)
private RepoData(RepoConfig config, string sourcesPath, IEnumerable<NuGetFeed> nugetFeeds, IEnumerable<NuGetPackage> floatingPackages)
{
SourcesPath = sourcesPath;
RepoConfig = config;
NuGetFeeds = nugetFeeds.ToImmutableArray();
FloatingToolsetPackages = floatingPackages
.Where(x => RepoConfig.ToolsetPackages.Contains(x.Name, Constants.NugetPackageNameComparer))
.OrderBy(x => x.Name)
......@@ -57,7 +59,7 @@ internal static RepoData Create(RepoConfig config, string sourcesPath)
var repoData = Create(config, sourcesPath, out conflicts);
if (conflicts?.Count > 0)
{
throw new Exception("Creation failed because of conflicting packages");
throw new ConflictingPackagesException(conflicts);
}
return repoData;
......@@ -65,12 +67,24 @@ internal static RepoData Create(RepoConfig config, string sourcesPath)
internal static RepoData Create(RepoConfig config, string sourcesPath, out List<NuGetPackageConflict> conflicts)
{
var nugetFeeds = new List<NuGetFeed>();
foreach (var nugetConfig in NuGetConfigUtil.GetNuGetConfigFiles(sourcesPath))
{
var nugetFeed = NuGetConfigUtil.GetNuGetFeeds(nugetConfig);
nugetFeeds.AddRange(nugetFeed);
}
conflicts = null;
var fixedPackageSet = new HashSet<NuGetPackage>(config.FixedPackages);
var floatingPackageMap = new Dictionary<string, NuGetPackageSource>(Constants.NugetPackageNameComparer);
foreach (var filePath in ProjectJsonUtil.GetProjectJsonFiles(sourcesPath))
{
if (config.ProjectJsonExcludes.Any(x => x.IsMatch(filePath)))
{
continue;
}
var fileName = FileName.FromFullPath(sourcesPath, filePath);
foreach (var package in ProjectJsonUtil.GetDependencies(filePath))
{
......@@ -99,7 +113,7 @@ internal static RepoData Create(RepoConfig config, string sourcesPath, out List<
}
}
return new RepoData(config, sourcesPath, floatingPackageMap.Values.Select(x => x.NuGetPackage));
return new RepoData(config, sourcesPath, nugetFeeds, floatingPackageMap.Values.Select(x => x.NuGetPackage));
}
}
}
......@@ -41,6 +41,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ChangeCommand.cs" />
<Compile Include="ConflictingPackagesException.cs" />
<Compile Include="Constants.cs" />
<Compile Include="ConsumesCommand.cs" />
<Compile Include="FileName.cs" />
......@@ -48,6 +49,8 @@
<Compile Include="GenerateUtil.cs" />
<Compile Include="ICommand.cs" />
<Compile Include="JsonTypes.cs" />
<Compile Include="NuGetConfigUtil.cs" />
<Compile Include="NuGetFeed.cs" />
<Compile Include="NuGetPackage.cs" />
<Compile Include="NuGetPackageChange.cs" />
<Compile Include="NuGetPackageSource.cs" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册