-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from messerli-informatik-ag/new-api
- Loading branch information
Showing
11 changed files
with
111 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using Xunit; | ||
|
||
namespace Messerli.TempDirectory.Test; | ||
|
||
public sealed class TempSubdirectoryTest | ||
{ | ||
private const string Prefix = "prefix"; | ||
|
||
[Fact] | ||
public void CreatesTempDirectory() | ||
{ | ||
using var tempDirectory = TempSubdirectory.Create(); | ||
Assert.True(Directory.Exists(tempDirectory.FullName)); | ||
} | ||
|
||
[Fact] | ||
public void CreatesTempDirectoryWithPrefix() | ||
{ | ||
using var tempDirectory = TempSubdirectory.Create(Prefix); | ||
Assert.True(Directory.Exists(tempDirectory.FullName)); | ||
Assert.StartsWith(Prefix, Path.GetFileName(tempDirectory.FullName)); | ||
} | ||
|
||
[Fact] | ||
public void DeletesTempDirectoryOnDispose() | ||
{ | ||
var tempDirectory = TempSubdirectory.Create(); | ||
|
||
using (tempDirectory) | ||
{ | ||
Assert.True(Directory.Exists(tempDirectory.FullName)); | ||
} | ||
|
||
Assert.False(Directory.Exists(tempDirectory.FullName)); | ||
} | ||
|
||
[Fact] | ||
public void AllowsTempDirectoryToBeDisposedMoreThanOnce() | ||
{ | ||
var tempDirectory = TempSubdirectory.Create(); | ||
|
||
using (tempDirectory) | ||
using (tempDirectory) | ||
{ | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
namespace Messerli.TempDirectory; | ||
|
||
/// <summary>A temporary subdirectory in the temporary folder of the current user. | ||
/// Disposing will remove the temporary subdirectory.</summary> | ||
public sealed class TempSubdirectory : IDisposable | ||
{ | ||
private bool _isDisposed; | ||
|
||
private TempSubdirectory(string fullName) | ||
{ | ||
FullName = fullName; | ||
} | ||
|
||
/// <summary>Gets the full path of the directory.</summary> | ||
public string FullName { get; } | ||
|
||
/// <summary>Creates a temporary subdirectory in the temporary folder of the current user.</summary> | ||
public static TempSubdirectory Create(string? prefix = null) | ||
{ | ||
#if NET7_0_OR_GREATER | ||
var tempSubdirectory = Directory.CreateTempSubdirectory(prefix); | ||
return new TempSubdirectory(tempSubdirectory.FullName); | ||
#else | ||
var fullName = Path.Combine(Path.GetTempPath(), $"{prefix}{Guid.NewGuid()}"); | ||
Directory.CreateDirectory(fullName); | ||
return new TempSubdirectory(fullName); | ||
#endif | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (!_isDisposed) | ||
{ | ||
_isDisposed = true; | ||
Directory.Delete(FullName, recursive: true); | ||
} | ||
} | ||
|
||
public override string ToString() => $"{nameof(TempSubdirectory)}({FullName})"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
# Changelog | ||
## 0.3.0 | ||
- Simplify API surface | ||
|
||
## 0.1.0 | ||
- Initial release | ||
## 0.2.1 | ||
- Update metadata of NuGet package. | ||
|
||
## 0.2.0 | ||
- Target framework is now .NET Standard 2.0 | ||
|
||
## 0.2.1 | ||
- Update metadata of NuGet package. | ||
## 0.1.0 | ||
- Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
# TempDirectoryProvider | ||
# TempDirectory | ||
[![Build](https://github.com/messerli-informatik-ag/temp-directory/actions/workflows/build.yml/badge.svg)](https://github.com/messerli-informatik-ag/temp-directory/actions/workflows/build.yml) | ||
[![NuGet](https://img.shields.io/nuget/v/Messerli.TempDirectory.svg)](https://www.nuget.org/packages/Messerli.TempDirectory/) | ||
|
||
A `TempDirectoryBuilder` that provides unique temporary directories and automatically cleans them up for you. | ||
This library provides a `TempSubdirectory` class that provides a unique subdirectory in the user's temp directory | ||
that is automatically cleaned up when the class is disposed. | ||
|
||
### Usage | ||
```cs | ||
using var directory = TempSubdirectory.Create("prefix"); | ||
File.WriteAllText(Path.Combine(directory.FullName, "example.txt"), contents: "..."); | ||
``` |