You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
recyclarr/src/TrashLib/Radarr/CustomFormat/Guide/LocalRepoCustomFormatJsonPa...

59 lines
1.9 KiB

using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Threading.Tasks;
using LibGit2Sharp;
using TrashLib.Radarr.Config;
namespace TrashLib.Radarr.CustomFormat.Guide
{
internal class LocalRepoCustomFormatJsonParser : IRadarrGuideService
{
private readonly IFileSystem _fileSystem;
private readonly string _repoPath;
public LocalRepoCustomFormatJsonParser(IFileSystem fileSystem, IResourcePaths paths)
{
_fileSystem = fileSystem;
_repoPath = paths.RepoPath;
}
public async Task<IEnumerable<string>> GetCustomFormatJsonAsync()
{
await Task.Run(() =>
{
if (!Repository.IsValid(_repoPath))
{
if (_fileSystem.Directory.Exists(_repoPath))
{
_fileSystem.Directory.Delete(_repoPath, true);
}
Repository.Clone("https://github.com/TRaSH-/Guides.git", _repoPath, new CloneOptions
{
RecurseSubmodules = false
});
}
using var repo = new Repository(_repoPath);
Commands.Checkout(repo, "master", new CheckoutOptions
{
CheckoutModifiers = CheckoutModifiers.Force
});
var origin = repo.Network.Remotes["origin"];
Commands.Fetch(repo, origin.Name, origin.FetchRefSpecs.Select(s => s.Specification), null, "");
repo.Reset(ResetMode.Hard, repo.Branches["origin/master"].Tip);
});
var jsonDir = Path.Combine(_repoPath, "docs/json/radarr");
var tasks = _fileSystem.Directory.GetFiles(jsonDir, "*.json")
.Select(async f => await _fileSystem.File.ReadAllTextAsync(f));
return await Task.WhenAll(tasks);
}
}
}