This commit is contained in:
Matthew Parker
2023-08-30 01:08:03 +10:00
parent aefb12e688
commit c343612873
6 changed files with 68 additions and 53 deletions

View File

@@ -26,4 +26,8 @@
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.0-preview8"/> <PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.0-preview8"/>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.0"/> <PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.0"/>
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SolutionParityChecker\SolutionParityChecker.csproj" />
</ItemGroup>
</Project> </Project>

View File

@@ -1,4 +1,6 @@
using System; using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
@@ -13,14 +15,37 @@ namespace SolutionParityChecker.App.ViewModels;
public partial class MainWindowViewModel : ViewModelBase public partial class MainWindowViewModel : ViewModelBase
{ {
public string SolutionFolderPath { get; set; } = string.Empty; [ObservableProperty]
public string SolutionFilePath { get; set; } = string.Empty; private string _solutionFolderPath;
[ObservableProperty]
private string _solutionFilePath;
[ObservableProperty] [ObservableProperty]
private string? _fileText; private string? _fileText;
[ObservableProperty]
private ObservableCollection<string> _parityResults = new ObservableCollection<string>()
{
"Test"
};
[RelayCommand] [RelayCommand]
private async Task OpenFile(CancellationToken token) private async Task ExecuteParityChecker(CancellationToken token)
{
var results = SolutionParityChecker.CompareSolutionAndCSharpProjects(
SolutionFolderPath,
SolutionFilePath
);
ParityResults.Clear();
foreach (var result in results)
{
ParityResults.Add(result);
}
}
[RelayCommand]
private async Task LoadSolutionFile(CancellationToken token)
{ {
ErrorMessages?.Clear(); ErrorMessages?.Clear();
try try
@@ -29,17 +54,7 @@ public partial class MainWindowViewModel : ViewModelBase
if (file is null) if (file is null)
return; return;
// Limit the text file to 1MB so that the demo won't lag. SolutionFilePath = file.Path.AbsolutePath;
if ((await file.GetBasicPropertiesAsync()).Size <= 1024 * 1024 * 1)
{
await using var readStream = await file.OpenReadAsync();
using var reader = new StreamReader(readStream);
FileText = await reader.ReadToEndAsync(token);
}
else
{
throw new Exception("File exceeded 1MB limit.");
}
} }
catch (Exception e) catch (Exception e)
{ {
@@ -48,26 +63,16 @@ public partial class MainWindowViewModel : ViewModelBase
} }
[RelayCommand] [RelayCommand]
private async Task SaveFile() private async Task LoadSolutionFolder(CancellationToken token)
{ {
ErrorMessages?.Clear(); ErrorMessages?.Clear();
try try
{ {
var file = await DoSaveFilePickerAsync(); var folder = await DoOpenFolderPickerAsync();
if (file is null) if (folder is null)
return; return;
// Limit the text file to 1MB so that the demo won't lag. SolutionFolderPath = folder.Path.AbsolutePath;
if (FileText?.Length <= 1024 * 1024 * 1)
{
var stream = new MemoryStream(Encoding.Default.GetBytes((string)FileText));
await using var writeStream = await file.OpenWriteAsync();
await stream.CopyToAsync(writeStream);
}
else
{
throw new Exception("File exceeded 1MB limit.");
}
} }
catch (Exception e) catch (Exception e)
{ {
@@ -98,7 +103,7 @@ public partial class MainWindowViewModel : ViewModelBase
return files?.Count >= 1 ? files[0] : null; return files?.Count >= 1 ? files[0] : null;
} }
private async Task<IStorageFile?> DoSaveFilePickerAsync() private async Task<IStorageFolder?> DoOpenFolderPickerAsync()
{ {
// For learning purposes, we opted to directly get the reference // For learning purposes, we opted to directly get the reference
// for StorageProvider APIs here inside the ViewModel. // for StorageProvider APIs here inside the ViewModel.
@@ -106,7 +111,7 @@ public partial class MainWindowViewModel : ViewModelBase
// For your real-world apps, you should follow the MVVM principles // For your real-world apps, you should follow the MVVM principles
// by making service classes and locating them with DI/IoC. // by making service classes and locating them with DI/IoC.
// See DepInject project for a sample of how to accomplish this. // See IoCFileOps project for an example of how to accomplish this.
if ( if (
Application.Current?.ApplicationLifetime Application.Current?.ApplicationLifetime
is not IClassicDesktopStyleApplicationLifetime desktop is not IClassicDesktopStyleApplicationLifetime desktop
@@ -114,8 +119,10 @@ public partial class MainWindowViewModel : ViewModelBase
) )
throw new NullReferenceException("Missing StorageProvider instance."); throw new NullReferenceException("Missing StorageProvider instance.");
return await provider.SaveFilePickerAsync( var folder = await provider.OpenFolderPickerAsync(
new FilePickerSaveOptions() { Title = "Save Text File" } new FolderPickerOpenOptions() { Title = "Open Text File", AllowMultiple = false }
); );
return folder?.Count >= 1 ? folder[0] : null;
} }
} }

View File

@@ -16,13 +16,13 @@
<Panel> <Panel>
<StackPanel> <StackPanel>
<TextBlock>Welcome to Avalonia!</TextBlock> <TextBlock>Welcome to Avalonia!</TextBlock>
<Button Click="LoadSolutionFolder">Select Solution Folder</Button> <Button Command="{Binding LoadSolutionFolderCommand}" >Select Solution Folder</Button>
<Button Click="LoadSolutionFile">Select Solution File</Button> <Button Command="{Binding LoadSolutionFileCommand}">Select Solution File</Button>
<Button Command="{Binding OpenFileCommand}">Open File</Button> <Button Command="{Binding ExecuteParityCheckerCommand}">Check Solution Parity</Button>
<Button Command="{Binding SaveFileCommand}">Save File</Button>
<TextBlock Name="SolutionFilePath" Text="{Binding SolutionFilePath}" /> <TextBlock Name="SolutionFilePath" Text="{Binding SolutionFilePath}" />
<TextBlock Name="SolutionFolderPath" Text="{Binding SolutionFolderPath}" /> <TextBlock Name="SolutionFolderPath" Text="{Binding SolutionFolderPath}" />
<ListBox DockPanel.Dock="Bottom" ItemsSource="{Binding ErrorMessages}"/> <ListBox Name="Results" ItemsSource="{Binding ParityResults}"/>
<ListBox ItemsSource="{Binding ErrorMessages}"/>
</StackPanel> </StackPanel>
</Panel> </Panel>

View File

@@ -1,5 +1,6 @@
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using SolutionParityChecker.App.ViewModels;
namespace SolutionParityChecker.App.Views; namespace SolutionParityChecker.App.Views;
@@ -9,14 +10,4 @@ public partial class MainWindow : Window
{ {
InitializeComponent(); InitializeComponent();
} }
private void LoadSolutionFolder(object? sender, RoutedEventArgs e)
{
SolutionFolderPath.Text = "Solution folder path";
}
private void LoadSolutionFile(object? sender, RoutedEventArgs e)
{
SolutionFilePath.Text = "Solution file path";
}
} }

View File

@@ -4,13 +4,26 @@ namespace SolutionParityChecker;
public static class SolutionParityChecker public static class SolutionParityChecker
{ {
public static void CompareSolutionAndCSharpProjects( public static List<string> CompareSolutionAndCSharpProjects(
string solutionFolderPath, string solutionFolderPath,
string solutionFilePath string solutionFilePath
) { } )
{
var csprojList = RetrieveAllCSharpProjectNamesFromFolder(solutionFolderPath);
var solutionFile = ParseSolutionFileFromPath(solutionFilePath);
ArgumentNullException.ThrowIfNull(solutionFile);
var projectsMissingFromSolution = FindProjectsMissingFromSolution(csprojList, solutionFile);
return projectsMissingFromSolution;
}
public static string[] RetrieveAllCSharpProjectNamesFromFolder(string solutionFolderPath) public static string[] RetrieveAllCSharpProjectNamesFromFolder(string solutionFolderPath)
{ {
// if solutionFolderPath does not end with a slash, add one
if (solutionFolderPath[^1] != Path.DirectorySeparatorChar)
{
solutionFolderPath += Path.DirectorySeparatorChar;
}
var csprojList = Directory.GetFiles( var csprojList = Directory.GetFiles(
solutionFolderPath, solutionFolderPath,
"*.csproj", "*.csproj",