Skip to content
This repository has been archived by the owner on Jun 10, 2023. It is now read-only.

Commit

Permalink
Add Combine for Uri
Browse files Browse the repository at this point in the history
  • Loading branch information
j2ghz committed Aug 15, 2018
1 parent 7b1f45b commit 4575379
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 24 deletions.
19 changes: 11 additions & 8 deletions src/ModSink.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,16 @@ private static void AddDump(this CommandLineApplication app)
else
{
var formatter = new BinaryFormatter();
foreach (var repoUri in ((Group) formatter.Deserialize(groupUriStr.GetStreamAsync().GetAwaiter()
.GetResult()))
.RepoInfos)
var group = (Group) formatter.Deserialize(groupUriStr.GetStreamAsync().GetAwaiter()
.GetResult());
group.BaseUri = groupUri;
foreach (var repoInfo in group.RepoInfos)
{
var repo = (Repo) formatter.Deserialize(new Uri(groupUri, repoUri.Uri).ToString()
var repoUri = group.CombineBaseUri(repoInfo.Uri);
var repo = (Repo) formatter.Deserialize(repoUri.ToString()
.GetStreamAsync().GetAwaiter()
.GetResult());
repo.BaseUri = repoUri;
DumpRepo(repo);
}
}
Expand Down Expand Up @@ -328,15 +331,15 @@ private static void DumpRepo(Repo repo)
{
Console.WriteLine($"Repo at {repo.BaseUri}");
Console.WriteLine("Files:");
foreach (var file in repo.Files.OrderBy(f=>f.Key))
Console.WriteLine($"\t{file.Key} at {new Uri(repo.BaseUri, file.Value)}");
foreach (var file in repo.Files.OrderBy(f => f.Key))
Console.WriteLine($"\t{file.Key} at {repo.CombineBaseUri(file.Value)}");

Console.WriteLine("ModPacks:");
foreach (var modpack in repo.Modpacks.OrderBy(m=>m.Name))
foreach (var modpack in repo.Modpacks.OrderBy(m => m.Name))
{
Console.WriteLine($"\tModpack '{modpack.Name}'");
Console.WriteLine("\tMods:");
foreach (var mod in modpack.Mods.OrderBy(m=>m.Mod.Name))
foreach (var mod in modpack.Mods.OrderBy(m => m.Mod.Name))
Console.WriteLine($"\t\tMod: '{mod.Mod.Name}' [{mod.Mod.Files.Count} files]");
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/ModSink.Common.Tests/Models/WithBaseUriTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using FluentAssertions;
using ModSink.Common.Models;
using Xunit;

namespace ModSink.Common.Tests.Models
{
public class WithBaseUriTests
{
[Theory]
[InlineData("http://example.com/group.bin", "Repo/repo.bin", "http://example.com/Repo/repo.bin")]
[InlineData("http://example.com/Repo/repo.bin","Modpack/Mod/File.bin", "http://example.com/Repo/Modpack/Mod/File.bin")]
public void CombinesCorrectly(string @base, string relative, string expected)
{
var withBaseUri = new WithBaseUriTestClass(){BaseUri = new Uri(@base) };
var actual = withBaseUri.CombineBaseUri(new Uri(relative,UriKind.Relative));
actual.Should().Be(new Uri(expected));
}
private class WithBaseUriTestClass : WithBaseUri
{

}
}
}
2 changes: 1 addition & 1 deletion src/ModSink.Common/Client/ClientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private Stream GetTemporaryFileStream(FileSignature argFileSignature)
return fileAccessService.Write(argFileSignature, true);
}

private async Task<T> Load<T>(Uri uri) where T : IBaseUri
private async Task<T> Load<T>(Uri uri) where T : WithBaseUri
{
LogTo.Information("Loading {T} from {url}", typeof(T), uri);
using (var mem = new MemoryStream())
Expand Down
4 changes: 2 additions & 2 deletions src/ModSink.Common/Client/DynamicDataChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static IObservable<IChangeSet<OnlineFile, FileSignature>> GetOnlineFileFr
{
return repos
.TransformMany(
repo => repo.Files.Select(kvp => new OnlineFile(kvp.Key, new Uri(repo.BaseUri, kvp.Value))),
repo => repo.Files.Select(kvp => new OnlineFile(kvp.Key, repo.CombineBaseUri(kvp.Value))),
of => of.FileSignature);
}

Expand All @@ -45,7 +45,7 @@ public static IObservable<IChangeSet<Repo, Uri>> GetReposFromGroups(
return groups
.Transform(g => new Uri(g))
.TransformAsync(loadGroup)
.TransformMany(g => g.RepoInfos.Select(r => new Uri(g.BaseUri, r.Uri)), repoUri => repoUri)
.TransformMany(g => g.RepoInfos.Select(r => g.CombineBaseUri(r.Uri)), repoUri => repoUri)
.TransformAsync(loadRepo)
.OnItemUpdated((repo, _) => LogTo.Information("Repo from {url} has been loaded", repo.BaseUri));
}
Expand Down
3 changes: 1 addition & 2 deletions src/ModSink.Common/Models/Group/Group.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
namespace ModSink.Common.Models.Group
{
[Serializable]
public class Group : IBaseUri
public class Group : WithBaseUri
{
public ICollection<RepoInfo> RepoInfos { get; set; }
public Uri BaseUri { get; set; }
}
}
9 changes: 0 additions & 9 deletions src/ModSink.Common/Models/IBaseUri.cs

This file was deleted.

3 changes: 1 addition & 2 deletions src/ModSink.Common/Models/Repo/Repo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
namespace ModSink.Common.Models.Repo
{
[Serializable]
public class Repo : IBaseUri
public class Repo : WithBaseUri
{
public IDictionary<FileSignature, Uri> Files { get; set; }

public ICollection<Modpack> Modpacks { get; set; }
public Uri BaseUri { get; set; }
}
}
15 changes: 15 additions & 0 deletions src/ModSink.Common/Models/WithBaseUri.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace ModSink.Common.Models
{
[Serializable]
public abstract class WithBaseUri
{
public Uri BaseUri { get; set; }

public Uri CombineBaseUri(Uri relative)
{
return new Uri(BaseUri, relative);
}
}
}

0 comments on commit 4575379

Please sign in to comment.