2021-12-31 17:48:04 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Threading;
|
|
|
|
|
using PettingZoo.Core.Generator;
|
|
|
|
|
using PettingZoo.Core.Settings;
|
|
|
|
|
using PettingZoo.Tapeti.AssemblyLoader;
|
|
|
|
|
using PettingZoo.Tapeti.NuGet;
|
|
|
|
|
using PettingZoo.Tapeti.UI.ClassSelection;
|
|
|
|
|
using PettingZoo.Tapeti.UI.PackageSelection;
|
2022-01-11 19:28:49 +00:00
|
|
|
|
using PettingZoo.WPF.ProgressWindow;
|
2022-01-01 20:45:15 +00:00
|
|
|
|
using Serilog;
|
2021-12-31 17:48:04 +00:00
|
|
|
|
|
|
|
|
|
namespace PettingZoo.Tapeti
|
|
|
|
|
{
|
|
|
|
|
public class TapetiClassLibraryExampleGenerator : IExampleGenerator
|
|
|
|
|
{
|
2022-01-01 20:45:15 +00:00
|
|
|
|
private readonly ILogger logger;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public TapetiClassLibraryExampleGenerator(ILogger logger)
|
|
|
|
|
{
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-12-31 17:48:04 +00:00
|
|
|
|
public void Select(object? ownerWindow, Action<IExample> onExampleSelected)
|
|
|
|
|
{
|
2022-01-01 20:45:15 +00:00
|
|
|
|
var packageManager = new NuGetPackageManager(logger)
|
2021-12-31 17:48:04 +00:00
|
|
|
|
.WithSourcesFrom(Path.Combine(PettingZooPaths.InstallationRoot, @"nuget.config"))
|
|
|
|
|
.WithSourcesFrom(Path.Combine(PettingZooPaths.AppDataRoot, @"nuget.config"));
|
|
|
|
|
|
|
|
|
|
var viewModel = new PackageSelectionViewModel(packageManager);
|
|
|
|
|
var selectionWindow = new PackageSelectionWindow(viewModel)
|
|
|
|
|
{
|
|
|
|
|
Owner = ownerWindow as Window
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
viewModel.Select += (_, args) =>
|
|
|
|
|
{
|
2022-01-10 12:41:21 +00:00
|
|
|
|
Application.Current.Dispatcher.Invoke(() =>
|
2021-12-31 17:48:04 +00:00
|
|
|
|
{
|
|
|
|
|
var windowBounds = selectionWindow.RestoreBounds;
|
|
|
|
|
selectionWindow.Close();
|
|
|
|
|
|
2022-01-11 19:28:49 +00:00
|
|
|
|
var progressWindow = new ProgressWindow(TapetiClassLibraryExampleGeneratorStrings.ProgressWindowTitle);
|
2021-12-31 17:48:04 +00:00
|
|
|
|
progressWindow.Left = windowBounds.Left + (windowBounds.Width - progressWindow.Width) / 2;
|
2022-01-03 14:00:30 +00:00
|
|
|
|
progressWindow.Top = windowBounds.Top + (windowBounds.Height - progressWindow.Height) / 2;
|
2021-12-31 17:48:04 +00:00
|
|
|
|
progressWindow.Show();
|
|
|
|
|
|
2022-01-10 10:52:07 +00:00
|
|
|
|
var cancellationToken = progressWindow.CancellationToken;
|
|
|
|
|
|
2021-12-31 17:48:04 +00:00
|
|
|
|
Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-01-10 10:52:07 +00:00
|
|
|
|
var assemblies = await args.Assemblies.GetAssemblies(progressWindow, cancellationToken);
|
2021-12-31 17:48:04 +00:00
|
|
|
|
|
|
|
|
|
// var classes =
|
|
|
|
|
var examples = LoadExamples(assemblies);
|
|
|
|
|
|
2022-01-10 12:41:21 +00:00
|
|
|
|
Application.Current.Dispatcher.Invoke(() =>
|
2021-12-31 17:48:04 +00:00
|
|
|
|
{
|
|
|
|
|
progressWindow.Close();
|
|
|
|
|
progressWindow = null;
|
|
|
|
|
|
|
|
|
|
var classSelectionViewModel = new ClassSelectionViewModel(examples);
|
|
|
|
|
var classSelectionWindow = new ClassSelectionWindow(classSelectionViewModel)
|
|
|
|
|
{
|
|
|
|
|
Top = windowBounds.Top,
|
|
|
|
|
Left = windowBounds.Left,
|
|
|
|
|
Width = windowBounds.Width,
|
|
|
|
|
Height = windowBounds.Height
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
classSelectionViewModel.Select += (_, example) =>
|
|
|
|
|
{
|
|
|
|
|
classSelectionWindow.Close();
|
|
|
|
|
onExampleSelected(example);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
classSelectionWindow.ShowDialog();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2022-01-10 12:41:21 +00:00
|
|
|
|
Application.Current.Dispatcher.Invoke(() =>
|
2021-12-31 17:48:04 +00:00
|
|
|
|
{
|
|
|
|
|
// ReSharper disable once ConstantConditionalAccessQualifier - if I remove it, there's a "Dereference of a possibly null reference" warning instead
|
|
|
|
|
progressWindow?.Close();
|
|
|
|
|
|
2022-01-10 10:52:07 +00:00
|
|
|
|
if (e is not OperationCanceledException)
|
|
|
|
|
MessageBox.Show($"Error while loading assembly: {e.Message}", "Petting Zoo - Exception", MessageBoxButton.OK, MessageBoxImage.Error);
|
2021-12-31 17:48:04 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
2022-01-10 10:52:07 +00:00
|
|
|
|
}, CancellationToken.None);
|
2021-12-31 17:48:04 +00:00
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
selectionWindow.ShowDialog();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<IClassTypeExample> LoadExamples(IEnumerable<IPackageAssembly> assemblies)
|
|
|
|
|
{
|
2022-01-01 20:45:15 +00:00
|
|
|
|
var assemblyParser = new AssemblyParser.AssemblyParser(
|
|
|
|
|
PettingZooPaths.AppDataAssemblies,
|
|
|
|
|
PettingZooPaths.InstallationAssemblies
|
|
|
|
|
);
|
|
|
|
|
|
2021-12-31 17:48:04 +00:00
|
|
|
|
return assemblies
|
|
|
|
|
.SelectMany(a =>
|
|
|
|
|
{
|
|
|
|
|
using var stream = a.GetStream();
|
|
|
|
|
return assemblyParser.GetExamples(stream).ToArray();
|
|
|
|
|
})
|
|
|
|
|
.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|