From 98542bbb68ab4b66234ef7953dfa69847a7be80b Mon Sep 17 00:00:00 2001 From: Ionite Date: Sat, 3 Jun 2023 23:09:17 -0400 Subject: [PATCH] Add path extensions saving and injection --- StabilityMatrix/App.xaml.cs | 6 ++++- StabilityMatrix/Helper/ISettingsManager.cs | 8 +++++++ StabilityMatrix/Helper/PrerequisiteHelper.cs | 7 +++++- StabilityMatrix/Helper/SettingsManager.cs | 25 ++++++++++++++++++++ StabilityMatrix/Models/Settings.cs | 1 + StabilityMatrix/Python/PyRunner.cs | 2 +- 6 files changed, 46 insertions(+), 3 deletions(-) diff --git a/StabilityMatrix/App.xaml.cs b/StabilityMatrix/App.xaml.cs index 4960ccc9..e18ef977 100644 --- a/StabilityMatrix/App.xaml.cs +++ b/StabilityMatrix/App.xaml.cs @@ -86,11 +86,15 @@ namespace StabilityMatrix serviceCollection.AddTransient(); serviceCollection.AddTransient(); + var settingsManager = new SettingsManager(); + serviceCollection.AddSingleton(settingsManager); + // Insert path extensions + settingsManager.InsertPathExtensions(); + serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); - serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); diff --git a/StabilityMatrix/Helper/ISettingsManager.cs b/StabilityMatrix/Helper/ISettingsManager.cs index b251349d..8f883566 100644 --- a/StabilityMatrix/Helper/ISettingsManager.cs +++ b/StabilityMatrix/Helper/ISettingsManager.cs @@ -14,6 +14,14 @@ public interface ISettingsManager void SetActiveInstalledPackage(InstalledPackage? p); void SetNavExpanded(bool navExpanded); void UpdatePackageVersionNumber(Guid id, string? newVersion); + + void AddPathExtension(string pathExtension); + string GetPathExtensionsAsString(); + /// + /// Insert path extensions to the front of the PATH environment variable + /// + void InsertPathExtensions(); + void SetLastUpdateCheck(InstalledPackage package); List GetLaunchArgs(Guid packageId); void SaveLaunchArgs(Guid packageId, List launchArgs); diff --git a/StabilityMatrix/Helper/PrerequisiteHelper.cs b/StabilityMatrix/Helper/PrerequisiteHelper.cs index 03731fa4..e8959e2e 100644 --- a/StabilityMatrix/Helper/PrerequisiteHelper.cs +++ b/StabilityMatrix/Helper/PrerequisiteHelper.cs @@ -22,6 +22,7 @@ public class PrerequisiteHelper : IPrerequisiteHelper private readonly ILogger logger; private readonly IGitHubClient gitHubClient; private readonly IDownloadService downloadService; + private readonly ISettingsManager settingsManager; private static readonly string PortableGitInstallDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StabilityMatrix", @@ -36,11 +37,12 @@ public class PrerequisiteHelper : IPrerequisiteHelper public static readonly string GitBinPath = Path.Combine(PortableGitInstallDir, "bin"); public PrerequisiteHelper(ILogger logger, IGitHubClient gitHubClient, - IDownloadService downloadService) + IDownloadService downloadService, ISettingsManager settingsManager) { this.logger = logger; this.gitHubClient = gitHubClient; this.downloadService = downloadService; + this.settingsManager = settingsManager; } public event EventHandler? DownloadProgressChanged; @@ -89,6 +91,9 @@ public class PrerequisiteHelper : IPrerequisiteHelper OnInstallProgressChanged(this, new ProgressReport(-1, isIndeterminate: true)); File.Delete(PortableGitDownloadPath); + // Also add git to the path + settingsManager.AddPathExtension(GitBinPath); + settingsManager.InsertPathExtensions(); OnInstallComplete(this, new ProgressReport(progress: 1f)); } diff --git a/StabilityMatrix/Helper/SettingsManager.cs b/StabilityMatrix/Helper/SettingsManager.cs index 24f8f8d9..918b7c6e 100644 --- a/StabilityMatrix/Helper/SettingsManager.cs +++ b/StabilityMatrix/Helper/SettingsManager.cs @@ -15,6 +15,7 @@ public class SettingsManager : ISettingsManager private static readonly string SettingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StabilityMatrix", SettingsFileName); + private readonly string? originalEnvPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process); public Settings Settings { get; private set; } = new(); @@ -64,6 +65,30 @@ public class SettingsManager : ISettingsManager Settings.IsNavExpanded = navExpanded; SaveSettings(); } + + public void AddPathExtension(string pathExtension) + { + Settings.PathExtensions ??= new List(); + Settings.PathExtensions.Add(pathExtension); + SaveSettings(); + } + + public string GetPathExtensionsAsString() + { + return string.Join(";", Settings.PathExtensions ?? new List()); + } + + /// + /// Insert path extensions to the front of the PATH environment variable + /// + public void InsertPathExtensions() + { + if (Settings.PathExtensions == null) return; + var toInsert = GetPathExtensionsAsString(); + // Append the original path, if any + toInsert += originalEnvPath ?? ""; + Environment.SetEnvironmentVariable("PATH", toInsert, EnvironmentVariableTarget.Process); + } public void UpdatePackageVersionNumber(Guid id, string? newVersion) { diff --git a/StabilityMatrix/Models/Settings.cs b/StabilityMatrix/Models/Settings.cs index 937fc8f4..51178c08 100644 --- a/StabilityMatrix/Models/Settings.cs +++ b/StabilityMatrix/Models/Settings.cs @@ -11,4 +11,5 @@ public class Settings public List InstalledPackages { get; set; } = new(); public Guid? ActiveInstalledPackage { get; set; } public bool IsNavExpanded { get; set; } + public List? PathExtensions { get; set; } } diff --git a/StabilityMatrix/Python/PyRunner.cs b/StabilityMatrix/Python/PyRunner.cs index feb14fd4..e07e38ce 100644 --- a/StabilityMatrix/Python/PyRunner.cs +++ b/StabilityMatrix/Python/PyRunner.cs @@ -50,7 +50,7 @@ public class PyRunner : IPyRunner // Get existing PATH var currentEnvPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process); // Append Python path to PATH - Environment.SetEnvironmentVariable("PATH", $"{PrerequisiteHelper.GitBinPath};{currentEnvPath};{HomePath}", EnvironmentVariableTarget.Process); + Environment.SetEnvironmentVariable("PATH", $"{HomePath};{currentEnvPath}", EnvironmentVariableTarget.Process); Logger.Info("Initializing Python runtime with DLL: {DllPath}", DllPath); // Check PythonDLL exists