Skip to content

Commit ca0c07a

Browse files
author
Carlos José jiménez bermúdez
committed
More move and restructuring.
1 parent a46ef42 commit ca0c07a

17 files changed

+386
-24
lines changed
Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
5-
</PropertyGroup>
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
</PropertyGroup>
67

7-
<ItemGroup>
8-
<Folder Include="Dapper\" />
9-
<Folder Include="RepoDB\" />
10-
</ItemGroup>
8+
<ItemGroup>
9+
<Folder Include="Dapper\" />
10+
</ItemGroup>
1111

12-
<ItemGroup>
13-
<ProjectReference Include="..\DbReflector.Utils\DbReflector.Utils.csproj" />
14-
</ItemGroup>
12+
<ItemGroup>
13+
<PackageReference Include="Humanizer.Core" Version="2.11.10" />
14+
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.11.0" />
15+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.11.0" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<ProjectReference Include="..\DbReflector.Utils\DbReflector.Common.csproj" />
20+
</ItemGroup>
1521

1622
</Project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace DbReflector.CodeGeneration.Exceptions
8+
{
9+
public class CodeGenerationException : Exception
10+
{
11+
public CodeGenerationException(string message) : base(message)
12+
{
13+
14+
}
15+
16+
public CodeGenerationException(string message, Exception innerException) : base(message, innerException)
17+
{
18+
19+
}
20+
}
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using DbReflector.Common;
7+
using DbReflector.CodeGeneration.Models;
8+
9+
namespace DbReflector.CodeGeneration.Interfaces
10+
{
11+
public interface IGenerator
12+
{
13+
public void Generate(CommandLineConfiguration cliConfig, VSProjectMetadata projectMetadata, Database database);
14+
}
15+
}

DbReflector.Databases/EntityDataModel/Column.cs renamed to DbReflector.CodeGeneration/Models/Column.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Humanizer;
2-
using DbReflector.Utils;
2+
using DbReflector.Common;
33

4-
namespace DbReflector.Databases.EntityDataModel
4+
namespace DbReflector.CodeGeneration.Models
55
{
66
public class Column
77
{

DbReflector.Databases/EntityDataModel/ColumnType.cs renamed to DbReflector.CodeGeneration/Models/ColumnType.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
using System.Linq;
44
using System.Text;
55
using System.Threading.Tasks;
6-
using DbReflector.Utils;
6+
using DbReflector.Common;
77

8-
namespace DbReflector.Databases.EntityDataModel
8+
namespace DbReflector.CodeGeneration.Models
99
{
1010
public class ColumnType
1111
{

DbReflector.Databases/EntityDataModel/Database.cs renamed to DbReflector.CodeGeneration/Models/Database.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
using System.Linq;
44
using System.Text;
55
using System.Threading.Tasks;
6-
using DbReflector.Utils;
6+
using DbReflector.Common;
77

8-
namespace DbReflector.Databases.EntityDataModel
8+
namespace DbReflector.CodeGeneration.Models
99
{
1010
public class Database
1111
{

DbReflector.Databases/EntityDataModel/Table.cs renamed to DbReflector.CodeGeneration/Models/Table.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using Humanizer;
22
using System.Collections.Generic;
3-
using DbReflector.Utils;
3+
using DbReflector.Common;
44

55

6-
namespace DbReflector.Databases.EntityDataModel
6+
namespace DbReflector.CodeGeneration.Models
77
{
88
public class Table
99
{
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
using DbReflector.Common;
2+
using DbReflector.CodeGeneration.Interfaces;
3+
using DbReflector.CodeGeneration.Models;
4+
using Microsoft.CodeAnalysis;
5+
using Microsoft.CodeAnalysis.CSharp;
6+
using DbReflector.CodeGeneration.Exceptions;
7+
using System.IO;
8+
using System.Linq;
9+
using System;
10+
11+
namespace DbReflector.CodeGeneration.RepoDB
12+
{
13+
public class EntityGenerator : IGenerator
14+
{
15+
public void Generate(CommandLineConfiguration cliConfig, VSProjectMetadata projectMetadata, Database database)
16+
{
17+
var entitiesDirectory = new DirectoryInfo($"{projectMetadata.BasePath}/{cliConfig.EntitiesFolder}");
18+
var shouldGenerate = false;
19+
20+
if(entitiesDirectory.Exists)
21+
{
22+
//Entities have already been created or files exist in the target folder.
23+
if(DirectoryHasFiles(entitiesDirectory.FullName))
24+
{
25+
if(!cliConfig.ForceRecreate)
26+
{
27+
throw new CodeGenerationException("To be able to run the generation routine when you have previously generated code, you must set ForceRecreate to true or your target directories must be empty.");
28+
}
29+
else
30+
{
31+
shouldGenerate = true;
32+
//Delete existing files.
33+
foreach (var file in entitiesDirectory.EnumerateFiles())
34+
{
35+
file.Delete();
36+
}
37+
}
38+
}
39+
else
40+
shouldGenerate = true;
41+
}
42+
else
43+
{
44+
Directory.CreateDirectory($"{projectMetadata.BasePath}/{cliConfig.EntitiesFolder}");
45+
shouldGenerate = true;
46+
}
47+
48+
if(shouldGenerate)
49+
{
50+
foreach (var tableMeta in database.Tables)
51+
{
52+
var entityCode = GenerateEntity(cliConfig.EntitiesFolder, tableMeta, projectMetadata);
53+
WriteEntityFileToDisk($"{projectMetadata.BasePath}/{cliConfig.EntitiesFolder}/{tableMeta.FormattedTableName}.cs", entityCode);
54+
}
55+
}
56+
}
57+
58+
private string GenerateEntity(string entitiesFolder, Table tableMetadata, VSProjectMetadata projectMetadata)
59+
{
60+
var compilationUnit = SyntaxFactory.CompilationUnit();
61+
62+
var usings = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("System"));
63+
64+
compilationUnit = compilationUnit.AddUsings(usings);
65+
66+
var entityNamespace = SyntaxFactory.NamespaceDeclaration(SyntaxFactory.ParseName($"{projectMetadata.DefaultNamespace}.{entitiesFolder}")).NormalizeWhitespace();
67+
68+
var entityClass = SyntaxFactory.ClassDeclaration(tableMetadata.FormattedTableName)
69+
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword));
70+
71+
foreach (var columnMeta in tableMetadata.Columns)
72+
{
73+
var columnType = columnMeta.Type.CSharpTypeString;
74+
var columnName = columnMeta.FormattedColumnName;
75+
var databaseType = columnMeta.Type.DatabaseType;
76+
77+
var property = SyntaxFactory.ParseMemberDeclaration($"public {columnType} {columnName} {{ get; set; }}");
78+
79+
if(property != null && !string.IsNullOrWhiteSpace(columnType)) {
80+
entityClass = entityClass.AddMembers(property);
81+
}
82+
else
83+
{
84+
Console.WriteLine("Skipping Column Generation");
85+
Console.WriteLine($"Table Column belongs to: {tableMetadata.TableName}. Column: {columnName}. Database type {databaseType} not mapped.");
86+
}
87+
}
88+
89+
entityNamespace = entityNamespace.AddMembers(entityClass);
90+
91+
compilationUnit = compilationUnit.AddMembers(entityNamespace);
92+
93+
var code = compilationUnit
94+
.NormalizeWhitespace()
95+
.ToFullString();
96+
97+
return code;
98+
}
99+
100+
private void WriteEntityFileToDisk(string file, string code)
101+
{
102+
using(var sourceWriter = new StreamWriter(file))
103+
{
104+
sourceWriter.WriteLine(code);
105+
}
106+
}
107+
108+
private bool DirectoryHasFiles(string path)
109+
{
110+
return Directory.EnumerateFileSystemEntries(path).Any();
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)