-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
💾 Feat: Try dotnet source code generator
- Loading branch information
1 parent
7e9c2fd
commit 67feebe
Showing
3 changed files
with
138 additions
and
1 deletion.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
Generators/KitX.Sdk.Generators.CSharp/KitX.Sdk.Generators.CSharp.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<LangVersion>preview</LangVersion> | ||
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.9.2" PrivateAssets="all" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" GeneratePathProperty="true" PrivateAssets="all" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\KitX Standard\KitX Contracts\KitX.Contract.CSharp\KitX.Contract.CSharp.csproj" PrivateAssets="all" OutputItemType="Analyzer" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup> | ||
<GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn> | ||
</PropertyGroup> | ||
|
||
<Target Name="GetDependencyTargetPaths"> | ||
<ItemGroup> | ||
<TargetPathWithTargetPlatformMoniker Include="$(PKGNewtonsoft_Json)\lib\netstandard2.0\Newtonsoft.Json.dll" IncludeRuntimeDependency="false" /> | ||
<!--<TargetPathWithTargetPlatformMoniker Include="$(PKGKitX.Contract.CSharp)\lib\netstandard2.0\KitX.Contract.CSharp.dll" IncludeRuntimeDependency="false" />--> | ||
</ItemGroup> | ||
</Target> | ||
|
||
</Project> |
104 changes: 104 additions & 0 deletions
104
Generators/KitX.Sdk.Generators.CSharp/PluginFunctionsGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
using KitX.Contract.CSharp.Attributes; | ||
using KitX.Shared.CSharp.Plugin; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Text; | ||
using Newtonsoft.Json; | ||
|
||
namespace KitX.Sdk.Generators.CSharp; | ||
|
||
[Generator] | ||
public class PluginFunctionsGenerator : ISourceGenerator | ||
{ | ||
public void Initialize(GeneratorInitializationContext context) | ||
{ | ||
|
||
} | ||
|
||
//public Dictionary<string, string> GetTranslations(string field, IEnumerable<TranslationAttribute> translations) | ||
//{ | ||
// var result = new Dictionary<string, string>(); | ||
|
||
// foreach (var item in translations.Where(t => t.Field.Equals(field))) | ||
// result.Add(item.Language, item.Value); | ||
|
||
// return result; | ||
//} | ||
|
||
public void Execute(GeneratorExecutionContext context) | ||
{ | ||
var entryAttrType = typeof(EntryClassAttribute); | ||
var funcAttrType = typeof(FunctionAttribute); | ||
|
||
//var functions = context.Compilation.Assembly.GetAttributes() | ||
// .Where(x => x.AttributeClass?.GetType().Equals(entryAttrType) ?? false) | ||
// .SelectMany(x => x.GetType().GetMethods()) | ||
// .Where(f => f.CustomAttributes.Any( | ||
// x => x.AttributeType.Equals(funcAttrType) | ||
// )) | ||
// ; | ||
|
||
//var result = functions.Select(func => | ||
//{ | ||
// var parameters = func.GetParameters().Select(param => | ||
// { | ||
// var paramAttr = param.GetCustomAttribute<ParameterAttribute>()!; | ||
|
||
// return new Parameter | ||
// { | ||
// Name = paramAttr.Name, | ||
// DisplayNames = GetTranslations("DisplayName", param.GetCustomAttributes<TranslationAttribute>()), | ||
// Type = param.ParameterType.Name, | ||
// IsOptional = param.IsOptional, | ||
// }; | ||
// }); | ||
|
||
// var funcAttr = func.GetCustomAttribute<FunctionAttribute>()!; | ||
|
||
// return new Function | ||
// { | ||
// Name = funcAttr.Name, | ||
// DisplayNames = GetTranslations("DisplayName", func.GetCustomAttributes<TranslationAttribute>()), | ||
// ReturnValueType = func.ReturnType.Name, | ||
// Parameters = parameters.ToList(), | ||
// }; | ||
//}); | ||
|
||
//var sourceText = JsonConvert.SerializeObject(result); | ||
|
||
//context.AddSource("PluginFunctions.json", SourceText.From(sourceText, Encoding.UTF8)); | ||
|
||
context.AddSource("Test.json", SourceText.From("", Encoding.UTF8)); | ||
|
||
context.ReportDiagnostic( | ||
Diagnostic.Create( | ||
new DiagnosticDescriptor( | ||
"KSDK0001", | ||
"Plugin Info Generated", | ||
"PluginFunctions.json generated successfully.", | ||
"Plugin", | ||
DiagnosticSeverity.Info, | ||
true | ||
), | ||
Location.None) | ||
); | ||
|
||
context.ReportDiagnostic( | ||
Diagnostic.Create( | ||
new DiagnosticDescriptor( | ||
"KSDK0000", | ||
"Your location", | ||
$"You are at {Path.GetFullPath(".")}", | ||
"Plugin", | ||
DiagnosticSeverity.Warning, | ||
true | ||
), | ||
Location.None) | ||
); | ||
} | ||
} |
Submodule KitX Plugins
updated
4 files
+1 −1 | TestPlugin.WPF.Core/Controller.cs | |
+27 −0 | TestPlugin.WPF.Core/Functions.cs | |
+0 −53 | TestPlugin.WPF.Core/PluginStruct.json | |
+1 −3 | TestPlugin.WPF.Core/TestPlugin.WPF.Core.csproj |