From ba4cc0dde881e288dbe6512094f537e3dd1afeb9 Mon Sep 17 00:00:00 2001 From: JT Date: Thu, 25 May 2023 19:34:37 -0700 Subject: [PATCH] Download / unzip / install stuff - still need to hook up settings so we don't install venv every time --- StabilityMatrix/InstallPage.xaml | 6 -- StabilityMatrix/LaunchPage.xaml | 1 + StabilityMatrix/Models/BasePackage.cs | 1 + StabilityMatrix/Models/Settings.cs | 2 + .../ViewModels/InstallerViewModel.cs | 75 ++++++++++++++++++- StabilityMatrix/ViewModels/LaunchViewModel.cs | 1 + 6 files changed, 77 insertions(+), 9 deletions(-) diff --git a/StabilityMatrix/InstallPage.xaml b/StabilityMatrix/InstallPage.xaml index 1885a09f..c90029ed 100644 --- a/StabilityMatrix/InstallPage.xaml +++ b/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"> - - - - diff --git a/StabilityMatrix/LaunchPage.xaml b/StabilityMatrix/LaunchPage.xaml index e648119b..5457c828 100644 --- a/StabilityMatrix/LaunchPage.xaml +++ b/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"/> diff --git a/StabilityMatrix/Models/BasePackage.cs b/StabilityMatrix/Models/BasePackage.cs index ac598f81..2c8912bf 100644 --- a/StabilityMatrix/Models/BasePackage.cs +++ b/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 DownloadProgressChanged; public event EventHandler DownloadComplete; diff --git a/StabilityMatrix/Models/Settings.cs b/StabilityMatrix/Models/Settings.cs index be162818..ddb71aaa 100644 --- a/StabilityMatrix/Models/Settings.cs +++ b/StabilityMatrix/Models/Settings.cs @@ -8,4 +8,6 @@ public class Settings public string Theme { get; set; } public List InstalledPackages { get; set; } = new(); public Guid? ActiveInstalledPackage { get; set; } + public bool HasInstalledPip { get; set; } + public bool HasInstalledVenv { get; set; } } diff --git a/StabilityMatrix/ViewModels/InstallerViewModel.cs b/StabilityMatrix/ViewModels/InstallerViewModel.cs index bf55c23a..9826e814 100644 --- a/StabilityMatrix/ViewModels/InstallerViewModel.cs +++ b/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 logger; private string installedText; private int progressValue; private bool isIndeterminate; private BasePackage selectedPackage; - public InstallerViewModel() + public InstallerViewModel(ILogger logger) { + this.logger = logger; InstalledText = "shrug"; ProgressValue = 0; Packages = new ObservableCollection @@ -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 InstallGitIfNecessary() { var gitOutput = await ProcessRunner.GetProcessOutputAsync("git", "--version"); diff --git a/StabilityMatrix/ViewModels/LaunchViewModel.cs b/StabilityMatrix/ViewModels/LaunchViewModel.cs index 36a39ecd..9b4d6e6c 100644 --- a/StabilityMatrix/ViewModels/LaunchViewModel.cs +++ b/StabilityMatrix/ViewModels/LaunchViewModel.cs @@ -109,6 +109,7 @@ public partial class LaunchViewModel : ObservableObject foreach (var package in packages) { + InstalledPackages.Clear(); InstalledPackages.Add(package); } }