BlueWest.EfGenerator/src/BlueWest.EfMethods/EfMethodsSource.cs

113 lines
4.2 KiB
C#
Raw Normal View History

2022-09-06 00:47:58 +03:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
2022-09-06 01:14:31 +03:00
using BlueWest.EfMethods.Extensions;
2022-09-06 00:47:58 +03:00
using Microsoft.CodeAnalysis.CSharp;
2022-09-06 01:14:31 +03:00
using static BlueWest.EfMethods.Sources.Constants;
2022-09-06 00:47:58 +03:00
2022-09-06 01:14:31 +03:00
namespace BlueWest.EfMethods.Sources
2022-09-06 00:47:58 +03:00
{
internal static class EfMethodsSource
{
2022-09-08 06:16:31 +03:00
internal static SourceCode Generate(EfMethodsModel model, EfTemplates templates)
2022-09-06 00:47:58 +03:00
{
using var builder = new SourceBuilder();
2022-09-08 06:16:31 +03:00
2022-09-06 00:47:58 +03:00
builder
.WriteLine(GeneratedFilesHeader)
.WriteUsings(model.Usings)
.WriteLine("using Microsoft.EntityFrameworkCore;")
.WriteLine("using System.Linq;")
.WriteLine("using System.Linq.Expressions;")
.WriteLine("using System.Threading.Tasks;")
.WriteLine()
// Namespace declaration
.WriteLine($"namespace {model.Namespace}")
.WriteOpeningBracket()
// Class declaration
2022-09-10 08:05:39 +03:00
.WriteLine($"internal static partial class {model.ContextTypeName}Extensions")
2022-09-06 00:47:58 +03:00
.WriteOpeningBracket()
.WriteLine();
foreach (var methodModel in model.MethodsModels.OrderBy(x => x.PropertyName))
{
switch (methodModel)
{
2022-09-08 06:16:31 +03:00
case EfAddMethodsModel currentModel:
2022-09-06 00:47:58 +03:00
builder
2022-09-08 06:16:31 +03:00
.EfAddAddMethod(model, currentModel, templates.AddTemplate)
2022-09-06 00:47:58 +03:00
.WriteLine();
break;
2022-09-08 06:16:31 +03:00
case EfUpdateMethodsModel currentModel:
2022-09-06 00:47:58 +03:00
builder
2022-09-08 06:16:31 +03:00
.EfAddUpdateMethod(model, currentModel, templates.UpdateTemplate)
2022-09-06 00:47:58 +03:00
.WriteLine();
break;
2022-09-08 06:16:31 +03:00
case EfGetOneByModel currentModel:
2022-09-06 00:47:58 +03:00
builder
2022-09-08 06:16:31 +03:00
.EfAddGetOneEntityByMethod(model, currentModel, templates.GetOneByTemplate)
.WriteLine();
2022-09-06 00:47:58 +03:00
break;
2022-09-08 06:16:31 +03:00
case EfGetManyModel currentModel:
2022-09-06 00:47:58 +03:00
builder
2022-09-08 06:16:31 +03:00
.EfAddGetManyMethod(model, currentModel, templates.GetManyTemplate)
.WriteLine();
2022-09-06 00:47:58 +03:00
break;
2022-09-08 06:16:31 +03:00
case EfGetOneWithModel currentModel:
2022-09-06 00:47:58 +03:00
builder
2022-09-08 06:16:31 +03:00
.EfAddGetOneMethod(model, currentModel, templates.GetOneTemplate)
.WriteLine();
2022-09-06 00:47:58 +03:00
break;
2022-09-09 06:26:27 +03:00
2022-09-08 06:16:31 +03:00
case EfGetListModel currentModel:
builder
.EfAddGetListMethod(model, currentModel, templates.GetListTemplate)
.WriteLine();
break;
case EfAddToListModel currentModel:
builder
.EfAddAddToListMethod(model, currentModel, templates.AddToListTemplate)
.WriteLine();
break;
2022-09-09 06:26:27 +03:00
case EfGetOneFromListModel currentModel:
builder
.EfAddGetOneFromListMethod(model, currentModel, templates.GetOneFromListTemplate)
.WriteLine();
break;
2022-09-06 00:47:58 +03:00
}
}
2022-09-08 06:16:31 +03:00
2022-09-06 00:47:58 +03:00
builder
// End class declaration
.WriteClosingBracket()
.WriteLine()
// End namespace declaration
.WriteClosingBracket();
var generatedCode = builder.ToString();
var hintName = $"{model.ContextTypeName}Extensions.g.cs";
return new(generatedCode, hintName);
}
internal static SourceBuilder ParseTemplate(this SourceBuilder builder, string finalTemplate)
{
string[] array = finalTemplate.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
for (var i = 0; i < array.Length; i++)
{
var line = array[i];
builder.WriteLine(line);
}
return builder;
}
}
}