Skip to content

Commit

Permalink
Small improvements to code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
popcatalin81 committed May 15, 2020
1 parent df487b4 commit f96d3f0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
39 changes: 38 additions & 1 deletion Geco/Common/BaseGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Humanizer;

namespace Geco.Common
{
Expand Down Expand Up @@ -100,7 +101,7 @@ protected void Comma()
}

/// <summary>
/// Write comma , on the previous line if a new line is written
/// Write comma , on the previous line if a new line is written with the same indent. Changing indent removes comma.
/// </summary>
protected void CommaIfNewLine()
{
Expand Down Expand Up @@ -163,6 +164,20 @@ protected void WD(string text = "", bool write = true)
Dedent();
}

/// <summary>
/// Write on Previous line
/// </summary>
/// <param name="text"></param>
/// <param name="write"></param>
protected void WP(string text, bool write = true)
{
if (write)
{
_tw.Write(text);
if (OutputToConsole) Console.Write(text);
}
}

/// <summary>
/// Write line with current indent
/// </summary>
Expand Down Expand Up @@ -207,6 +222,7 @@ protected void W(string text = "", bool write = true)
protected void Indent()
{
_indent++;
commaNewLine = false;
}

/// <summary>
Expand All @@ -215,18 +231,39 @@ protected void Indent()
protected void Dedent()
{
_indent--;
commaNewLine = false;
}

/// <summary>
/// Returns a string with comma joined values
/// </summary>
protected string CommaJoin(IEnumerable<string> values)
{
return string.Join(", ", values);
}

/// <summary>
/// Returns a string with comma joined values
/// </summary>
protected string CommaJoin<T>(IEnumerable<T> values, Func<T, string> selector)
{
return string.Join(", ", values.Select(selector));
}

/// <summary>
/// Pluralizes the input term if collection contains more than one element
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="inputTern">The input term singular</param>
/// <param name="collection">The collection that determines the pluralization</param>
/// <returns>The inputTerm pluralized if collection has more than 1 element, inputTerm unchanged otherwise</returns>
public string Pluralize<T>(string inputTern, IReadOnlyCollection<T> collection)
{
if (collection?.Count > 1)
return inputTern.Pluralize();
return inputTern;
}

protected IDisposable OnBlockEnd(Action action = null, bool write = true)
{
return new DisposableAction(write ? action : null);
Expand Down
17 changes: 9 additions & 8 deletions Geco/Database/EntityFrameworkCoreReverseModelGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public EntityFrameworkCoreReverseModelGenerator(IMetadataProvider provider, IInf

protected override void Generate()
{
IgnoreUnsuportedColumns();
IgnoreUnsupportedColumns();
FilterTables();
WriteEntityFiles();
WriteContextFile();
Expand Down Expand Up @@ -59,7 +59,7 @@ private void WriteEntityFiles()

private void WriteContextFile()
{
using (BeginFile($"{options.ContextName ?? Inf.Pascalise(Db.Name)}.cs"))
using (BeginFile($"{options.ContextName ?? Inf.Pascalise(Db.Name)}.Context.cs"))
using (WriteHeader())
{
W($"[GeneratedCode(\"Geco\", \"{Assembly.GetEntryAssembly().GetName().Version}\")]",
Expand Down Expand Up @@ -170,7 +170,8 @@ private void WriteDbSets()
var className = table.Metadata["Class"];
var plural = Inf.Pluralise(className);
table.Metadata["DbSet"] = plural;
W($"public virtual DbSet<{className}> {plural} {{ get; set; }}");
W($"public virtual DbSet<{className}> {plural} {{ get; set; }}", options.AdvancedGeneration == false);
W($"public virtual DbSet<{className}> {plural} => Set<{className}>();", options.AdvancedGeneration);
}
}

Expand Down Expand Up @@ -245,8 +246,8 @@ private void WriteEntity(Table table)
foreach (var column in fk.FromColumns) column.Metadata["NavProperty"] = propertyName;
W("[JsonIgnore]", options.JsonSerialization);
W($"public {targetClassName} {propertyName} {{ get; set; }}");
WP($" //{Pluralize("Column", fk.FromColumns)}: {CommaJoin(fk.FromColumns, c => c.Name)}, FK: {fk.Name}", options.GenerateComments);
}

W();
}

Expand Down Expand Up @@ -329,7 +330,7 @@ private void WriteModelBuilderConfigurations()
IW($".HasColumnName(\"{column.Name}\")");
W($".HasColumnType(\"{GetColumnType(column)}\")");
if (!string.IsNullOrEmpty(column.DefaultValue))
W($".HasDefaultValueSql(\"{RemoveExtraParantesis(column.DefaultValue)}\")");
W($".HasDefaultValueSql(\"{RemoveExtraParenthesis(column.DefaultValue)}\")");
if (IsString(column.DataType) && !column.IsNullable) W(".IsRequired()");
if (IsString(column.DataType) && column.MaxLength != -1)
W($".HasMaxLength({column.MaxLength})");
Expand Down Expand Up @@ -454,14 +455,14 @@ private string GetColumnType(Column column)
return column.DataType;
}

private string RemoveExtraParantesis(string stringValue)
private string RemoveExtraParenthesis(string stringValue)
{
if (stringValue.StartsWith("(") && stringValue.EndsWith(")"))
return RemoveExtraParantesis(stringValue.Substring(1, stringValue.Length - 2));
return RemoveExtraParenthesis(stringValue.Substring(1, stringValue.Length - 2));
return stringValue;
}

private void IgnoreUnsuportedColumns()
private void IgnoreUnsupportedColumns()
{
foreach (var schema in Db.Schemas)
foreach (var table in schema.Tables)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ public class EntityFrameworkCoreReverseModelGeneratorOptions
public string TablesRegex { get; set; }
public List<string> ExcludedTables { get; } = new List<string>();
public string ExcludedTablesRegex { get; set; }
public bool AdvancedGeneration { get; set; }
}
}

0 comments on commit f96d3f0

Please sign in to comment.