Skip to content

Commit

Permalink
Allow setting the path to registry.json file (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdovaz authored Dec 10, 2024
1 parent 18fe0c3 commit f783cea
Show file tree
Hide file tree
Showing 18 changed files with 288 additions and 129 deletions.
5 changes: 3 additions & 2 deletions examples/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ services:
- Registry:UnityScope=org.custom # Packages prefix, default is "org.nuget" but it can be modified to be able to have several containers with different prefixes and to be able to add several scope registries.
- Registry:MinimumUnityVersion=2020.1 # Minimum version of Unity required to install packages, default is "2019.1".
- Registry:PackageNameNuGetPostFix= (Custom NuGet) # Suffix of the package title, useful in case of having several containers and several scope registries, default is " (NuGet)".
- Registry:RootPersistentFolder=custom_unity_packages # Path to the folder where the packages cache will be stored, default is "unity_packages".
- Registry:RegistryFilePath=custom_registry.json # Path to the file (relative or absolute) where the packages registry file will be stored, default is "registry.json".
- Registry:RootPersistentFolder=custom_unity_packages # Path to the folder (relative or absolute) where the packages cache will be stored, default is "unity_packages".
- Registry:UpdateInterval=00:01:00 # Packages update interval, default is "00:10:00" (10 minutes).
- Logging:LogLevel:Default=Information
ports:
- 5000:8080
volumes:
- ./registry.json:/app/custom_registry.json # Override the package registry to be able to add or remove packages.
- ./unity_packages:/app/custom_unity_packages # Map the folder with the packages cache.
- ./registry.json:/app/registry.json # Override the package registry to be able to add or remove packages.
- ./NuGet.Config:/root/.nuget/NuGet/NuGet.Config # Override Nuget.config file with repository information. This file can be used to configure a custom NuGet repository: https://docs.microsoft.com/en-us/nuget/reference/nuget-config-file
2 changes: 2 additions & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
<ItemGroup>
<PackageVersion Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Diagnostics.Testing" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Options" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Options.DataAnnotations" Version="9.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageVersion Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
<PackageVersion Include="Moq" Version="4.20.72" />
<PackageVersion Include="NuGet.PackageManagement" Version="6.12.1" />
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="9.0.0" />
<PackageVersion Include="NUglify" Version="1.21.10" />
Expand Down
3 changes: 3 additions & 0 deletions src/UnityNuGet.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

var builder = WebApplication.CreateBuilder(args);

// Add the registry
builder.Services.AddSingleton<Registry>();
builder.Services.AddHostedService(serviceProvider => serviceProvider.GetRequiredService<Registry>());
// Add the registry cache initializer
builder.Services.AddHostedService<RegistryCacheInitializer>();
// Add the registry cache updater
Expand Down
48 changes: 27 additions & 21 deletions src/UnityNuGet.Server/RegistryCacheInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@ public class RegistryCacheInitializer(IConfiguration configuration, IHostEnviron
{
private readonly IConfiguration configuration = configuration;
private readonly IHostEnvironment hostEnvironment = hostEnvironment;
private readonly ILoggerFactory loggerFactory = loggerFactory;
private readonly ILoggerFactory loggerFactory = loggerFactory;
private readonly RegistryOptions registryOptions = registryOptionsAccessor.Value;
private readonly RegistryCacheSingleton registryCacheSingleton = registryCacheSingleton;

public Task StartAsync(CancellationToken cancellationToken)
{
var loggerRedirect = new NuGetRedirectLogger(loggerFactory.CreateLogger("NuGet"));
{
var logger = loggerFactory.CreateLogger("NuGet");
var loggerRedirect = new NuGetRedirectLogger(logger);

Uri uri = registryOptions.RootHttpUrl!;

bool isDevelopment = hostEnvironment.IsDevelopment();
bool isDevelopment = hostEnvironment.IsDevelopment();

if (isDevelopment)
{
var urls = configuration[WebHostDefaults.ServerUrlsKey];
Expand All @@ -41,24 +43,28 @@ public Task StartAsync(CancellationToken cancellationToken)

// Get the current directory from registry options (prepend binary folder in dev)
string unityPackageFolder;

if (Path.IsPathRooted(registryOptions.RegistryFilePath))
{
unityPackageFolder = registryOptions.RootPersistentFolder!;
}
else
{
string currentDirectory;

if (isDevelopment)
{
currentDirectory = Path.GetDirectoryName(AppContext.BaseDirectory)!;
}
else
{
currentDirectory = Directory.GetCurrentDirectory();
}

unityPackageFolder = Path.Combine(currentDirectory, registryOptions.RootPersistentFolder!);
}

if (isDevelopment)
{
var currentDirectory = Path.GetDirectoryName(AppContext.BaseDirectory)!;
unityPackageFolder = Path.Combine(currentDirectory, new DirectoryInfo(registryOptions.RootPersistentFolder!).Name);
}
else
{
if (Path.IsPathRooted(registryOptions.RootPersistentFolder))
{
unityPackageFolder = registryOptions.RootPersistentFolder;
}
else
{
unityPackageFolder = Path.Combine(Directory.GetCurrentDirectory(), registryOptions.RootPersistentFolder!);
}
}
loggerRedirect.LogInformation($"Using Unity Package folder `{unityPackageFolder}`");
logger.LogInformation("Using Unity Package folder `{UnityPackageFolder}`", unityPackageFolder);

// Add the cache accessible from the services
registryCacheSingleton.UnityPackageFolder = unityPackageFolder;
Expand Down
13 changes: 7 additions & 6 deletions src/UnityNuGet.Server/RegistryCacheUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ namespace UnityNuGet.Server
/// <summary>
/// Update the RegistryCache at a regular interval
/// </summary>
internal sealed class RegistryCacheUpdater(RegistryCacheReport registryCacheReport, RegistryCacheSingleton currentRegistryCache, ILogger<RegistryCacheUpdater> logger, IOptions<RegistryOptions> registryOptionsAccessor) : BackgroundService
{
internal sealed class RegistryCacheUpdater(Registry registry, RegistryCacheReport registryCacheReport, RegistryCacheSingleton currentRegistryCache, ILogger<RegistryCacheUpdater> logger, IOptions<RegistryOptions> registryOptionsAccessor) : BackgroundService
{
private readonly Registry registry = registry;
private readonly RegistryCacheReport _registryCacheReport = registryCacheReport;
private readonly RegistryCacheSingleton _currentRegistryCache = currentRegistryCache;
private readonly ILogger _logger = logger;
Expand All @@ -28,17 +29,17 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)

_registryCacheReport.Start();

var newRegistryCache = new RegistryCache(_currentRegistryCache.UnityPackageFolder!, _currentRegistryCache.ServerUri!, _registryOptions.UnityScope!, _registryOptions.MinimumUnityVersion!, _registryOptions.PackageNameNuGetPostFix!, _registryOptions.TargetFrameworks!, _currentRegistryCache.NuGetRedirectLogger!)
var newRegistryCache = new RegistryCache(registry, _currentRegistryCache.UnityPackageFolder!, _currentRegistryCache.ServerUri!, _registryOptions.UnityScope!, _registryOptions.MinimumUnityVersion!, _registryOptions.PackageNameNuGetPostFix!, _registryOptions.TargetFrameworks!, _currentRegistryCache.NuGetRedirectLogger!)
{
// Update progress
OnProgress = (current, total) =>
{
_currentRegistryCache.ProgressTotalPackageCount = total;
_currentRegistryCache.ProgressPackageIndex = current;
},
OnInformation = message => _registryCacheReport.AddInformation(message),
OnWarning = message => _registryCacheReport.AddWarning(message),
OnError = message => _registryCacheReport.AddError(message)
OnInformation = _registryCacheReport.AddInformation,
OnWarning = _registryCacheReport.AddWarning,
OnError = _registryCacheReport.AddError
};

await newRegistryCache.Build();
Expand Down
17 changes: 9 additions & 8 deletions src/UnityNuGet.Server/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
"UnityScope": "org.nuget",
"MinimumUnityVersion": "2019.1",
"PackageNameNuGetPostFix": " (NuGet)",
"RegistryFilePath": "registry.json",
"RootPersistentFolder": "unity_packages",
"UpdateInterval": "00:10:00",
"TargetFrameworks": [
{
"Name": "netstandard2.1",
"DefineConstraints": [ "UNITY_2021_2_OR_NEWER" ]
},
{
"Name": "netstandard2.0",
"DefineConstraints": [ "!UNITY_2021_2_OR_NEWER" ]
}
{
"Name": "netstandard2.1",
"DefineConstraints": [ "UNITY_2021_2_OR_NEWER" ]
},
{
"Name": "netstandard2.0",
"DefineConstraints": [ "!UNITY_2021_2_OR_NEWER" ]
}
]
},
"Logging": {
Expand Down
21 changes: 19 additions & 2 deletions src/UnityNuGet.Tests/NativeTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing;
using Microsoft.Extensions.Options;
using Moq;
using NUnit.Framework;

namespace UnityNuGet.Tests
Expand All @@ -10,13 +16,24 @@ public class NativeTests
{
[Test]
public async Task TestBuild()
{
{
var hostEnvironmentMock = new Mock<IHostEnvironment>();
hostEnvironmentMock.Setup(h => h.EnvironmentName).Returns(Environments.Development);

var loggerFactory = new LoggerFactory();
loggerFactory.AddProvider(new FakeLoggerProvider());

var unityPackages = Path.Combine(Path.GetDirectoryName(typeof(RegistryCacheTests).Assembly.Location)!, "unity_packages");
Directory.Delete(unityPackages, true);

var errorsTriggered = false;

var registry = new Registry(hostEnvironmentMock.Object, loggerFactory, Options.Create(new RegistryOptions { RegistryFilePath = "registry.json" }));

await registry.StartAsync(CancellationToken.None);

var registryCache = new RegistryCache(
var registryCache = new RegistryCache(
registry,
unityPackages,
new Uri("http://localhost/"),
"org.nuget",
Expand Down
42 changes: 21 additions & 21 deletions src/UnityNuGet.Tests/NuGetHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ public void IsApplicableUnitySupportedRoslynVersionFolder_Invalid(string input)
[Test]
public void GetCompatiblePackageDependencyGroups_SpecificSingleFramework()
{
IList<PackageDependencyGroup> packageDependencyGroups = new PackageDependencyGroup[]
{
new(CommonFrameworks.NetStandard13, Array.Empty<PackageDependency>()),
new(CommonFrameworks.NetStandard16, Array.Empty<PackageDependency>()),
new(CommonFrameworks.NetStandard20, Array.Empty<PackageDependency>()),
new(CommonFrameworks.NetStandard21, Array.Empty<PackageDependency>())
};
IList<PackageDependencyGroup> packageDependencyGroups =
[
new(CommonFrameworks.NetStandard13, []),
new(CommonFrameworks.NetStandard16, []),
new(CommonFrameworks.NetStandard20, []),
new(CommonFrameworks.NetStandard21, [])
];

IEnumerable<RegistryTargetFramework> targetFrameworks = new RegistryTargetFramework[] { new() { Framework = CommonFrameworks.NetStandard20 } };
IEnumerable<RegistryTargetFramework> targetFrameworks = [new() { Framework = CommonFrameworks.NetStandard20 }];

IEnumerable<PackageDependencyGroup> compatibleDependencyGroups = NuGetHelper.GetCompatiblePackageDependencyGroups(packageDependencyGroups, targetFrameworks);

Expand All @@ -80,15 +80,15 @@ public void GetCompatiblePackageDependencyGroups_SpecificSingleFramework()
[Test]
public void GetCompatiblePackageDependencyGroups_SpecificMultipleFrameworks()
{
IList<PackageDependencyGroup> packageDependencyGroups = new PackageDependencyGroup[]
{
new(CommonFrameworks.NetStandard13, Array.Empty<PackageDependency>()),
new(CommonFrameworks.NetStandard16, Array.Empty<PackageDependency>()),
new(CommonFrameworks.NetStandard20, Array.Empty<PackageDependency>()),
new(CommonFrameworks.NetStandard21, Array.Empty<PackageDependency>())
};
IList<PackageDependencyGroup> packageDependencyGroups =
[
new(CommonFrameworks.NetStandard13, []),
new(CommonFrameworks.NetStandard16, []),
new(CommonFrameworks.NetStandard20, []),
new(CommonFrameworks.NetStandard21, [])
];

IEnumerable<RegistryTargetFramework> targetFrameworks = new RegistryTargetFramework[] { new() { Framework = CommonFrameworks.NetStandard20 }, new() { Framework = CommonFrameworks.NetStandard21 } };
IEnumerable<RegistryTargetFramework> targetFrameworks = [new() { Framework = CommonFrameworks.NetStandard20 }, new() { Framework = CommonFrameworks.NetStandard21 }];

IEnumerable<PackageDependencyGroup> compatibleDependencyGroups = NuGetHelper.GetCompatiblePackageDependencyGroups(packageDependencyGroups, targetFrameworks);

Expand All @@ -98,12 +98,12 @@ public void GetCompatiblePackageDependencyGroups_SpecificMultipleFrameworks()
[Test]
public void GetCompatiblePackageDependencyGroups_AnyFramework()
{
IList<PackageDependencyGroup> packageDependencyGroups = new PackageDependencyGroup[]
{
new(new NuGetFramework(SpecialIdentifiers.Any), Array.Empty<PackageDependency>())
};
IList<PackageDependencyGroup> packageDependencyGroups =
[
new(new NuGetFramework(SpecialIdentifiers.Any), [])
];

IEnumerable<RegistryTargetFramework> targetFrameworks = new RegistryTargetFramework[] { new() { Framework = CommonFrameworks.NetStandard20 } };
IEnumerable<RegistryTargetFramework> targetFrameworks = [new() { Framework = CommonFrameworks.NetStandard20 }];

IEnumerable<PackageDependencyGroup> compatibleDependencyGroups = NuGetHelper.GetCompatiblePackageDependencyGroups(packageDependencyGroups, targetFrameworks);

Expand Down
6 changes: 3 additions & 3 deletions src/UnityNuGet.Tests/PlatformDefinitionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void CanFindDefinitions()
});
Assert.Multiple(() =>
{
Assert.That(win64.Cpu, Is.EqualTo(UnityCpu.X64));
Assert.That(win64?.Cpu, Is.EqualTo(UnityCpu.X64));
Assert.That(win.Children, Does.Contain(win64));
});

Expand Down Expand Up @@ -84,10 +84,10 @@ public void RemainingPlatforms_LeafVisited()

// The remaining platforms should be all non-windows, as well as all !x64 windows
var expected = platformDefs.Children
.Except(new[] { win64!.Parent })
.Except([win64!.Parent])
.Concat(
win64.Parent!.Children
.Except(new[] { win64 }))
.Except([win64]))
.ToHashSet();
var actual = platformDefs.GetRemainingPlatforms(visited);
Assert.That(expected.SetEquals(actual), Is.True);
Expand Down
17 changes: 17 additions & 0 deletions src/UnityNuGet.Tests/RegistryCacheTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging.Testing;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using NUnit.Framework;

namespace UnityNuGet.Tests
Expand All @@ -12,8 +18,19 @@ public async Task TestBuild()
{
var errorsTriggered = false;

var hostEnvironmentMock = new Mock<IHostEnvironment>();
hostEnvironmentMock.Setup(h => h.EnvironmentName).Returns(Environments.Development);

var loggerFactory = new LoggerFactory();
loggerFactory.AddProvider(new FakeLoggerProvider());

var unityPackages = Path.Combine(Path.GetDirectoryName(typeof(RegistryCacheTests).Assembly.Location)!, "unity_packages");
var registry = new Registry(hostEnvironmentMock.Object, loggerFactory, Options.Create(new RegistryOptions { RegistryFilePath = "registry.json" }));

await registry.StartAsync(CancellationToken.None);

var registryCache = new RegistryCache(
registry,
unityPackages,
new Uri("http://localhost/"),
"org.nuget",
Expand Down
Loading

0 comments on commit f783cea

Please sign in to comment.