-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow setting the path to registry.json file
- Loading branch information
Showing
11 changed files
with
112 additions
and
57 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,49 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Options; | ||
using Newtonsoft.Json; | ||
|
||
namespace UnityNuGet | ||
{ | ||
/// <summary> | ||
/// Loads the `registry.json` file at startup | ||
/// </summary> | ||
[Serializable] | ||
public sealed class Registry : Dictionary<string, RegistryEntry> | ||
{ | ||
private const string RegistryFileName = "registry.json"; | ||
private static readonly Lock LockRead = new(); | ||
private static Registry? _registry = null; | ||
|
||
// A comparer is established for cases where the dependency name is not set to the correct case. | ||
// Example: https://www.nuget.org/packages/NeoSmart.Caching.Sqlite/0.1.0#dependencies-body-tab | ||
public Registry() : base(StringComparer.OrdinalIgnoreCase) | ||
{ | ||
} | ||
|
||
public static Registry Parse(string json) | ||
{ | ||
ArgumentNullException.ThrowIfNull(json); | ||
return JsonConvert.DeserializeObject<Registry>(json, JsonCommonExtensions.Settings)!; | ||
} | ||
|
||
public static Registry GetInstance() | ||
{ | ||
lock (LockRead) | ||
{ | ||
_registry ??= Parse(File.ReadAllText(Path.Combine(Path.GetDirectoryName(AppContext.BaseDirectory)!, RegistryFileName))); | ||
} | ||
return _registry; | ||
} | ||
public sealed class Registry(IOptions<RegistryOptions> registryOptionsAccessor) : IHostedService, IReadOnlyCollection<KeyValuePair<string, RegistryEntry>>, IEnumerable<KeyValuePair<string, RegistryEntry>> | ||
{ | ||
private IDictionary<string, RegistryEntry>? _data; | ||
|
||
private readonly RegistryOptions registryOptions = registryOptionsAccessor.Value; | ||
|
||
public int Count => _data!.Count; | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => _data!.GetEnumerator(); | ||
|
||
public IEnumerator<KeyValuePair<string, RegistryEntry>> GetEnumerator() => _data!.GetEnumerator(); | ||
|
||
public bool TryGetValue(string key, out RegistryEntry value) => _data!.TryGetValue(key, out value!); | ||
|
||
public async Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
string registryFilePath; | ||
|
||
if (Path.IsPathRooted(registryOptions.RegistryFilePath)) | ||
{ | ||
registryFilePath = registryOptions.RegistryFilePath; | ||
} | ||
else | ||
{ | ||
registryFilePath = Path.Combine(Directory.GetCurrentDirectory(), registryOptions.RegistryFilePath!); | ||
} | ||
|
||
string json = await File.ReadAllTextAsync(registryFilePath, cancellationToken); | ||
|
||
_data = JsonConvert.DeserializeObject<IDictionary<string, RegistryEntry>>(json, JsonCommonExtensions.Settings)!; | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; | ||
} | ||
} |
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
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,12 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<configuration> | ||
<packageSources> | ||
<clear /> | ||
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" /> | ||
</packageSources> | ||
<packageSourceMapping> | ||
<packageSource key="nuget.org"> | ||
<package pattern="*" /> | ||
</packageSource> | ||
</packageSourceMapping> | ||
</configuration> |