| | | 1 | | using CShells.Features; |
| | | 2 | | using Elsa.Features.Contracts; |
| | | 3 | | using Elsa.Features.Models; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Features.Services; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Shell-based implementation of <see cref="IInstalledFeatureProvider"/> that reads from CShells feature descriptors. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// This implementation automatically discovers features from the shell's feature descriptors, |
| | | 12 | | /// mapping them to Elsa's <see cref="FeatureDescriptor"/> model. Features are read from the |
| | | 13 | | /// shell's DI container where they were registered during shell initialization. |
| | | 14 | | /// </remarks> |
| | | 15 | | public class ShellInstalledFeatureProvider : IInstalledFeatureProvider |
| | | 16 | | { |
| | | 17 | | private readonly IEnumerable<ShellFeatureDescriptor> _shellFeatures; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Initializes a new instance of the <see cref="ShellInstalledFeatureProvider"/> class. |
| | | 21 | | /// </summary> |
| | 0 | 22 | | public ShellInstalledFeatureProvider(IEnumerable<ShellFeatureDescriptor> shellFeatures) |
| | | 23 | | { |
| | 0 | 24 | | _shellFeatures = shellFeatures; |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <inheritdoc /> |
| | | 28 | | public IEnumerable<FeatureDescriptor> List() |
| | | 29 | | { |
| | 0 | 30 | | return _shellFeatures |
| | 0 | 31 | | .Where(sf => sf.StartupType != null) |
| | 0 | 32 | | .Select(MapToElsaFeatureDescriptor); |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <inheritdoc /> |
| | | 36 | | public FeatureDescriptor? Find(string fullName) |
| | | 37 | | { |
| | 0 | 38 | | var shellFeature = _shellFeatures |
| | 0 | 39 | | .Where(sf => sf.StartupType != null) |
| | 0 | 40 | | .FirstOrDefault(sf => MapToFullName(sf) == fullName); |
| | | 41 | | |
| | 0 | 42 | | return shellFeature != null ? MapToElsaFeatureDescriptor(shellFeature) : null; |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | private static FeatureDescriptor MapToElsaFeatureDescriptor(ShellFeatureDescriptor shell) |
| | | 46 | | { |
| | | 47 | | // Extract namespace and name from shell feature |
| | 0 | 48 | | var type = shell.StartupType!; |
| | 0 | 49 | | var ns = type.Namespace ?? "Unknown"; |
| | 0 | 50 | | var name = shell.Id; |
| | | 51 | | |
| | | 52 | | // Try to get display name and description from metadata |
| | | 53 | | // These can be set via ShellFeatureAttribute properties |
| | 0 | 54 | | var displayName = shell.Metadata.TryGetValue("DisplayName", out var dn) |
| | 0 | 55 | | ? dn.ToString() ?? name |
| | 0 | 56 | | : name; |
| | | 57 | | |
| | 0 | 58 | | var description = shell.Metadata.TryGetValue("Description", out var desc) |
| | 0 | 59 | | ? desc.ToString() ?? string.Empty |
| | 0 | 60 | | : string.Empty; |
| | | 61 | | |
| | 0 | 62 | | return new FeatureDescriptor(name, ns, displayName, description); |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | private static string MapToFullName(ShellFeatureDescriptor shell) |
| | | 66 | | { |
| | 0 | 67 | | var ns = shell.StartupType?.Namespace ?? "Unknown"; |
| | 0 | 68 | | return $"{ns}.{shell.Id}"; |
| | | 69 | | } |
| | | 70 | | } |