Skip to content

Commit

Permalink
Reducung visible propertites in restore collector logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Ankit Mishra committed Aug 25, 2017
1 parent 6f725f7 commit 1697f19
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,68 @@ public class RestoreCollectorLogger : LoggerBase, ICollectorLogger
private readonly ILogger _innerLogger;
private readonly ConcurrentQueue<IRestoreLogMessage> _errors;
private readonly bool _hideWarningsAndErrors;
private IEnumerable<RestoreTargetGraph> _restoreTargetGraphs;
private PackageSpec _projectSpec;
private WarningPropertiesCollection _transitiveWarningPropertiesCollection;

public string ProjectPath => _projectSpec?.RestoreMetadata?.ProjectPath;

public IEnumerable<IRestoreLogMessage> Errors => _errors.ToArray();

public WarningPropertiesCollection ProjectWarningPropertiesCollection { get; set; }

public WarningPropertiesCollection TransitiveWarningPropertiesCollection { get; set; }

public IEnumerable<RestoreTargetGraph> RestoreTargetGraphs { get; set; }
public WarningPropertiesCollection TransitiveWarningPropertiesCollection
{
get
{
if (_transitiveWarningPropertiesCollection == null)
{
// Populate TransitiveWarningPropertiesCollection only if it is null and we have RestoreTargetGraphs.
// This will happen at most once and only if we have the project spec with restore metadata.
if (_restoreTargetGraphs != null &&
_restoreTargetGraphs.Any() &&
_projectSpec != null &&
_projectSpec.RestoreMetadata != null)
{
var transitiveNoWarnUtils = new TransitiveNoWarnUtils();

TransitiveWarningPropertiesCollection = transitiveNoWarnUtils.CreateTransitiveWarningPropertiesCollection(
_restoreTargetGraphs,
_projectSpec);
}
}

return _transitiveWarningPropertiesCollection;
}

set => _transitiveWarningPropertiesCollection = value;
}

/// <summary>
/// Stores a reference to PackageSpec for the project from the restore request.
/// This are used to generate the warning properties for the project.
/// </summary>
/// <param name="projectSpec">PackageSpec to be stored for reference.</param>
public void ApplyRestoreInputs(PackageSpec projectSpec)
{
_projectSpec = projectSpec;

public PackageSpec ProjectSpec { get; set; }
ProjectWarningPropertiesCollection = new WarningPropertiesCollection(
projectSpec.RestoreMetadata?.ProjectWideWarningProperties,
PackageSpecificWarningProperties.CreatePackageSpecificWarningProperties(projectSpec),
projectSpec.TargetFrameworks.Select(f => f.FrameworkName).AsList().AsReadOnly()
);
}

public string ProjectPath => ProjectSpec?.RestoreMetadata?.ProjectPath;
/// <summary>
/// Stores a reference to RestoreTargetGraphs from the restore output.
/// These graphs are used to generate the transitive warning properties.
/// </summary>
/// <param name="restoreTargetGraphs">RestoreTargetGraphs to be stored for reference.</param>
public void ApplyRestoreOutput(IEnumerable<RestoreTargetGraph> restoreTargetGraphs)
{
_restoreTargetGraphs = restoreTargetGraphs;
}

/// <summary>
/// Initializes an instance of the <see cref="RestoreCollectorLogger"/>, while still
Expand Down Expand Up @@ -164,31 +214,11 @@ private bool IsWarningSuppressed(IRestoreLogMessage message)
}
else
{
// Initialize transitive warning properties only if the project does not suppress the warning.
TryPopulateTransitiveWarningPropertiesCollection(message);
// Use transitive warning properties only if the project does not suppress the warning.
return TransitiveWarningPropertiesCollection != null && TransitiveWarningPropertiesCollection.ApplyWarningProperties(message);
}
}

private void TryPopulateTransitiveWarningPropertiesCollection(ILogMessage message)
{
// Populate TransitiveWarningPropertiesCollection only if it is null and we have RestoreTargetGraphs.
// This will happen at most once and only if we have the project spec with restore metadata.
if (message.Level == LogLevel.Warning &&
TransitiveWarningPropertiesCollection == null &&
RestoreTargetGraphs != null &&
RestoreTargetGraphs.Any() &&
ProjectSpec != null &&
ProjectSpec.RestoreMetadata != null)
{
var transitiveNoWarnUtils = new TransitiveNoWarnUtils();

TransitiveWarningPropertiesCollection = transitiveNoWarnUtils.CreateTransitiveWarningPropertiesCollection(
RestoreTargetGraphs,
ProjectSpec);
}
}

private static IRestoreLogMessage ToRestoreLogMessage(ILogMessage message)
{
var restoreLogMessage = message as IRestoreLogMessage;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -121,7 +121,7 @@ await InstallPackagesAsync(runtimeGraphs,

// Update the logger with the restore target graphs
// This allows lazy initialization for the Transitive Warning Properties
_logger.RestoreTargetGraphs = graphs;
_logger.ApplyRestoreOutput(graphs);

// Warn for all dependencies that do not have exact matches or
// versions that have been bumped up unexpectedly.
Expand Down
15 changes: 3 additions & 12 deletions src/NuGet.Core/NuGet.Commands/RestoreCommand/RestoreCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -52,17 +52,8 @@ public RestoreCommand(RestoreRequest request)

var collectorLoggerHideWarningsAndErrors = request.Project.RestoreSettings.HideWarningsAndErrors
|| request.HideWarningsAndErrors;

var collectorLogger = new RestoreCollectorLogger(_request.Log, collectorLoggerHideWarningsAndErrors)
{
ProjectSpec = _request.Project,
ProjectWarningPropertiesCollection = new WarningPropertiesCollection(
request.Project.RestoreMetadata?.ProjectWideWarningProperties,
PackageSpecificWarningProperties.CreatePackageSpecificWarningProperties(request.Project),
request.Project.TargetFrameworks.Select(f => f.FrameworkName).AsList().AsReadOnly()
)
};

var collectorLogger = new RestoreCollectorLogger(_request.Log, collectorLoggerHideWarningsAndErrors);
collectorLogger.ApplyRestoreInputs(_request.Project);
_logger = collectorLogger;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void CollectorLogger_DoesNotPassLogCallsToInnerLoggerByDefaultWithFilePat
var innerLogger = new Mock<ILogger>();
var collector = new RestoreCollectorLogger(innerLogger.Object, LogLevel.Debug, hideWarningsAndErrors: false)
{
ProjectSpec = new PackageSpec()
_projectSpec = new PackageSpec()
{
RestoreMetadata = new ProjectRestoreMetadata()
{
Expand Down

0 comments on commit 1697f19

Please sign in to comment.