diff --git a/src/FileSearchCriteria.cs b/src/FileSearchCriteria.cs new file mode 100644 index 0000000..b9713e6 --- /dev/null +++ b/src/FileSearchCriteria.cs @@ -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 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; + } + } + + } +} \ No newline at end of file diff --git a/src/Program.cs b/src/Program.cs new file mode 100644 index 0000000..bc5fad7 --- /dev/null +++ b/src/Program.cs @@ -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 toRemove = duplicate.Except(new List { 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(); + } + } +} diff --git a/src/ProjectGinder.cs b/src/ProjectGinder.cs new file mode 100644 index 0000000..c161d81 --- /dev/null +++ b/src/ProjectGinder.cs @@ -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 Find(FileSearchCriteria criteria) + { + return criteria.AbsoluteTargetDirectories.SelectMany(td => FindFiles(td, criteria.Extensions, criteria.SearchOption)); + } + + public IEnumerable 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) + ); + } + } +} diff --git a/src/ProjectNugetDuplicateFinder.csproj b/src/ProjectNugetDuplicateFinder.csproj new file mode 100644 index 0000000..18fe259 --- /dev/null +++ b/src/ProjectNugetDuplicateFinder.csproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp3.1 + + + + + + +