This commit is contained in:
Matthew Parker
2023-08-29 23:19:28 +10:00
parent 18a4500c7b
commit fe9b8627c5
6 changed files with 93 additions and 23 deletions

View File

@@ -1,6 +1,8 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SlnAndCsprojParityChecker", "SlnAndCsprojParityChecker\SlnAndCsprojParityChecker.csproj", "{0EAC3CFF-28B2-4ACA-BC18-8148A75F89D9}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SolutionParityChecker.CLI", "SolutionParityChecker.CLI\SolutionParityChecker.CLI.csproj", "{0EAC3CFF-28B2-4ACA-BC18-8148A75F89D9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SolutionParityChecker", "SolutionParityChecker\SolutionParityChecker.csproj", "{2635CBAC-0CEF-4BEE-A6FD-154796A4F467}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -12,5 +14,9 @@ Global
{0EAC3CFF-28B2-4ACA-BC18-8148A75F89D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0EAC3CFF-28B2-4ACA-BC18-8148A75F89D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0EAC3CFF-28B2-4ACA-BC18-8148A75F89D9}.Release|Any CPU.Build.0 = Release|Any CPU
{2635CBAC-0CEF-4BEE-A6FD-154796A4F467}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2635CBAC-0CEF-4BEE-A6FD-154796A4F467}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2635CBAC-0CEF-4BEE-A6FD-154796A4F467}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2635CBAC-0CEF-4BEE-A6FD-154796A4F467}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View File

@@ -3,7 +3,7 @@ using Microsoft.Build.Construction;
using Spectre.Console;
using Spectre.Console.Cli;
namespace SlnAndCsprojParityChecker.Commands;
namespace SolutionParityChecker.CLI.Commands;
public class CompareCommand : Command<CompareCommand.Settings>
{
@@ -13,7 +13,7 @@ public class CompareCommand : Command<CompareCommand.Settings>
public required string SolutionFolderPath { get; set; }
[CommandArgument(1, "<SolutionFilePath>")]
public required string SolutionFilePath{ get; set; }
public required string SolutionFilePath { get; set; }
[CommandOption("-l|--logprojectfiles")]
[Description("true to enable logging of all project files. Default is false.")]
@@ -27,8 +27,9 @@ public class CompareCommand : Command<CompareCommand.Settings>
var pathToSolutionFile = settings.SolutionFilePath;
Console.WriteLine($"Retrieving C# Projects from {folderDirectory}");
var csprojList = Directory.GetFiles(folderDirectory, "*.csproj", SearchOption.AllDirectories);
csprojList = csprojList.Select(x => x.Replace(folderDirectory, "")).ToArray();
var csprojList = SolutionParityChecker.RetrieveAllCSharpProjectNamesFromFolder(
folderDirectory
);
if (settings.LogAllProjectFileNames)
{
@@ -43,29 +44,25 @@ public class CompareCommand : Command<CompareCommand.Settings>
Console.WriteLine($"Parsing Solution File: {pathToSolutionFile}");
// Load the solution file
var solutionFile = SolutionFile.Parse(pathToSolutionFile);
var solutionFile = SolutionParityChecker.ParseSolutionFileFromPath(pathToSolutionFile);
if (solutionFile == null)
{
Console.WriteLine("Failed to parse solution file. The file was either not found or malformed.");
Console.WriteLine(
"Failed to parse solution file. The file was either not found or malformed."
);
return 1;
}
// Get the list of projects
var projects = solutionFile.ProjectsInOrder;
var projectsMissingFromSolution = new List<string>();
foreach (var project in csprojList)
{
var projectInSolution = projects.FirstOrDefault(x => x.RelativePath == project);
if (projectInSolution == null)
{
projectsMissingFromSolution.Add(project);
}
}
var projectsMissingFromSolution = SolutionParityChecker.FindProjectsMissingFromSolution(
csprojList,
solutionFile
);
Console.WriteLine("==================================================");
Console.WriteLine($"Missing {projectsMissingFromSolution.Count} C# Projects from Solution File");
Console.WriteLine(
$"Missing {projectsMissingFromSolution.Count} C# Projects from Solution File"
);
foreach (var project in projectsMissingFromSolution)
{
@@ -75,4 +72,4 @@ public class CompareCommand : Command<CompareCommand.Settings>
Console.WriteLine("Done!");
return 0;
}
}
}

View File

@@ -1,10 +1,10 @@
using SlnAndCsprojParityChecker.Commands;
using SolutionParityChecker.CLI.Commands;
using Spectre.Console.Cli;
var app = new CommandApp();
app.Configure(config =>
{
config.SetApplicationName("SlnAndCsprojParityChecker");
config.SetApplicationName("SolutionParityChecker");
config.ValidateExamples();
config.AddCommand<CompareCommand>("compare");

View File

@@ -14,4 +14,8 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SolutionParityChecker\SolutionParityChecker.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,50 @@
using Microsoft.Build.Construction;
namespace SolutionParityChecker;
public static class SolutionParityChecker
{
public static void CompareSolutionAndCSharpProjects(
string solutionFolderPath,
string solutionFilePath
) { }
public static string[] RetrieveAllCSharpProjectNamesFromFolder(string solutionFolderPath)
{
var csprojList = Directory.GetFiles(
solutionFolderPath,
"*.csproj",
SearchOption.AllDirectories
);
csprojList = csprojList.Select(x => x.Replace(solutionFolderPath, "")).ToArray();
return csprojList;
}
public static SolutionFile? ParseSolutionFileFromPath(string solutionFilePath)
{
var solutionFile = SolutionFile.Parse(solutionFilePath);
return solutionFile;
}
public static List<string> FindProjectsMissingFromSolution(
string[] csprojList,
SolutionFile solutionFile
)
{
var projects = solutionFile.ProjectsInOrder;
var projectsMissingFromSolution = new List<string>();
foreach (var project in csprojList)
{
var projectInSolution = projects.FirstOrDefault(x => x.RelativePath == project);
if (projectInSolution == null)
{
projectsMissingFromSolution.Add(project);
}
}
return projectsMissingFromSolution;
}
}

View File

@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Build" Version="17.7.2" />
</ItemGroup>
</Project>