|
| 1 | +using System; |
| 2 | +using System.Reflection; |
| 3 | + |
| 4 | +namespace Reqnroll.Bindings; |
| 5 | +#nullable enable |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Provides extensions for working with step definition assemblies. |
| 9 | +/// </summary> |
| 10 | +public static class StepDefinitionAssemblyExtensions |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Tries to get the step registry for an assembly. |
| 14 | + /// </summary> |
| 15 | + /// <param name="assembly">The assembly to examine for a step registry.</param> |
| 16 | + /// <param name="stepRegistry">After the method returns, contains the step registry for the assembly, |
| 17 | + /// if one was found; otherwise <c>null</c>.</param> |
| 18 | + /// <param name="error">After the method returns, if the registry could not be found, contains an exception |
| 19 | + /// indicating the condition that prevented the registry being located; otherwise <c>null</c>.</param> |
| 20 | + /// <returns><c>true</c> if a registry was found; otherwise <c>false</c>.</returns> |
| 21 | + public static bool TryGetStepDefinitionRegistry( |
| 22 | + this Assembly assembly, |
| 23 | + out IStepDefinitionDescriptorsProvider? stepRegistry, |
| 24 | + out Exception? error) |
| 25 | + { |
| 26 | + var registryAttribute = assembly.GetCustomAttribute<StepDefinitionRegistryAttribute>(); |
| 27 | + stepRegistry = null; |
| 28 | + |
| 29 | + if (registryAttribute == null) |
| 30 | + { |
| 31 | + error = new InvalidOperationException( |
| 32 | + $"Assembly '{assembly.FullName}' does not have a '{nameof(StepDefinitionRegistryAttribute)}' defined."); |
| 33 | + return false; |
| 34 | + } |
| 35 | + |
| 36 | + if (registryAttribute.RegistryType == null) |
| 37 | + { |
| 38 | + error = new InvalidOperationException( |
| 39 | + $"Assembly '{assembly.FullName}' has a '{nameof(StepDefinitionRegistryAttribute)}' " + |
| 40 | + "defined but no registry type is specified."); |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + if (typeof(IStepDefinitionDescriptorsProvider).IsAssignableFrom(registryAttribute.RegistryType)) |
| 45 | + { |
| 46 | + error = new InvalidOperationException( |
| 47 | + $"Assembly '{assembly.FullName}' speciifes its registry type is " + |
| 48 | + $"'{registryAttribute.RegistryType.FullName}' however the type does not implement " + |
| 49 | + $"'{nameof(IStepDefinitionDescriptorsProvider)}'."); |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + var instanceProperty = registryAttribute.RegistryType.GetProperty( |
| 54 | + "Instance", |
| 55 | + BindingFlags.Public | BindingFlags.Static | BindingFlags.GetProperty); |
| 56 | + |
| 57 | + if (instanceProperty == null || instanceProperty.PropertyType != registryAttribute.RegistryType) |
| 58 | + { |
| 59 | + error = new InvalidOperationException( |
| 60 | + $"Assembly '{assembly.FullName}' speciifes its registry type is " + |
| 61 | + $"'{registryAttribute.RegistryType.FullName}' however the type does not expose the required " + |
| 62 | + $"'Instance' property. The property must be declared public and static and must return the single " + |
| 63 | + $"instance of the registry type."); |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + stepRegistry = (IStepDefinitionDescriptorsProvider?)instanceProperty.GetGetMethod().Invoke(null, null); |
| 68 | + |
| 69 | + if (stepRegistry == null) |
| 70 | + { |
| 71 | + error = new InvalidOperationException( |
| 72 | + $"Assembly '{assembly.FullName}' speciifes its registry type is " + |
| 73 | + $"'{registryAttribute.RegistryType.FullName}' however the 'Instance' property did not return " + |
| 74 | + "an instance."); |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + error = null; |
| 79 | + return true; |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Gets the step registry for an assembly. |
| 84 | + /// </summary> |
| 85 | + /// <param name="assembly">The assembly to examine for a step registry.</param> |
| 86 | + /// <returns>The step registry for the assembly.</returns> |
| 87 | + /// <exception cref="InvalidOperationException"> |
| 88 | + /// <para>The assembly does have a step registry.</para> |
| 89 | + /// </exception> |
| 90 | + public static IStepDefinitionDescriptorsProvider GetStepRegistry(this Assembly assembly) |
| 91 | + { |
| 92 | + if (assembly.TryGetStepDefinitionRegistry(out var registry, out var error)) |
| 93 | + { |
| 94 | + return registry!; |
| 95 | + } |
| 96 | + |
| 97 | + throw error!; |
| 98 | + } |
| 99 | +} |
0 commit comments