Skip to content

Commit

Permalink
Merge pull request #13 from messerli-informatik-ag/new-api
Browse files Browse the repository at this point in the history
  • Loading branch information
bash authored Nov 23, 2022
2 parents 2a11db7 + 857a9e4 commit d730b82
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 213 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: actions/setup-dotnet@v3
- uses: actions/setup-dotnet@v3
name: Install .NET SDK 6.x
with:
version: 6.x
- name: Restore dependencies
run: dotnet restore /p:TreatWarningsAsErrors=true
- name: Build
Expand All @@ -22,8 +26,8 @@ jobs:
name: Generate NuGet Packages
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-dotnet@v1
- uses: actions/checkout@v3
- uses: actions/setup-dotnet@v3
name: Install Current .NET SDK
- name: Generate NuGet Packages
run: dotnet pack --configuration Release --output nupkg /p:TreatWarningsAsErrors=true
Expand Down
2 changes: 1 addition & 1 deletion TempDirectory.Test/TempDirectory.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<RootNamespace>Messerli.TempDirectory.Test</RootNamespace>
<AssemblyName>Messerli.TempDirectory.Test</AssemblyName>
Expand Down
99 changes: 0 additions & 99 deletions TempDirectory.Test/TempDirectoryBuilderTest.cs

This file was deleted.

47 changes: 47 additions & 0 deletions TempDirectory.Test/TempSubdirectoryTest.cs
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)
{
}
}
}
14 changes: 0 additions & 14 deletions TempDirectory/ITempDirectoryBuilder.cs

This file was deleted.

21 changes: 0 additions & 21 deletions TempDirectory/TempDirectory.cs

This file was deleted.

4 changes: 2 additions & 2 deletions TempDirectory/TempDirectory.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>0.2.1</Version>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.3.0</Version>
<TargetFrameworks>net7.0;netstandard2.0</TargetFrameworks>
<RootNamespace>Messerli.TempDirectory</RootNamespace>
<AssemblyName>Messerli.TempDirectory</AssemblyName>
</PropertyGroup>
Expand Down
68 changes: 0 additions & 68 deletions TempDirectory/TempDirectoryBuilder.cs

This file was deleted.

40 changes: 40 additions & 0 deletions TempDirectory/TempSubdirectory.cs
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})";
}
10 changes: 6 additions & 4 deletions changelog.md
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
11 changes: 9 additions & 2 deletions readme.md
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: "...");
```

0 comments on commit d730b82

Please sign in to comment.