| File: Templating\ConfiguredTemplateSourceService.cs | Web Access |
| Project: src\src\Aspire.Cli\Aspire.Cli.csproj (aspire) |
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Globalization; using System.Text.Json; using System.Text.Json.Nodes; using Aspire.Cli.Configuration; using Aspire.Cli.Interaction; using Aspire.Cli.Resources; using Aspire.Cli.Utils; namespace Aspire.Cli.Templating; internal sealed class ConfiguredTemplateSourceService( IConfigurationService configurationService, ITemplateProvider templateProvider, IInteractionService interactionService, CliExecutionContext executionContext, ConfigSourceService configSources) { internal async Task<IReadOnlyList<KeyValuePair<string, ConfiguredTemplateSource>>> GetSourcesAsync(CancellationToken cancellationToken) { var settingsPath = configurationService.GetSettingsFilePath(isGlobal: true); AspireConfigFile? settings; try { settings = JsonSerializer.Deserialize( await File.ReadAllTextAsync(settingsPath, cancellationToken), JsonSourceGenerationContext.Default.AspireConfigFile); } catch (Exception ex) when (ex is FileNotFoundException or DirectoryNotFoundException) { settings = null; } var inherited = await configSources.GetTemplateSourcesAsync(cancellationToken); var inheritedSettings = new JsonObject { [AspireConfigFile.TemplateSourcesSectionName] = inherited }.Deserialize(JsonSourceGenerationContext.Default.AspireConfigFile); var definitions = TemplateSourceConfiguration.MergeSources( inheritedSettings?.TemplateSources ?? [], settings?.TemplateSources ?? []); List<KeyValuePair<string, ConfiguredTemplateSource>> sources = []; foreach (var (name, definition) in definitions) { cancellationToken.ThrowIfCancellationRequested(); TemplateSourceConfiguration.ValidateName(name); if (definition is null) { throw new TemplateSourceConfigurationException($"{AspireConfigFile.TemplateSourcesSectionName}.{name}", NewCommandStrings.InvalidConfiguredTemplateSource); } var source = TemplateSourceConfiguration.Parse( definition.Path, definition.Repository, definition.Ref, definition.DisplayName, $"{AspireConfigFile.TemplateSourcesSectionName}.{name}", requireComplete: true); sources.Add(new(name, source)); } return sources; } public async Task<IReadOnlyList<ConfiguredTemplate>> SearchAsync(string query, CancellationToken cancellationToken) { var sources = await GetSourcesAsync(cancellationToken); if (sources.Count == 0) { interactionService.DisplayMessage(KnownEmojis.Information, NewCommandStrings.NoConfiguredTemplateSources); return []; } interactionService.DisplayTemplateSources(sources.Select(source => (source.Value.DisplayName ?? source.Key, source.Value.Template ?? throw new InvalidOperationException("A complete template source must have a location."))), executionContext); List<ConfiguredTemplate> results = []; // The template engine owns mutable mount/resource state. Discover sequentially rather // than sharing a factory across concurrent source installations. foreach (var (name, configuredSource) in sources) { cancellationToken.ThrowIfCancellationRequested(); try { var source = configuredSource.Template ?? throw new InvalidOperationException("A complete template source must have a location."); var templates = await templateProvider.GetTemplatesAsync(source, cancellationToken); results.AddRange(templates .Where(template => template.Name.Contains(query, StringComparison.OrdinalIgnoreCase) || template.Description.Contains(query, StringComparison.OrdinalIgnoreCase) || template.SearchDescription?.Contains(query, StringComparison.OrdinalIgnoreCase) is true) .Select(template => new ConfiguredTemplate(name, template, configuredSource.DisplayName, source))); } catch (Exception ex) when (ex is TemplateDiscoveryException or IOException or UnauthorizedAccessException or ArgumentException) { Warn(name, ex.Message); } } return results.OrderBy(result => result.Template.Description, StringComparer.OrdinalIgnoreCase) .ThenBy(result => result.SourceName, StringComparers.TemplateSourceName) .ThenBy(result => result.Template.Name, StringComparer.OrdinalIgnoreCase) .ToArray(); } private void Warn(string source, string message) => interactionService.DisplayMessage(KnownEmojis.PoliceCarLight, string.Format( CultureInfo.CurrentCulture, NewCommandStrings.TemplateSourceSearchFailed, source, message)); } internal sealed record ConfiguredTemplate(string SourceName, ITemplate Template, string? SourceDisplayName, TemplateSource Source);