ok
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace ProjectNugetDuplicateFinder
|
||||
{
|
||||
public class FileSearchCriteria
|
||||
{
|
||||
public string[] Extensions { get; set; } = new string[0];
|
||||
|
||||
public string BaseDirectory { get; set; }
|
||||
|
||||
public string[] TargetDirectories { get; set; } = new string[0];
|
||||
|
||||
public IEnumerable<string> AbsoluteTargetDirectories
|
||||
{
|
||||
get
|
||||
{
|
||||
return TargetDirectories?
|
||||
.Select(td => Path.Combine(BaseDirectory, td))
|
||||
.Where(td => !string.IsNullOrWhiteSpace(td) && Directory.Exists(td));
|
||||
}
|
||||
}
|
||||
|
||||
public bool Recursive { get; set; }
|
||||
|
||||
public SearchOption SearchOption
|
||||
{
|
||||
get
|
||||
{
|
||||
return Recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
using Semver;
|
||||
|
||||
namespace ProjectNugetDuplicateFinder
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var searchCriteria = new FileSearchCriteria
|
||||
{
|
||||
Extensions = new string[] { ".csproj" },
|
||||
BaseDirectory = "D:\\Projects\\onelia",
|
||||
TargetDirectories = new string[] { "onelia" },
|
||||
Recursive = true,
|
||||
};
|
||||
|
||||
var result = new Finder().Find(searchCriteria);
|
||||
|
||||
|
||||
//result = result.Where(f => f.EndsWith("Falcon.Api.Payment.Tests.csproj")).ToList();
|
||||
|
||||
foreach (var project in result)
|
||||
{
|
||||
try
|
||||
{
|
||||
var text = File.ReadAllText(project);
|
||||
var document = XDocument.Parse(text);
|
||||
var dublicates = document
|
||||
.Descendants("ItemGroup")
|
||||
.Descendants("PackageReference")
|
||||
.GroupBy(f => f.Attribute("Include").Value.ToLower())
|
||||
.Where(f => f.Count() > 1);
|
||||
|
||||
var isAnyRemoved = false;
|
||||
foreach (var duplicate in dublicates)
|
||||
{
|
||||
var maxVersion = duplicate.Max(f => SemVersion.Parse(f.Attribute("Version").Value));
|
||||
|
||||
var max = duplicate.First(f => f.Attribute("Version").Value == maxVersion.ToString());
|
||||
IEnumerable<XElement> toRemove = duplicate.Except(new List<XElement> { max });
|
||||
foreach (var xElement in toRemove)
|
||||
{
|
||||
xElement.Remove();
|
||||
isAnyRemoved = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (isAnyRemoved)
|
||||
{
|
||||
var newXml = document.ToString();
|
||||
File.WriteAllText(project, newXml);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex);
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine(string.Join("\n", result));
|
||||
Console.ReadLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace ProjectNugetDuplicateFinder
|
||||
{
|
||||
public class Finder
|
||||
{
|
||||
public IEnumerable<string> Find(FileSearchCriteria criteria)
|
||||
{
|
||||
return criteria.AbsoluteTargetDirectories.SelectMany(td => FindFiles(td, criteria.Extensions, criteria.SearchOption));
|
||||
}
|
||||
|
||||
public IEnumerable<string> FindFiles(string folderPath, string[] extensions, SearchOption searchOption)
|
||||
{
|
||||
return Directory.GetFiles(folderPath, "*.*", searchOption)
|
||||
.Where(s =>
|
||||
!extensions.Any() ||
|
||||
extensions.Any(e => string.Compare(e, Path.GetExtension(s), true) == 0)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="semver" Version="2.0.6" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user