Data Type

This commit is contained in:
h z
2024-07-11 11:21:29 +01:00
commit 00bf1b438e
5 changed files with 108 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/

View File

@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
namespace Nocturnis.Generators.DataTypes;
[Generator]
public class DataTypeGenerator : ISourceGenerator
{
public void Initialize(GeneratorInitializationContext context)
{
}
public void Execute(GeneratorExecutionContext context)
{
var classesTxt = context.AdditionalFiles
.FirstOrDefault(f => Path.GetFileName(f.Path) == "BaseTypes");
if (classesTxt != null)
{
IEnumerable<string>? types =
classesTxt.GetText(context.CancellationToken)?.ToString().Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
if (types == null)
return;
StringBuilder sb = new();
sb.AppendLine("using Godot;");
sb.AppendLine("using Nocturnis.DataStructures.DataTypes;");
sb.AppendLine("namespace Nocturnis.GlobalManagement.Constants;");
sb.AppendLine("public static partial class DataTypeConstant");
sb.AppendLine("{");
sb.AppendLine(" public static class BaseDataTypeNames");
sb.AppendLine(" {");
StringBuilder sxa = new();
foreach (string? type in types)
{
sb.AppendLine($" public static StringName {type} = \"{type}\";");
sxa.AppendLine($" public static DataType {type} = new DataType(BaseDataTypeNames.{type});");
sxa.AppendLine($" public static DataType {type}Array = new DataType({type})");
}
sb.AppendLine(" }");
sb.AppendLine(" public static class BaseDataTypes");
sb.AppendLine(" {");
sb.Append(sxa);
sb.AppendLine(" }");
sb.AppendLine("}");
context.AddSource("DataTypeConstant.g.cs", SourceText.From(sb.ToString(), Encoding.UTF8));
}
}
}

View File

@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<IsRoslynComponent>true</IsRoslynComponent>
<RootNamespace>Nocturnis.Generators</RootNamespace>
<PackageId>Nocturnis.Generators</PackageId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.0"/>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.3.0"/>
</ItemGroup>
</Project>

16
Nocturnis.Generators.sln Normal file
View File

@@ -0,0 +1,16 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nocturnis.Generators", "Nocturnis.Generators.csproj", "{8CEDE314-85A0-40D8-80D5-8927E21133B8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8CEDE314-85A0-40D8-80D5-8927E21133B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8CEDE314-85A0-40D8-80D5-8927E21133B8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8CEDE314-85A0-40D8-80D5-8927E21133B8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8CEDE314-85A0-40D8-80D5-8927E21133B8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,9 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"DebugRoslynSourceGenerator": {
"commandName": "DebugRoslynComponent",
"targetProject": "../Nocturnis/Nocturnis.csproj"
}
}
}