| File: Utils\ManifestUtils.cs | Web Access |
| Project: src\tests\Aspire.Hosting.Tests\Aspire.Hosting.Tests.csproj (Aspire.Hosting.Tests) |
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using Aspire.Hosting.Publishing; using System.Text.Json.Nodes; using System.Text.Json; using Microsoft.Extensions.DependencyInjection; namespace Aspire.Hosting.Utils; public sealed class ManifestUtils { public static async Task<JsonNode> GetManifest(IResource resource, string? manifestDirectory = null) { var node = await GetManifestOrNull(resource, manifestDirectory); Assert.NotNull(node); return node; } public static async Task<JsonNode?> GetManifestOrNull(IResource resource, string? manifestDirectory = null) { manifestDirectory ??= Environment.CurrentDirectory; using var ms = new MemoryStream(); var writer = new Utf8JsonWriter(ms); var serviceCollection = new ServiceCollection(); var options = new DistributedApplicationExecutionContextOptions(DistributedApplicationOperation.Publish); var executionContext = new DistributedApplicationExecutionContext(options); serviceCollection.AddSingleton(executionContext); options.ServiceProvider = serviceCollection.BuildServiceProvider(); writer.WriteStartObject(); var context = new ManifestPublishingContext(executionContext, Path.Combine(manifestDirectory, "manifest.json"), writer); await context.WriteResourceAsync(resource); writer.WriteEndObject(); writer.Flush(); ms.Position = 0; var obj = JsonNode.Parse(ms); Assert.NotNull(obj); var resourceNode = obj[resource.Name]; return resourceNode; } public static async Task<JsonNode> GetManifestForModel(DistributedApplicationModel model, string? manifestDirectory = null) { manifestDirectory ??= Environment.CurrentDirectory; using var ms = new MemoryStream(); var writer = new Utf8JsonWriter(ms, new() { Indented = true }); var options = new DistributedApplicationExecutionContextOptions(DistributedApplicationOperation.Publish) { ServiceProvider = new ServiceCollection().BuildServiceProvider() }; var executionContext = new DistributedApplicationExecutionContext(options); var context = new ManifestPublishingContext(executionContext, Path.Combine(manifestDirectory, "manifest.json"), writer); await context.WriteModel(model, CancellationToken.None); ms.Position = 0; var obj = JsonNode.Parse(ms); Assert.NotNull(obj); return obj; } public static async Task<JsonNode[]> GetManifests(IResource[] resources) { using var ms = new MemoryStream(); var writer = new Utf8JsonWriter(ms); var options = new DistributedApplicationExecutionContextOptions(DistributedApplicationOperation.Publish) { ServiceProvider = new ServiceCollection().BuildServiceProvider() }; var executionContext = new DistributedApplicationExecutionContext(options); var context = new ManifestPublishingContext(executionContext, Path.Combine(Environment.CurrentDirectory, "manifest.json"), writer); var results = new List<JsonNode>(); foreach (var r in resources) { writer.WriteStartObject(); await context.WriteResourceAsync(r); writer.WriteEndObject(); writer.Flush(); ms.Position = 0; var obj = JsonNode.Parse(ms); Assert.NotNull(obj); var resourceNode = obj[r.Name]; Assert.NotNull(resourceNode); results.Add(resourceNode); ms.Position = 0; writer.Reset(ms); } return [.. results]; } }