Skip to content

Commit

Permalink
Merge branch 'release/1.1.91'
Browse files Browse the repository at this point in the history
  • Loading branch information
David Lebee committed Dec 12, 2018
2 parents a485a51 + 0df992c commit 8ac084d
Show file tree
Hide file tree
Showing 36 changed files with 976 additions and 499 deletions.
16 changes: 16 additions & 0 deletions Acme.DbUtils/Acme.DbUtils.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="PoweredSoft.CodeGenerator" Version="1.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\PoweredSoft.DbUtils.EF.Generator.Core\PoweredSoft.DbUtils.EF.Generator.Core.csproj" />
<ProjectReference Include="..\PoweredSoft.DbUtils.Schema.Core\PoweredSoft.DbUtils.Schema.Core.csproj" />
</ItemGroup>

</Project>
128 changes: 128 additions & 0 deletions Acme.DbUtils/CustomGeneratorService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using System;
using System.IO;
using PoweredSoft.CodeGenerator;
using PoweredSoft.CodeGenerator.Constants;
using PoweredSoft.CodeGenerator.Extensions;
using PoweredSoft.DbUtils.EF.Generator.Core;
using PoweredSoft.DbUtils.Schema.Core;

namespace Acme.DbUtils
{
public class ContextInterceptor : IContextInterceptor
{
public void InterceptContext(IGenerator generator)
{
if (!(generator is IGeneratorUsingGenerationContext) || !(generator is IGeneratorWithMeta))
throw new Exception("Not the kind of generator expected.");

var gen = generator as IGeneratorUsingGenerationContext;
var genMeta = generator as IGeneratorWithMeta;
var options = gen.GetOptions();
var gc = gen.GetGenerationContext();
var contextClassName = genMeta.ContextClassName();
var contextClass = gc.FindClass(contextClassName);
contextClass.Method(m => m.ReturnType("void").Name("CreateMethodBlah"));
}
}

public class ResolveTypeInterceptor : IResolveTypeInterceptor
{
public Tuple<string, bool> InterceptResolveType(IColumn column)
{
/*
if (column.Table.Name == "Phone" && column.Name == "PhoneTypeId")
return new Tuple<string, bool>("Acme.Enums.PhoneTypes", true);*/

return null;
}
}

public class TableInterceptor : ITableInterceptor
{
public void InterceptTable(IGenerator generator, ITable table)
{
if (!(generator is IGeneratorUsingGenerationContext) || !(generator is IGeneratorWithMeta))
throw new Exception("Not the kind of generator expected.");

var gen = generator as IGeneratorUsingGenerationContext;
var genMeta = generator as IGeneratorWithMeta;
var options = generator.GetOptions();
var ctx = gen.GetGenerationContext();


// model.
var modelClassName = genMeta.ModelClassName(table);
var modelClassNamespace = genMeta.ModelNamespace(table);
var modelFullClassName = genMeta.ModelClassFullName(table);

// poco.
var pocoClassName = genMeta.TableClassName(table);
var pocoClassNamespace = genMeta.TableNamespace(table);
var pocoFullClassName = genMeta.TableClassFullName(table);

// classes
var pocoClass = ctx.FindClass(pocoClassName, pocoClassNamespace);

var path = $"{options.OutputDir}{Path.DirectorySeparatorChar}transformations.generated.cs";

ctx.File(path, fb =>
{
fb.Namespace("Acme.Models", true, ns =>
{
ns.Class($"{table.Name}ModelTransformationService", true, c =>
{
c.Method(m =>
{
m
.AccessModifier(AccessModifiers.Public)
.Virtual(true)
.Name("ToModel")
.ReturnType("void")
.Parameter(p => p.Name("source").Type(pocoFullClassName))
.Parameter(p => p.Name("model").Type(modelFullClassName));

table.Columns.ForEach(column =>
{
m.RawLine($"model.{column.Name} = source.{column.Name}");
});
});

c.Method(m =>
{
m
.AccessModifier(AccessModifiers.Public)
.Virtual(true)
.Name("FromModel")
.ReturnType("void")
.Parameter(p => p.Name("model").Type(modelFullClassName))
.Parameter(p => p.Name("destination").Type(pocoFullClassName));

table.Columns.ForEach(column =>
{
bool isPropertyNullable =
column.IsNullable || options.GenerateModelPropertyAsNullable;
if (isPropertyNullable && !column.IsNullable)
{
var matchingProp = pocoClass.FindByMeta<PropertyBuilder>(column);
var ternary = TernaryBuilder
.Create()
.RawCondition(rc => rc.Condition($"model.{column.Name} != null"))
.True(RawInlineBuilder.Create(
$"destination.{column.Name} = ({matchingProp.GetTypeName()})model.{column.Name}"))
.False(RawInlineBuilder.Create(
$"destination.{column.Name} = default({matchingProp.GetTypeName()})"));

m.RawLine($"destination.{column.Name} = {ternary.GenerateInline()}");
}
else
{
m.RawLine($"destination.{column.Name} = model.{column.Name}");
}
});
});
});
});
});
}
}
}
12 changes: 12 additions & 0 deletions PoweredSoft.DbUtils.EF.Generator.Core/IContextInterceptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
using PoweredSoft.CodeGenerator;

namespace PoweredSoft.DbUtils.EF.Generator.Core
{
public interface IContextInterceptor
{
void InterceptContext(IGenerator generator);
}
}
34 changes: 33 additions & 1 deletion PoweredSoft.DbUtils.EF.Generator.Core/IGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using PoweredSoft.CodeGenerator;
using PoweredSoft.DbUtils.Schema.Core;

namespace PoweredSoft.DbUtils.EF.Generator.Core
Expand All @@ -11,6 +13,7 @@ public interface IGenerator
List<ISequence> ResolveSequencesToGenerate();
IGeneratorOptions GetOptions();
IGeneratorOptions GetDefaultOptions();
IDataTypeResolver DataTypeResolver { get; }
void InitializeOptionsWithDefault();
}

Expand All @@ -19,4 +22,33 @@ public interface IGenerator<TOptions> : IGenerator
{
TOptions Options { get; }
}

public interface IGeneratorUsingGenerationContext : IGenerator
{
GenerationContext GetGenerationContext();
}

public interface IGeneratorWithMeta : IGenerator
{
string ContextClassName();
string ContextFullClassName();
string ContextNamespace();
string EmptyMetas(string text);
string ModelClassName(ITable table);
string ModelNamespace(ITable table);
string ModelClassFullName(ITable table);
string ModelInterfaceName(ITable table);
string Pluralize(string text);
string ReplaceMetas(string text);
string TableClassFullName(ITable table);
string TableClassName(ITable table);
string TableInterfaceName(ITable table);
string TableInterfaceNamespace(ITable table);
string TableNamespace(ITable table);
string ModelInterfaceNamespace(ITable table);
Tuple<string, bool> GetColumnTypeInfo(IColumn column);
string GetColumnTypeName(IColumn column, bool alwaysAsNullable = false);
}


}
24 changes: 23 additions & 1 deletion PoweredSoft.DbUtils.EF.Generator.Core/IGeneratorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public interface IGeneratorOptions
string ConnectionString { get; set; }
string OutputDir { get; set; }
bool CleanOutputDir { get; set; }
bool OutputToSingleFile { get; }
bool GenerateContextSequenceMethods { get; set; }
string OutputSingleFileName { get; set; }
bool GenerateInterfaces { get; set; }
Expand All @@ -25,5 +24,28 @@ public interface IGeneratorOptions
List<string> ModelInheritances { get; set; }
string Version { get; }
string Engine { get; }

// namespace overrides.
string EntityNamespace { get; set; }
string EntityInterfaceNamespace { get; set; }
string ModelInterfaceNamespace { get; set; }
string ModelNamespace { get; set; }
string ContextNamespace { get; set; }

// file overrides.
string EntitiesOutputSingleFileName { get; set; }
string EntitiesInterfacesOutputSingleFileName { get; set; }
string ModelsInterfacesOutputSingleFileName { get; set; }
string ModelsOutputSingleFileName { get; set; }
string ContextOutputSingleFileName { get; set; }

// output dir overrides.
string EntitiesOutputDir { get; set; }
string EntitiesInterfacesOutputDir { get; set; }
string ModelsInterfacesOutputDir { get; set; }
string ModelsOutputDir { get; set; }
string ContextOutputDir { get; set; }

List<string> DynamicAssemblies { get; set; }
}
}
13 changes: 13 additions & 0 deletions PoweredSoft.DbUtils.EF.Generator.Core/ITableInterceptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
using PoweredSoft.CodeGenerator;
using PoweredSoft.DbUtils.Schema.Core;

namespace PoweredSoft.DbUtils.EF.Generator.Core
{
public interface ITableInterceptor
{
void InterceptTable(IGenerator generator, ITable table);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@
<PackageProjectUrl>https://github.com/PoweredSoft/DbUtils</PackageProjectUrl>
<RepositoryUrl>https://github.com/PoweredSoft/DbUtils/PoweredSoft.DbUtils.EF.Generator.Core</RepositoryUrl>
<RepositoryType>github</RepositoryType>
<PackageIconUrl></PackageIconUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.4</Version>
<PackageIconUrl>https://secure.gravatar.com/avatar/4e32f73820c16718909a06c2927f1f8b?s=512&amp;r=g&amp;d=retro</PackageIconUrl>
<Version>1.1.0$(VersionSuffix)</Version>
<Product>PoweredSoft.DbUtils.EF.Generator.Core</Product>
<Description>update</Description>
<PackageId>PoweredSoft.DbUtils.EF.Generator.Core</PackageId>
<PackageReleaseNotes>made something virtual</PackageReleaseNotes>
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
<Company>PoweredSoft.DbUtils.EF.Generator.Core</Company>
<Authors>PoweredSoft.DbUtils.EF.Generator.Core</Authors>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="PoweredSoft.CodeGenerator" Version="1.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\PoweredSoft.DbUtils.Schema.Core\PoweredSoft.DbUtils.Schema.Core.csproj" />
</ItemGroup>
Expand Down
17 changes: 17 additions & 0 deletions PoweredSoft.DbUtils.EF.Generator.Core/ResolveTypeInterceptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;
using PoweredSoft.DbUtils.Schema.Core;

namespace PoweredSoft.DbUtils.EF.Generator.Core
{
public interface IResolveTypeInterceptor
{
/// <summary>
/// Must return type name and if its a value type.
/// </summary>
/// <param name="column"></param>
/// <returns></returns>
Tuple<string, bool> InterceptResolveType(IColumn column);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.4</Version>
<Version>1.1.0$(VersionSuffix)</Version>
<Copyright>Powered Softwares Inc.</Copyright>
<PackageLicenseUrl>https://github.com/PoweredSoft/DbUtils/blob/master/LICENSE.md</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/PoweredSoft/DbUtils</PackageProjectUrl>
<RepositoryUrl>https://github.com/PoweredSoft/DbUtils/PoweredSoft.DbUtils.EF.Generator.EF6.Core</RepositoryUrl>
<RepositoryType>github</RepositoryType>
<PackageIconUrl></PackageIconUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageIconUrl>https://secure.gravatar.com/avatar/4e32f73820c16718909a06c2927f1f8b?s=512&amp;r=g&amp;d=retro</PackageIconUrl>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="PoweredSoft.CodeGenerator" Version="1.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\PoweredSoft.DbUtils.EF.Generator.Core\PoweredSoft.DbUtils.EF.Generator.Core.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace PoweredSoft.DbUtils.EF.Generator.EF6.SqlServer
{
public class DatabaseGenerator : EF6DatabaseGeneratorBase<IDatabaseSchema, GeneratorOptions>
{
protected override IDataTypeResolver DataTypeResolver { get; } = new DataTypeResolver();
public override IDataTypeResolver DataTypeResolver { get; } = new DataTypeResolver();
public override IDatabaseSchema CreateSchema() => new DatabaseSchema();
public override IGeneratorOptions GetDefaultOptions() => new GeneratorOptions();

Expand All @@ -25,8 +25,8 @@ protected override void GenerateGetNextSequenceLines(MethodBuilder method, strin
method.RawLine($"return Database.SqlQuery<{outputType}>(\"SELECT NEXT VALUE FOR [{sqlServerSequence.Schema}].[{sequence.Name}];\").First()");
}

protected override string EmptyMetas(string text) => base.EmptyMetas(text).EmptyMetas();
protected override string ReplaceMetas(string text, ITable table) => base.ReplaceMetas(text, table).ReplaceMetas((Table)table);
public override string EmptyMetas(string text) => base.EmptyMetas(text).EmptyMetas();
public override string ReplaceMetas(string text, ITable table) => base.ReplaceMetas(text, table).ReplaceMetas((Table)table);
public override List<ITable> ResolveTablesToGenerate() => base.ResolveTablesToGenerate().ShouldGenerate(Options);
public override List<ISequence> ResolveSequencesToGenerate() => base.ResolveSequencesToGenerate().ShouldGenerate(Options);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class GeneratorOptions : IEF6GeneratorOptions, ISqlServerGeneratorOptions
public string ConnectionString { get; set; }
public string OutputDir { get; set; }
public bool CleanOutputDir { get; set; } = false;
public bool OutputToSingleFile => !string.IsNullOrWhiteSpace(OutputSingleFileName);
public bool GenerateContextSequenceMethods { get; set; }
public string OutputSingleFileName { get; set; }
public bool GenerateInterfaces { get; set; } = false;
Expand All @@ -33,6 +32,22 @@ public class GeneratorOptions : IEF6GeneratorOptions, ISqlServerGeneratorOptions
public List<string> ModelInheritances { get; set; } = new List<string>();
public string Version => "6";
public string Engine => "SqlServer";
public string EntityNamespace { get; set; }
public string EntityInterfaceNamespace { get; set; }
public string ModelInterfaceNamespace { get; set; }
public string ModelNamespace { get; set; }
public string ContextNamespace { get; set; }
public string EntitiesOutputSingleFileName { get; set; }
public string EntitiesInterfacesOutputSingleFileName { get; set; }
public string ModelsInterfacesOutputSingleFileName { get; set; }
public string ModelsOutputSingleFileName { get; set; }
public string ContextOutputSingleFileName { get; set; }
public string EntitiesOutputDir { get; set; }
public string EntitiesInterfacesOutputDir { get; set; }
public string ModelsInterfacesOutputDir { get; set; }
public string ModelsOutputDir { get; set; }
public string ContextOutputDir { get; set; }
public List<string> DynamicAssemblies { get; set; }
public string ConnectionStringName { get; set; }
public string FluentConfigurationClassSuffix { get; set; } = "FluentConfiguration";
public List<string> IncludedSchemas { get; } = new List<string>();
Expand Down
Loading

0 comments on commit 8ac084d

Please sign in to comment.