Skip to content

Commit

Permalink
Merge branch 'main' into cpm
Browse files Browse the repository at this point in the history
  • Loading branch information
halgari authored Nov 12, 2024
2 parents b2d178c + 371b035 commit 79f5f4c
Show file tree
Hide file tree
Showing 16 changed files with 143 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static NxUnpackerBuilder AddFilesWithFileSystemOutput(this NxUnpackerBuil
for (var x = 0; x < entries.Length; x++)
{
var entry = entries[x];
var outputPath = outputDirectory.Combine(entry.FilePath);
var outputPath = outputDirectory / entry.FilePath;
outputProviders[x] = new OutputAbsolutePathProvider(outputPath, entry.FilePath, entry.Entry);
}
builder.Outputs.AddRange(outputProviders);
Expand Down
4 changes: 2 additions & 2 deletions src/NexusMods.Paths.Benchmarks/Benchmarks/Paths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public AbsolutePath NexusJoinSmall()
{
// Not a fair test here since one is a string concat; other includes
// normalization to OS path.
return CurrentPath.AbsolutePath.Combine("foo");
return CurrentPath.AbsolutePath / "foo";
}

[Benchmark]
Expand All @@ -104,7 +104,7 @@ public string SystemJoinLarge()
[Benchmark]
public AbsolutePath NexusJoinLarge()
{
return CurrentPath.AbsolutePath.Combine("foo/bar/baz/quz/qax");
return CurrentPath.AbsolutePath / "foo/bar/baz/quz/qax";
}

[Benchmark]
Expand Down
6 changes: 6 additions & 0 deletions src/NexusMods.Paths/AbsolutePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ public AbsolutePath Combine(RelativePath path)
var res = PathHelpers.JoinParts(GetFullPath(), path.Path, FileSystem.OS);
return FromSanitizedFullPath(res, FileSystem);
}

/// <summary>
/// Combines the current path with a relative path.
/// </summary>
public static AbsolutePath operator / (AbsolutePath path, RelativePath relativePath)
=> path.Combine(relativePath);

/// <summary/>
[Obsolete(message: "This will be removed once dependents have updated.", error: true)]
Expand Down
32 changes: 16 additions & 16 deletions src/NexusMods.Paths/FileSystemAbstraction/BaseFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ internal AbsolutePath GetMappedPath(AbsolutePath originalPath)
if (newParentDirectory == default) return originalPath;

var relativePath = originalPath.RelativeTo(originalParentDirectory);
var newPath = newParentDirectory.Combine(relativePath);
var newPath = newParentDirectory / relativePath;

return newPath;
}
Expand Down Expand Up @@ -189,10 +189,10 @@ public virtual AbsolutePath GetKnownPath(KnownPath knownPath)
KnownPath.MyDocumentsDirectory => FromUnsanitizedFullPath(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)),
KnownPath.MyGamesDirectory => FromUnsanitizedDirectoryAndFileName(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "My Games"),

KnownPath.XDG_CONFIG_HOME => GetXDGBaseDirectory($"{nameof(KnownPath.XDG_CONFIG_HOME)}", static fs => fs.GetKnownPath(KnownPath.HomeDirectory).Combine(".config")),
KnownPath.XDG_CACHE_HOME => GetXDGBaseDirectory($"{nameof(KnownPath.XDG_CACHE_HOME)}", static fs => fs.GetKnownPath(KnownPath.HomeDirectory).Combine(".cache")),
KnownPath.XDG_DATA_HOME => GetXDGBaseDirectory($"{nameof(KnownPath.XDG_DATA_HOME)}", static fs => fs.GetKnownPath(KnownPath.HomeDirectory).Combine(".local").Combine("share")),
KnownPath.XDG_STATE_HOME => GetXDGBaseDirectory($"{nameof(KnownPath.XDG_STATE_HOME)}", static fs => fs.GetKnownPath(KnownPath.HomeDirectory).Combine(".local").Combine("state")),
KnownPath.XDG_CONFIG_HOME => GetXDGBaseDirectory($"{nameof(KnownPath.XDG_CONFIG_HOME)}", static fs => fs.GetKnownPath(KnownPath.HomeDirectory) / ".config"),
KnownPath.XDG_CACHE_HOME => GetXDGBaseDirectory($"{nameof(KnownPath.XDG_CACHE_HOME)}", static fs => fs.GetKnownPath(KnownPath.HomeDirectory) / ".cache"),
KnownPath.XDG_DATA_HOME => GetXDGBaseDirectory($"{nameof(KnownPath.XDG_DATA_HOME)}", static fs => fs.GetKnownPath(KnownPath.HomeDirectory) / ".local" / "share"),
KnownPath.XDG_STATE_HOME => GetXDGBaseDirectory($"{nameof(KnownPath.XDG_STATE_HOME)}", static fs => fs.GetKnownPath(KnownPath.HomeDirectory) / ".local" / "state"),
KnownPath.XDG_RUNTIME_DIR => GetXDGBaseDirectory($"{nameof(KnownPath.XDG_RUNTIME_DIR)}", static fs => fs.GetKnownPath(KnownPath.TempDirectory)),
};

Expand All @@ -202,7 +202,7 @@ public virtual AbsolutePath GetKnownPath(KnownPath knownPath)
// ReSharper disable once InconsistentNaming
AbsolutePath GetXDGBaseDirectory(string environmentVariable, Func<IFileSystem, AbsolutePath> defaultFunc)
{
if (!OS.IsLinux) throw OS.CreatePlatformNotSupportedException();
if (!OS.IsLinux) OS.ThrowUnsupported();

var value = Environment.GetEnvironmentVariable(environmentVariable, EnvironmentVariableTarget.Process);
return value is null ? defaultFunc(this) : FromUnsanitizedFullPath(value);
Expand Down Expand Up @@ -244,18 +244,18 @@ public static (Dictionary<AbsolutePath, AbsolutePath> pathMappings, Dictionary<K
KnownPath.EntryDirectory => fileSystem.GetKnownPath(knownPath),
KnownPath.CurrentDirectory => fileSystem.GetKnownPath(knownPath),

KnownPath.CommonApplicationDataDirectory => rootDirectory.Combine("ProgramData"),
KnownPath.ProgramFilesDirectory => rootDirectory.Combine("Program Files"),
KnownPath.ProgramFilesX86Directory => rootDirectory.Combine("Program Files (x86)"),
KnownPath.CommonProgramFilesDirectory => rootDirectory.Combine("Program Files/Common Files"),
KnownPath.CommonProgramFilesX86Directory => rootDirectory.Combine("Program Files (x86)/Common Files"),
KnownPath.CommonApplicationDataDirectory => rootDirectory / "ProgramData",
KnownPath.ProgramFilesDirectory => rootDirectory / "Program Files",
KnownPath.ProgramFilesX86Directory => rootDirectory / "Program Files (x86)",
KnownPath.CommonProgramFilesDirectory => rootDirectory / "Program Files/Common Files",
KnownPath.CommonProgramFilesX86Directory => rootDirectory / "Program Files (x86)/Common Files",

KnownPath.HomeDirectory => newHomeDirectory,
KnownPath.MyDocumentsDirectory => newHomeDirectory.Combine("Documents"),
KnownPath.MyGamesDirectory => newHomeDirectory.Combine("Documents/My Games"),
KnownPath.LocalApplicationDataDirectory => newHomeDirectory.Combine("AppData/Local"),
KnownPath.ApplicationDataDirectory => newHomeDirectory.Combine("AppData/Roaming"),
KnownPath.TempDirectory => newHomeDirectory.Combine("AppData/Local/Temp"),
KnownPath.MyDocumentsDirectory => newHomeDirectory / "Documents",
KnownPath.MyGamesDirectory => newHomeDirectory / "Documents/My Games",
KnownPath.LocalApplicationDataDirectory => newHomeDirectory / "AppData/Local",
KnownPath.ApplicationDataDirectory => newHomeDirectory / "AppData/Roaming",
KnownPath.TempDirectory => newHomeDirectory / "AppData/Local/Temp",

_ => default
};
Expand Down
122 changes: 57 additions & 65 deletions src/NexusMods.Paths/IOSInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,15 @@ TOut MatchPlatform<TOut>(
Func<TOut>? onLinux = null,
Func<TOut>? onOSX = null)
{
if (IsWindows) return onWindows is null ? throw CreatePlatformNotSupportedException() : onWindows();
if (IsLinux) return onLinux is null ? throw CreatePlatformNotSupportedException() : onLinux();
if (IsOSX) return onOSX is null ? throw CreatePlatformNotSupportedException() : onOSX();
throw CreatePlatformNotSupportedException();
Func<TOut>? func = null;

if (IsWindows) func = onWindows;
else if (IsLinux) func = onLinux;
else if (IsOSX) func = onOSX;
else ThrowUnsupported();

if (func is null) ThrowUnsupported();
return func();
}

/// <summary>
Expand All @@ -80,10 +85,15 @@ TOut MatchPlatform<TState, TOut>(
FuncRef<TState, TOut>? onLinux = null,
FuncRef<TState, TOut>? onOSX = null)
{
if (IsWindows) return onWindows is null ? throw CreatePlatformNotSupportedException() : onWindows(ref state);
if (IsLinux) return onLinux is null ? throw CreatePlatformNotSupportedException() : onLinux(ref state);
if (IsOSX) return onOSX is null ? throw CreatePlatformNotSupportedException() : onOSX(ref state);
throw CreatePlatformNotSupportedException();
FuncRef<TState, TOut>? func = null;

if (IsWindows) func = onWindows;
else if (IsLinux) func = onLinux;
else if (IsOSX) func = onOSX;
else ThrowUnsupported();

if (func is null) ThrowUnsupported();
return func(ref state);
}

/// <summary>
Expand All @@ -102,39 +112,20 @@ void SwitchPlatform(
Action? onLinux = null,
Action? onOSX = null)
{
if (IsWindows)
{
if (onWindows is null)
throw CreatePlatformNotSupportedException();

onWindows();
return;
}

if (IsLinux)
{
if (onLinux is null)
throw CreatePlatformNotSupportedException();

onLinux();
return;
}

if (IsOSX)
{
if (onOSX is null)
throw CreatePlatformNotSupportedException();

onOSX();
return;
}

throw CreatePlatformNotSupportedException();
Action? action = null;

if (IsWindows) action = onWindows;
else if (IsLinux) action = onLinux;
else if (IsOSX) action = onOSX;
else ThrowUnsupported();

if (action is null) ThrowUnsupported();
action();
}

/// <summary>
/// Switches on the current platform and allows <paramref name="state"/> to be
/// passed to the each handler, preventing lambda allocations.
/// passed to each handler, preventing lambda allocations.
/// </summary>
/// <param name="onWindows"></param>
/// <param name="onLinux"></param>
Expand All @@ -152,34 +143,15 @@ void SwitchPlatform<TState>(
ActionRef<TState>? onLinux = null,
ActionRef<TState>? onOSX = null)
{
if (IsWindows)
{
if (onWindows is null)
throw CreatePlatformNotSupportedException();

onWindows(ref state);
return;
}

if (IsLinux)
{
if (onLinux is null)
throw CreatePlatformNotSupportedException();

onLinux(ref state);
return;
}

if (IsOSX)
{
if (onOSX is null)
throw CreatePlatformNotSupportedException();

onOSX(ref state);
return;
}

throw CreatePlatformNotSupportedException();
ActionRef<TState>? action = null;

if (IsWindows) action = onWindows;
else if (IsLinux) action = onLinux;
else if (IsOSX) action = onOSX;
else ThrowUnsupported();

if (action is null) ThrowUnsupported();
action(ref state);
}

/// <summary>
Expand All @@ -197,10 +169,29 @@ bool IsPlatformSupported()
/// <returns></returns>
bool IsUnix() => IsLinux || IsOSX;

/// <summary>
/// Throws <see cref="PlatformNotSupportedException"/> for the current platform.
/// </summary>
/// <exception cref="PlatformNotSupportedException"/>
[DoesNotReturn]
void ThrowUnsupported() => throw new PlatformNotSupportedException($"The operation or feature isn't unsupported on the current platform `{Platform}`");

/// <summary>
/// Throws <see cref="PlatformNotSupportedException"/> for the current platform.
/// </summary>
/// <exception cref="PlatformNotSupportedException"/>
[DoesNotReturn]
T ThrowUnsupported<T>()
{
ThrowUnsupported();
return default;
}

/// <summary>
/// Guard statement for platform support.
/// </summary>
/// <exception cref="PlatformNotSupportedException">Thrown when the current platform is not supported.</exception>
[Obsolete(message: $"Use {nameof(ThrowUnsupported)} instead")]
void PlatformSupportedGuard()
{
if (!IsPlatformSupported())
Expand All @@ -211,6 +202,7 @@ void PlatformSupportedGuard()
/// Creates a new <see cref="PlatformNotSupportedException"/>.
/// </summary>
/// <returns></returns>
[Obsolete(message: $"Use {nameof(ThrowUnsupported)} instead")]
PlatformNotSupportedException CreatePlatformNotSupportedException()
{
return new PlatformNotSupportedException($"The current platform is not supported: {Platform}");
Expand Down
5 changes: 5 additions & 0 deletions src/NexusMods.Paths/RelativePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ public RelativePath Join(RelativePath other)
{
return new RelativePath(PathHelpers.JoinParts(Path, other.Path, OS));
}

/// <summary>
/// Appends another path to an existing path.
/// </summary>
public static RelativePath operator /(RelativePath self, RelativePath other) => self.Join(other);

/// <summary>
/// Returns true if the relative path starts with a given string.
Expand Down
4 changes: 2 additions & 2 deletions src/NexusMods.Paths/TemporaryFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public TemporaryPath CreateFile(Extension? ext = default, bool deleteOnDispose =
if (_isDisposed)
throw new ObjectDisposedException(nameof(TemporaryFileManager));

var path = _basePath.Combine(Guid.NewGuid().ToString());
var path = _basePath / Guid.NewGuid().ToString();
if (path.Extension != default)
path = path.AppendExtension(ext ?? KnownExtensions.Tmp);

Expand All @@ -58,7 +58,7 @@ public TemporaryPath CreateFolder(string prefix = "", bool deleteOnDispose = tru
if (_isDisposed)
throw new ObjectDisposedException(nameof(TemporaryFileManager));

var path = _basePath.Combine(prefix + Guid.NewGuid());
var path = _basePath / (prefix + Guid.NewGuid());
_fileSystem.CreateDirectory(path);
return new TemporaryPath(_fileSystem, path, deleteOnDispose);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ public class NxBuilderExtensionsTests
public async Task NxPackerBuilder_CanAddFolderFromIFileSystem_AndExtractToIFileSystem(InMemoryFileSystem fs, AbsolutePath folderPath)
{
// Arrange
var file1 = folderPath.Combine("file1.txt");
var file2 = folderPath.Combine("subfolder/file2.txt");
var file1 = folderPath / "file1.txt";
var file2 = folderPath / "subfolder/file2.txt";
await fs.WriteAllTextAsync(file1, "Content 1");
await fs.WriteAllTextAsync(file2, "Content 2");

var builder = new NxPackerBuilder();
var outputPath = folderPath.Parent.Combine("output.nx");
var outputPath = folderPath.Parent / "output.nx";

// Act
builder.AddFolder(folderPath)
Expand All @@ -38,11 +38,11 @@ public async Task NxPackerBuilder_CanAddFolderFromIFileSystem_AndExtractToIFileS
entries.Should().Contain(e => e.FilePath == "subfolder/file2.txt");

// Verify we can extract all files
var extractFolder = folderPath.Parent.Combine("extracted");
var extractFolder = folderPath.Parent / "extracted";
unpacker.AddAllFilesWithFileSystemOutput(extractFolder).Extract();

var extractedFile1 = extractFolder.Combine("file1.txt");
var extractedFile2 = extractFolder.Combine("subfolder/file2.txt");
var extractedFile1 = extractFolder / "file1.txt";
var extractedFile2 = extractFolder / "subfolder/file2.txt";

fs.FileExists(extractedFile1).Should().BeTrue();
fs.FileExists(extractedFile2).Should().BeTrue();
Expand All @@ -52,7 +52,7 @@ public async Task NxPackerBuilder_CanAddFolderFromIFileSystem_AndExtractToIFileS

// Verify we can extract a single file.
unpacker = NxUnpackerBuilderExtensions.FromFile(outputPath);
var extractedFile1Copy = extractFolder.Combine("file1-copy.txt");
var extractedFile1Copy = extractFolder / "file1-copy.txt";
var file1Entry = entries.First(x => x.FilePath == "file1.txt");
unpacker.AddFileWithFileSystemOutput(file1Entry, extractedFile1Copy).Extract();
(await fs.ReadAllTextAsync(extractedFile1Copy)).Should().Be("Content 1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public async Task GetFileData_HandlesOutOfBoundsOverflow(IFileSystem fileSystem)

private static async Task<AbsolutePath> CreateTestFile(IFileSystem fileSystem, byte[] testData)
{
var path = fileSystem.GetKnownPath(KnownPath.TempDirectory).Combine(Guid.NewGuid().ToString());
var path = fileSystem.GetKnownPath(KnownPath.TempDirectory) / Guid.NewGuid().ToString();
await fileSystem.WriteAllBytesAsync(path, testData);
return path;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void Constructor_CreatesFile(IFileSystem fileSystem)

private static AbsolutePath CreateTestPath(IFileSystem fileSystem)
{
return fileSystem.GetKnownPath(KnownPath.TempDirectory).Combine(Guid.NewGuid().ToString());
return fileSystem.GetKnownPath(KnownPath.TempDirectory) / Guid.NewGuid().ToString();
}

private static void CleanupTestFile(IFileSystem fileSystem, AbsolutePath path)
Expand Down
8 changes: 4 additions & 4 deletions tests/NexusMods.Paths.Tests/FileSystem/BaseFileSystemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public void Test_PathMapping(InMemoryFileSystem fs, AbsolutePath originalPath, A
public void Test_PathMapping_WithDirectory(InMemoryFileSystem fs,
AbsolutePath originalDirectoryPath, AbsolutePath newDirectoryPath, string fileName)
{
var originalFilePath = originalDirectoryPath.Combine(fileName);
var newFilePath = newDirectoryPath.Combine(fileName);
var originalFilePath = originalDirectoryPath / fileName;
var newFilePath = newDirectoryPath / fileName;

var overlayFileSystem = (BaseFileSystem)fs.CreateOverlayFileSystem(
new Dictionary<AbsolutePath, AbsolutePath>
Expand Down Expand Up @@ -116,7 +116,7 @@ public void Test_EnumerateRootDirectories_Windows()
{
var driveLetter = (char)iDriveLetter;
var originalPath = fs.FromUnsanitizedFullPath($"{driveLetter}:/");
var newPath = rootDirectory.Combine(Guid.NewGuid().ToString("D"));
var newPath = rootDirectory / Guid.NewGuid().ToString("D");
return (originalPath, newPath);
}).ToDictionary(x => x.originalPath, x => x.newPath);

Expand Down Expand Up @@ -167,7 +167,7 @@ public void Test_EnumerateRootDirectories_WithCrossPlatformPathMappings()
{
var driveLetter = (char)iDriveLetter;
var originalPath = fs.FromUnsanitizedDirectoryAndFileName("/", driveLetter.ToString());
var newPath = rootDirectory.Combine(Guid.NewGuid().ToString("D"));
var newPath = rootDirectory / Guid.NewGuid().ToString("D");
return (originalPath, newPath);
}).ToDictionary(x => x.originalPath, x => x.newPath);

Expand Down
Loading

0 comments on commit 79f5f4c

Please sign in to comment.