Run long running work with Task.Run

This commit is contained in:
Matthew Parker [SSW]
2024-01-21 13:31:35 +10:00
parent 94dd69570d
commit 3bd688966b
4 changed files with 108 additions and 63 deletions

View File

@@ -22,7 +22,7 @@ public partial class MainWindowViewModel : ViewModelBase
private string _csprojFilePath = string.Empty; private string _csprojFilePath = string.Empty;
[ObservableProperty] [ObservableProperty]
private ObservableCollection<string> _parityResults = new() { }; private ObservableCollection<string> _operationResults = new() { };
[ObservableProperty] [ObservableProperty]
private string _resultsLabel = "Ready"; private string _resultsLabel = "Ready";
@@ -30,164 +30,214 @@ public partial class MainWindowViewModel : ViewModelBase
[RelayCommand] [RelayCommand]
private async Task ExecuteParityChecker(CancellationToken token) private async Task ExecuteParityChecker(CancellationToken token)
{ {
ParityResults.Clear(); OperationResults.Clear();
ErrorMessages?.Clear();
ResultsLabel = string.Empty; ResultsLabel = string.Empty;
try try
{ {
var results = SolutionProjectParity.CompareSolutionAndCSharpProjects(SolutionFolderPath, SolutionFilePath); ResultsLabel = "Running...";
foreach (var result in results) await Task.Run(() =>
ParityResults.Add(result); {
ResultsLabel = $"{results.Count} Projects in folder missing from solution"; var results = SolutionProjectParity.CompareSolutionAndCSharpProjects(SolutionFolderPath, SolutionFilePath);
foreach (var result in results)
OperationResults.Add(result);
ResultsLabel = $"{results.Count} Projects in folder missing from solution";
}, token);
} }
catch (Exception e) catch (Exception e)
{ {
ResultsLabel = "Error"; ResultsLabel = "Error";
ParityResults?.Add(e.Message); OperationResults?.Add(e.Message);
} }
} }
[RelayCommand] [RelayCommand]
private async Task FormatCsProjFile(CancellationToken token) private async Task FormatCsProjFile(CancellationToken token)
{ {
FormatCsproj.FormatCsprojFile(CsprojFilePath); OperationResults.Clear();
ResultsLabel = string.Empty;
try
{
ResultsLabel = "Running...";
await Task.Run(() =>
{
FormatCsproj.FormatCsprojFile(CsprojFilePath);
}, token);
ResultsLabel = "Successfully formatted csproj file";
}
catch (Exception e)
{
ResultsLabel = "Failed to format csproj file";
OperationResults?.Add(e.Message);
OperationResults?.Add(e.ToString());
}
} }
[RelayCommand] [RelayCommand]
private async Task FormatAllCsprojFilesInSolutionFile(CancellationToken token) private async Task FormatAllCsprojFilesInSolutionFile(CancellationToken token)
{ {
ErrorMessages?.Clear(); OperationResults.Clear();
ResultsLabel = string.Empty;
try try
{ {
var csprojList = SolutionProjectParity.RetrieveAllCSharpProjectFullPathsFromFolder(SolutionFolderPath); ResultsLabel = "Running...";
foreach (var csproj in csprojList) await Task.Run(() =>
{ {
FormatCsproj.FormatCsprojFile(csproj); var csprojList = SolutionProjectParity.RetrieveAllCSharpProjectFullPathsFromFolder(SolutionFolderPath);
} foreach (var csproj in csprojList)
{
FormatCsproj.FormatCsprojFile(csproj);
}
}, token);
} }
catch (Exception e) catch (Exception e)
{ {
ResultsLabel = "Error"; ResultsLabel = "Failed to format all csproj files in solution file";
ParityResults?.Add(e.Message); OperationResults?.Add(e.Message);
OperationResults?.Add(e.ToString());
} }
} }
[RelayCommand] [RelayCommand]
private async Task FormatAllCsprojFilesInSolutionFolder(CancellationToken token) private async Task FormatAllCsprojFilesInSolutionFolder(CancellationToken token)
{ {
ErrorMessages?.Clear(); OperationResults.Clear();
try try
{ {
var csprojList = SolutionProjectParity.RetrieveAllCSharpProjectFullPathsFromFolder(SolutionFolderPath); ResultsLabel = "Running...";
foreach (var csproj in csprojList) await Task.Run(() =>
{ {
FormatCsproj.FormatCsprojFile(csproj); var csprojList = SolutionProjectParity.RetrieveAllCSharpProjectFullPathsFromFolder(SolutionFolderPath);
} foreach (var csproj in csprojList)
{
FormatCsproj.FormatCsprojFile(csproj);
}
}, token);
} }
catch (Exception e) catch (Exception e)
{ {
ResultsLabel = "Error"; ResultsLabel = "Failed to format all csproj files in solution folder";
ParityResults?.Add(e.Message); OperationResults?.Add(e.Message);
OperationResults?.Add(e.ToString());
} }
} }
[RelayCommand] [RelayCommand]
private async Task CheckForMissingImplicitUsingsInSolutionFile(CancellationToken token) private async Task CheckForMissingImplicitUsingsInSolutionFile(CancellationToken token)
{ {
ErrorMessages?.Clear(); OperationResults.Clear();
ParityResults.Clear();
ResultsLabel = string.Empty; ResultsLabel = string.Empty;
try try
{ {
var results = ImplicitUsings.FindCSharpProjectsMissingImplicitUsings(SolutionFilePath); ResultsLabel = "Running...";
results.ForEach(s => ParityResults.Add(s)); await Task.Run(() =>
ResultsLabel = $"{results.Count} Projects missing ImplicitUsings"; {
var results = ImplicitUsings.FindCSharpProjectsMissingImplicitUsings(SolutionFilePath);
results.ForEach(s => OperationResults.Add(s));
ResultsLabel = $"{results.Count} Projects missing ImplicitUsings";
}, token);
} }
catch (Exception e) catch (Exception e)
{ {
ResultsLabel = "Error"; ResultsLabel = "Failed to check for missing implicit usings in solution file";
ParityResults?.Add(e.Message); OperationResults?.Add(e.Message);
OperationResults?.Add(e.ToString());
} }
} }
[RelayCommand] [RelayCommand]
private async Task CheckForMissingTreatWarningsAsErrorsInSolutionFile(CancellationToken token) private async Task CheckForMissingTreatWarningsAsErrorsInSolutionFile(CancellationToken token)
{ {
ErrorMessages?.Clear(); OperationResults.Clear();
ParityResults.Clear();
ResultsLabel = string.Empty; ResultsLabel = string.Empty;
try try
{ {
var results = WarningsAsErrors.FindCSharpProjectsMissingTreatWarningsAsErrors(SolutionFilePath); ResultsLabel = "Running...";
results.ForEach(s => ParityResults.Add(s)); await Task.Run(() =>
ResultsLabel = $"{results.Count} Projects missing TreatWarningsAsErrors"; {
var results = WarningsAsErrors.FindCSharpProjectsMissingTreatWarningsAsErrors(SolutionFilePath);
results.ForEach(s => OperationResults.Add(s));
ResultsLabel = $"{results.Count} Projects missing TreatWarningsAsErrors";
}, token);
} }
catch (Exception e) catch (Exception e)
{ {
ResultsLabel = "Error"; ResultsLabel = "Error";
ParityResults?.Add(e.Message); OperationResults?.Add(e.Message);
OperationResults?.Add(e.ToString());
} }
} }
[RelayCommand] [RelayCommand]
private async Task DeleteBinAndObjFoldersInFolder(CancellationToken token) private async Task DeleteBinAndObjFoldersInFolder(CancellationToken token)
{ {
ErrorMessages?.Clear(); OperationResults.Clear();
ParityResults.Clear();
ResultsLabel = string.Empty; ResultsLabel = string.Empty;
try try
{ {
CleanFolder.DeleteFolderWithOnlyBinAndObjSubFolders(SolutionFolderPath); ResultsLabel = "Running...";
await Task.Run(() =>
{
CleanFolder.DeleteBinObjAndNodeModulesFoldersInFolder(SolutionFolderPath);
}, token);
ResultsLabel = "Successfully deleted bin and obj folders"; ResultsLabel = "Successfully deleted bin and obj folders";
} }
catch (Exception e) catch (Exception e)
{ {
ResultsLabel = "Failed to delete bin and obj folders"; ResultsLabel = "Failed to delete bin and obj folders";
ParityResults?.Add(e.Message); OperationResults?.Add(e.Message);
OperationResults?.Add(e.ToString());
} }
} }
[RelayCommand] [RelayCommand]
private async Task UpdateAllProjectsToNet80(CancellationToken token) private async Task UpdateAllProjectsToNet80(CancellationToken token)
{ {
ErrorMessages?.Clear(); OperationResults.Clear();
ParityResults.Clear();
ResultsLabel = string.Empty; ResultsLabel = string.Empty;
try try
{ {
await DotNetUpgrade.UpdateProjectsInSolutionToNet80(SolutionFilePath); ResultsLabel = "Running...";
await Task.Run(async () =>
{
await DotNetUpgrade.UpdateProjectsInSolutionToNet80(SolutionFilePath);
}, token);
ResultsLabel = "Successfully updated all projects in solution to .NET 8"; ResultsLabel = "Successfully updated all projects in solution to .NET 8";
} }
catch (Exception e) catch (Exception e)
{ {
ResultsLabel = "Failed to update all projects in solution to .NET 8"; ResultsLabel = "Failed to update all projects in solution to .NET 8";
ParityResults?.Add(e.Message); OperationResults?.Add(e.Message);
ParityResults?.Add(e.ToString()); OperationResults?.Add(e.ToString());
} }
} }
[RelayCommand] [RelayCommand]
private async Task UpdateProjectToNet80(CancellationToken token) private async Task UpdateProjectToNet80(CancellationToken token)
{ {
ErrorMessages?.Clear(); OperationResults.Clear();
ParityResults.Clear();
ResultsLabel = string.Empty; ResultsLabel = string.Empty;
try try
{ {
await DotNetUpgrade.UpdateProjectAtPathToNet80(CsprojFilePath); ResultsLabel = "Running...";
await Task.Run(async () =>
{
await DotNetUpgrade.UpdateProjectAtPathToNet80(CsprojFilePath);
}, token);
ResultsLabel = "Successfully updated project to .NET 8"; ResultsLabel = "Successfully updated project to .NET 8";
} }
catch (Exception e) catch (Exception e)
{ {
ResultsLabel = "Failed to update project to .NET 8"; ResultsLabel = "Failed to update project to .NET 8";
ParityResults?.Add(e.Message); OperationResults?.Add(e.Message);
} }
} }
[RelayCommand] [RelayCommand]
private async Task LoadSolutionFile(CancellationToken token) private async Task LoadSolutionFile(CancellationToken token)
{ {
ErrorMessages?.Clear();
try try
{ {
var file = await _fileService.DoOpenFilePickerAsync(); var file = await _fileService.DoOpenFilePickerAsync();
@@ -200,7 +250,7 @@ public partial class MainWindowViewModel : ViewModelBase
catch (Exception e) catch (Exception e)
{ {
ResultsLabel = "Error"; ResultsLabel = "Error";
ParityResults?.Add(e.Message); OperationResults?.Add(e.Message);
} }
} }
@@ -214,7 +264,6 @@ public partial class MainWindowViewModel : ViewModelBase
[RelayCommand] [RelayCommand]
private async Task LoadSolutionFolder(CancellationToken token) private async Task LoadSolutionFolder(CancellationToken token)
{ {
ErrorMessages?.Clear();
try try
{ {
var folder = await _fileService.DoOpenFolderPickerAsync(); var folder = await _fileService.DoOpenFolderPickerAsync();
@@ -227,7 +276,7 @@ public partial class MainWindowViewModel : ViewModelBase
catch (Exception e) catch (Exception e)
{ {
ResultsLabel = "Error"; ResultsLabel = "Error";
ParityResults?.Add(e.Message); OperationResults?.Add(e.Message);
} }
} }
@@ -241,7 +290,6 @@ public partial class MainWindowViewModel : ViewModelBase
[RelayCommand] [RelayCommand]
private async Task LoadCsprojFile(CancellationToken token) private async Task LoadCsprojFile(CancellationToken token)
{ {
ErrorMessages?.Clear();
try try
{ {
var folder = await _fileService.DoOpenFilePickerAsync(); var folder = await _fileService.DoOpenFilePickerAsync();
@@ -254,7 +302,7 @@ public partial class MainWindowViewModel : ViewModelBase
catch (Exception e) catch (Exception e)
{ {
ResultsLabel = "Error"; ResultsLabel = "Error";
ParityResults?.Add(e.Message); OperationResults?.Add(e.Message);
} }
} }

View File

@@ -7,9 +7,6 @@ public partial class ViewModelBase : ObservableObject
{ {
protected ViewModelBase() protected ViewModelBase()
{ {
ErrorMessages = new ObservableCollection<string>();
} }
[ObservableProperty]
private ObservableCollection<string>? _errorMessages;
} }

View File

@@ -120,7 +120,7 @@
<Label HorizontalAlignment="Center" Name="ResultsLabel" Content="{Binding ResultsLabel}"></Label> <Label HorizontalAlignment="Center" Name="ResultsLabel" Content="{Binding ResultsLabel}"></Label>
</StackPanel> </StackPanel>
<ScrollViewer Width="{Binding $parent.Bounds.Width}"> <ScrollViewer Width="{Binding $parent.Bounds.Width}">
<ListBox ScrollViewer.VerticalScrollBarVisibility="Visible" Name="Results" ItemsSource="{Binding ParityResults}" /> <ListBox ScrollViewer.VerticalScrollBarVisibility="Visible" Name="Results" ItemsSource="{Binding OperationResults}" />
</ScrollViewer> </ScrollViewer>
</DockPanel> </DockPanel>
</Window> </Window>

View File

@@ -3,7 +3,7 @@ namespace DotNetSolutionTools.Core;
public static class CleanFolder public static class CleanFolder
{ {
public static void DeleteFolderWithOnlyBinAndObjSubFolders(string folderPath) public static void DeleteBinObjAndNodeModulesFoldersInFolder(string folderPath)
{ {
var nodeModulesFolders = Directory.GetDirectories(folderPath, "*", SearchOption.AllDirectories) var nodeModulesFolders = Directory.GetDirectories(folderPath, "*", SearchOption.AllDirectories)
.Where(x => x.EndsWith(Path.DirectorySeparatorChar + "node_modules")) .Where(x => x.EndsWith(Path.DirectorySeparatorChar + "node_modules"))