提交 fc3bb71b 编写于 作者: J Jared Parsons

Better consumes implementation

上级 de349809
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RepoUtil
{
internal sealed class ConsumesUtil
{
private readonly RepoData _repoData;
internal ConsumesUtil(RepoData repoData)
{
Debug.Assert(repoData.HasCurentData);
_repoData = repoData;
}
internal static string Go(string sourcesPath, RepoData repoData)
{
Debug.Assert(!repoData.HasCurentData);
repoData = repoData.PopulateWithCurrentData(sourcesPath);
var util = new ConsumesUtil(repoData);
var obj = util.GoCore();
return obj.ToString(Formatting.Indented);
}
private JObject GoCore()
{
var obj = new JObject();
obj.Add(GetStaticPackages());
obj.Add(GetBuildPackages());
obj.Add(GetToolsetPackages());
return obj;
}
private JProperty GetStaticPackages()
{
var obj = new JObject();
foreach (var pair in _repoData.StaticPackagesMap.OrderBy(x => x.Key))
{
obj.Add(GetProperty(pair.Key, pair.Value));
}
return new JProperty("static", obj);
}
private JProperty GetBuildPackages()
{
return GetFloatingPackages("build", _repoData.FloatingBuildPackages);
}
private JProperty GetToolsetPackages()
{
return GetFloatingPackages("toolset", _repoData.FloatingToolsetPackages);
}
private JProperty GetFloatingPackages(string name, IEnumerable<string> packages)
{
var array = new JArray();
foreach (var package in packages)
{
array.Add(package);
}
return new JProperty(name, array);
}
private static JProperty GetProperty(NuGetPackage package)
{
return new JProperty(package.Name, package.Version);
}
private static JProperty GetProperty(string packageName, ImmutableArray<string> versions)
{
if (versions.Length == 1)
{
return GetProperty(new NuGetPackage(packageName, versions[0]));
}
var content = JArray.FromObject(versions.ToArray());
return new JProperty(packageName, content);
}
private static string GetKey(NuGetPackage nugetRef)
{
return $"{nugetRef.Name}:{nugetRef.Version}";
}
}
}
......@@ -6,12 +6,12 @@
namespace RepoUtil
{
internal struct NuGetReference
internal struct NuGetPackage
{
internal string Name { get; }
internal string Version { get; }
internal NuGetReference(string name, string version)
internal NuGetPackage(string name, string version)
{
Name = name;
Version = version;
......
......@@ -42,36 +42,13 @@ private static bool Run(string[] args)
case Mode.Verify:
return VerifyUtil.Go(sourcesPath, repoData);
case Mode.Consumes:
return Consumes(sourcesPath);
default:
throw new Exception("Unrecognized mode");
}
}
private static bool Consumes(string sourcesPath)
{
var set = new HashSet<string>(StringComparer.Ordinal);
var list = new List<string>();
var all = Data.FloatingList.Concat(Data.StaticList).Select(x => new FileName(sourcesPath, x));
foreach (var fileName in all)
{
foreach (var nugetRef in ProjectJsonUtil.GetDependencies(fileName.FullPath))
{
var key = $@"""{nugetRef.Name}"" : ""{nugetRef.Version}""";
if (set.Add(key))
{
list.Add(key);
Console.WriteLine(ConsumesUtil.Go(sourcesPath, repoData));
return true;
}
}
}
list.Sort();
foreach (var key in list)
{
Console.WriteLine(key);
default:
throw new Exception("Unrecognized mode");
}
return true;
}
private static void Usage()
......
......@@ -21,17 +21,17 @@ internal static bool NeedsTracking(string filePath)
}
// TOOD: use FileName here
internal static ImmutableArray<NuGetReference> GetDependencies(string filePath)
internal static ImmutableArray<NuGetPackage> GetDependencies(string filePath)
{
// Need to track any file that has dependencies
var obj = JObject.Parse(File.ReadAllText(filePath));
var dependencies = (JObject)obj["dependencies"];
if (dependencies == null)
{
return ImmutableArray<NuGetReference>.Empty;
return ImmutableArray<NuGetPackage>.Empty;
}
var builder = ImmutableArray.CreateBuilder<NuGetReference>();
var builder = ImmutableArray.CreateBuilder<NuGetPackage>();
foreach (var dependency in dependencies.Properties())
{
builder.Add(ParseDependency(dependency));
......@@ -43,7 +43,7 @@ internal static ImmutableArray<NuGetReference> GetDependencies(string filePath)
/// <summary>
/// Parse out a dependency entry from the project.json file.
/// </summary>
internal static NuGetReference ParseDependency(JProperty prop)
internal static NuGetPackage ParseDependency(JProperty prop)
{
var name = prop.Name;
......@@ -57,7 +57,7 @@ internal static NuGetReference ParseDependency(JProperty prop)
version = ((JObject)prop.Value).Value<string>("version");
}
return new NuGetReference(name, version);
return new NuGetPackage(name, version);
}
internal static bool VerifyTracked(string sourcesPath, IEnumerable<FileName> fileNames)
......@@ -80,5 +80,11 @@ internal static bool VerifyTracked(string sourcesPath, IEnumerable<FileName> fil
return allGood;
}
// TODO: Need to include our toolset files not named project.json.
internal static IEnumerable<string> GetProjectJsonFiles(string sourcesPath)
{
return Directory.EnumerateFiles(sourcesPath, "project.json", SearchOption.AllDirectories);
}
}
}
......@@ -39,7 +39,7 @@ internal class RepoData
/// <summary>
/// Fixed references which do not change during a build.
/// </summary>
internal ImmutableArray<NuGetReference> StaticPackages { get; }
internal ImmutableArray<NuGetPackage> StaticPackages { get; }
/// <summary>
/// This is a map of static package names to the list of supported versions.
......@@ -47,13 +47,13 @@ internal class RepoData
internal ImmutableDictionary<string, ImmutableArray<string>> StaticPackagesMap { get; }
internal ImmutableArray<string> FloatingBuildPackages { get; }
internal ImmutableArray<string> FloatingToolsetPackages { get; }
internal ImmutableArray<string> FloatingPackages { get; }
internal bool HasCurentData { get; }
internal RepoData(IEnumerable<NuGetReference> staticPackages, IEnumerable<string> floatingBuildPackages, IEnumerable<string> floatingToolsetPackages)
internal RepoData(IEnumerable<NuGetPackage> staticPackages, IEnumerable<string> floatingBuildPackages, IEnumerable<string> floatingToolsetPackages, bool hasCurrentData = false)
{
HasCurentData = hasCurrentData;
StaticPackages = staticPackages.OrderBy(x => x.Name).ToImmutableArray();
// TODO: Validate duplicate names in the floating lists
......@@ -86,20 +86,20 @@ internal static RepoData ReadFrom(string jsonFilePath)
// Need to track any file that has dependencies
var obj = JObject.Parse(File.ReadAllText(jsonFilePath));
var staticPackages = (JObject)obj["staticPackages"];
var staticPackagesList = ImmutableArray.CreateBuilder<NuGetReference>();
var staticPackagesList = ImmutableArray.CreateBuilder<NuGetPackage>();
foreach (var prop in staticPackages.Properties())
{
if (prop.Value.Type == JTokenType.String)
{
var version = (string)prop.Value;
var nugetRef = new NuGetReference(prop.Name, version);
var nugetRef = new NuGetPackage(prop.Name, version);
staticPackagesList.Add(nugetRef);
}
else
{
foreach (var version in ((JArray)prop.Value).Values<string>())
{
var nugetRef = new NuGetReference(prop.Name, version);
var nugetRef = new NuGetPackage(prop.Name, version);
staticPackagesList.Add(nugetRef);
}
}
......@@ -114,5 +114,32 @@ internal static RepoData ReadFrom(string jsonFilePath)
build.Values<string>(),
toolset.Values<string>());
}
/// <summary>
/// The raw RepoData contains only the static + toolset packages that we need to track. This method will examine the current
/// state of the repo and add in the current data.
/// </summary>
internal RepoData PopulateWithCurrentData(string sourcesPath)
{
var list = new List<string>(FloatingPackages);
foreach (var fileName in ProjectJsonUtil.GetProjectJsonFiles(sourcesPath))
{
foreach (var nuget in ProjectJsonUtil.GetDependencies(fileName))
{
if (StaticPackagesMap.ContainsKey(nuget.Name) || FloatingToolsetPackages.Contains(nuget.Name))
{
continue;
}
list.Add(nuget.Name);
}
}
return new RepoData(
StaticPackages,
list,
FloatingToolsetPackages,
hasCurrentData: true);
}
}
}
......@@ -23,10 +23,11 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="ConsumesUtil.cs" />
<Compile Include="Data.cs" />
<Compile Include="FileName.cs" />
<Compile Include="JsonTypes.cs" />
<Compile Include="NuGetReference.cs" />
<Compile Include="NuGetPackage.cs" />
<Compile Include="Program.cs" />
<Compile Include="ProjectJsonUtil.cs" />
<Compile Include="RepoData.cs" />
......
......@@ -16,10 +16,10 @@ internal sealed class VerifyUtil
{
private struct NuGetReferenceSource
{
internal NuGetReference NuGetReference { get; }
internal NuGetPackage NuGetReference { get; }
internal FileName FileName { get; }
internal NuGetReferenceSource(NuGetReference nugetRef, FileName fileName)
internal NuGetReferenceSource(NuGetPackage nugetRef, FileName fileName)
{
NuGetReference = nugetRef;
FileName = fileName;
......@@ -50,7 +50,7 @@ private bool Go()
private bool VerifyPackages()
{
var allGood = false;
foreach (var filePath in GetProjectJsonFiles())
foreach (var filePath in ProjectJsonUtil.GetProjectJsonFiles(_sourcesPath))
{
var fileName = FileName.FromFullPath(_sourcesPath, filePath);
foreach (var nugetRef in ProjectJsonUtil.GetDependencies(filePath))
......@@ -75,7 +75,7 @@ private bool VerifyPackages()
return allGood;
}
private bool VerifyFloatingPackage(NuGetReference nugetRef, FileName fileName)
private bool VerifyFloatingPackage(NuGetPackage nugetRef, FileName fileName)
{
NuGetReferenceSource source;
if (_floatingPackageMap.TryGetValue(nugetRef.Name, out source))
......@@ -95,7 +95,7 @@ private bool VerifyFloatingPackage(NuGetReference nugetRef, FileName fileName)
return true;
}
private bool VerifyStaticPackage(NuGetReference nugetRef, FileName fileName)
private bool VerifyStaticPackage(NuGetPackage nugetRef, FileName fileName)
{
Debug.Assert(_repoData.StaticPackagesMap.ContainsKey(nugetRef.Name));
var versions = _repoData.StaticPackagesMap[nugetRef.Name];
......@@ -108,10 +108,5 @@ private bool VerifyStaticPackage(NuGetReference nugetRef, FileName fileName)
return true;
}
// TODO: Need to include our toolset files not named project.json.
private IEnumerable<string> GetProjectJsonFiles()
{
return Directory.EnumerateFiles(_sourcesPath, "project.json", SearchOption.AllDirectories);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册