|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Diagnostics.CodeAnalysis; |
| 5 | +using Aspire.Hosting.ApplicationModel; |
| 6 | +using Microsoft.Extensions.Configuration; |
| 7 | +using Microsoft.Extensions.Options; |
| 8 | + |
| 9 | +namespace Aspire.Hosting.Pipelines; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Default implementation of <see cref="IPipelineOutputService"/>. |
| 13 | +/// </summary> |
| 14 | +[Experimental("ASPIREPIPELINES004", UrlFormat = "https://aka.ms/aspire/diagnostics/{0}")] |
| 15 | +internal sealed class PipelineOutputService : IPipelineOutputService |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Stores the resolved output directory path, or <c>null</c> if not specified. |
| 19 | + /// </summary> |
| 20 | + private readonly string? _outputPath; |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Lazily creates and stores the path to the temporary directory for pipeline output. |
| 24 | + /// </summary> |
| 25 | + private readonly Lazy<string> _tempDirectory; |
| 26 | + |
| 27 | + public PipelineOutputService(IOptions<PipelineOptions> options, IConfiguration configuration) |
| 28 | + { |
| 29 | + _outputPath = options.Value.OutputPath is not null ? Path.GetFullPath(options.Value.OutputPath) : null; |
| 30 | + _tempDirectory = new Lazy<string>(() => CreateTempDirectory(configuration)); |
| 31 | + } |
| 32 | + |
| 33 | + /// <inheritdoc/> |
| 34 | + public string GetOutputDirectory() |
| 35 | + { |
| 36 | + return _outputPath ?? Path.Combine(Environment.CurrentDirectory, "aspire-output"); |
| 37 | + } |
| 38 | + |
| 39 | + /// <inheritdoc/> |
| 40 | + public string GetOutputDirectory(IResource resource) |
| 41 | + { |
| 42 | + ArgumentNullException.ThrowIfNull(resource); |
| 43 | + |
| 44 | + var baseOutputDir = GetOutputDirectory(); |
| 45 | + return Path.Combine(baseOutputDir, resource.Name); |
| 46 | + } |
| 47 | + |
| 48 | + /// <inheritdoc/> |
| 49 | + public string GetTempDirectory() |
| 50 | + { |
| 51 | + return _tempDirectory.Value; |
| 52 | + } |
| 53 | + |
| 54 | + /// <inheritdoc/> |
| 55 | + public string GetTempDirectory(IResource resource) |
| 56 | + { |
| 57 | + ArgumentNullException.ThrowIfNull(resource); |
| 58 | + |
| 59 | + var baseTempDir = GetTempDirectory(); |
| 60 | + return Path.Combine(baseTempDir, resource.Name); |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Creates a temporary directory for pipeline build artifacts. |
| 65 | + /// Uses AppHost:PathSha256 from configuration to create an isolated temp directory per app host, |
| 66 | + /// enabling multiple app hosts to run concurrently without conflicts. |
| 67 | + /// If AppHost:PathSha256 is not available, falls back to a generic "aspire" temp directory. |
| 68 | + /// </summary> |
| 69 | + private static string CreateTempDirectory(IConfiguration configuration) |
| 70 | + { |
| 71 | + var appHostSha = configuration["AppHost:PathSha256"]; |
| 72 | + |
| 73 | + if (!string.IsNullOrEmpty(appHostSha)) |
| 74 | + { |
| 75 | + return Directory.CreateTempSubdirectory($"aspire-{appHostSha}").FullName; |
| 76 | + } |
| 77 | + |
| 78 | + // Fallback if AppHost:PathSha256 is not available |
| 79 | + return Directory.CreateTempSubdirectory("aspire").FullName; |
| 80 | + } |
| 81 | +} |
0 commit comments