refactor: Use metadata.json to find paths to CFs and RPs

pull/108/head
Robert Dailey 2 years ago
parent 9e3aab6e96
commit 1b6d42f719

@ -43,13 +43,13 @@ internal class CompositionRoot : ICompositionRoot
builder.RegisterModule<RadarrAutofacModule>();
builder.RegisterModule<VersionControlAutofacModule>();
builder.RegisterModule<MigrationAutofacModule>();
builder.RegisterModule<RepoAutofacModule>();
// Needed for Autofac.Extras.Ordering
builder.RegisterSource<OrderedRegistrationSource>();
builder.RegisterModule<CacheAutofacModule>();
builder.RegisterType<CacheStoragePath>().As<ICacheStoragePath>();
builder.RegisterType<RepoUpdater>().As<IRepoUpdater>();
builder.RegisterType<ProgressBar>();
ConfigurationRegistrations(builder);

@ -2,9 +2,12 @@ using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using AutoFixture.NUnit3;
using FluentAssertions;
using NSubstitute;
using NUnit.Framework;
using TestLibrary.AutoFixture;
using TrashLib.Radarr.CustomFormat.Guide;
using TrashLib.Repo;
using TrashLib.Startup;
using TrashLib.TestLibrary;
namespace TrashLib.Tests.Radarr.CustomFormat.Guide;
@ -16,10 +19,11 @@ public class LocalRepoCustomFormatJsonParserTest
[Test, AutoMockData]
public void Get_custom_format_json_works(
[Frozen(Matching.ImplementedInterfaces)] MockFileSystem fs,
[Frozen(Matching.ImplementedInterfaces)] TestAppPaths paths,
[Frozen] IAppPaths appPaths,
[Frozen] IRepoPaths repoPaths,
LocalRepoCustomFormatJsonParser sut)
{
var jsonDir = paths.RepoDirectory
var jsonDir = appPaths.RepoDirectory
.SubDirectory("docs")
.SubDirectory("json")
.SubDirectory("radarr");
@ -27,6 +31,8 @@ public class LocalRepoCustomFormatJsonParserTest
fs.AddFile(jsonDir.File("first.json").FullName, new MockFileData("{'name':'first','trash_id':'1'}"));
fs.AddFile(jsonDir.File("second.json").FullName, new MockFileData("{'name':'second','trash_id':'2'}"));
repoPaths.RadarrCustomFormatPaths.Returns(new[] {jsonDir});
var results = sut.GetCustomFormatData();
results.Should().BeEquivalentTo(new[]

@ -3,12 +3,14 @@ using System.IO.Abstractions.TestingHelpers;
using AutoFixture.NUnit3;
using FluentAssertions;
using Newtonsoft.Json;
using NSubstitute;
using NUnit.Framework;
using TestLibrary;
using TestLibrary.AutoFixture;
using TrashLib.Repo;
using TrashLib.Sonarr.ReleaseProfile;
using TrashLib.Sonarr.ReleaseProfile.Guide;
using TrashLib.TestLibrary;
using TrashLib.Startup;
namespace TrashLib.Tests.Sonarr.ReleaseProfile.Guide;
@ -19,7 +21,8 @@ public class LocalRepoReleaseProfileJsonParserTest
[Test, AutoMockData]
public void Get_custom_format_json_works(
[Frozen(Matching.ImplementedInterfaces)] MockFileSystem fs,
[Frozen(Matching.ImplementedInterfaces)] TestAppPaths paths,
[Frozen] IAppPaths appPaths,
[Frozen] IRepoPaths repoPaths,
LocalRepoReleaseProfileJsonParser sut)
{
static ReleaseProfileData MakeMockObject(string term) => new()
@ -38,7 +41,7 @@ public class LocalRepoReleaseProfileJsonParserTest
var mockData1 = MakeMockObject("first");
var mockData2 = MakeMockObject("second");
var baseDir = paths.RepoDirectory
var baseDir = appPaths.RepoDirectory
.SubDirectory("docs")
.SubDirectory("json")
.SubDirectory("sonarr");
@ -46,6 +49,8 @@ public class LocalRepoReleaseProfileJsonParserTest
fs.AddFile(baseDir.File("first.json").FullName, MockFileData(mockData1));
fs.AddFile(baseDir.File("second.json").FullName, MockFileData(mockData2));
repoPaths.SonarrReleaseProfilePaths.Returns(new[] {baseDir});
var results = sut.GetReleaseProfileData();
results.Should().BeEquivalentTo(new[]
@ -58,10 +63,11 @@ public class LocalRepoReleaseProfileJsonParserTest
[Test, AutoMockData]
public void Json_exceptions_do_not_interrupt_parsing_other_files(
[Frozen(Matching.ImplementedInterfaces)] MockFileSystem fs,
[Frozen(Matching.ImplementedInterfaces)] TestAppPaths paths,
[Frozen] IAppPaths appPaths,
[Frozen] IRepoPaths repoPaths,
LocalRepoReleaseProfileJsonParser sut)
{
var rootPath = paths.RepoDirectory
var rootPath = appPaths.RepoDirectory
.SubDirectory("docs")
.SubDirectory("json")
.SubDirectory("sonarr");
@ -80,6 +86,8 @@ public class LocalRepoReleaseProfileJsonParserTest
fs.AddFile(rootPath.File("0_bad_data.json").FullName, MockData.FromString(badData));
fs.AddFile(rootPath.File("1_good_data.json").FullName, MockData.FromJson(goodData));
repoPaths.SonarrReleaseProfilePaths.Returns(new[] {rootPath});
var results = sut.GetReleaseProfileData();
results.Should().BeEquivalentTo(new[] {goodData});

@ -6,29 +6,28 @@ using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Serilog;
using TrashLib.Radarr.CustomFormat.Models;
using TrashLib.Startup;
using TrashLib.Repo;
namespace TrashLib.Radarr.CustomFormat.Guide;
public class LocalRepoCustomFormatJsonParser : IRadarrGuideService
{
private readonly IAppPaths _paths;
private readonly IRepoPathsFactory _pathsFactory;
private readonly ILogger _log;
public LocalRepoCustomFormatJsonParser(IAppPaths paths, ILogger log)
public LocalRepoCustomFormatJsonParser(IRepoPathsFactory pathsFactory, ILogger log)
{
_paths = paths;
_pathsFactory = pathsFactory;
_log = log;
}
public ICollection<CustomFormatData> GetCustomFormatData()
{
var jsonDir = _paths.RepoDirectory
.SubDirectory("docs")
.SubDirectory("json")
.SubDirectory("radarr");
var paths = _pathsFactory.Create();
var jsonFiles = paths.RadarrCustomFormatPaths
.SelectMany(x => x.GetFiles("*.json"));
return jsonDir.EnumerateFiles("*.json").ToObservable()
return jsonFiles.ToObservable()
.Select(x => Observable.Defer(() => LoadJsonFromFile(x)))
.Merge(8)
.NotNull()

@ -0,0 +1,6 @@
namespace TrashLib.Repo;
public interface IRepoMetadataParser
{
RepoMetadata Deserialize();
}

@ -0,0 +1,9 @@
using System.IO.Abstractions;
namespace TrashLib.Repo;
public interface IRepoPaths
{
IReadOnlyCollection<IDirectoryInfo> RadarrCustomFormatPaths { get; }
IReadOnlyCollection<IDirectoryInfo> SonarrReleaseProfilePaths { get; }
}

@ -0,0 +1,6 @@
namespace TrashLib.Repo;
public interface IRepoPathsFactory
{
IRepoPaths Create();
}

@ -0,0 +1,14 @@
using Autofac;
namespace TrashLib.Repo;
public class RepoAutofacModule : Module
{
protected override void Load(ContainerBuilder builder)
{
base.Load(builder);
builder.RegisterType<RepoUpdater>().As<IRepoUpdater>();
builder.RegisterType<RepoMetadataParser>().As<IRepoMetadataParser>();
builder.RegisterType<RepoPathsFactory>().As<IRepoPathsFactory>().SingleInstance();
}
}

@ -0,0 +1,18 @@
namespace TrashLib.Repo;
public record RadarrMetadata(
IReadOnlyCollection<string> CustomFormats
);
public record SonarrMetadata(
IReadOnlyCollection<string> ReleaseProfiles
);
public record JsonPaths(
RadarrMetadata Radarr,
SonarrMetadata Sonarr
);
public record RepoMetadata(
JsonPaths JsonPaths
);

@ -0,0 +1,38 @@
using System.IO.Abstractions;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using TrashLib.Startup;
namespace TrashLib.Repo;
public class RepoMetadataParser : IRepoMetadataParser
{
private readonly IAppPaths _paths;
public RepoMetadataParser(IAppPaths paths)
{
_paths = paths;
}
public RepoMetadata Deserialize()
{
var serializer = JsonSerializer.Create(new JsonSerializerSettings
{
ContractResolver = new DefaultContractResolver
{
NamingStrategy = new SnakeCaseNamingStrategy()
}
});
var metadataFile = _paths.RepoDirectory.File("metadata.json");
using var stream = new JsonTextReader(metadataFile.OpenText());
var metadata = serializer.Deserialize<RepoMetadata>(stream);
if (metadata is null)
{
throw new InvalidDataException("Unable to deserialize metadata.json");
}
return metadata;
}
}

@ -0,0 +1,8 @@
using System.IO.Abstractions;
namespace TrashLib.Repo;
public record RepoPaths(
IReadOnlyCollection<IDirectoryInfo> RadarrCustomFormatPaths,
IReadOnlyCollection<IDirectoryInfo> SonarrReleaseProfilePaths
) : IRepoPaths;

@ -0,0 +1,34 @@
using System.IO.Abstractions;
using TrashLib.Startup;
namespace TrashLib.Repo;
public class RepoPathsFactory : IRepoPathsFactory
{
private readonly IAppPaths _paths;
private readonly IFileSystem _fs;
private readonly Lazy<RepoMetadata> _metadata;
public RepoPathsFactory(IRepoMetadataParser parser, IAppPaths paths, IFileSystem fs)
{
_paths = paths;
_fs = fs;
_metadata = new Lazy<RepoMetadata>(parser.Deserialize);
}
private List<IDirectoryInfo> ToDirectoryInfoList(IEnumerable<string> listOfDirectories)
{
return listOfDirectories
.Select(x => _paths.RepoDirectory.SubDirectory(x))
.Where(x => x.Exists)
.ToList();
}
public IRepoPaths Create()
{
var metadata = _metadata.Value;
return new RepoPaths(
ToDirectoryInfoList(metadata.JsonPaths.Radarr.CustomFormats),
ToDirectoryInfoList(metadata.JsonPaths.Sonarr.ReleaseProfiles));
}
}

@ -3,34 +3,33 @@ using Common.Extensions;
using MoreLinq;
using Newtonsoft.Json;
using Serilog;
using TrashLib.Repo;
using TrashLib.Sonarr.ReleaseProfile.Filters;
using TrashLib.Startup;
namespace TrashLib.Sonarr.ReleaseProfile.Guide;
public class LocalRepoReleaseProfileJsonParser : ISonarrGuideService
{
private readonly IAppPaths _paths;
private readonly IRepoPathsFactory _pathFactory;
private readonly ILogger _log;
private readonly IFileSystem _fs;
private readonly Lazy<IEnumerable<ReleaseProfileData>> _data;
public LocalRepoReleaseProfileJsonParser(IAppPaths paths, ILogger log)
public LocalRepoReleaseProfileJsonParser(IRepoPathsFactory pathFactory, ILogger log, IFileSystem fs)
{
_paths = paths;
_pathFactory = pathFactory;
_log = log;
_fs = fs;
_data = new Lazy<IEnumerable<ReleaseProfileData>>(GetReleaseProfileDataImpl);
}
private IEnumerable<ReleaseProfileData> GetReleaseProfileDataImpl()
{
var converter = new TermDataConverter();
var jsonDir = _paths.RepoDirectory
.SubDirectory("docs")
.SubDirectory("json")
.SubDirectory("sonarr");
var tasks = jsonDir.GetFiles("*.json")
.Select(f => LoadAndParseFile(f, converter));
var paths = _pathFactory.Create();
var tasks = paths.SonarrReleaseProfilePaths
.SelectMany(x => x.GetFiles("*.json"))
.Select(x => LoadAndParseFile(x, converter));
var data = Task.WhenAll(tasks).Result
// Make non-nullable type and filter out null values

Loading…
Cancel
Save