-
Notifications
You must be signed in to change notification settings - Fork 695
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check for duplicate NuGet items, and raise warnings/errors where appr…
…opriate (#4484)
- Loading branch information
Showing
13 changed files
with
994 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
src/NuGet.Core/NuGet.Build.Tasks/CheckForDuplicateNuGetItemsTask.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// 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; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using Microsoft.Build.Framework; | ||
using Newtonsoft.Json; | ||
using NuGet.Commands; | ||
using NuGet.Common; | ||
using NuGet.ProjectModel; | ||
using Task = Microsoft.Build.Utilities.Task; | ||
|
||
|
||
namespace NuGet.Build.Tasks | ||
{ | ||
public class CheckForDuplicateNuGetItemsTask : Task | ||
{ | ||
[Required] | ||
public ITaskItem[] Items { get; set; } | ||
|
||
[Required] | ||
public string ItemName { get; set; } | ||
|
||
[Required] | ||
public string LogCode { get; set; } | ||
|
||
[Required] | ||
public string MSBuildProjectFullPath { get; set; } | ||
|
||
public string TreatWarningsAsErrors { get; set; } | ||
|
||
public string WarningsAsErrors { get; set; } | ||
|
||
public string NoWarn { get; set; } | ||
|
||
[Output] | ||
public ITaskItem[] DeduplicatedItems { get; set; } | ||
|
||
public override bool Execute() | ||
{ | ||
DeduplicatedItems = Array.Empty<ITaskItem>(); | ||
var itemGroups = Items.GroupBy(i => i.ItemSpec, StringComparer.OrdinalIgnoreCase); | ||
var duplicateItems = itemGroups.Where(g => g.Count() > 1).ToList(); | ||
|
||
if (duplicateItems.Any()) | ||
{ | ||
var logger = new PackCollectorLogger( | ||
new MSBuildLogger(Log), | ||
EvaluateWarningProperties(WarningsAsErrors, NoWarn, TreatWarningsAsErrors) | ||
); | ||
string duplicateItemsFormatted = string.Join("; ", duplicateItems.Select(d => string.Join(", ", d.Select(e => $"{e.ItemSpec} {e.GetMetadata("version")}")))); | ||
NuGetLogCode logCode = (NuGetLogCode)Enum.Parse(typeof(NuGetLogCode), LogCode); | ||
|
||
logger.Log(new RestoreLogMessage( | ||
LogLevel.Warning, | ||
logCode, | ||
string.Format( | ||
CultureInfo.CurrentCulture, | ||
Strings.Error_DuplicateItems, | ||
ItemName, | ||
duplicateItemsFormatted)) | ||
{ | ||
FilePath = MSBuildProjectFullPath, | ||
}); | ||
|
||
// Set Output | ||
DeduplicatedItems = itemGroups.Select(g => g.First()).ToArray(); | ||
} | ||
|
||
return !Log.HasLoggedErrors; | ||
} | ||
|
||
private WarningProperties EvaluateWarningProperties(string warningsAsErrors, string noWarn, string treatWarningsAsErrors) | ||
{ | ||
var warnAsErrorCodes = new HashSet<NuGetLogCode>(); | ||
ReadNuGetLogCodes(warningsAsErrors, warnAsErrorCodes); | ||
var noWarnCodes = new HashSet<NuGetLogCode>(); | ||
ReadNuGetLogCodes(noWarn, noWarnCodes); | ||
_ = bool.TryParse(treatWarningsAsErrors, out bool allWarningsAsErrors); | ||
|
||
return new WarningProperties(warnAsErrorCodes, noWarnCodes, allWarningsAsErrors); | ||
} | ||
|
||
private static void ReadNuGetLogCodes(string str, HashSet<NuGetLogCode> hashCodes) | ||
{ | ||
foreach (var code in MSBuildStringUtility.Split(str)) | ||
{ | ||
if (Enum.TryParse(code, out NuGetLogCode logCode)) | ||
{ | ||
hashCodes.Add(logCode); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.