Browse Source

Download / unzip / install stuff - still need to hook up settings so we don't install venv every time

pull/5/head
JT 2 years ago
parent
commit
ba4cc0dde8
  1. 6
      StabilityMatrix/InstallPage.xaml
  2. 1
      StabilityMatrix/LaunchPage.xaml
  3. 1
      StabilityMatrix/Models/BasePackage.cs
  4. 2
      StabilityMatrix/Models/Settings.cs
  5. 75
      StabilityMatrix/ViewModels/InstallerViewModel.cs
  6. 1
      StabilityMatrix/ViewModels/LaunchViewModel.cs

6
StabilityMatrix/InstallPage.xaml

@ -2,12 +2,10 @@
x:Class="StabilityMatrix.InstallPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:StabilityMatrix"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
xmlns:models="clr-namespace:StabilityMatrix.Models"
xmlns:viewModels="clr-namespace:StabilityMatrix.ViewModels"
d:DesignHeight="700"
d:DesignWidth="1100"
Loaded="InstallPage_OnLoaded"
@ -15,10 +13,6 @@
Foreground="{DynamicResource TextFillColorPrimaryBrush}"
mc:Ignorable="d">
<Page.DataContext>
<viewModels:InstallerViewModel/>
</Page.DataContext>
<Grid Margin="16">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>

1
StabilityMatrix/LaunchPage.xaml

@ -56,6 +56,7 @@
Margin="10,10,10,10"
Text="{Binding ConsoleOutput, Mode=OneWay}"
FontFamily="Consolas"
TextWrapping="Wrap"
Background="{DynamicResource ControlFillColorDisabledBrush}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"/>

1
StabilityMatrix/Models/BasePackage.cs

@ -12,6 +12,7 @@ public abstract class BasePackage
public abstract Task DownloadPackage();
internal virtual string DownloadLocation => $"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\StabilityMatrix\\Packages\\{Name}.zip";
internal virtual string InstallLocation => $"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\StabilityMatrix\\Packages\\{Name}";
public event EventHandler<int> DownloadProgressChanged;
public event EventHandler<string> DownloadComplete;

2
StabilityMatrix/Models/Settings.cs

@ -8,4 +8,6 @@ public class Settings
public string Theme { get; set; }
public List<InstalledPackage> InstalledPackages { get; set; } = new();
public Guid? ActiveInstalledPackage { get; set; }
public bool HasInstalledPip { get; set; }
public bool HasInstalledVenv { get; set; }
}

75
StabilityMatrix/ViewModels/InstallerViewModel.cs

@ -4,23 +4,28 @@ using StabilityMatrix.Helper;
using StabilityMatrix.Models;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Runtime.CompilerServices;
using StabilityMatrix.Models.Packages;
using System.Linq;
using System.Windows;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.Logging;
namespace StabilityMatrix.ViewModels;
public class InstallerViewModel : INotifyPropertyChanged
{
private readonly ILogger<InstallerViewModel> logger;
private string installedText;
private int progressValue;
private bool isIndeterminate;
private BasePackage selectedPackage;
public InstallerViewModel()
public InstallerViewModel(ILogger<InstallerViewModel> logger)
{
this.logger = logger;
InstalledText = "shrug";
ProgressValue = 0;
Packages = new ObservableCollection<BasePackage>
@ -85,13 +90,36 @@ public class InstallerViewModel : INotifyPropertyChanged
public Visibility ProgressBarVisibility => ProgressValue > 0 || IsIndeterminate ? Visibility.Visible : Visibility.Collapsed;
public AsyncRelayCommand InstallCommand => new(async () =>
{
var installSuccess = await InstallGitIfNecessary();
if (!installSuccess)
{
logger.LogError("Git installation failed");
return;
}
await DownloadPackage();
UnzipPackage();
InstalledText = "Installing dependencies...";
await PyRunner.Initialize();
await PyRunner.SetupPip();
await PyRunner.InstallPackage("virtualenv");
InstalledText = "Done";
IsIndeterminate = false;
ProgressValue = 100;
});
private async Task DownloadPackage()
{
SelectedPackage.DownloadProgressChanged += (_, progress) =>
{
if (progress == -1)
{
IsIndeterminate = true;
ProgressValue = 1;
}
else
{
@ -100,8 +128,49 @@ public class InstallerViewModel : INotifyPropertyChanged
}
};
SelectedPackage.DownloadComplete += (_, _) => InstalledText = "Download Complete";
InstalledText = "Downloading package...";
await SelectedPackage.DownloadPackage();
});
}
private void UnzipPackage()
{
InstalledText = "Unzipping package...";
ProgressValue = 0;
Directory.CreateDirectory(SelectedPackage.InstallLocation);
using var zip = ZipFile.OpenRead(SelectedPackage.DownloadLocation);
var zipDirName = string.Empty;
var totalEntries = zip.Entries.Count;
var currentEntry = 0;
foreach (var entry in zip.Entries)
{
if (string.IsNullOrWhiteSpace(entry.Name) && entry.FullName.EndsWith("/"))
{
if (string.IsNullOrWhiteSpace(zipDirName))
{
zipDirName = entry.FullName;
continue;
}
var folderPath = Path.Combine(SelectedPackage.InstallLocation,
entry.FullName.Replace(zipDirName, string.Empty));
Directory.CreateDirectory(folderPath);
continue;
}
var destinationPath = Path.GetFullPath(Path.Combine(SelectedPackage.InstallLocation,
entry.FullName.Replace(zipDirName, string.Empty)));
entry.ExtractToFile(destinationPath, true);
currentEntry++;
ProgressValue = (int) ((double) currentEntry / totalEntries * 100);
}
}
private async Task<bool> InstallGitIfNecessary()
{
var gitOutput = await ProcessRunner.GetProcessOutputAsync("git", "--version");

1
StabilityMatrix/ViewModels/LaunchViewModel.cs

@ -109,6 +109,7 @@ public partial class LaunchViewModel : ObservableObject
foreach (var package in packages)
{
InstalledPackages.Clear();
InstalledPackages.Add(package);
}
}

Loading…
Cancel
Save