Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #27 from NickvisionApps/sysdirs
Browse files Browse the repository at this point in the history
Add SystemDirectories
  • Loading branch information
nlogozzo authored Sep 22, 2023
2 parents 146432e + e405177 commit 269c416
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Nickvision.Aura.Tests/SystemDirectoriesTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Linq;
using System.Runtime.InteropServices;

namespace Nickvision.Aura.Tests;

public class SystemDirectoriesTest
{
[Fact]
public void Path()
{
var path = new []{ "test0", "test1" };
Environment.SetEnvironmentVariable("PATH", string.Join(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ";" : ":", path));
Assert.True(SystemDirectories.Path.SequenceEqual(path));
}

[Fact]
public void Config()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Assert.True(SystemDirectories.Config.Length == 0);
}
var config = new []{ "test0", "test1" };
Environment.SetEnvironmentVariable("XDG_CONFIG_DIRS", string.Join(":", config));
Assert.True(SystemDirectories.Config.SequenceEqual(config));
}

[Fact]
public void Data()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Assert.True(SystemDirectories.Data.Length == 0);
}
var data = new []{ "test0", "test1" };
Environment.SetEnvironmentVariable("XDG_DATA_DIRS", string.Join(":", data));
Assert.True(SystemDirectories.Data.SequenceEqual(data));
}
}
60 changes: 60 additions & 0 deletions Nickvision.Aura/SystemDirectories.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Linq;
using System.Runtime.InteropServices;

namespace Nickvision.Aura;

/// <summary>
/// System directories paths
/// </summary>
public class SystemDirectories {
private static string[]? _path;
private static string[]? _config;
private static string[]? _data;

/// <summary>
/// Array of paths from PATH variable
/// </summary>
public static string[] Path
{
get
{
_path ??= Environment.GetEnvironmentVariable("PATH")?.Split(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ';' : ':').ToArray() ?? Array.Empty<string>();
return _path;
}
}

/// <summary>
/// Array of paths from XDG_CONFIG_DIRS
/// </summary>
/// <remarks>Returns empty array when accessed not on Linux</remarks>
public static string[] Config
{
get
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return Array.Empty<string>();
}
_config ??= Environment.GetEnvironmentVariable("XDG_CONFIG_DIRS")?.Split(':').ToArray() ?? Array.Empty<string>();
return _config;
}
}

/// <summary>
/// Array of paths from XDG_DATA_DIRS
/// </summary>
/// <remarks>Returns empty array when accessed not on Linux</remarks>
public static string[] Data
{
get
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return Array.Empty<string>();
}
_data ??= Environment.GetEnvironmentVariable("XDG_DATA_DIRS")?.Split(':').ToArray() ?? Array.Empty<string>();
return _data;
}
}
}

0 comments on commit 269c416

Please sign in to comment.