Skip to content

Commit

Permalink
Merge pull request #42 from nventive/dev/beca/logging
Browse files Browse the repository at this point in the history
Dev/beca/logging
  • Loading branch information
Benjamin Cartier authored Feb 27, 2020
2 parents 29d8559 + 1dbc6a0 commit d67b86c
Show file tree
Hide file tree
Showing 18 changed files with 343 additions and 132 deletions.
10 changes: 7 additions & 3 deletions src/NuGet.Downloader.Tool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Mono.Options;
using NuGet.Downloader.Entities;
using NuGet.Shared.Entities;
using NuGet.Shared.Extensions;
using Uno.Extensions;

namespace NuGet.Downloader.Tool
Expand Down Expand Up @@ -40,9 +39,14 @@ public static async Task Main(string[] args)
options.WriteOptionDescriptions(Console.Out);
}

var packages = await NuGetDownloader.DownloadAsync(CancellationToken.None, parameters, ConsoleLogger.Instance);
var result = await NuGetDownloader.RunAsync(CancellationToken.None, parameters, ConsoleLogger.Instance);

Console.WriteLine($"{packages.Length} packages have been downloaded under {parameters.PackageOutputPath}");
Console.WriteLine($"{result.DownloadedPackages.Length} packages have been downloaded under {parameters.PackageOutputPath}");

if(parameters.Target != null)
{
Console.WriteLine($"{result.PushedPackages?.Length ?? 0} packages have been pushed to {parameters.Target.Url}");
}
}
catch(Exception ex)
{
Expand Down
11 changes: 11 additions & 0 deletions src/NuGet.Downloader/Entities/DownloaderResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using NuGet.Shared.Entities;

namespace NuGet.Downloader.Entities
{
public class DownloaderResult
{
public LocalPackage[] DownloadedPackages { get; set; }

public LocalPackage[] PushedPackages { get; set; }
}
}
63 changes: 44 additions & 19 deletions src/NuGet.Downloader/NuGetDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ namespace NuGet.Downloader
{
public class NuGetDownloader
{
public static async Task<LocalPackage[]> DownloadAsync(CancellationToken ct, DownloaderParameters parameters, ILogger log)
public static async Task<DownloaderResult> RunAsync(CancellationToken ct, DownloaderParameters parameters, ILogger log)
{
var downloader = new NuGetDownloader(log);

return await downloader.DownloadPackages(ct, parameters);
return await downloader.RunAsync(ct, parameters);
}

private readonly ILogger _log;
Expand All @@ -30,45 +30,70 @@ private NuGetDownloader(ILogger log)
_log = log;
}

public async Task<LocalPackage[]> DownloadPackages(CancellationToken ct, DownloaderParameters parameters)
public async Task<DownloaderResult> RunAsync(CancellationToken ct, DownloaderParameters parameters)
{
var stopwatch = Stopwatch.StartNew();

var localPackages = new List<LocalPackage>();

Directory.CreateDirectory(parameters.PackageOutputPath);
var result = new DownloaderResult();

var packages = await GetPackagesToDownload(ct, parameters.SolutionPath, parameters.Source);

_log.LogInformation($"Found {packages.Count()} packages to download.");
_log.LogInformation($"Found {packages.Count()} packages to download");

result.DownloadedPackages = await DownloadPackages(ct, packages, parameters.Source, parameters.PackageOutputPath);
result.PushedPackages = await PushPackages(ct, result.DownloadedPackages, parameters.Target);

stopwatch.Stop();

_log.LogInformation($"Operation completed in {stopwatch.Elapsed}");

return result;
}

private async Task<LocalPackage[]> DownloadPackages(
CancellationToken ct,
IEnumerable<PackageIdentity> packages,
IPackageFeed sourceFeed,
string outputPath
)
{
var downloadedPackages = new List<LocalPackage>();

Directory.CreateDirectory(outputPath);

foreach(var package in packages)
{
var localPackage = await parameters.Source.DownloadPackage(ct, package, parameters.PackageOutputPath);
var localPackage = await sourceFeed.DownloadPackage(ct, package, outputPath);

if(localPackage == null)
{
throw new PackageNotFoundException(package, parameters.Source.Url); //Shouldn't happen
throw new PackageNotFoundException(package, sourceFeed.Url); //Shouldn't happen
}

localPackages.Add(localPackage);
downloadedPackages.Add(localPackage);
}

if(parameters.Target != null)
return downloadedPackages.ToArray();
}

private async Task<LocalPackage[]> PushPackages(CancellationToken ct, IEnumerable<LocalPackage> packages, IPackageFeed targetFeed)
{
var pushedPackages = new List<LocalPackage>();

if(targetFeed != null)
{
_log.LogInformation($"Pushing packages to {parameters.Target.Url}.");
_log.LogInformation($"Pushing packages to {targetFeed.Url}");

foreach(var package in localPackages)
foreach(var package in packages)
{
await parameters.Target.PushPackage(ct, package);
if(await targetFeed.PushPackage(ct, package))
{
pushedPackages.Add(package);
}
}
}

stopwatch.Stop();

_log.LogInformation($"Operation completed in {stopwatch.Elapsed}.");

return localPackages.ToArray();
return pushedPackages.ToArray();
}

private async Task<IEnumerable<PackageIdentity>> GetPackagesToDownload(CancellationToken ct, string solutionPath, IPackageFeed source)
Expand Down
4 changes: 2 additions & 2 deletions src/NuGet.Shared/Entities/IPackageFeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public interface IPackageFeed
/// </summary>
/// <param name="ct"></param>
/// <param name="package"></param>
/// <returns></returns>
Task PushPackage(CancellationToken ct, LocalPackage package);
/// <returns>A value indicating whether or not the package has been pushed</returns>
Task<bool> PushPackage(CancellationToken ct, LocalPackage package);
}
}
8 changes: 4 additions & 4 deletions src/NuGet.Shared/Entities/PackageFeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,17 @@ string downloadLocation
return new LocalPackage(packageIdentity, localPackagePath);
}

public async Task PushPackage(CancellationToken ct, LocalPackage package)
public async Task<bool> PushPackage(CancellationToken ct, LocalPackage package)
{
var version = await _packageSource.GetPackageVersion(ct, package.Identity);

if(version != null)
{
Logger.LogInformation($"{package.Identity} already exists in source, skipping.");
return;
Logger.LogInformation($"{package.Identity} already exists in source, skipping");
return false;
}

await _packageSource.PushPackage(ct, package, Logger);
return await _packageSource.PushPackage(ct, package, Logger);
}

public override int GetHashCode() => _packageSource.GetHashCode();
Expand Down
2 changes: 1 addition & 1 deletion src/NuGet.Shared/Entities/PackageNotFoundException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public PackageNotFoundException()
}

public PackageNotFoundException(PackageIdentity package, Uri sourceUrl)
: this($"{package} not found in {sourceUrl.AbsoluteUri}.")
: this($"{package} not found in {sourceUrl.AbsoluteUri}")
{
}

Expand Down
13 changes: 9 additions & 4 deletions src/NuGet.Shared/Extensions/PackageSourceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,19 @@ ILogger log
.GetDownloadResourceResultAsync(identity, downloadContext, downloadDirectory, log, ct);
}

public static Task PushPackage(
public static async Task<bool> PushPackage(
this PackageSource source,
CancellationToken ct,
LocalPackage package,
ILogger log
) => source
.GetResource<PackageUpdateResource>()
.Push(package.Path, null, 100, true, s => s, s => s, true, log);
)
{
await source
.GetResource<PackageUpdateResource>()
.Push(package.Path, null, 100, true, s => s, s => s, true, log);

return true;
}

private static TResource GetResource<TResource>(this PackageSource source)
where TResource : class, INuGetResource
Expand Down
61 changes: 52 additions & 9 deletions src/NuGet.Shared/Helpers/MarkdownHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,76 @@ internal static string CodeBlock(string text, bool isMultiline = false, string l
return $"`{text}`";
}

internal class TableBuilder
internal static string Table(
string[] header,
string[][] rows,
bool prettify
) => new TableBuilder()
.AddHeader(header)
.AddRows(rows)
.Build(prettify);

private class TableBuilder
{
private readonly List<string> _header = new List<string>();
private readonly List<string[]> _body = new List<string[]>();

public void AddHeader(string text) => _header.Add(text);
public TableBuilder AddHeader(params string[] header)
=> this.Apply(b => b._header.AddRange(header));

public void AddLine(params string[] columns) => _body.Add(columns.Trim().ToArray());
public TableBuilder AddRows(params string[][] rows)
=> this.Apply(b => _body.AddRange(rows));

public string Build()
public string Build(bool prettify)
{
var builder = new StringBuilder();

if(_header.Any())
{
builder
.AppendLine($"|{string.Join("|", _header)}|")
.AppendLine($"|{string.Join("|", Enumerable.Repeat("-", _header.Count))}|");
.AppendLine(GetTableRow(_header, prettify))
.AppendLine(GetTableRow(_header.Select(_ => "-"), prettify));
}

foreach(var line in _body)
foreach(var row in _body)
{
builder.AppendLine($"|{string.Join("|", line)}|");
builder.AppendLine(GetTableRow(row, prettify));
}

return builder.ToString();
}

private string GetTableRow(IEnumerable<string> row, bool prettify)
=> "|"
+ row.Select((c, index) => GetTableCellContent(c, index, prettify)).JoinBy("|")
+ "|";

private string GetTableCellContent(string content, int columnIndex, bool prettify)
{
var columnLength = prettify ? GetColumnLength(columnIndex) + 2 : 0;

if(columnLength <= content.Length)
{
return content;
}
else if(content == "-")
{
return string.Join("", Enumerable.Repeat(content, columnLength));
}
else
{
return content
.PadLeft(content.Length + 1)
.PadRight(columnLength);
}
}

private int GetColumnLength(int index)
=> _body
.Select(l => l.ElementAtOrDefault(index))
.Concat(_header.ElementAtOrDefault(index))
.Trim()
.Max(s => s.Length);
}
}
}
}
5 changes: 5 additions & 0 deletions src/NuGet.Shared/Helpers/PackageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ internal static class PackageHelper

public static string GetUrl(string packageId, NuGetVersion version, Uri feedUri)
{
if(feedUri == null)
{
return default;
}

if(feedUri.AbsoluteUri.StartsWith("https://api.nuget.org", StringComparison.OrdinalIgnoreCase))
{
return $"https://www.nuget.org/packages/{packageId}/{version.ToFullString()}";
Expand Down
2 changes: 1 addition & 1 deletion src/NuGet.Updater.Tests/Entities/TestPackageFeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ public async Task<FeedVersion[]> GetPackageVersions(
?.Select(v => new FeedVersion(v, Url))
.ToArray() ?? new FeedVersion[0];

public Task PushPackage(CancellationToken ct, LocalPackage package) => throw new NotSupportedException();
public Task<bool> PushPackage(CancellationToken ct, LocalPackage package) => throw new NotSupportedException();
}
}
21 changes: 21 additions & 0 deletions src/NuGet.Updater/Entities/LogDisplayOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace NuGet.Updater.Entities
{
internal struct LogDisplayOptions
{
public static readonly LogDisplayOptions Summary = new LogDisplayOptions
{
IncludeUrls = true,
PrettifyTable = false,
};

public static readonly LogDisplayOptions Console = new LogDisplayOptions
{
IncludeUrls = false,
PrettifyTable = true,
};

public bool IncludeUrls { get; set; }

public bool PrettifyTable { get; set; }
}
}
33 changes: 33 additions & 0 deletions src/NuGet.Updater/Extensions/FeedVersionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Shared.Entities;
using NuGet.Updater.Entities;

namespace NuGet.Updater.Extensions
{
public static class FeedVersionExtensions
{
/// <summary>
/// Gets the latest version for the given reference by looking up first in a list of known packages.
/// Useful in the cases where refernces to multiple versions of the same packages are found.
/// </summary>
public static async Task<FeedVersion> GetLatestVersion(
this PackageReference reference,
CancellationToken ct,
IEnumerable<UpdaterPackage> knownPackages,
UpdaterParameters parameters
)
{
var knownVersion = knownPackages.FirstOrDefault(p => p.PackageId == reference.Identity.Id)?.Version;

if(knownVersion == null)
{
knownVersion = await reference.GetLatestVersion(ct, parameters);
}

return knownVersion;
}
}
}
Loading

0 comments on commit d67b86c

Please sign in to comment.