From 9111ab80db6c503127c4ec57bad201367e691dc8 Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Sat, 12 Sep 2020 19:35:20 -0500 Subject: [PATCH] =?UTF-8?q?feat(archival):=20=E2=9C=A8=F0=9F=9A=A7=20WIP?= =?UTF-8?q?=20impl=20UoW=20w/git?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Persistence/ConfigurationExtensions.cs | 2 +- .../Persistence/GitFileArchiver.cs | 28 ++++++++++++------- .../Persistence/IArchiveFile.cs | 11 -------- .../Persistence/IArchiveFiles.cs | 14 ++++++++++ .../Persistence/IUnitOfWork.cs | 9 ++++++ 5 files changed, 42 insertions(+), 22 deletions(-) delete mode 100644 services/Archival/FilterLists.Archival.Infrastructure/Persistence/IArchiveFile.cs create mode 100644 services/Archival/FilterLists.Archival.Infrastructure/Persistence/IArchiveFiles.cs create mode 100644 services/Archival/FilterLists.Archival.Infrastructure/Persistence/IUnitOfWork.cs diff --git a/services/Archival/FilterLists.Archival.Infrastructure/Persistence/ConfigurationExtensions.cs b/services/Archival/FilterLists.Archival.Infrastructure/Persistence/ConfigurationExtensions.cs index 03aaeb98c..0c4c95088 100644 --- a/services/Archival/FilterLists.Archival.Infrastructure/Persistence/ConfigurationExtensions.cs +++ b/services/Archival/FilterLists.Archival.Infrastructure/Persistence/ConfigurationExtensions.cs @@ -20,7 +20,7 @@ public static void AddPersistenceServices(this IServiceCollection services, ICon return new Repository(gitOptions.RepositoryDirectory); }); - services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/services/Archival/FilterLists.Archival.Infrastructure/Persistence/GitFileArchiver.cs b/services/Archival/FilterLists.Archival.Infrastructure/Persistence/GitFileArchiver.cs index 59ed546cb..0f6a3e169 100644 --- a/services/Archival/FilterLists.Archival.Infrastructure/Persistence/GitFileArchiver.cs +++ b/services/Archival/FilterLists.Archival.Infrastructure/Persistence/GitFileArchiver.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using System.Threading; using System.Threading.Tasks; @@ -8,8 +9,9 @@ namespace FilterLists.Archival.Infrastructure.Persistence { - internal class GitFileArchiver : IArchiveFile + internal class GitFileArchiver : IArchiveFiles { + private readonly IList _filePaths = new List(); private readonly GitOptions _options; private readonly IRepository _repository; @@ -21,22 +23,28 @@ public GitFileArchiver(IOptions gitOptions, IRepository gitRepositor public async Task ArchiveFileAsync( Stream fileContents, - string fileName, + string filePath, CancellationToken cancellationToken = default) { - await using var output = File.OpenWrite(fileName); + await using var output = File.OpenWrite(filePath); await fileContents.CopyToAsync(output, cancellationToken); - if (!cancellationToken.IsCancellationRequested) - Commit(fileName); + _filePaths.Add(filePath); } - private void Commit(string fileName) + public void Commit() { - Commands.Stage(_repository, fileName); - var utcNow = DateTime.UtcNow; - var signature = new Signature(_options.UserName, _options.UserEmail, utcNow); - var message = $"archive {fileName}"; + Commands.Stage(_repository, _filePaths); + var signature = new Signature(_options.UserName, _options.UserEmail, DateTime.UtcNow); + var message = $"feat(archives): archive {_filePaths.Count} files{Environment.NewLine}{string.Join(Environment.NewLine, _filePaths)}"; _repository.Commit(message, signature, signature); } + + public void Dispose() + { + foreach (var file in _filePaths) + if (File.Exists(file)) + File.Delete(file); + _repository.CheckoutPaths("HEAD", _filePaths); + } } } \ No newline at end of file diff --git a/services/Archival/FilterLists.Archival.Infrastructure/Persistence/IArchiveFile.cs b/services/Archival/FilterLists.Archival.Infrastructure/Persistence/IArchiveFile.cs deleted file mode 100644 index 24642d736..000000000 --- a/services/Archival/FilterLists.Archival.Infrastructure/Persistence/IArchiveFile.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System.IO; -using System.Threading; -using System.Threading.Tasks; - -namespace FilterLists.Archival.Infrastructure.Persistence -{ - public interface IArchiveFile - { - Task ArchiveFileAsync(Stream fileContents, string fileName, CancellationToken cancellationToken = default); - } -} \ No newline at end of file diff --git a/services/Archival/FilterLists.Archival.Infrastructure/Persistence/IArchiveFiles.cs b/services/Archival/FilterLists.Archival.Infrastructure/Persistence/IArchiveFiles.cs new file mode 100644 index 000000000..400340683 --- /dev/null +++ b/services/Archival/FilterLists.Archival.Infrastructure/Persistence/IArchiveFiles.cs @@ -0,0 +1,14 @@ +using System.IO; +using System.Threading; +using System.Threading.Tasks; + +namespace FilterLists.Archival.Infrastructure.Persistence +{ + public interface IArchiveFiles : IUnitOfWork + { + Task ArchiveFileAsync( + Stream fileContents, + string filePath, + CancellationToken cancellationToken = default); + } +} \ No newline at end of file diff --git a/services/Archival/FilterLists.Archival.Infrastructure/Persistence/IUnitOfWork.cs b/services/Archival/FilterLists.Archival.Infrastructure/Persistence/IUnitOfWork.cs new file mode 100644 index 000000000..a2304949b --- /dev/null +++ b/services/Archival/FilterLists.Archival.Infrastructure/Persistence/IUnitOfWork.cs @@ -0,0 +1,9 @@ +using System; + +namespace FilterLists.Archival.Infrastructure.Persistence +{ + public interface IUnitOfWork : IDisposable + { + void Commit(); + } +} \ No newline at end of file