Skip to content

Commit 67feebe

Browse files
committed
💾 Feat: Try dotnet source code generator
1 parent 7e9c2fd commit 67feebe

File tree

3 files changed

+138
-1
lines changed

3 files changed

+138
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<LangVersion>preview</LangVersion>
6+
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.9.2" PrivateAssets="all" />
11+
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" GeneratePathProperty="true" PrivateAssets="all" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<ProjectReference Include="..\..\..\KitX Standard\KitX Contracts\KitX.Contract.CSharp\KitX.Contract.CSharp.csproj" PrivateAssets="all" OutputItemType="Analyzer" />
20+
</ItemGroup>
21+
22+
<PropertyGroup>
23+
<GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
24+
</PropertyGroup>
25+
26+
<Target Name="GetDependencyTargetPaths">
27+
<ItemGroup>
28+
<TargetPathWithTargetPlatformMoniker Include="$(PKGNewtonsoft_Json)\lib\netstandard2.0\Newtonsoft.Json.dll" IncludeRuntimeDependency="false" />
29+
<!--<TargetPathWithTargetPlatformMoniker Include="$(PKGKitX.Contract.CSharp)\lib\netstandard2.0\KitX.Contract.CSharp.dll" IncludeRuntimeDependency="false" />-->
30+
</ItemGroup>
31+
</Target>
32+
33+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

Comments
 (0)