add package version consistency check
This commit is contained in:
@@ -158,6 +158,27 @@ public partial class MainWindowViewModel : ObservableObject
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[RelayCommand]
|
||||||
|
private async Task CheckForInconsistentNugetPackageVersionsInSolutionFile(CancellationToken token)
|
||||||
|
{
|
||||||
|
SetBeginCommandState();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await Task.Run(
|
||||||
|
() =>
|
||||||
|
{
|
||||||
|
var results = PackageVersionConsistency.FindInconsistentNugetPackageVersions(SolutionFilePath);
|
||||||
|
SetCommandSuccessState($"{results.Count} packages with inconsistent versions", results);
|
||||||
|
},
|
||||||
|
token
|
||||||
|
);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
SetCommandFailureState("Failed to check for inconsistent package versions in solution file", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[RelayCommand]
|
[RelayCommand]
|
||||||
private async Task CheckForMissingImplicitUsingsInSolutionFile(CancellationToken token)
|
private async Task CheckForMissingImplicitUsingsInSolutionFile(CancellationToken token)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -31,7 +31,7 @@
|
|||||||
<TextBlock Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center" Name="CsprojFilePath" Text="{Binding CsprojFilePath}" />
|
<TextBlock Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center" Name="CsprojFilePath" Text="{Binding CsprojFilePath}" />
|
||||||
<Button Grid.Column="2" Width="60" HorizontalContentAlignment="Center" Command="{Binding ClearCsprojFileCommand}">Clear</Button>
|
<Button Grid.Column="2" Width="60" HorizontalContentAlignment="Center" Command="{Binding ClearCsprojFileCommand}">Clear</Button>
|
||||||
</Grid>
|
</Grid>
|
||||||
<Grid Margin="0 5 0 0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ShowGridLines="False" RowDefinitions="*,*,*" ColumnDefinitions="*,*,*">
|
<Grid Margin="0 5 0 0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ShowGridLines="False" RowDefinitions="*,*,*,*" ColumnDefinitions="*,*,*">
|
||||||
<Button Grid.Row="0" Grid.Column="0" MinHeight="100" Padding="10" Margin="5"
|
<Button Grid.Row="0" Grid.Column="0" MinHeight="100" Padding="10" Margin="5"
|
||||||
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
|
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
|
||||||
VerticalContentAlignment="Center" HorizontalContentAlignment="Center"
|
VerticalContentAlignment="Center" HorizontalContentAlignment="Center"
|
||||||
@@ -118,6 +118,15 @@
|
|||||||
Update C# Project to .NET 8.0
|
Update C# Project to .NET 8.0
|
||||||
</TextBlock>
|
</TextBlock>
|
||||||
</Button>
|
</Button>
|
||||||
|
<Button Grid.Row="3" Grid.Column="0" MinHeight="100" Padding="10" Margin="5"
|
||||||
|
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
|
||||||
|
VerticalContentAlignment="Center" HorizontalContentAlignment="Center"
|
||||||
|
IsEnabled="{Binding SolutionFilePath, Mode=OneWay, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"
|
||||||
|
Command="{Binding CheckForInconsistentNugetPackageVersionsInSolutionFileCommand}">
|
||||||
|
<TextBlock TextWrapping="Wrap">
|
||||||
|
Check for Inconsistent Nuget Package Versions
|
||||||
|
</TextBlock>
|
||||||
|
</Button>
|
||||||
</Grid>
|
</Grid>
|
||||||
<Label Width="{Binding $parent.Bounds.Width}" Background="{Binding ResultsLabelColor}" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Name="ResultsLabel" Content="{Binding ResultsLabel}"></Label>
|
<Label Width="{Binding $parent.Bounds.Width}" Background="{Binding ResultsLabelColor}" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Name="ResultsLabel" Content="{Binding ResultsLabel}"></Label>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
|
|||||||
76
DotNetSolutionTools.Core/PackageVersionConsistency.cs
Normal file
76
DotNetSolutionTools.Core/PackageVersionConsistency.cs
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
using DotNetSolutionTools.Core.Common;
|
||||||
|
using Microsoft.Build.Construction;
|
||||||
|
using Microsoft.Build.Definition;
|
||||||
|
using Microsoft.Build.Evaluation;
|
||||||
|
using NuGet.Versioning;
|
||||||
|
|
||||||
|
namespace DotNetSolutionTools.Core;
|
||||||
|
|
||||||
|
public class PackageVersionConsistency
|
||||||
|
{
|
||||||
|
public static List<string> FindInconsistentNugetPackageVersions(string solutionFilePath)
|
||||||
|
{
|
||||||
|
var solutionFile = SolutionFile.Parse(solutionFilePath);
|
||||||
|
var csprojList = SlnHelper.GetCSharpProjectObjectsFromSolutionFile(solutionFile);
|
||||||
|
var packageList = new List<(string, NuGetVersion)>();
|
||||||
|
var outOfSyncPackages = new List<(string packageName, NuGetVersion version)>();
|
||||||
|
foreach (var project in csprojList)
|
||||||
|
{
|
||||||
|
var projectPackages = GetPackageVersions(project);
|
||||||
|
packageList.AddRange(projectPackages);
|
||||||
|
}
|
||||||
|
|
||||||
|
var groupedByPackage = packageList.GroupBy(s => s.Item1).ToList();
|
||||||
|
foreach (var package in groupedByPackage)
|
||||||
|
{
|
||||||
|
var versions = package.Select(s => s.Item2).Distinct().ToList();
|
||||||
|
if (versions.Count > 1)
|
||||||
|
{
|
||||||
|
outOfSyncPackages.Add((package.Key, versions.First()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine("Logging out of sync packages");
|
||||||
|
outOfSyncPackages.ForEach(s => Console.WriteLine(s.packageName));
|
||||||
|
|
||||||
|
return outOfSyncPackages.Select(s => s.packageName).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<(string, NuGetVersion)> GetPackageVersions(ProjectRootElement project)
|
||||||
|
{
|
||||||
|
Project? evalProject = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
evalProject = Project.FromProjectRootElement(
|
||||||
|
project,
|
||||||
|
new ProjectOptions { LoadSettings = ProjectLoadSettings.IgnoreMissingImports }
|
||||||
|
);
|
||||||
|
var packages = evalProject
|
||||||
|
.Items.Where(x => x.ItemType == "PackageReference" && x.HasMetadata("Version"))
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
var packageNameAndVersion = packages
|
||||||
|
.Select(x =>
|
||||||
|
(
|
||||||
|
x.EvaluatedInclude,
|
||||||
|
NuGetVersion.Parse(x.Metadata.First(s => s.Name == "Version").UnevaluatedValue)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.ToList();
|
||||||
|
return packageNameAndVersion;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Console.WriteLine(e);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (evalProject is not null)
|
||||||
|
{
|
||||||
|
ProjectCollection.GlobalProjectCollection.UnloadProject(evalProject);
|
||||||
|
ProjectCollection.GlobalProjectCollection.TryUnloadProject(project);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user