diff --git a/README.md b/README.md index 26447f2..78380b6 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Ensure that a Markdown file's created date is synchronised with the created-at d ## Usage -Execute FmSync passing a path to a directory which contains files you wish to recursively scan. For any Markdown (*.md) files found, the file's created date will be updated to match that of the `created` date found in the file's Front Matter where one exists. +Execute FmSync passing a path to a directory which contains files you wish to recursively scan. For any Markdown files found, the file's created date will be updated to match that of the `created` date found in the file's Front Matter where one exists. ```powershell fmsync c:\my-markdownfiles @@ -31,3 +31,7 @@ Alternatively, `TimeZoneId` can be set to any time zone as specified in the `Tim If the date given in a file's Front Matter contains a time offset, the TimeZoneId given here will be ignored and the offset given will be taken into account when setting the created date on a file. +### FileSystemOptions + +This contains a single setting, `FilenamePattern`, which by default is `*.md`. Only files matching this filter will be acted upon by FmSync. + diff --git a/src/Elzik.FmSync.Application/Elzik.FmSync.Application.csproj b/src/Elzik.FmSync.Application/Elzik.FmSync.Application.csproj index dcd16c0..5d96248 100644 --- a/src/Elzik.FmSync.Application/Elzik.FmSync.Application.csproj +++ b/src/Elzik.FmSync.Application/Elzik.FmSync.Application.csproj @@ -13,6 +13,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -22,6 +23,7 @@ + diff --git a/src/Elzik.FmSync.Application/FrontMatterFolderSynchroniser.cs b/src/Elzik.FmSync.Application/FrontMatterFolderSynchroniser.cs index cd4e409..2398301 100644 --- a/src/Elzik.FmSync.Application/FrontMatterFolderSynchroniser.cs +++ b/src/Elzik.FmSync.Application/FrontMatterFolderSynchroniser.cs @@ -1,5 +1,7 @@ using System.Diagnostics; +using Elzik.FmSync.Infrastructure; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using Thinktecture.IO; namespace Elzik.FmSync; @@ -9,22 +11,25 @@ public class FrontMatterFolderSynchroniser : IFrontMatterFolderSynchroniser private readonly ILogger _logger; private readonly IDirectory _directory; private readonly IFrontMatterFileSynchroniser _frontMatterFileSynchroniser; + private readonly FileSystemOptions _options; - public FrontMatterFolderSynchroniser(ILogger logger, - IDirectory directory, IFrontMatterFileSynchroniser frontMatterFileSynchroniser) + public FrontMatterFolderSynchroniser(ILogger logger, IDirectory directory, + IFrontMatterFileSynchroniser frontMatterFileSynchroniser, IOptions options) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _directory = directory ?? throw new ArgumentNullException(nameof(directory)); - _frontMatterFileSynchroniser = frontMatterFileSynchroniser ?? throw new ArgumentNullException(nameof(frontMatterFileSynchroniser)); + _frontMatterFileSynchroniser = frontMatterFileSynchroniser + ?? throw new ArgumentNullException(nameof(frontMatterFileSynchroniser)); + _options = options.Value; } public void SyncCreationDates(string directoryPath) { var loggingInfo = (StartTime: Stopwatch.GetTimestamp(), EditedCount: 0, ErrorCount: 0,TotalCount: 0); - _logger.LogInformation("Synchronising files in {DirectoryPath}", directoryPath); + _logger.LogInformation("Synchronising {FilenamePattern} files in {DirectoryPath}", _options.FilenamePattern, directoryPath); - var markdownFiles = _directory.EnumerateFiles(directoryPath, "*.md", new EnumerationOptions + var markdownFiles = _directory.EnumerateFiles(directoryPath, _options.FilenamePattern, new EnumerationOptions { MatchCasing = MatchCasing.CaseInsensitive, RecurseSubdirectories = true diff --git a/src/Elzik.FmSync.Console/Presentation/Program.cs b/src/Elzik.FmSync.Console/Presentation/Program.cs index 716a159..05114f9 100644 --- a/src/Elzik.FmSync.Console/Presentation/Program.cs +++ b/src/Elzik.FmSync.Console/Presentation/Program.cs @@ -17,6 +17,7 @@ services.AddTransient(); services.AddTransient(); services.Configure(context.Configuration.GetSection("FrontMatterOptions")); + services.Configure(context.Configuration.GetSection("FileSystemOptions")); services.AddLogging(loggingBuilder => { loggingBuilder.AddConfiguration(context.Configuration.GetSection("Logging")); diff --git a/src/Elzik.FmSync.Console/appSettings.json b/src/Elzik.FmSync.Console/appSettings.json index 53c1127..b6d84e1 100644 --- a/src/Elzik.FmSync.Console/appSettings.json +++ b/src/Elzik.FmSync.Console/appSettings.json @@ -12,5 +12,8 @@ }, "FrontMatterOptions": { "TimeZoneId": "" - } + }, + "FileSystemOptions": { + "FilenamePattern": "*.md" + } } \ No newline at end of file diff --git a/src/Elzik.FmSync.Infrastructure/FileSystemOptions.cs b/src/Elzik.FmSync.Infrastructure/FileSystemOptions.cs new file mode 100644 index 0000000..89c1f39 --- /dev/null +++ b/src/Elzik.FmSync.Infrastructure/FileSystemOptions.cs @@ -0,0 +1,6 @@ +namespace Elzik.FmSync.Infrastructure; + +public class FileSystemOptions +{ + public string FilenamePattern { get; set; } +} \ No newline at end of file diff --git a/tests/Elzik.FmSync.Application.Tests.Unit/FrontMatterFolderSynchroniserTests.cs b/tests/Elzik.FmSync.Application.Tests.Unit/FrontMatterFolderSynchroniserTests.cs index c5a76ef..9075234 100644 --- a/tests/Elzik.FmSync.Application.Tests.Unit/FrontMatterFolderSynchroniserTests.cs +++ b/tests/Elzik.FmSync.Application.Tests.Unit/FrontMatterFolderSynchroniserTests.cs @@ -1,6 +1,7 @@ using AutoFixture; -using Castle.Core.Logging; +using Elzik.FmSync.Infrastructure; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using NSubstitute; using NSubstitute.ExceptionExtensions; using Thinktecture.IO; @@ -14,19 +15,27 @@ public class FrontMatterFolderSynchroniserTests private readonly MockLogger _mockLogger; private readonly IDirectory _mockDirectory; private readonly IFrontMatterFileSynchroniser _mockFileSynchroniser; + private readonly FileSystemOptions _testFileSystemOptions; private readonly FrontMatterFolderSynchroniser _frontMatterFolderSynchroniser; public FrontMatterFolderSynchroniserTests() { + _fixture = new Fixture(); + _mockLogger = Substitute.For>(); _mockDirectory = Substitute.For(); _mockFileSynchroniser = Substitute.For(); - - _fixture = new Fixture(); + var fileSystemOptions = Options.Create(new FileSystemOptions() + { + FilenamePattern = _fixture.Create() + }); + _testFileSystemOptions = fileSystemOptions.Value; _fixture.Register>(() => _mockLogger); _fixture.Register(() => _mockDirectory); _fixture.Register(() => _mockFileSynchroniser); + _fixture.Register(() => fileSystemOptions); + _frontMatterFolderSynchroniser = _fixture.Create(); } @@ -40,7 +49,7 @@ public void SyncCreationDates_DirectoryPathSupplied_OnlyLogs() _frontMatterFolderSynchroniser.SyncCreationDates(testDirectoryPath); // Assert - _mockLogger.Received(1).Log(LogLevel.Information, $"Synchronising files in {testDirectoryPath}"); + _mockLogger.Received(1).Log(LogLevel.Information, $"Synchronising {_testFileSystemOptions.FilenamePattern} files in {testDirectoryPath}"); _mockFileSynchroniser.DidNotReceiveWithAnyArgs().SyncCreationDate(default!); } @@ -161,7 +170,7 @@ private void SetMockDirectoryFilePaths(string testDirectoryPath, IEnumerable(options => options.MatchCasing == MatchCasing.CaseInsensitive && options.RecurseSubdirectories)) .Returns(testFileList.Select(pair => pair.Key));