Skip to content

Commit

Permalink
Allow file filter to be configured
Browse files Browse the repository at this point in the history
  • Loading branch information
elzik committed Sep 15, 2023
1 parent 83751d4 commit a6a0561
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 12 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.

2 changes: 2 additions & 0 deletions src/Elzik.FmSync.Application/Elzik.FmSync.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.1" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.52.0.60960">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -22,6 +23,7 @@

<ItemGroup>
<ProjectReference Include="..\Elzik.FmSync.Domain\Elzik.FmSync.Domain.csproj" />
<ProjectReference Include="..\Elzik.FmSync.Infrastructure\Elzik.FmSync.Infrastructure.csproj" />
</ItemGroup>

</Project>
15 changes: 10 additions & 5 deletions src/Elzik.FmSync.Application/FrontMatterFolderSynchroniser.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,22 +11,25 @@ public class FrontMatterFolderSynchroniser : IFrontMatterFolderSynchroniser
private readonly ILogger<FrontMatterFolderSynchroniser> _logger;
private readonly IDirectory _directory;
private readonly IFrontMatterFileSynchroniser _frontMatterFileSynchroniser;
private readonly FileSystemOptions _options;

public FrontMatterFolderSynchroniser(ILogger<FrontMatterFolderSynchroniser> logger,
IDirectory directory, IFrontMatterFileSynchroniser frontMatterFileSynchroniser)
public FrontMatterFolderSynchroniser(ILogger<FrontMatterFolderSynchroniser> logger, IDirectory directory,
IFrontMatterFileSynchroniser frontMatterFileSynchroniser, IOptions<FileSystemOptions> 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
Expand Down
1 change: 1 addition & 0 deletions src/Elzik.FmSync.Console/Presentation/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
services.AddTransient<IFrontMatterFileSynchroniser, FrontMatterFileSynchroniser>();
services.AddTransient<IFrontMatterFolderSynchroniser, FrontMatterFolderSynchroniser>();
services.Configure<FrontMatterOptions>(context.Configuration.GetSection("FrontMatterOptions"));
services.Configure<FileSystemOptions>(context.Configuration.GetSection("FileSystemOptions"));
services.AddLogging(loggingBuilder =>
{
loggingBuilder.AddConfiguration(context.Configuration.GetSection("Logging"));
Expand Down
5 changes: 4 additions & 1 deletion src/Elzik.FmSync.Console/appSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
},
"FrontMatterOptions": {
"TimeZoneId": ""
}
},
"FileSystemOptions": {
"FilenamePattern": "*.md"
}
}
6 changes: 6 additions & 0 deletions src/Elzik.FmSync.Infrastructure/FileSystemOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Elzik.FmSync.Infrastructure;

public class FileSystemOptions
{
public string FilenamePattern { get; set; }
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,19 +15,27 @@ public class FrontMatterFolderSynchroniserTests
private readonly MockLogger<FrontMatterFolderSynchroniser> _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<MockLogger<FrontMatterFolderSynchroniser>>();
_mockDirectory = Substitute.For<IDirectory>();
_mockFileSynchroniser = Substitute.For<IFrontMatterFileSynchroniser>();

_fixture = new Fixture();
var fileSystemOptions = Options.Create(new FileSystemOptions()
{
FilenamePattern = _fixture.Create<string>()
});
_testFileSystemOptions = fileSystemOptions.Value;
_fixture.Register<ILogger<FrontMatterFolderSynchroniser>>(() => _mockLogger);
_fixture.Register(() => _mockDirectory);
_fixture.Register(() => _mockFileSynchroniser);
_fixture.Register(() => fileSystemOptions);

_frontMatterFolderSynchroniser = _fixture.Create<FrontMatterFolderSynchroniser>();
}

Expand All @@ -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!);
}
Expand Down Expand Up @@ -161,7 +170,7 @@ private void SetMockDirectoryFilePaths(string testDirectoryPath, IEnumerable<Key
{
var testFileList = testFiles.ToList();

_mockDirectory.EnumerateFiles(testDirectoryPath, "*.md",
_mockDirectory.EnumerateFiles(testDirectoryPath, _testFileSystemOptions.FilenamePattern,
Arg.Is<EnumerationOptions>(options =>
options.MatchCasing == MatchCasing.CaseInsensitive && options.RecurseSubdirectories))
.Returns(testFileList.Select(pair => pair.Key));
Expand Down

0 comments on commit a6a0561

Please sign in to comment.