Skip to content

Commit

Permalink
EnvironmentService
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikMennen committed Mar 13, 2024
1 parent 8847cfd commit 975deed
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/OneWare.Core/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ protected override void RegisterTypes(IContainerRegistry containerRegistry)
containerRegistry.RegisterSingleton<BackupService>();
containerRegistry.RegisterSingleton<IChildProcessService, ChildProcessService>();
containerRegistry.RegisterSingleton<IFileIconService, FileIconService>();
containerRegistry.RegisterSingleton<IEnvironmentService, EnvironmentService>();

//ViewModels - Singletons
containerRegistry.RegisterSingleton<MainWindowViewModel>();
Expand Down
46 changes: 46 additions & 0 deletions src/OneWare.Core/Services/EnvironmentService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using OneWare.Essentials.Helpers;
using OneWare.Essentials.Services;

namespace OneWare.Core.Services;

public class EnvironmentService : IEnvironmentService
{
private static readonly string InitialPath = Environment.GetEnvironmentVariable("PATH")!;
private readonly Dictionary<string, string> _paths = new();

public void SetEnvironmentVariable(string key, string? value)
{
Environment.SetEnvironmentVariable(key, value);
}

public void SetPath(string key, string? path)
{
if (path != null)
_paths[key] = path;
else _paths.Remove(key);
UpdateEnvironment();
}

public void RemovePath(string key)
{
_paths.Remove(key);
UpdateEnvironment();
}

private void UpdateEnvironment()
{
//var currentPath = Environment.GetEnvironmentVariable("PATH");

var pathSeparator = PlatformHelper.Platform switch
{
PlatformId.WinArm64 or PlatformId.WinX64 => ';',
_ => ':'
};

var paths = string.Join(pathSeparator, _paths);

Console.WriteLine(InitialPath);

Environment.SetEnvironmentVariable("PATH", $"{paths}{pathSeparator}{InitialPath}");
}
}

0 comments on commit 975deed

Please sign in to comment.