2019-12-14 20:11:39 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Common
|
|
|
|
|
{
|
2020-03-28 13:33:39 +00:00
|
|
|
|
public class TempFolder : IAsyncDisposable
|
2019-12-14 20:11:39 +00:00
|
|
|
|
{
|
2020-03-23 12:57:18 +00:00
|
|
|
|
public AbsolutePath Dir { get; }
|
2019-12-14 20:11:39 +00:00
|
|
|
|
public bool DeleteAfter = true;
|
2020-04-22 04:13:07 +00:00
|
|
|
|
private static Task _cleanTask;
|
|
|
|
|
|
|
|
|
|
static TempFolder()
|
|
|
|
|
{
|
2020-04-28 03:17:06 +00:00
|
|
|
|
_cleanTask = Task.Run(() => "tmp_files".RelativeTo(AbsolutePath.EntryPoint).DeleteDirectory());
|
2020-04-22 04:13:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-28 03:17:06 +00:00
|
|
|
|
public static void Init()
|
2020-04-22 04:13:07 +00:00
|
|
|
|
{
|
2020-04-28 03:17:06 +00:00
|
|
|
|
// Nothing to do, as work is done in static ctor
|
2020-04-22 04:13:07 +00:00
|
|
|
|
}
|
2019-12-14 20:11:39 +00:00
|
|
|
|
|
2020-04-28 03:17:06 +00:00
|
|
|
|
private TempFolder(bool deleteAfter = true)
|
2019-12-14 20:11:39 +00:00
|
|
|
|
{
|
2020-04-17 03:52:19 +00:00
|
|
|
|
Dir = Path.Combine("tmp_files", Guid.NewGuid().ToString()).RelativeTo(AbsolutePath.EntryPoint);
|
2020-03-23 12:57:18 +00:00
|
|
|
|
if (!Dir.Exists)
|
|
|
|
|
Dir.CreateDirectory();
|
|
|
|
|
DeleteAfter = deleteAfter;
|
2019-12-14 20:11:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-28 03:17:06 +00:00
|
|
|
|
public static async Task<TempFolder> Create(bool deleteAfter = true)
|
|
|
|
|
{
|
|
|
|
|
await _cleanTask;
|
|
|
|
|
return new TempFolder(deleteAfter: deleteAfter);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
public TempFolder(AbsolutePath dir, bool deleteAfter = true)
|
2019-12-14 20:11:39 +00:00
|
|
|
|
{
|
2020-03-23 12:57:18 +00:00
|
|
|
|
Dir = dir;
|
2019-12-14 20:11:39 +00:00
|
|
|
|
if (!dir.Exists)
|
|
|
|
|
{
|
2020-03-23 12:57:18 +00:00
|
|
|
|
Dir.Create();
|
2019-12-14 20:11:39 +00:00
|
|
|
|
}
|
2020-03-23 12:57:18 +00:00
|
|
|
|
DeleteAfter = deleteAfter;
|
2019-12-14 20:11:39 +00:00
|
|
|
|
}
|
2020-04-28 03:17:06 +00:00
|
|
|
|
|
2020-03-28 13:33:39 +00:00
|
|
|
|
public async ValueTask DisposeAsync()
|
2019-12-14 20:11:39 +00:00
|
|
|
|
{
|
2020-04-22 04:13:07 +00:00
|
|
|
|
Utils.Log($"Deleting {Dir}");
|
2020-03-23 12:57:18 +00:00
|
|
|
|
if (DeleteAfter && Dir.Exists)
|
2019-12-14 20:11:39 +00:00
|
|
|
|
{
|
2020-03-28 13:33:39 +00:00
|
|
|
|
await Utils.DeleteDirectory(Dir);
|
2019-12-14 20:11:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|