Skip to content

Commit

Permalink
Simplify DependencyResolver class, improve console output
Browse files Browse the repository at this point in the history
  • Loading branch information
bouassaba committed Dec 23, 2021
1 parent 60ef022 commit d38f19d
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 182 deletions.
3 changes: 0 additions & 3 deletions NuSave.Core/Dependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,5 @@ public class Dependency

[JsonProperty("version")]
public NuGetVersion Version { get; set; }

[JsonProperty("authors")]
public string Authors { get; set; }
}
}
137 changes: 93 additions & 44 deletions NuSave.Core/DependencyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class Options
{
public string Source { get; set; }

public string TargetFramework { get; set; }
public List<string> TargetFrameworks { get; set; }

public bool AllowPreRelease { get; set; }

Expand Down Expand Up @@ -52,10 +52,10 @@ public DependencyResolver(Options options, Cache cache)

public void ResolveByIdAndVersion(string id, string version)
{
Log($"Resolving dependencies for {id}@{version} 🪄️", ConsoleColor.Yellow);
LogLine($"Resolving dependencies for {id}@{version} 🪄️", ConsoleColor.Yellow);

_dependencies = new List<Dependency>();

IPackageSearchMetadata package = FindPackage(id, SemanticVersion.Parse(version), _options.AllowPreRelease,
_options.AllowUnlisted);

Expand All @@ -64,13 +64,20 @@ public void ResolveByIdAndVersion(string id, string version)
throw new Exception("Could not resolve package");
}

AppendFromPackageSearchMetadata(package);
Append(package);

RemoveDuplicates();

if (!_options.NoCache)
{
RemoveCached();
}
}

public void ResolveBySln(string path)
{
Log($"Resolving dependencies using {path} ️", ConsoleColor.Yellow);
LogLine($"Resolving dependencies using {path} 🪄️", ConsoleColor.Yellow);

_dependencies = new List<Dependency>();

var solutionFile = SolutionFile.Parse(path);
Expand All @@ -87,8 +94,8 @@ public void ResolveBySln(string path)

public void ResolveByCsProj(string path)
{
Log($"Resolving dependencies using {path} ️", ConsoleColor.Yellow);
LogLine($"Resolving dependencies using {path} 🪄️", ConsoleColor.Yellow);

_dependencies = new List<Dependency>();

XmlDocument xmlDocument = new XmlDocument();
Expand All @@ -99,85 +106,105 @@ public void ResolveByCsProj(string path)
return;
}

List<PackageReference> references = new List<PackageReference>();
List<MsBuildPackageReference> packageReferences = new List<MsBuildPackageReference>();
foreach (var node in nodes)
{
var json = JObject.Parse(node.ToJson());
references.Add(new PackageReference
packageReferences.Add(new MsBuildPackageReference
{
Include = json["PackageReference"]?["@Include"]?.ToString(),
Version = json["PackageReference"]?["@Version"]?.ToString()
});
}

AppendFromPackageReferences(references);
}
foreach (var packageReference in packageReferences)
{
IPackageSearchMetadata nugetPackage = FindPackage(packageReference.Include,
SemanticVersion.Parse(packageReference.Version), true, true);
Append(nugetPackage);
}

private void AppendFromPackageSearchMetadata(IPackageSearchMetadata package)
{
if (!_options.NoCache && _cache.PackageExists(package.Identity.Id, package.Identity.Version))
RemoveDuplicates();

if (!_options.NoCache)
{
return;
RemoveCached();
}
}

private void Append(IPackageSearchMetadata package)
{
_dependencies.Add(package.ToDependency());

Log($"{package.Identity.Id}@{package.Identity.Version}");

foreach (var set in package.DependencySets)
{
string targetFramework = set.TargetFramework.ToString();
if (!string.IsNullOrWhiteSpace(_options.TargetFramework) &&
!targetFramework.ToLowerInvariant().Contains(_options.TargetFramework.ToLowerInvariant()))
string setTargetFramework = set.TargetFramework.ToString();
if (_options.TargetFrameworks.Any() &&
!_options.TargetFrameworks.Any(e =>
setTargetFramework.ToLowerInvariant().Equals(TranslateTargetFrameworkSyntax(e).ToLowerInvariant())))
{
continue;
}

foreach (var dependency in set.Packages)
{
if (!_options.NoCache && _cache.PackageExists(dependency.Id, dependency.VersionRange.ToNuGetVersion()))
{
continue;
}

if (_dependencies.Any(p => p.Id == dependency.Id && p.Version == dependency.VersionRange.ToNuGetVersion()))
{
continue;
}

var found = FindPackage(
dependency.Id,
dependency.VersionRange.ToNuGetVersion(),
_options.AllowPreRelease,
_options.AllowUnlisted);
if (found == null)
{
Log($"Could not resolve dependency: {dependency.Id} {dependency.VersionRange.ToNuGetVersion().ToString()}", ConsoleColor.Red);
continue;
}

_dependencies.Add(found.ToDependency());

Log($"{found.Identity.Id}@{found.Identity.Version}");

AppendFromPackageSearchMetadata(found);
Append(found);
}
}
}

private void AppendFromPackageReference(PackageReference packageReference)
private void RemoveDuplicates()
{
IPackageSearchMetadata nugetPackage = FindPackage(packageReference.Include,
SemanticVersion.Parse(packageReference.Version), true, true);
AppendFromPackageSearchMetadata(nugetPackage);
List<Dependency> result = new List<Dependency>();
foreach (var dependency in _dependencies)
{
if (!result.Any(e => e.Id == dependency.Id && e.Version == dependency.Version))
{
result.Add(dependency);
}
}

_dependencies = result;
}

private void AppendFromPackageReferences(IEnumerable<PackageReference> references)
private void RemoveCached()
{
foreach (var packageRef in references)
List<Dependency> result = new List<Dependency>();
foreach (var dependency in _dependencies)
{
AppendFromPackageReference(packageRef);
if (!_cache.PackageExists(dependency.Id, dependency.Version))
{
result.Add(dependency);
}
}

_dependencies = result;
}

private static string TranslateTargetFrameworkSyntax(string localSyntax)
{
var split = localSyntax.Split("@");
string value = $"{split[0]},Version=v{split[1]}";
return value;
}

private static string ToLocalTargetFrameworkSyntax(string localSyntax)
{
var split = localSyntax.Replace(" ", "").Split(",");
string value = $"{split[0]}@{split[1].Replace("Version=v", "")}";
return value;
}

private IPackageSearchMetadata FindPackage(string id, SemanticVersion version, bool includePrerelease, bool includeUnlisted)
Expand All @@ -201,7 +228,7 @@ private IPackageSearchMetadata FindPackage(string id, SemanticVersion version, b
return null;
}

private void Log(string message)
private void LogLine(string message)
{
if (_options.Silent)
{
Expand All @@ -211,7 +238,7 @@ private void Log(string message)
Console.WriteLine(message);
}

private void Log(string message, ConsoleColor consoleColor)
private void LogLine(string message, ConsoleColor consoleColor)
{
if (_options.Silent)
{
Expand All @@ -222,5 +249,27 @@ private void Log(string message, ConsoleColor consoleColor)
Console.WriteLine(message);
Console.ResetColor();
}

private void Log(string message)
{
if (_options.Silent)
{
return;
}

Console.Write(message);
}

private void Log(string message, ConsoleColor consoleColor)
{
if (_options.Silent)
{
return;
}

Console.ForegroundColor = consoleColor;
Console.Write(message);
Console.ResetColor();
}
}
}
18 changes: 15 additions & 3 deletions NuSave.Core/Downloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ namespace NuSave.Core
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using Konsole;
using NuGet.Common;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
Expand Down Expand Up @@ -39,16 +41,26 @@ public Downloader(Options options, DependencyResolver dependencyResolver, Cache

public void Download()
{
if (!_dependencyResolver.Dependencies.Any())
{
Log("Package(s) already cached.");
return;
}

Log("Downloading ⚡️", ConsoleColor.Yellow);

var pb = new ProgressBar(PbStyle.SingleLine, 100);

int counter = 0;
foreach (var dependency in _dependencyResolver.Dependencies)
{
if (_cache.PackageExists(dependency.Id, dependency.Version))
{
continue;
}

Log($"{dependency.Id} {dependency.Version}");
pb.Refresh(counter * 100 / _dependencyResolver.Dependencies.Count(), $"{dependency.Id} {dependency.Version} 📦");
counter++;

// We keep retrying forever until the user will press Ctrl-C
// This lets the user decide when to stop retrying.
Expand Down Expand Up @@ -80,8 +92,8 @@ public void Download()
}
}
}
Log("Done 🎉", ConsoleColor.Yellow);

pb.Refresh(100, "Done 🎉");
}

private void Log(string message)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace NuSave.Core
{
public class PackageReference
public class MsBuildPackageReference
{
public string Include { get; set; }

Expand Down
1 change: 1 addition & 0 deletions NuSave.Core/NuSave.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<RootNamespace>NuSave.Core</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Goblinfactory.Konsole" Version="6.2.0" />
<PackageReference Include="Microsoft.Build" Version="16.8.0" />
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="16.8.0" />
<PackageReference Include="Microsoft.Web.Xdt" Version="3.1.0" />
Expand Down
9 changes: 4 additions & 5 deletions NuSave.Core/PackageSearchMetadataExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ namespace NuSave.Core

public static class PackageSearchMetadataExtensions
{
public static Dependency ToDependency(this IPackageSearchMetadata packageDependency)
public static Dependency ToDependency(this IPackageSearchMetadata packageSearchMetadata)
{
return new Dependency
return new()
{
Id = packageDependency.Identity.Id,
Version = packageDependency.Identity.Version,
Authors = string.Join(" ", packageDependency.Authors)
Id = packageSearchMetadata.Identity.Id,
Version = packageSearchMetadata.Identity.Version
};
}
}
Expand Down
Loading

0 comments on commit d38f19d

Please sign in to comment.