Created
October 7, 2019 00:07
-
-
Save hastarin/7e61d262171a73ec95fe938abf729085 to your computer and use it in GitHub Desktop.
Using Regex renaming for EFCorePowerTools
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Query Kind="Program"> | |
<Reference><RuntimeDirectory>\System.Dynamic.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Globalization.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Text.RegularExpressions.dll</Reference> | |
<NuGetReference>Humanizer.Core</NuGetReference> | |
<NuGetReference>Newtonsoft.Json</NuGetReference> | |
<Namespace>Humanizer</Namespace> | |
<Namespace>Newtonsoft.Json</Namespace> | |
<Namespace>Newtonsoft.Json.Converters</Namespace> | |
<Namespace>System.Dynamic</Namespace> | |
<Namespace>System.Globalization</Namespace> | |
<Namespace>System.Linq</Namespace> | |
<Namespace>System.Text.RegularExpressions</Namespace> | |
</Query> | |
void Main() | |
{ | |
Directory.SetCurrentDirectory (Path.GetDirectoryName (Util.CurrentQueryPath)); | |
var file = @"Replacements.csv"; | |
var data = DynamicCsvStream.Parse(file).AsDynamicEnumerable(); | |
Dictionary <string,string> replacements = new Dictionary<string, string>(); | |
foreach (dynamic element in data) | |
{ | |
replacements.Add(element.From, element.To); | |
} | |
//replacements.Dump(); | |
file = @"TableColumnNames.csv"; | |
var allData = DynamicCsvStream.Parse(file).AsDynamicEnumerable(); | |
Regex suffix = new Regex("_r$", RegexOptions.Compiled); | |
string newName = string.Empty; | |
bool matched = false; | |
var schemas = new List<Schema>(); | |
TableRenamer rename = null; | |
Schema schema = null; | |
List<TableRenamer> renamer = null; | |
foreach (dynamic element in allData) | |
{ | |
if (!schemas.Any(s => s.SchemaName == element.Schema)) | |
{ | |
if (rename != null && renamer != null) | |
{ | |
renamer.Add(rename); | |
} | |
schema = new Schema(); | |
schema.UseSchemaName = false; | |
schema.SchemaName = element.Schema; | |
renamer = schema.Tables; | |
schemas.Add(schema); | |
} | |
if (rename is null || element.Table != rename.Name) | |
{ | |
if (rename != null && rename.NewName != null) | |
{ | |
renamer.Add(rename); | |
} | |
rename = new TableRenamer(); | |
rename.Name = element.Table; | |
newName = suffix.Replace(rename.Name, "").Humanize().Dehumanize(); | |
foreach (var replacment in replacements) | |
{ | |
newName = Regex.Replace(newName, replacment.Key, replacment.Value); | |
} | |
rename.NewName = newName; | |
} | |
matched = false; | |
newName = element.Column; | |
newName = newName.Humanize().Dehumanize(); | |
foreach (var replacment in replacements) | |
{ | |
var replacement = Regex.Replace(newName, replacment.Key, replacment.Value); | |
if (replacement != newName) | |
{ | |
newName = replacement; | |
matched = true; | |
} | |
} | |
if (matched) | |
{ | |
if (rename.Columns == null) | |
{ | |
rename.Columns = new List<UserQuery.ColumnNamer>(); | |
} | |
rename.Columns.Add(new ColumnNamer { Name = element.Column, NewName = newName }); | |
} | |
} | |
renamer.Add(rename); | |
JsonConvert.DefaultSettings = () => new JsonSerializerSettings | |
{ | |
Formatting = Newtonsoft.Json.Formatting.Indented, | |
NullValueHandling = NullValueHandling.Ignore | |
}; | |
File.WriteAllText("efpt.renaming.json", JsonConvert.SerializeObject(schemas)); | |
} | |
public static class Helper { | |
public static string ToCamelCase(this string name) | |
{ | |
TextInfo myTI = new CultureInfo("en-AU",false).TextInfo; | |
return myTI.ToTitleCase(myTI.ToLower(name)).Replace(" ",string.Empty); | |
} | |
} | |
public class Schema | |
{ | |
public Schema() | |
{ | |
Tables = new List<TableRenamer>(); | |
} | |
public bool UseSchemaName { get; set; } | |
public string SchemaName { get; set; } | |
public List<TableRenamer> Tables { get; set; } | |
} | |
public class TableRenamer | |
{ | |
public string Name { get; set; } | |
public string NewName { get; set; } | |
public List<ColumnNamer> Columns { get; set; } | |
} | |
public class ColumnNamer | |
{ | |
public string Name { get; set; } | |
public string NewName { get; set; } | |
} | |
/* Requires DynamicCsv classes from the following to work | |
https://www.codeproject.com/Articles/436406/Power-of-Dynamic-Reading-XML-and-CSV-files-made-ea | |
*/ | |
/* Example contents for Replacements.csv | |
From,To | |
Comp(?![a-z]),Competition | |
*/ | |
/* Example contents for TableColumnNames.csv | |
Schema,Table,Column | |
ref,comp,name | |
ref,comp,comp_end_date | |
ref,comp,comp_type_id | |
ref,comp,championship_id | |
ref,comp,comp_start_date | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment