Skip to content

Commit

Permalink
Finishing
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Wilms committed Nov 11, 2024
1 parent 19b9954 commit f971542
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Nexus.PackageManagement.Services;

namespace Microsoft.Extensions.DependencyInjection;

/// <summary>
/// Extension methods for setting up MVC services in an <see cref="IServiceCollection" />.
/// </summary>
public static class MvcServiceCollectionExtensions
{
/// <summary>
/// Adds services required for the package management.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection AddPackageManagement(this IServiceCollection services)
{
return services
.AddSingleton<IPackageService, PackageService>()
.AddSingleton<IPackageManagementDatabaseService, PackageManagementDatabaseService>()
.AddSingleton<IExtensionHive, ExtensionHive>()
.Configure<IPackageManagementPathsOptions>(options => new PackageManagementPathsOptions());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.Options;

namespace Nexus.PackageManagement.Services;

Expand All @@ -18,4 +19,40 @@ public interface IPackageManagementDatabaseService
/// Returns a stream to write the package reference map to.
/// </summary>
Stream WritePackageReferenceMap();
}

internal class PackageManagementDatabaseService(IOptions<IPackageManagementPathsOptions> pathsOptions)
: IPackageManagementDatabaseService
{
private readonly IPackageManagementPathsOptions _pathsOptions = pathsOptions.Value;

private const string FILE_EXTENSION = ".json";

private const string PACKAGES = "packages";

public bool TryReadPackageReferenceMap([NotNullWhen(true)] out string? packageReferenceMap)
{
var folderPath = _pathsOptions.Config;
var packageReferencesFilePath = Path.Combine(folderPath, PACKAGES + FILE_EXTENSION);

packageReferenceMap = default;

if (File.Exists(packageReferencesFilePath))
{
packageReferenceMap = File.ReadAllText(packageReferencesFilePath);
return true;
}

return false;
}

public Stream WritePackageReferenceMap()
{
var folderPath = _pathsOptions.Config;
var packageReferencesFilePath = Path.Combine(folderPath, PACKAGES + FILE_EXTENSION);

Directory.CreateDirectory(folderPath);

return File.Open(packageReferencesFilePath, FileMode.Create, FileAccess.Write);
}
}
4 changes: 2 additions & 2 deletions src/Nexus/API/v1/PackageReferencesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ internal class PackageReferencesController(
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<IReadOnlyDictionary<Guid, PackageReference>> GetAsync()
public Task<IReadOnlyDictionary<Guid, PackageReference>> GetAsync()
{
return await _packageService.GetAllAsync();
return _packageService.GetAllAsync();
}

/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions tests/Nexus.PackageManagement.Tests/PackageServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ public async Task CanGetAllPackages()
};

var filePath = Path.GetTempFileName();
var pipelineService = GetPackageService(filePath, packageReferencesMap);
var packageService = GetPackageService(filePath, packageReferencesMap);

// Act
var actualPackageMap = await pipelineService.GetAllAsync();
var actualPackageMap = await packageService.GetAllAsync();

// Assert
var expected = JsonSerializerHelper.SerializeIndented(packageReferencesMap.OrderBy(current => current.Key));
Expand All @@ -155,8 +155,8 @@ private static IPackageService GetPackageService(
.Setup(databaseService => databaseService.WritePackageReferenceMap())
.Returns(() => File.OpenWrite(filePath));

var pipelineService = new PackageService(databaseService);
var packageService = new PackageService(databaseService);

return pipelineService;
return packageService;
}
}

0 comments on commit f971542

Please sign in to comment.