From eb5b34f5cda00f77e768790c3a74ee1893c00719 Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Sun, 17 Dec 2023 15:23:23 -0600 Subject: [PATCH] refactor: Move app path creation to DefaultAppDataSetup --- src/Recyclarr.Cli/CompositionRoot.cs | 3 +-- ...cs => ProgramInformationLogInterceptor.cs} | 4 ++- .../Console/Setup/AppPathSetupTask.cs | 23 ---------------- src/Recyclarr.Platform/DefaultAppDataSetup.cs | 11 +++++++- .../BaseCommandSetupIntegrationTest.cs | 26 +------------------ 5 files changed, 15 insertions(+), 52 deletions(-) rename src/Recyclarr.Cli/Console/Interceptors/{VersionLogInterceptor.cs => ProgramInformationLogInterceptor.cs} (57%) delete mode 100644 src/Recyclarr.Cli/Console/Setup/AppPathSetupTask.cs diff --git a/src/Recyclarr.Cli/CompositionRoot.cs b/src/Recyclarr.Cli/CompositionRoot.cs index 1cc99675..7dc23672 100644 --- a/src/Recyclarr.Cli/CompositionRoot.cs +++ b/src/Recyclarr.Cli/CompositionRoot.cs @@ -98,11 +98,10 @@ public static class CompositionRoot private static void CliRegistrations(ContainerBuilder builder) { builder.RegisterType().As(); - builder.RegisterType().As(); + builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterTypes( - typeof(AppPathSetupTask), typeof(JanitorCleanupTask)) .As() .OrderByRegistration(); diff --git a/src/Recyclarr.Cli/Console/Interceptors/VersionLogInterceptor.cs b/src/Recyclarr.Cli/Console/Interceptors/ProgramInformationLogInterceptor.cs similarity index 57% rename from src/Recyclarr.Cli/Console/Interceptors/VersionLogInterceptor.cs rename to src/Recyclarr.Cli/Console/Interceptors/ProgramInformationLogInterceptor.cs index 2e42efc3..90b755ac 100644 --- a/src/Recyclarr.Cli/Console/Interceptors/VersionLogInterceptor.cs +++ b/src/Recyclarr.Cli/Console/Interceptors/ProgramInformationLogInterceptor.cs @@ -1,11 +1,13 @@ +using Recyclarr.Platform; using Spectre.Console.Cli; namespace Recyclarr.Cli.Console.Interceptors; -public class VersionLogInterceptor(ILogger log) : ICommandInterceptor +public class ProgramInformationLogInterceptor(ILogger log, IAppPaths paths) : ICommandInterceptor { public void Intercept(CommandContext context, CommandSettings settings) { log.Debug("Recyclarr Version: {Version}", GitVersionInformation.InformationalVersion); + log.Debug("App Data Dir: {AppData}", paths.AppDataDirectory); } } diff --git a/src/Recyclarr.Cli/Console/Setup/AppPathSetupTask.cs b/src/Recyclarr.Cli/Console/Setup/AppPathSetupTask.cs deleted file mode 100644 index 4938103d..00000000 --- a/src/Recyclarr.Cli/Console/Setup/AppPathSetupTask.cs +++ /dev/null @@ -1,23 +0,0 @@ -using Recyclarr.Platform; - -namespace Recyclarr.Cli.Console.Setup; - -public class AppPathSetupTask(ILogger log, IAppPaths paths) : IGlobalSetupTask -{ - public void OnStart() - { - log.Debug("App Data Dir: {AppData}", paths.AppDataDirectory); - - // Initialize other directories used throughout the application - // Do not initialize the repo directory here; the GitRepositoryFactory handles that later. - paths.CacheDirectory.Create(); - paths.LogDirectory.Create(); - paths.ConfigsDirectory.Create(); - paths.IncludesDirectory.Create(); - } - - public void OnFinish() - { - // No work to do for this event - } -} diff --git a/src/Recyclarr.Platform/DefaultAppDataSetup.cs b/src/Recyclarr.Platform/DefaultAppDataSetup.cs index 5e02999b..a00d8ef6 100644 --- a/src/Recyclarr.Platform/DefaultAppDataSetup.cs +++ b/src/Recyclarr.Platform/DefaultAppDataSetup.cs @@ -9,7 +9,16 @@ public class DefaultAppDataSetup(IEnvironment env, IFileSystem fs) : IAppDataSet public IAppPaths CreateAppPaths() { var appDir = GetAppDataDirectory(AppDataDirectoryOverride); - return new AppPaths(fs.DirectoryInfo.New(appDir)); + var paths = new AppPaths(fs.DirectoryInfo.New(appDir)); + + // Initialize other directories used throughout the application + // Do not initialize the repo directory here; the GitRepositoryFactory handles that later. + paths.CacheDirectory.Create(); + paths.LogDirectory.Create(); + paths.ConfigsDirectory.Create(); + paths.IncludesDirectory.Create(); + + return paths; } private string GetAppDataDirectory(string? appDataDirectoryOverride) diff --git a/tests/Recyclarr.Cli.IntegrationTests/BaseCommandSetupIntegrationTest.cs b/tests/Recyclarr.Cli.IntegrationTests/BaseCommandSetupIntegrationTest.cs index 86b52190..0fffa37b 100644 --- a/tests/Recyclarr.Cli.IntegrationTests/BaseCommandSetupIntegrationTest.cs +++ b/tests/Recyclarr.Cli.IntegrationTests/BaseCommandSetupIntegrationTest.cs @@ -1,7 +1,6 @@ using System.IO.Abstractions; using Recyclarr.Cli.Console.Setup; using Recyclarr.Settings; -using Recyclarr.TestLibrary; namespace Recyclarr.Cli.IntegrationTests; @@ -14,8 +13,7 @@ internal class BaseCommandSetupIntegrationTest : CliIntegrationFixture var registrations = Resolve>(); registrations.Select(x => x.GetType()).Should().BeEquivalentTo(new[] { - typeof(JanitorCleanupTask), - typeof(AppPathSetupTask) + typeof(JanitorCleanupTask) }); } @@ -60,26 +58,4 @@ internal class BaseCommandSetupIntegrationTest : CliIntegrationFixture Fs.AllFiles.Where(x => x.StartsWith(Paths.LogDirectory.FullName)) .Should().HaveCount(maxFiles); } - - [Test] - public void App_paths_setup_creates_initial_directories() - { - for (var i = 0; i < 50; ++i) - { - Fs.AddFile(Paths.LogDirectory.File($"logfile-{i}.log").FullName, new MockFileData("")); - } - - var sut = Resolve(); - sut.OnStart(); - - var expectedDirs = new[] - { - Paths.CacheDirectory.FullName, - Paths.LogDirectory.FullName, - Paths.ConfigsDirectory.FullName, - Paths.IncludesDirectory.FullName - }; - - Fs.LeafDirectories().Should().BeEquivalentTo(expectedDirs); - } }