Ionite
2 years ago
committed by
GitHub
16 changed files with 491 additions and 108 deletions
After Width: | Height: | Size: 25 KiB |
@ -0,0 +1,13 @@
|
||||
using System; |
||||
|
||||
namespace StabilityMatrix.Exceptions; |
||||
|
||||
/// <summary> |
||||
/// Exception that is thrown when a process fails. |
||||
/// </summary> |
||||
public class ProcessException: Exception |
||||
{ |
||||
public ProcessException(string message) : base(message) |
||||
{ |
||||
} |
||||
} |
@ -1,11 +1,23 @@
|
||||
using System.Windows.Controls; |
||||
using System.Windows; |
||||
using System.Windows.Controls; |
||||
using StabilityMatrix.ViewModels; |
||||
|
||||
namespace StabilityMatrix; |
||||
|
||||
public partial class LaunchPage : Page |
||||
public sealed partial class LaunchPage : Page |
||||
{ |
||||
public LaunchPage() |
||||
private readonly LaunchViewModel viewModel; |
||||
|
||||
public LaunchPage(LaunchViewModel viewModel) |
||||
{ |
||||
this.viewModel = viewModel; |
||||
InitializeComponent(); |
||||
DataContext = viewModel; |
||||
} |
||||
|
||||
private void LaunchPage_OnLoaded(object sender, RoutedEventArgs e) |
||||
{ |
||||
viewModel.OnLoaded(); |
||||
SelectPackageComboBox.ItemsSource = viewModel.InstalledPackages; |
||||
} |
||||
} |
||||
|
@ -0,0 +1,21 @@
|
||||
using System; |
||||
|
||||
namespace StabilityMatrix.Models; |
||||
|
||||
/// <summary> |
||||
/// Profile information for a user-installed package. |
||||
/// </summary> |
||||
public class InstalledPackage |
||||
{ |
||||
// Unique ID for the installation |
||||
public Guid Id { get; set; } |
||||
// User defined name |
||||
public string Name { get; set; } |
||||
// Package name |
||||
public string PackageName { get; set; } |
||||
// Package version |
||||
public string PackageVersion { get; set; } |
||||
// Install path |
||||
public string Path { get; set; } |
||||
|
||||
} |
@ -1,6 +1,11 @@
|
||||
namespace StabilityMatrix.Models; |
||||
using System; |
||||
using System.Collections.Generic; |
||||
|
||||
namespace StabilityMatrix.Models; |
||||
|
||||
public class Settings |
||||
{ |
||||
public string Theme { get; set; } |
||||
public List<InstalledPackage> InstalledPackages { get; set; } = new(); |
||||
public Guid? ActiveInstalledPackage { get; set; } |
||||
} |
||||
|
@ -0,0 +1,115 @@
|
||||
using System; |
||||
using System.Collections.ObjectModel; |
||||
using System.Collections.Specialized; |
||||
using System.ComponentModel; |
||||
using System.Diagnostics; |
||||
using System.Diagnostics.Eventing.Reader; |
||||
using System.IO; |
||||
using System.Linq; |
||||
using System.Net.Mime; |
||||
using System.Threading.Tasks; |
||||
using System.Windows.Threading; |
||||
using CommunityToolkit.Mvvm.ComponentModel; |
||||
using CommunityToolkit.Mvvm.Input; |
||||
using StabilityMatrix.Helper; |
||||
using StabilityMatrix.Models; |
||||
using Wpf.Ui.Appearance; |
||||
|
||||
namespace StabilityMatrix.ViewModels; |
||||
|
||||
public partial class LaunchViewModel : ObservableObject |
||||
{ |
||||
private readonly ISettingsManager settingsManager; |
||||
|
||||
[ObservableProperty] |
||||
public string consoleInput = ""; |
||||
|
||||
[ObservableProperty] |
||||
public string consoleOutput = ""; |
||||
|
||||
private InstalledPackage? selectedPackage; |
||||
|
||||
public InstalledPackage? SelectedPackage |
||||
{ |
||||
get => selectedPackage; |
||||
set |
||||
{ |
||||
if (value == selectedPackage) return; |
||||
selectedPackage = value; |
||||
settingsManager.SetActiveInstalledPackage(value); |
||||
OnPropertyChanged(); |
||||
} |
||||
} |
||||
|
||||
public ObservableCollection<InstalledPackage> InstalledPackages = new(); |
||||
|
||||
public LaunchViewModel(ISettingsManager settingsManager) |
||||
{ |
||||
this.settingsManager = settingsManager; |
||||
} |
||||
|
||||
public AsyncRelayCommand LaunchCommand => new(async () => |
||||
{ |
||||
// Clear console |
||||
ConsoleOutput = ""; |
||||
|
||||
if (SelectedPackage == null) |
||||
{ |
||||
ConsoleOutput = "No package selected"; |
||||
return; |
||||
} |
||||
|
||||
// Get path from package |
||||
var packagePath = SelectedPackage.Path; |
||||
var venvPath = Path.Combine(packagePath, "venv"); |
||||
|
||||
// Setup venv |
||||
var venv = new PyVenvRunner(venvPath); |
||||
if (!venv.Exists()) |
||||
{ |
||||
await venv.Setup(); |
||||
} |
||||
|
||||
var onConsoleOutput = new Action<string?>(s => |
||||
{ |
||||
if (s == null) return; |
||||
Dispatcher.CurrentDispatcher.Invoke(() => |
||||
{ |
||||
Debug.WriteLine($"process stdout: {s}"); |
||||
ConsoleOutput += s + "\n"; |
||||
}); |
||||
}); |
||||
|
||||
var onExit = new Action<int>(i => |
||||
{ |
||||
Dispatcher.CurrentDispatcher.Invoke(() => |
||||
{ |
||||
Debug.WriteLine($"Venv process exited with code {i}"); |
||||
ConsoleOutput += $"Venv process exited with code {i}"; |
||||
}); |
||||
}); |
||||
|
||||
var args = "\"" + Path.Combine(packagePath, "launch.py") + "\""; |
||||
|
||||
venv.RunDetached(args, onConsoleOutput, onExit); |
||||
}); |
||||
|
||||
public void OnLoaded() |
||||
{ |
||||
LoadPackages(); |
||||
} |
||||
|
||||
private void LoadPackages() |
||||
{ |
||||
var packages = settingsManager.Settings.InstalledPackages; |
||||
if (!packages.Any()) |
||||
{ |
||||
return; |
||||
} |
||||
|
||||
foreach (var package in packages) |
||||
{ |
||||
InstalledPackages.Add(package); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue