|
1 | 1 | using FlowSynx.PluginCore; |
| 2 | +using System.Reflection; |
2 | 3 | using System.Text.Json; |
3 | 4 |
|
4 | 5 | namespace FlowPack; |
@@ -52,13 +53,128 @@ public static class PluginReflector |
52 | 53 | MinimumFlowSynxVersion = plugin.Metadata.MinimumFlowSynxVersion.ToString(), |
53 | 54 | TargetFlowSynxVersion = plugin.Metadata.TargetFlowSynxVersion == null |
54 | 55 | ? "" |
55 | | - : plugin.Metadata.TargetFlowSynxVersion.ToString() |
| 56 | + : plugin.Metadata.TargetFlowSynxVersion.ToString(), |
| 57 | + Specifications = ExtractSpecificationMetadata(plugin), |
| 58 | + Operations = ExtractOperations(plugin) |
56 | 59 | }; |
57 | 60 |
|
| 61 | + private static List<SpecificationMetadata> ExtractSpecificationMetadata(IPlugin plugin) |
| 62 | + { |
| 63 | + if (plugin == null) |
| 64 | + throw new ArgumentNullException(nameof(plugin)); |
| 65 | + |
| 66 | + var result = new List<SpecificationMetadata>(); |
| 67 | + |
| 68 | + // Try to find the concrete Specifications type |
| 69 | + var specProperty = plugin.GetType().GetProperty("Specifications", BindingFlags.Public | BindingFlags.Instance); |
| 70 | + if (specProperty == null) |
| 71 | + return result; |
| 72 | + |
| 73 | + Type specType; |
| 74 | + |
| 75 | + // If the property is already instantiated, use its type |
| 76 | + var specInstance = specProperty.GetValue(plugin); |
| 77 | + if (specInstance != null) |
| 78 | + { |
| 79 | + specType = specInstance.GetType(); |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + var declaredType = specProperty.PropertyType; |
| 84 | + specType = plugin.GetType().Assembly |
| 85 | + .GetTypes() |
| 86 | + .FirstOrDefault(t => |
| 87 | + declaredType.IsAssignableFrom(t) && |
| 88 | + !t.IsAbstract && |
| 89 | + t.IsClass |
| 90 | + ) ?? throw new InvalidOperationException("No concrete Specifications class found."); |
| 91 | + |
| 92 | + // Create an instance to get default values |
| 93 | + specInstance = Activator.CreateInstance(specType); |
| 94 | + } |
| 95 | + |
| 96 | + // Iterate over properties of the concrete type |
| 97 | + foreach (var prop in specType.GetProperties(BindingFlags.Public | BindingFlags.Instance)) |
| 98 | + { |
| 99 | + var attr = prop.GetCustomAttribute<SpecificationMetadataAttribute>(); |
| 100 | + if (attr == null) |
| 101 | + continue; |
| 102 | + |
| 103 | + var defaultValue = specInstance != null ? prop.GetValue(specInstance) : null; |
| 104 | + |
| 105 | + result.Add(new SpecificationMetadata |
| 106 | + { |
| 107 | + Name = prop.Name, |
| 108 | + Description = attr.Description, |
| 109 | + Type = TypeExtensions.GetCanonicalAiTypeName(prop.PropertyType), |
| 110 | + DefaultValue = defaultValue?.ToString(), |
| 111 | + IsRequired = attr.IsRequired |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + return result; |
| 116 | + } |
| 117 | + |
| 118 | + private static List<PluginOperationMetadata> ExtractOperations(IPlugin plugin) |
| 119 | + { |
| 120 | + var operations = new List<PluginOperationMetadata>(); |
| 121 | + var asm = plugin.GetType().Assembly; |
| 122 | + |
| 123 | + foreach (var type in asm.GetTypes()) |
| 124 | + { |
| 125 | + if (type.IsAbstract || type.IsInterface) |
| 126 | + continue; |
| 127 | + |
| 128 | + var opInterface = type.GetInterfaces() |
| 129 | + .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition().Name == "IPluginOperation`2"); |
| 130 | + |
| 131 | + if (opInterface == null) |
| 132 | + continue; |
| 133 | + |
| 134 | + object? opInstance = null; |
| 135 | + try { opInstance = Activator.CreateInstance(type); } catch { continue; } |
| 136 | + |
| 137 | + var nameProp = type.GetProperty("Name", BindingFlags.Instance | BindingFlags.Public); |
| 138 | + var descProp = type.GetProperty("Description", BindingFlags.Instance | BindingFlags.Public); |
| 139 | + |
| 140 | + var opMeta = new PluginOperationMetadata |
| 141 | + { |
| 142 | + Name = nameProp?.GetValue(opInstance)?.ToString() ?? type.Name, |
| 143 | + Description = descProp?.GetValue(opInstance)?.ToString() |
| 144 | + }; |
| 145 | + |
| 146 | + var paramType = opInterface.GetGenericArguments()[0]; |
| 147 | + List<PluginOperationParameterMetadata> paramMetas = new(); |
| 148 | + |
| 149 | + foreach (var prop in paramType.GetProperties(BindingFlags.Instance | BindingFlags.Public)) |
| 150 | + { |
| 151 | + var attr = prop.GetCustomAttribute<OperationParameterMetadataAttribute>(); |
| 152 | + if (attr == null) continue; |
| 153 | + |
| 154 | + object? paramInstance = null; |
| 155 | + try { paramInstance = Activator.CreateInstance(paramType); } catch { } |
| 156 | + |
| 157 | + paramMetas.Add(new PluginOperationParameterMetadata |
| 158 | + { |
| 159 | + Name = prop.Name, |
| 160 | + Description = attr.Description, |
| 161 | + Type = TypeExtensions.GetCanonicalAiTypeName(prop.PropertyType), |
| 162 | + DefaultValue = paramInstance != null ? prop.GetValue(paramInstance)?.ToString() : null, |
| 163 | + IsRequired = attr.IsRequired |
| 164 | + }); |
| 165 | + } |
| 166 | + |
| 167 | + opMeta.Parameters = paramMetas; |
| 168 | + operations.Add(opMeta); |
| 169 | + } |
| 170 | + |
| 171 | + return operations; |
| 172 | + } |
| 173 | + |
58 | 174 | public static string SaveMetadataToFile(PluginMetadata metadata, string outputDirectory) |
59 | 175 | { |
60 | 176 | var json = JsonSerializer.Serialize(metadata, new JsonSerializerOptions { WriteIndented = true }); |
61 | | - var path = Path.Combine(outputDirectory, "manifest.json"); |
| 177 | + var path = Path.Combine(outputDirectory, "metadata.json"); |
62 | 178 | File.WriteAllText(path, json); |
63 | 179 | return path; |
64 | 180 | } |
|
0 commit comments