update packages
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user