|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Reflection; |
| 6 | +using System.Text; |
| 7 | +using KitX.Contract.CSharp.Attributes; |
| 8 | +using KitX.Shared.CSharp.Plugin; |
| 9 | +using Microsoft.CodeAnalysis; |
| 10 | +using Microsoft.CodeAnalysis.Text; |
| 11 | +using Newtonsoft.Json; |
| 12 | + |
| 13 | +namespace KitX.Sdk.Generators.CSharp; |
| 14 | + |
| 15 | +[Generator] |
| 16 | +public class PluginFunctionsGenerator : ISourceGenerator |
| 17 | +{ |
| 18 | + public void Initialize(GeneratorInitializationContext context) |
| 19 | + { |
| 20 | + |
| 21 | + } |
| 22 | + |
| 23 | + //public Dictionary<string, string> GetTranslations(string field, IEnumerable<TranslationAttribute> translations) |
| 24 | + //{ |
| 25 | + // var result = new Dictionary<string, string>(); |
| 26 | + |
| 27 | + // foreach (var item in translations.Where(t => t.Field.Equals(field))) |
| 28 | + // result.Add(item.Language, item.Value); |
| 29 | + |
| 30 | + // return result; |
| 31 | + //} |
| 32 | + |
| 33 | + public void Execute(GeneratorExecutionContext context) |
| 34 | + { |
| 35 | + var entryAttrType = typeof(EntryClassAttribute); |
| 36 | + var funcAttrType = typeof(FunctionAttribute); |
| 37 | + |
| 38 | + //var functions = context.Compilation.Assembly.GetAttributes() |
| 39 | + // .Where(x => x.AttributeClass?.GetType().Equals(entryAttrType) ?? false) |
| 40 | + // .SelectMany(x => x.GetType().GetMethods()) |
| 41 | + // .Where(f => f.CustomAttributes.Any( |
| 42 | + // x => x.AttributeType.Equals(funcAttrType) |
| 43 | + // )) |
| 44 | + // ; |
| 45 | + |
| 46 | + //var result = functions.Select(func => |
| 47 | + //{ |
| 48 | + // var parameters = func.GetParameters().Select(param => |
| 49 | + // { |
| 50 | + // var paramAttr = param.GetCustomAttribute<ParameterAttribute>()!; |
| 51 | + |
| 52 | + // return new Parameter |
| 53 | + // { |
| 54 | + // Name = paramAttr.Name, |
| 55 | + // DisplayNames = GetTranslations("DisplayName", param.GetCustomAttributes<TranslationAttribute>()), |
| 56 | + // Type = param.ParameterType.Name, |
| 57 | + // IsOptional = param.IsOptional, |
| 58 | + // }; |
| 59 | + // }); |
| 60 | + |
| 61 | + // var funcAttr = func.GetCustomAttribute<FunctionAttribute>()!; |
| 62 | + |
| 63 | + // return new Function |
| 64 | + // { |
| 65 | + // Name = funcAttr.Name, |
| 66 | + // DisplayNames = GetTranslations("DisplayName", func.GetCustomAttributes<TranslationAttribute>()), |
| 67 | + // ReturnValueType = func.ReturnType.Name, |
| 68 | + // Parameters = parameters.ToList(), |
| 69 | + // }; |
| 70 | + //}); |
| 71 | + |
| 72 | + //var sourceText = JsonConvert.SerializeObject(result); |
| 73 | + |
| 74 | + //context.AddSource("PluginFunctions.json", SourceText.From(sourceText, Encoding.UTF8)); |
| 75 | + |
| 76 | + context.AddSource("Test.json", SourceText.From("", Encoding.UTF8)); |
| 77 | + |
| 78 | + context.ReportDiagnostic( |
| 79 | + Diagnostic.Create( |
| 80 | + new DiagnosticDescriptor( |
| 81 | + "KSDK0001", |
| 82 | + "Plugin Info Generated", |
| 83 | + "PluginFunctions.json generated successfully.", |
| 84 | + "Plugin", |
| 85 | + DiagnosticSeverity.Info, |
| 86 | + true |
| 87 | + ), |
| 88 | + Location.None) |
| 89 | + ); |
| 90 | + |
| 91 | + context.ReportDiagnostic( |
| 92 | + Diagnostic.Create( |
| 93 | + new DiagnosticDescriptor( |
| 94 | + "KSDK0000", |
| 95 | + "Your location", |
| 96 | + $"You are at {Path.GetFullPath(".")}", |
| 97 | + "Plugin", |
| 98 | + DiagnosticSeverity.Warning, |
| 99 | + true |
| 100 | + ), |
| 101 | + Location.None) |
| 102 | + ); |
| 103 | + } |
| 104 | +} |
0 commit comments