| | | 1 | | using System.ComponentModel; |
| | | 2 | | using System.Reflection; |
| | | 3 | | using Elsa.Extensions; |
| | | 4 | | using Elsa.Features.Attributes; |
| | | 5 | | using Elsa.Features.Contracts; |
| | | 6 | | using Elsa.Features.Models; |
| | | 7 | | using Elsa.Features.Services; |
| | | 8 | | using Microsoft.Extensions.DependencyInjection; |
| | | 9 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 10 | | using Microsoft.Extensions.Hosting; |
| | | 11 | | |
| | | 12 | | namespace Elsa.Features.Implementations; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public class Module : IModule |
| | | 16 | | { |
| | 9 | 17 | | private sealed record HostedServiceDescriptor(int Order, Type Type); |
| | | 18 | | |
| | 1 | 19 | | private Dictionary<Type, IFeature> _features = new(); |
| | 1 | 20 | | private readonly HashSet<IFeature> _configuredFeatures = new(); |
| | 1 | 21 | | private readonly List<HostedServiceDescriptor> _hostedServiceDescriptors = new(); |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Constructor. |
| | | 25 | | /// </summary> |
| | 1 | 26 | | public Module(IServiceCollection services) |
| | | 27 | | { |
| | 1 | 28 | | Services = services; |
| | 1 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | 167 | 32 | | public IServiceCollection Services { get; } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc /> |
| | 7 | 35 | | public IDictionary<object, object> Properties { get; } = new Dictionary<object, object>(); |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | | 38 | | public bool HasFeature<T>() where T : class, IFeature |
| | | 39 | | { |
| | 0 | 40 | | return HasFeature(typeof(T)); |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <inheritdoc /> |
| | | 44 | | public bool HasFeature(Type featureType) |
| | | 45 | | { |
| | 0 | 46 | | return _features.ContainsKey(featureType); |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | public T Configure<T>(Action<T>? configure = null) where T : class, IFeature |
| | | 51 | | { |
| | 89 | 52 | | return Configure(module => (T)Activator.CreateInstance(typeof(T), module)!, configure); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <inheritdoc /> |
| | | 56 | | public T Configure<T>(Func<IModule, T> factory, Action<T>? configure = null) where T : class, IFeature |
| | | 57 | | { |
| | 64 | 58 | | if (!_features.TryGetValue(typeof(T), out var feature)) |
| | | 59 | | { |
| | 25 | 60 | | feature = factory(this); |
| | 25 | 61 | | _features[typeof(T)] = feature; |
| | | 62 | | } |
| | | 63 | | |
| | 64 | 64 | | configure?.Invoke((T)feature); |
| | | 65 | | |
| | 64 | 66 | | if (!_isApplying) |
| | 1 | 67 | | return (T)feature; |
| | | 68 | | |
| | 63 | 69 | | var dependencies = GetDependencyTypes(feature.GetType()).ToHashSet(); |
| | 768 | 70 | | foreach (var dependency in dependencies.Select(GetOrCreateFeature)) |
| | 321 | 71 | | ConfigureFeature(dependency); |
| | | 72 | | |
| | 63 | 73 | | ConfigureFeature(feature); |
| | 63 | 74 | | return (T)feature; |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <inheritdoc /> |
| | | 78 | | public IModule ConfigureHostedService<T>(int priority = 0) where T : class, IHostedService |
| | | 79 | | { |
| | 3 | 80 | | return ConfigureHostedService(typeof(T), priority); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <inheritdoc /> |
| | | 84 | | public IModule ConfigureHostedService(Type hostedServiceType, int priority = 0) |
| | | 85 | | { |
| | 3 | 86 | | _hostedServiceDescriptors.Add(new(priority, hostedServiceType)); |
| | 3 | 87 | | return this; |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | private bool _isApplying; |
| | | 91 | | |
| | | 92 | | /// <inheritdoc /> |
| | | 93 | | public void Apply() |
| | | 94 | | { |
| | 1 | 95 | | _isApplying = true; |
| | 1 | 96 | | var featureTypes = GetFeatureTypes(); |
| | 35 | 97 | | _features = featureTypes.ToDictionary(featureType => featureType, featureType => _features.TryGetValue(featureTy |
| | | 98 | | |
| | | 99 | | // Iterate over a copy of the features to avoid concurrent modification exceptions. |
| | 36 | 100 | | foreach (var feature in _features.Values.ToList()) |
| | | 101 | | { |
| | | 102 | | // This will cause additional features to be added to _features. |
| | 17 | 103 | | ConfigureFeature(feature); |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | // Filter out features that depend on other features that are not installed. |
| | 89 | 107 | | _features = ExcludeFeaturesWithMissingDependencies(_features.Values).ToDictionary(x => x.GetType(), x => x); |
| | | 108 | | |
| | | 109 | | // Add hosted services in order of priority. |
| | 11 | 110 | | foreach (var hostedServiceDescriptor in _hostedServiceDescriptors.OrderBy(x => x.Order)) |
| | 3 | 111 | | Services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IHostedService), hostedServiceDescriptor.Type)) |
| | | 112 | | |
| | | 113 | | // Make sure to use the complete list of features when applying them. |
| | 90 | 114 | | foreach (var feature in _features.Values) |
| | 44 | 115 | | feature.Apply(); |
| | | 116 | | |
| | | 117 | | // Add a registry of enabled features to the service collection for client applications to reflect on what featu |
| | 1 | 118 | | var registry = new InstalledFeatureRegistry(); |
| | 90 | 119 | | foreach (var feature in _features.Values) |
| | | 120 | | { |
| | 44 | 121 | | var type = feature.GetType(); |
| | 44 | 122 | | var name = type.Name.Replace("Feature", string.Empty); |
| | 44 | 123 | | var ns = "Elsa"; |
| | 44 | 124 | | var displayName = type.GetCustomAttribute<DisplayNameAttribute>()?.DisplayName ?? name; |
| | 44 | 125 | | var description = type.GetCustomAttribute<DescriptionAttribute>()?.Description; |
| | 44 | 126 | | registry.Add(new(name, ns, displayName, description)); |
| | | 127 | | } |
| | | 128 | | |
| | 1 | 129 | | Services.AddSingleton<IInstalledFeatureRegistry>(registry); |
| | 1 | 130 | | } |
| | | 131 | | |
| | | 132 | | private IEnumerable<IFeature> ExcludeFeaturesWithMissingDependencies(IEnumerable<IFeature> features) |
| | | 133 | | { |
| | 1 | 134 | | return |
| | 1 | 135 | | from feature in features |
| | 44 | 136 | | let featureType = feature.GetType() |
| | 44 | 137 | | let dependencyOfAttributes = featureType.GetCustomAttributes<DependencyOfAttribute>(true).ToList() |
| | 46 | 138 | | let missingDependencies = dependencyOfAttributes.Where(x => !_features.ContainsKey(x.Type)).ToList() |
| | 44 | 139 | | where missingDependencies.Count == 0 |
| | 45 | 140 | | select feature; |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | private void ConfigureFeature(IFeature feature) |
| | | 144 | | { |
| | 401 | 145 | | if (_configuredFeatures.Contains(feature)) |
| | 357 | 146 | | return; |
| | | 147 | | |
| | 44 | 148 | | feature.Configure(); |
| | 44 | 149 | | feature.ConfigureHostedServices(); |
| | 44 | 150 | | _features[feature.GetType()] = feature; |
| | 44 | 151 | | _configuredFeatures.Add(feature); |
| | 44 | 152 | | } |
| | | 153 | | |
| | | 154 | | private IFeature GetOrCreateFeature(Type featureType) |
| | | 155 | | { |
| | 321 | 156 | | return _features.TryGetValue(featureType, out var existingFeature) ? existingFeature : (IFeature)Activator.Creat |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | private HashSet<Type> GetFeatureTypes() |
| | | 160 | | { |
| | 1 | 161 | | var featureTypes = _features.Keys.ToHashSet(); |
| | 1 | 162 | | var featureTypesWithDependencies = featureTypes.Concat(featureTypes.SelectMany(GetDependencyTypes)).ToHashSet(); |
| | 39 | 163 | | return featureTypesWithDependencies.TSort(x => x.GetCustomAttributes<DependsOnAttribute>(true).Select(dependsOn |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | // Recursively get dependency types. |
| | | 167 | | private IEnumerable<Type> GetDependencyTypes(Type type) |
| | | 168 | | { |
| | 920 | 169 | | var dependencies = type.GetCustomAttributes<DependsOnAttribute>(true).Select(dependsOn => dependsOn.Type).ToList |
| | 492 | 170 | | return dependencies.Concat(dependencies.SelectMany(GetDependencyTypes)); |
| | | 171 | | } |
| | | 172 | | } |