parent
9e3aab6e96
commit
1b6d42f719
@ -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));
|
||||
}
|
||||
}
|
Loading…
Reference in new issue