support preview versions
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
using Microsoft.Build.Construction;
|
using Microsoft.Build.Construction;
|
||||||
using Microsoft.Build.Definition;
|
using Microsoft.Build.Definition;
|
||||||
using Microsoft.Build.Evaluation;
|
using Microsoft.Build.Evaluation;
|
||||||
|
using NuGet.Versioning;
|
||||||
|
|
||||||
namespace DotNetSolutionTools.Core;
|
namespace DotNetSolutionTools.Core;
|
||||||
|
|
||||||
@@ -24,54 +25,71 @@ public static class DotNetUpgrade
|
|||||||
.PropertyGroups
|
.PropertyGroups
|
||||||
.SelectMany(x => x.Properties)
|
.SelectMany(x => x.Properties)
|
||||||
.FirstOrDefault(x => x.Name == "TargetFramework");
|
.FirstOrDefault(x => x.Name == "TargetFramework");
|
||||||
if (targetFramework?.Value is "net7.0" or "net6.0" or "net5.0")
|
if (targetFramework?.Value is "net8.0" or "net7.0" or "net6.0" or "net5.0")
|
||||||
|
{
|
||||||
|
if (targetFramework.Value is not "net8.0")
|
||||||
{
|
{
|
||||||
targetFramework.Value = "net8.0";
|
targetFramework.Value = "net8.0";
|
||||||
project.Save();
|
project.Save();
|
||||||
FormatCsproj.FormatCsprojFile(project.FullPath);
|
FormatCsproj.FormatCsprojFile(project.FullPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
await UpdatePackagesToLatest(project);
|
await UpdatePackagesToLatest(project);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static async Task UpdatePackagesToLatest(ProjectRootElement project)
|
public static async Task UpdatePackagesToLatest(ProjectRootElement project)
|
||||||
{
|
{
|
||||||
var evalProject = Project.FromProjectRootElement(
|
Project? evalProject = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
evalProject = Project.FromProjectRootElement(
|
||||||
project,
|
project,
|
||||||
new ProjectOptions() { LoadSettings = ProjectLoadSettings.IgnoreMissingImports }
|
new ProjectOptions() { LoadSettings = ProjectLoadSettings.IgnoreMissingImports }
|
||||||
);
|
);
|
||||||
var packages = evalProject.Items.Where(x => x.ItemType == "PackageReference");
|
var packages = evalProject.Items.Where(x =>
|
||||||
|
x.ItemType == "PackageReference" && x.HasMetadata("Version") &&
|
||||||
|
x.EvaluatedInclude.StartsWith("Microsoft.")).ToList();
|
||||||
|
|
||||||
var packageNameAndVersion = packages
|
var packageNameAndVersion = packages
|
||||||
.Where(s => s.EvaluatedInclude.StartsWith("Microsoft."))
|
|
||||||
.Select(
|
.Select(
|
||||||
x =>
|
x =>
|
||||||
new
|
new
|
||||||
{
|
{
|
||||||
Package = x,
|
Package = x,
|
||||||
Name = x.EvaluatedInclude,
|
Name = x.EvaluatedInclude,
|
||||||
Version = Version.Parse(
|
NugetVersion = NuGetVersion.Parse(
|
||||||
x.Metadata.First(s => s.Name == "Version").UnevaluatedValue
|
x.Metadata.First(s => s.Name == "Version").UnevaluatedValue
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
var shouldFormat = false;
|
var shouldSave = false;
|
||||||
foreach (var package in packageNameAndVersion)
|
foreach (var package in packageNameAndVersion)
|
||||||
{
|
{
|
||||||
var latestNugetVersion = await NugetLookup.FetchPackageMetadataAsync(package.Name);
|
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 (latestNugetVersion > package.NugetVersion)
|
||||||
if (normalizedVersion > package.Version)
|
|
||||||
{
|
{
|
||||||
shouldFormat = true;
|
shouldSave = true;
|
||||||
package.Package.SetMetadataValue("Version", latestNugetVersion.ToString());
|
package.Package.SetMetadataValue("Version", latestNugetVersion.ToString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (shouldSave)
|
||||||
|
{
|
||||||
project.Save();
|
project.Save();
|
||||||
if (shouldFormat)
|
|
||||||
FormatCsproj.FormatCsprojFile(project.FullPath);
|
FormatCsproj.FormatCsprojFile(project.FullPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (evalProject is not null)
|
||||||
|
{
|
||||||
|
ProjectCollection.GlobalProjectCollection.UnloadProject(evalProject);
|
||||||
|
ProjectCollection.GlobalProjectCollection.TryUnloadProject(project);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user