Browse Source

Starting with it

Piotr Czajkowski 4 months ago
parent
commit
a81275d5f9

+ 1 - 0
.gitignore

@@ -396,3 +396,4 @@ FodyWeavers.xsd
 
 # JetBrains Rider
 *.sln.iml
+CodeGenerators/.idea/*

+ 22 - 0
CodeGenerators/CodeGenerators.sln

@@ -0,0 +1,22 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeGenerators", "CodeGenerators\CodeGenerators.csproj", "{A9BA9DAD-3101-4840-AF42-F84DE417F5E7}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeGeneratorsTests", "CodeGeneratorsTests\CodeGeneratorsTests.csproj", "{251E5058-FEC4-4160-B33F-0E801C9E5523}"
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|Any CPU = Debug|Any CPU
+		Release|Any CPU = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{A9BA9DAD-3101-4840-AF42-F84DE417F5E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{A9BA9DAD-3101-4840-AF42-F84DE417F5E7}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{A9BA9DAD-3101-4840-AF42-F84DE417F5E7}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{A9BA9DAD-3101-4840-AF42-F84DE417F5E7}.Release|Any CPU.Build.0 = Release|Any CPU
+		{251E5058-FEC4-4160-B33F-0E801C9E5523}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{251E5058-FEC4-4160-B33F-0E801C9E5523}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{251E5058-FEC4-4160-B33F-0E801C9E5523}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{251E5058-FEC4-4160-B33F-0E801C9E5523}.Release|Any CPU.Build.0 = Release|Any CPU
+	EndGlobalSection
+EndGlobal

+ 15 - 0
CodeGenerators/CodeGenerators/AttributeElement.cs

@@ -0,0 +1,15 @@
+namespace CodeGenerators;
+
+public class AttributeElement : IGenerator
+{
+    private readonly string name;
+    private readonly string value;
+    
+    public AttributeElement(string name, string value)
+    {
+        this.name = name;
+        this.value = value;
+    }
+
+    public string Build(int indent) => $"{new string('\t', indent)}[{name}(\"{value}\")]";
+}

+ 48 - 0
CodeGenerators/CodeGenerators/ClassRecordGenerator.cs

@@ -0,0 +1,48 @@
+using System.Text;
+
+namespace CodeGenerators;
+
+public class ClassRecordGenerator : IGenerator
+{
+    public List<string> Usings { get; set; } = new List<string>();
+    public List<Property> Properties { get; set; } = new List<Property>();
+    private readonly string namespaceName;
+    private readonly string name;
+    private readonly bool isRecord;
+
+    public ClassRecordGenerator(string namespaceName, string name, bool isRecord = false)
+    {
+        this.namespaceName = namespaceName;
+        this.name = name;
+        this.isRecord = isRecord;
+    }
+
+    public string Build(int indent = 0)
+    {
+        var sb = new StringBuilder();
+        if (Usings.Any())
+        {
+            foreach (var usingItem in Usings)
+                sb.AppendLine($"using {usingItem};");
+
+            sb.AppendLine();
+        }
+
+        sb.AppendLine($"namespace {namespaceName};");
+        sb.AppendLine();
+
+        sb.AppendLine($"public {(isRecord ? "record" : "class")} {name}\n{{");
+        indent++;
+
+        if (Properties.Any())
+        {
+            foreach (var property in Properties)
+                sb.AppendLine(property.Build(indent));
+        }
+
+        indent--;
+        sb.AppendLine("}");
+
+        return sb.ToString();
+    }
+}

+ 9 - 0
CodeGenerators/CodeGenerators/CodeGenerators.csproj

@@ -0,0 +1,9 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+    <PropertyGroup>
+        <TargetFramework>net7.0</TargetFramework>
+        <ImplicitUsings>enable</ImplicitUsings>
+        <Nullable>enable</Nullable>
+    </PropertyGroup>
+
+</Project>

+ 6 - 0
CodeGenerators/CodeGenerators/IGenerator.cs

@@ -0,0 +1,6 @@
+namespace CodeGenerators;
+
+public interface IGenerator
+{
+    string Build(int indent);
+}

+ 30 - 0
CodeGenerators/CodeGenerators/Property.cs

@@ -0,0 +1,30 @@
+using System.Text;
+
+namespace CodeGenerators;
+
+public class Property : IGenerator
+{
+    private readonly string name;
+    private readonly string type;
+    public List<AttributeElement> Attributes { get; set; } = new List<AttributeElement>();
+
+    public Property(string name, string type)
+    {
+        this.name = name;
+        this.type = type;
+    }
+
+    public string Build(int indent)
+    {
+        var sb = new StringBuilder();
+        if (Attributes.Any())
+        {
+            foreach (var attribute in Attributes)
+                sb.AppendLine(attribute.Build(indent));
+        }
+        
+        sb.Append($"{(new string('\t', indent))}public {type} {name} {{ get; set; }}");
+
+        return sb.ToString();
+    } 
+}

+ 20 - 0
CodeGenerators/CodeGeneratorsTests/ClassRecordGeneratorTests.cs

@@ -0,0 +1,20 @@
+using CodeGenerators;
+
+namespace CodeGeneratorsTests;
+
+public class ClassRecordGeneratorTests
+{
+    [Fact]
+    public void BuildTest()
+    {
+        var test = new ClassRecordGenerator("TestNamespace", "Test");
+        test.Usings.Add("ExcelORM");
+        
+        var property = new Property("TestProperty", "string");
+        property.Attributes.Add(new AttributeElement("Column", "Test Property"));
+        test.Properties.Add(property);
+
+        var result = test.Build();
+        Assert.NotEmpty(result);
+    }
+}

+ 29 - 0
CodeGenerators/CodeGeneratorsTests/CodeGeneratorsTests.csproj

@@ -0,0 +1,29 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+    <PropertyGroup>
+        <TargetFramework>net7.0</TargetFramework>
+        <ImplicitUsings>enable</ImplicitUsings>
+        <Nullable>enable</Nullable>
+
+        <IsPackable>false</IsPackable>
+        <IsTestProject>true</IsTestProject>
+    </PropertyGroup>
+
+    <ItemGroup>
+        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0"/>
+        <PackageReference Include="xunit" Version="2.4.2"/>
+        <PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
+            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+            <PrivateAssets>all</PrivateAssets>
+        </PackageReference>
+        <PackageReference Include="coverlet.collector" Version="3.2.0">
+            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+            <PrivateAssets>all</PrivateAssets>
+        </PackageReference>
+    </ItemGroup>
+
+    <ItemGroup>
+      <ProjectReference Include="..\CodeGenerators\CodeGenerators.csproj" />
+    </ItemGroup>
+
+</Project>

+ 1 - 0
CodeGenerators/CodeGeneratorsTests/Usings.cs

@@ -0,0 +1 @@
+global using Xunit;