w4c-workflows-api/Services/Execution/RuntimeRegistry.cs

47 lines
1.6 KiB
C#

namespace w4c_workflows.Services.Execution;
/// <summary>
/// A language as exposed by <c>GET /api/languages</c>: identity + whether its
/// runtime is actually executable on this host (availability is probed at
/// startup and cached).
/// </summary>
public sealed record RuntimeInfo(
string Id,
string DisplayName,
string DefaultFunction,
string Extension,
bool Available);
/// <summary>
/// Maps each registered language to its <see cref="IScriptExecutor"/>. Built on
/// top of <see cref="LanguageRegistry"/> (the source of truth for known
/// languages); the executor set is DI-injected so new languages/runtimes plug in
/// without touching this class.
/// </summary>
public class RuntimeRegistry
{
private readonly IReadOnlyDictionary<string, IScriptExecutor> _executors;
private readonly IReadOnlyList<RuntimeInfo> _all;
public RuntimeRegistry(LanguageRegistry languages, IEnumerable<IScriptExecutor> executors)
{
_executors = executors.ToDictionary(e => e.Language, StringComparer.Ordinal);
_all = languages.All
.Select(l => new RuntimeInfo(
l.Id,
l.DisplayName,
l.DefaultFunction,
l.Extension,
_executors.TryGetValue(l.Id, out var executor) && executor.IsAvailable()))
.ToList();
}
public IReadOnlyList<RuntimeInfo> All => _all;
public IScriptExecutor? Resolve(string language)
=> _executors.TryGetValue(language, out var executor) ? executor : null;
public bool IsAvailable(string language)
=> _executors.TryGetValue(language, out var executor) && executor.IsAvailable();
}