-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df670d6
commit 540ccdb
Showing
3 changed files
with
93 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.IO.Abstractions; | ||
|
||
namespace Husky.TaskRunner; | ||
|
||
public sealed class TemporaryFile : IDisposable | ||
{ | ||
private readonly IFileSystem _fileSystem; | ||
private readonly string _filePath; | ||
|
||
public TemporaryFile(IFileSystem fileSystem, FileArgumentInfo fileArgumentInfo) | ||
{ | ||
_fileSystem = fileSystem; | ||
|
||
var path = fileArgumentInfo.PathMode == PathModes.Absolute | ||
? fileArgumentInfo.AbsolutePath | ||
: fileArgumentInfo.RelativePath; | ||
|
||
var fileInfo = _fileSystem.FileInfo.FromFileName(path); | ||
var guid = Guid.NewGuid().ToString()[..5]; | ||
_filePath = path.Replace(fileInfo.Name, $"{guid}_{fileInfo.Name}"); | ||
} | ||
|
||
public static implicit operator string(TemporaryFile temporaryFile) => temporaryFile._filePath; | ||
|
||
public void Dispose() | ||
{ | ||
if (_fileSystem.File.Exists(_filePath)) | ||
_fileSystem.File.Delete(_filePath); | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return _filePath; | ||
} | ||
} |
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,41 @@ | ||
using Husky.Stdout; | ||
|
||
namespace Husky.Utils; | ||
public sealed class DisposableScope : IDisposable | ||
{ | ||
private readonly Action? _beforeDispose; | ||
private readonly Action? _afterDispose; | ||
|
||
// you can use ConcurrentQueue if you need thread-safe solution | ||
private readonly Queue<IDisposable> _disposables = new(); | ||
|
||
public DisposableScope(Action? beforeDispose = default, Action? afterDispose = default) | ||
{ | ||
_beforeDispose = beforeDispose; | ||
_afterDispose = afterDispose; | ||
} | ||
|
||
public T Using<T>(T disposable) where T : IDisposable | ||
{ | ||
_disposables.Enqueue(disposable); | ||
return disposable; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_beforeDispose?.Invoke(); | ||
foreach (var item in _disposables) | ||
{ | ||
try | ||
{ | ||
item.Dispose(); | ||
} | ||
catch (Exception e) | ||
{ | ||
e.Message.LogVerbose(ConsoleColor.DarkRed); | ||
} | ||
} | ||
_afterDispose?.Invoke(); | ||
} | ||
} | ||
|