update packages

This commit is contained in:
Matthew Parker
2023-11-22 22:11:07 +10:00
parent 1461b5cb82
commit 091c118b34
3 changed files with 75 additions and 9 deletions

View File

@@ -8,6 +8,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Build" Version="17.8.3" />
<PackageReference Include="NuGet.Protocol" Version="6.8.0" />
</ItemGroup>
</Project>

View File

@@ -1,4 +1,5 @@
using Microsoft.Build.Construction;
using DotNetSolutionTools.Core.Infrastructure;
using Microsoft.Build.Construction;
using Microsoft.Build.Definition;
using Microsoft.Build.Evaluation;
@@ -6,23 +7,21 @@ namespace DotNetSolutionTools.Core;
public static class DotNetUpgrade
{
public static void UpdateProjectsInSolutionToNet80(string solutionFilePath)
public static async Task UpdateProjectsInSolutionToNet80(string solutionFilePath)
{
var solutionFile = SolutionFile.Parse(solutionFilePath);
var csprojList = SolutionProjectParity.GetCSharpProjectObjectsFromSolutionFile(
solutionFile
);
UpdateProjectsToNet80(csprojList);
await UpdateProjectsToNet80(csprojList);
}
private static void UpdateProjectsToNet80(List<ProjectRootElement> projects)
private static async Task UpdateProjectsToNet80(List<ProjectRootElement> projects)
{
foreach (var project in projects)
{
var evalProject = Project.FromProjectRootElement(project, new ProjectOptions(){LoadSettings = ProjectLoadSettings.IgnoreMissingImports});
var packages = evalProject.GetItems("PackageReference");
//packages.First().Metadata.First().UnevaluatedValue = "9.0.0";
var targetFramework = project.PropertyGroups
var targetFramework = project
.PropertyGroups
.SelectMany(x => x.Properties)
.FirstOrDefault(x => x.Name == "TargetFramework");
if (targetFramework?.Value is "net7.0")
@@ -31,6 +30,46 @@ public static class DotNetUpgrade
project.Save();
FormatCsproj.FormatCsprojFile(project.FullPath);
}
await UpdatePackagesToLatest(project);
}
}
}
public static async Task UpdatePackagesToLatest(ProjectRootElement project)
{
var evalProject = Project.FromProjectRootElement(
project,
new ProjectOptions() { LoadSettings = ProjectLoadSettings.IgnoreMissingImports }
);
var packages = evalProject.Items.Where(x => x.ItemType == "PackageReference");
var packageNameAndVersion = packages
.Select(
x =>
new
{
Package = x,
Name = x.EvaluatedInclude,
Version = Version.Parse(
x.Metadata.First(s => s.Name == "Version").UnevaluatedValue
)
}
)
.ToList();
var shouldFormat = false;
foreach (var package in packageNameAndVersion)
{
var latestNugetVersion = await NugetLookup.FetchPackageMetadataAsync(package.Name);
// it will compare 6.8.0.0 to 6.8.0, and says left is newer, we dont want to say its newer, so use the normalized string to make a new version
var normalizedVersion = Version.Parse(latestNugetVersion.ToString());
if (normalizedVersion > package.Version)
{
shouldFormat = true;
package.Package.SetMetadataValue("Version", latestNugetVersion.ToString());
project.Save();
}
}
if (shouldFormat)
FormatCsproj.FormatCsprojFile(project.FullPath);
}
}

View File

@@ -0,0 +1,26 @@
using NuGet.Common;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using NuGet.Versioning;
namespace DotNetSolutionTools.Core.Infrastructure;
public static class NugetLookup
{
public static async Task<NuGetVersion> FetchPackageMetadataAsync(string packageId)
{
var cache = new SourceCacheContext();
var repositories = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
var logger = NullLogger.Instance;
var resource = await repositories.GetResourceAsync<FindPackageByIdResource>();
var versions = await resource.GetAllVersionsAsync(
packageId,
cache,
logger,
CancellationToken.None
);
return versions.Last(s => s.IsPrerelease == false);
}
}