-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support loading repositories from files embedded in GirTool
- Loading branch information
Showing
13 changed files
with
319 additions
and
20 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/Generation/GirLoader/Helper/ChainedRepositoryResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace GirLoader; | ||
|
||
/// <summary> | ||
/// Tries multiple repository resolvers, returning the first result found | ||
/// </summary> | ||
public class ChainedRepositoryResolver : IRepositoryResolver | ||
{ | ||
private readonly IRepositoryResolver[] _resolvers; | ||
|
||
public ChainedRepositoryResolver(IEnumerable<IRepositoryResolver> resolvers) | ||
{ | ||
_resolvers = resolvers.ToArray(); | ||
} | ||
|
||
public Input.Repository? ResolveRepository(string fileName) | ||
{ | ||
foreach (var resolver in _resolvers) | ||
{ | ||
var repository = resolver.ResolveRepository(fileName); | ||
if (repository != null) | ||
{ | ||
return repository; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Generation/GirLoader/Helper/DirectoryRepositoryResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using System.IO; | ||
|
||
namespace GirLoader; | ||
|
||
/// <summary> | ||
/// Resolves repository files from a local directory | ||
/// </summary> | ||
public class DirectoryRepositoryResolver : IRepositoryResolver | ||
{ | ||
private readonly string _inputDirectory; | ||
|
||
public DirectoryRepositoryResolver(string inputDirectory) | ||
{ | ||
_inputDirectory = inputDirectory ?? throw new ArgumentNullException(nameof(inputDirectory)); | ||
} | ||
|
||
public Input.Repository? ResolveRepository(string fileName) | ||
{ | ||
var path = Path.Combine(_inputDirectory, fileName); | ||
return File.Exists(path) | ||
? new FileInfo(path).OpenRead().DeserializeGirInputModel() | ||
: null; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Generation/GirLoader/Helper/EmbeddedRepositoryResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.IO; | ||
using System.Reflection; | ||
|
||
namespace GirLoader; | ||
|
||
public class EmbeddedRepositoryResolver : IRepositoryResolver | ||
{ | ||
private readonly Assembly _assembly; | ||
private readonly string _platformName; | ||
private readonly string _assemblyName; | ||
|
||
public EmbeddedRepositoryResolver(Assembly assembly, string platformName) | ||
{ | ||
_assembly = assembly ?? throw new ArgumentNullException(nameof(assembly)); | ||
_platformName = platformName ?? throw new ArgumentNullException(nameof(platformName)); | ||
_assemblyName = _assembly.GetName().Name ?? throw new Exception("Could not get assembly name"); | ||
} | ||
|
||
public Input.Repository? ResolveRepository(string fileName) | ||
{ | ||
var resourceName = $"{_assemblyName}.{_platformName}.{fileName}"; | ||
try | ||
{ | ||
using var stream = _assembly.GetManifestResourceStream(resourceName); | ||
return stream?.DeserializeGirInputModel(); | ||
} | ||
catch (FileNotFoundException) | ||
{ | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace GirLoader; | ||
|
||
/// <summary> | ||
/// Resolves input repository definitions from GIR file names | ||
/// </summary> | ||
public interface IRepositoryResolver | ||
{ | ||
Input.Repository? ResolveRepository(string fileName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,18 @@ | ||
using System.IO; | ||
|
||
namespace GirLoader; | ||
namespace GirLoader; | ||
|
||
public class IncludeResolver | ||
{ | ||
private readonly string _inputDirectory; | ||
private readonly IRepositoryResolver _repositoryResolver; | ||
|
||
public IncludeResolver(string inputDirectory) | ||
public IncludeResolver(IRepositoryResolver repositoryResolver) | ||
{ | ||
_inputDirectory = inputDirectory; | ||
_repositoryResolver = repositoryResolver; | ||
} | ||
|
||
public Input.Repository? ResolveInclude(Output.Include include) | ||
{ | ||
var fileName = $"{include.Name}-{include.Version}.gir"; | ||
|
||
var path = Path.Combine(_inputDirectory, fileName); | ||
return File.Exists(path) | ||
? new FileInfo(path).OpenRead().DeserializeGirInputModel() | ||
: null; | ||
return _repositoryResolver.ResolveRepository(fileName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/Tests/Generation/GirLoader.Tests/ChainedRepositoryResolverTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System.Collections.Generic; | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace GirLoader.Test; | ||
|
||
[TestClass, TestCategory("UnitTest")] | ||
public class ChainedRepositoryResolverTests | ||
{ | ||
[TestMethod] | ||
public void ReturnsNullWhenNoRepositoryFound() | ||
{ | ||
var resolver = new ChainedRepositoryResolver(new[] | ||
{ | ||
new StubRepositoryResolver(new Dictionary<string, Input.Repository>()) | ||
}); | ||
|
||
var repository = resolver.ResolveRepository("GObject-2.0.gir"); | ||
|
||
repository.Should().BeNull(); | ||
} | ||
|
||
[TestMethod] | ||
public void ReturnsFirstResolvedRepositoryWhenMultipleMatch() | ||
{ | ||
var resolver = new ChainedRepositoryResolver(new[] | ||
{ | ||
new StubRepositoryResolver(new Dictionary<string, Input.Repository> | ||
{ | ||
{"GObject-2.0.gir", Helper.GetInputRepository("GObject", "2.1")}, | ||
}), | ||
new StubRepositoryResolver(new Dictionary<string, Input.Repository> | ||
{ | ||
{"GObject-2.0.gir", Helper.GetInputRepository("GObject", "2.2")}, | ||
}), | ||
}); | ||
|
||
var repository = resolver.ResolveRepository("GObject-2.0.gir"); | ||
|
||
repository.Should().NotBeNull(); | ||
repository!.Namespace.Should().NotBeNull(); | ||
repository.Namespace!.Version.Should().Be("2.1"); | ||
} | ||
|
||
[TestMethod] | ||
public void ReturnsRepositoryFromFallbackResolver() | ||
{ | ||
var resolver = new ChainedRepositoryResolver(new[] | ||
{ | ||
new StubRepositoryResolver(new Dictionary<string, Input.Repository> | ||
{ | ||
{"TestLibrary-1.0.gir", Helper.GetInputRepository("TestLibrary", "1.0")}, | ||
}), | ||
new StubRepositoryResolver(new Dictionary<string, Input.Repository> | ||
{ | ||
{"GObject-2.0.gir", Helper.GetInputRepository("GObject", "2.0")}, | ||
}), | ||
}); | ||
|
||
var repository = resolver.ResolveRepository("GObject-2.0.gir"); | ||
|
||
repository.Should().NotBeNull(); | ||
repository!.Namespace.Should().NotBeNull(); | ||
repository.Namespace!.Name.Should().Be("GObject"); | ||
} | ||
|
||
private class StubRepositoryResolver : IRepositoryResolver | ||
{ | ||
private readonly IReadOnlyDictionary<string, Input.Repository> _repositories; | ||
|
||
public StubRepositoryResolver(IReadOnlyDictionary<string, Input.Repository> repositories) | ||
{ | ||
_repositories = repositories; | ||
} | ||
|
||
public Input.Repository? ResolveRepository(string fileName) | ||
{ | ||
return _repositories.GetValueOrDefault(fileName); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Tests/Generation/GirLoader.Tests/DirectoryRepositoryResolverTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.IO; | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace GirLoader.Test; | ||
|
||
[TestClass, TestCategory("UnitTest")] | ||
public class DirectoryRepositoryResolverTests | ||
{ | ||
[TestMethod] | ||
public void CanLoadRepositoryFromDirectory() | ||
{ | ||
using var directory = new DisposableTempDirectory(); | ||
var filePath = Path.Join(directory.DirectoryPath, "GObject-2.0.gir"); | ||
File.WriteAllText(filePath, Helper.GetInputRepositoryXml("GObject", "2.0")); | ||
|
||
var resolver = new DirectoryRepositoryResolver(directory.DirectoryPath); | ||
|
||
var repository = resolver.ResolveRepository("GObject-2.0.gir"); | ||
|
||
repository.Should().NotBeNull(); | ||
repository!.Namespace.Should().NotBeNull(); | ||
repository.Namespace!.Name.Should().Be("GObject"); | ||
} | ||
|
||
[TestMethod] | ||
public void ReturnsNullWhenFileNotFound() | ||
{ | ||
using var directory = new DisposableTempDirectory(); | ||
var resolver = new DirectoryRepositoryResolver(directory.DirectoryPath); | ||
|
||
var repository = resolver.ResolveRepository("GObject-2.0.gir"); | ||
|
||
repository.Should().BeNull(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Tests/Generation/GirLoader.Tests/DisposableTempDirectory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using System.IO; | ||
|
||
namespace GirLoader.Test; | ||
|
||
public class DisposableTempDirectory : IDisposable | ||
{ | ||
private readonly string _directoryPath; | ||
|
||
public DisposableTempDirectory() | ||
{ | ||
_directoryPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); | ||
Directory.CreateDirectory(_directoryPath); | ||
} | ||
|
||
public string DirectoryPath => _directoryPath; | ||
|
||
public void Dispose() | ||
{ | ||
Directory.Delete(_directoryPath, recursive: true); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Tests/Generation/GirLoader.Tests/EmbeddedRepositoryResolverTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace GirLoader.Test; | ||
|
||
[TestClass, TestCategory("UnitTest")] | ||
public class EmbeddedRepositoryResolverTests | ||
{ | ||
[TestMethod] | ||
public void CanLoadRepositoryFromEmbeddedResource() | ||
{ | ||
var resolver = new EmbeddedRepositoryResolver( | ||
typeof(EmbeddedRepositoryResolverTests).Assembly, "linux"); | ||
|
||
var repository = resolver.ResolveRepository("GObject-2.0.gir"); | ||
|
||
repository.Should().NotBeNull(); | ||
repository!.Namespace.Should().NotBeNull(); | ||
repository.Namespace!.Name.Should().Be("GObject"); | ||
} | ||
|
||
[TestMethod] | ||
public void ReturnsNullWhenEmbeddedResourceNotFound() | ||
{ | ||
var resolver = new EmbeddedRepositoryResolver( | ||
typeof(EmbeddedRepositoryResolverTests).Assembly, "linux"); | ||
|
||
var repository = resolver.ResolveRepository("TestLibrary-1.0.gir"); | ||
|
||
repository.Should().BeNull(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.