dotnet upgrade

This commit is contained in:
Matthew Parker [SSW]
2023-11-22 17:58:39 +10:00
parent 83c5676f1d
commit 1461b5cb82
3 changed files with 63 additions and 0 deletions

View File

@@ -153,6 +153,24 @@ public partial class MainWindowViewModel : ViewModelBase
ParityResults?.Add(e.Message);
}
}
[RelayCommand]
private async Task UpdateAllProjectsToNet80(CancellationToken token)
{
ErrorMessages?.Clear();
ParityResults.Clear();
ResultsLabel = string.Empty;
try
{
DotNetUpgrade.UpdateProjectsInSolutionToNet80(SolutionFilePath);
ResultsLabel = "Successfully updated all projects in solution to .NET 8";
}
catch (Exception e)
{
ResultsLabel = "Failed to update all projects in solution to .NET 8";
ParityResults?.Add(e.Message);
}
}
[RelayCommand]
private async Task LoadSolutionFile(CancellationToken token)

View File

@@ -95,6 +95,15 @@
Clear bin and obj folders
</TextBlock>
</Button>
<Button Grid.Row="2" Grid.Column="1" MinHeight="130" Padding="10" Margin="5"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
VerticalContentAlignment="Center" HorizontalContentAlignment="Center"
IsEnabled="{Binding SolutionFilePath, Mode=OneWay, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"
Command="{Binding UpdateAllProjectsToNet80Command}">
<TextBlock TextWrapping="Wrap">
Update all projects in Solution to .NET 8.0
</TextBlock>
</Button>
</Grid>
<Label HorizontalAlignment="Center" Name="ResultsLabel" Content="{Binding ResultsLabel}"></Label>
</StackPanel>

View File

@@ -0,0 +1,36 @@
using Microsoft.Build.Construction;
using Microsoft.Build.Definition;
using Microsoft.Build.Evaluation;
namespace DotNetSolutionTools.Core;
public static class DotNetUpgrade
{
public static void UpdateProjectsInSolutionToNet80(string solutionFilePath)
{
var solutionFile = SolutionFile.Parse(solutionFilePath);
var csprojList = SolutionProjectParity.GetCSharpProjectObjectsFromSolutionFile(
solutionFile
);
UpdateProjectsToNet80(csprojList);
}
private static void 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
.SelectMany(x => x.Properties)
.FirstOrDefault(x => x.Name == "TargetFramework");
if (targetFramework?.Value is "net7.0")
{
targetFramework.Value = "net8.0";
project.Save();
FormatCsproj.FormatCsprojFile(project.FullPath);
}
}
}
}