Skip to content

Commit

Permalink
💾 Feat: Try dotnet source code generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Dynesshely committed Jun 21, 2024
1 parent 7e9c2fd commit 67feebe
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 1 deletion.
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 Generators/KitX.Sdk.Generators.CSharp/PluginFunctionsGenerator.cs
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)
);
}
}

0 comments on commit 67feebe

Please sign in to comment.