Skip to content

Commit

Permalink
Enhance setting renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
cheng-wei committed Nov 27, 2019
1 parent eff636e commit 7214646
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 37 deletions.
2 changes: 2 additions & 0 deletions src/GUI/RevEng.Shared/TableRenamer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class TableRenamer

public string NewName { get; set; }

public string VariableName { get; set; }

public List<ColumnNamer> Columns { get; set; }
}
}
25 changes: 25 additions & 0 deletions src/GUI/ReverseEngineer20/ReverseEngineer/InflectorPluralizer.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
using Humanizer;
using Microsoft.EntityFrameworkCore.Design;
using System.Collections.Generic;
using System.Linq;

namespace ReverseEngineer20.ReverseEngineer
{
public class InflectorPluralizer : IPluralizer
{
private readonly Dictionary<string, string> _customerCharacterPluralize;
private readonly Dictionary<string, string> _customerCharacterSingularize;

public InflectorPluralizer(List<Schema> customReplacers)
{
_customerCharacterPluralize = customReplacers
.SelectMany(x => x.Tables)
.Where(x => !string.IsNullOrWhiteSpace(x.VariableName))
.ToDictionary(key => key.Name.ToPascalCase(), value => value.VariableName);

_customerCharacterSingularize = customReplacers
.SelectMany(x => x.Tables)
.Where(x => !string.IsNullOrWhiteSpace(x.NewName))
.ToDictionary(key => key.Name.ToPascalCase(), value => value.NewName);

}

public string Pluralize(string name)
{
if (name != null)
{
if (_customerCharacterPluralize.ContainsKey(name))
return _customerCharacterPluralize[name];

return name.Pluralize(inputIsKnownToBeSingular: false);
}
return name;
Expand All @@ -18,6 +40,9 @@ public string Singularize(string name)
{
if (name != null)
{
if (_customerCharacterSingularize.ContainsKey(name))
return _customerCharacterSingularize[name];

return name.Singularize(inputIsKnownToBePlural: false);
}
return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public override string GenerateCandidateIdentifier(DatabaseTable originalTable)

if (schema.UseSchemaName)
{
candidateStringBuilder.Append(ToPascalCase(originalTable.Schema));
candidateStringBuilder.Append(ToPascalCase(originalTable.Name));
candidateStringBuilder.Append(originalTable.Schema.ToPascalCase());
candidateStringBuilder.Append(originalTable.Name.ToPascalCase());

return candidateStringBuilder.ToString();
}
Expand All @@ -50,7 +50,7 @@ public override string GenerateCandidateIdentifier(DatabaseTable originalTable)

if (string.IsNullOrWhiteSpace(newTableName))
{
candidateStringBuilder.Append(ToPascalCase(originalTable.Name));
candidateStringBuilder.Append(originalTable.Name.ToPascalCase());

return candidateStringBuilder.ToString();
}
Expand Down Expand Up @@ -120,7 +120,7 @@ public override string GetDependentEndCandidateNavigationPropertyName(IForeignKe
{
if (schema.UseSchemaName)
{
return ToPascalCase(schema.SchemaName) + baseName;
return schema.SchemaName.ToPascalCase() + baseName;
}
return baseName;
}
Expand All @@ -133,37 +133,5 @@ public override string GetDependentEndCandidateNavigationPropertyName(IForeignKe
private Schema GetSchema(string originalSchema)
=> _customNameOptions?
.FirstOrDefault(x => x.SchemaName == originalSchema);

private static string ToPascalCase(string value)
{
var candidateStringBuilder = new StringBuilder();
var previousLetterCharInWordIsLowerCase = false;
var isFirstCharacterInWord = true;

foreach (var c in value)
{
var isNotLetterOrDigit = !char.IsLetterOrDigit(c);
if (isNotLetterOrDigit
|| (previousLetterCharInWordIsLowerCase && char.IsUpper(c)))
{
isFirstCharacterInWord = true;
previousLetterCharInWordIsLowerCase = false;
if (isNotLetterOrDigit)
{
continue;
}
}

candidateStringBuilder.Append(
isFirstCharacterInWord ? char.ToUpperInvariant(c) : char.ToLowerInvariant(c));
isFirstCharacterInWord = false;
if (char.IsLower(c))
{
previousLetterCharInWordIsLowerCase = true;
}
}

return candidateStringBuilder.ToString();
}
}
}
39 changes: 39 additions & 0 deletions src/GUI/ReverseEngineer20/ReverseEngineer/StringExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Text;

namespace ReverseEngineer20.ReverseEngineer
{
public static class StringExtension
{
public static string ToPascalCase(this string value)
{
var candidateStringBuilder = new StringBuilder();
var previousLetterCharInWordIsLowerCase = false;
var isFirstCharacterInWord = true;

foreach (var c in value)
{
var isNotLetterOrDigit = !char.IsLetterOrDigit(c);
if (isNotLetterOrDigit
|| (previousLetterCharInWordIsLowerCase && char.IsUpper(c)))
{
isFirstCharacterInWord = true;
previousLetterCharInWordIsLowerCase = false;
if (isNotLetterOrDigit)
{
continue;
}
}

candidateStringBuilder.Append(
isFirstCharacterInWord ? char.ToUpperInvariant(c) : char.ToLowerInvariant(c));
isFirstCharacterInWord = false;
if (char.IsLower(c))
{
previousLetterCharInWordIsLowerCase = true;
}
}

return candidateStringBuilder.ToString();
}
}
}
1 change: 1 addition & 0 deletions src/GUI/ReverseEngineer20/ReverseEngineer20.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@
<Compile Include="EFCoreReverseEngineer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReverseEngineer\InflectorPluralizer.cs" />
<Compile Include="ReverseEngineer\StringExtension.cs" />
<Compile Include="ServiceProviderBuilder.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion src/GUI/ReverseEngineer20/ServiceProviderBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public static ServiceProvider Build(ReverseEngineerOptions options)

if (options.CustomReplacers != null)
{
serviceCollection.AddSingleton<ICandidateNamingService>(provider => new ReplacingCandidateNamingService(options.CustomReplacers));
serviceCollection.AddSingleton(options.CustomReplacers)
.AddSingleton<ICandidateNamingService, ReplacingCandidateNamingService>();
}

if (options.UseInflector)
Expand Down

0 comments on commit 7214646

Please sign in to comment.