public void PrimaryKeyReasignTests() { var c = new Core.Config.Entity { Name = "Dimension.City" }; c.AddPKOverride("City Key"); c.AddPKOverride("WWI City ID"); Internal.AppSettings.Instance.Configuration.Entities.Add(c); var db1 = new TemplateInputFileSource(SchemaFileName).LoadSchema(Internal.AppSettings.Instance.Configuration); var lst = db1.FindEntities("Dimension.City"); Assert.True(lst.Count == 1, "Should be 1 entity that match the pattern 'Dimension.City'"); Assert.True((lst[0].PrimaryKeys[0].Name.Equals("City Key")), "First Primary Key Name should be 'City Key'"); Assert.True((lst[0].PrimaryKeys[1].Name.Equals("WWI City ID")), "First Primary Key Name should be 'WWI City ID'"); c.ClearPKOverrides(); c.AddPKOverride("Region"); c.AddPKOverride("WWI City ID"); c.AddPKOverride("City Key"); var db2 = new TemplateInputFileSource(SchemaFileName).LoadSchema(Internal.AppSettings.Instance.Configuration); var lst2 = db2.FindEntities("Dimension.City"); Assert.True(lst.Count == 1, "Should be 1 entity that match the pattern 'Dimension.City'"); Assert.True((lst2[0].PrimaryKeys[0].Name.Equals("Region")), "First Primary Key Name should be 'Region'"); Assert.True((lst2[0].PrimaryKeys[1].Name.Equals("WWI City ID")), "First Primary Key Name should be 'WWI City ID'"); Assert.True((lst2[0].PrimaryKeys[2].Name.Equals("City Key")), "First Primary Key Name should be 'City Key'"); }
public void CompareObjectTestsAreEqual() { var db1 = new TemplateInputFileSource(SchemaFileName).LoadSchema(Internal.AppSettings.Instance.Configuration); var db2 = new TemplateInputFileSource(SchemaFileName).LoadSchema(Internal.AppSettings.Instance.Configuration); var list = db1.CompareTo(db2); Assert.True(list.Count == 0, "Both schemas should equal"); }
public void CompareObjectTestsAreNotEqual() { var db1 = new TemplateInputFileSource(SchemaFileName).LoadSchema(Internal.AppSettings.Instance.Configuration); var db2 = new TemplateInputFileSource(SchemaFileName).LoadSchema(Internal.AppSettings.Instance.Configuration); var Entity = db2.Entities[db2.Keys[0]]; Entity.Name = Entity.Name + "_CHANGED"; var list = db1.CompareTo(db2); Assert.True(list.Count > 0, "Both schemas should not equal"); }
public void WlldCardSearchTest() { var db1 = new TemplateInputFileSource(SchemaFileName).LoadSchema(Internal.AppSettings.Instance.Configuration); var lst = db1.FindEntities("Integration.Trans*"); Assert.True(lst.Count == 2, "Should be 2 entities that match the pattern 'Integration.Trans*'"); var lst2 = db1.FindEntities("Dimension.*"); Assert.True(lst2.Count == 8, "Should be 8 entities that match the pattern 'Dimension.*'"); var lst3 = db1.FindEntities("*Stock*"); Assert.True(lst3.Count == 4, "Should be 4 entities that match the pattern '*Stock*'"); }
public void RenderTemplateSingleNoOutputPathTest() { try { var codeGenerator = new CodeGenerator(); ITemplateInput template = new TemplateInputFileSource(SchemaFileName); var database = template.LoadSchema(Configuration.FromFile("ezdbcodegen.config.json")); codeGenerator.ProcessTemplate((@"{ASSEMBLY_PATH}Templates" + Path.DirectorySeparatorChar + @"SchemaRenderAsFiles.hbs").ResolvePathVars(), template); Assert.True(Directory.Exists(codeGenerator.OutputPath), string.Format("Template Rendered Output files in path {0} was not created", codeGenerator.OutputPath)); } catch (Exception ex) { Assert.True(false, ex.Message); } }
public void AliasPatternTests() { var cfg = Internal.AppSettings.Instance.Configuration; cfg.Database.AliasNamePattern = Configuration.SCHEMA_NAME + Configuration.OBJECT_NAME; var db1 = new TemplateInputFileSource(SchemaFileName).LoadSchema(cfg); var e1 = db1.FindEntity("Dimension.City"); Assert.True((e1 != null), "Should find entity Dimension.City"); Assert.True((e1.Alias.Equals("DimensionCity")), "Alias of entity should equal DimensionCity"); cfg.Database.AliasNamePattern = Configuration.SCHEMA_NAME + "____" + Configuration.OBJECT_NAME; var db2 = new TemplateInputFileSource(SchemaFileName).LoadSchema(cfg); var e2 = db2.FindEntity("Dimension.City"); Assert.True((e2 != null), "Should find entity Dimension.City"); Assert.True((e2.Alias.Equals("Dimension____City")), "Alias of entity should equal Dimension____City"); }
public void RenderTemplateFileTest() { try { var codeGenerator = new CodeGenerator(); ITemplateInput template = new TemplateInputFileSource(SchemaFileName); var database = template.LoadSchema(Configuration.FromFile("ezdbcodegen.config.json")); var OutputPath = System.IO.Path.GetTempPath() + "MySchemaNameRender.txt"; if (File.Exists(OutputPath)) { File.Delete(OutputPath); } codeGenerator.ProcessTemplate((@"{ASSEMBLY_PATH}Templates" + Path.DirectorySeparatorChar + @"SchemaRender.hbs").ResolvePathVars(), template, OutputPath); Assert.True(File.Exists(codeGenerator.OutputPath), string.Format("Template Rendered Output file {0} was not created.", codeGenerator.OutputPath)); } catch (Exception ex) { Assert.True(false, ex.Message); } }
public static void Enable(CommandLineApplication app) { app.Name = "ezdb.codegen.cli"; app.Description = "EzDbCodeGen - Code Generation Utility"; app.ExtendedHelpText = "This application will allow you to trigger code generation based on a template file or a list of template files." + Environment.NewLine + ""; app.HelpOption("-?|-h|--help"); var sampleFilesOption = app.Option("-i|--init-files <path>", "This option will download the template files and required powerscript to the [path] directory, renaming assets using the value sent through -a/--app-name ", CommandOptionType.SingleValue); var appNameOption = app.Option("-a|--app-name <appame>", "This option will be used to customize the name and files on --init-files, this default value will be MyApp", CommandOptionType.SingleValue); var verboseOption = app.Option("-v|--verbose", "Will output more detailed message about what is happening during application processing. This parm is optional and will override the value in appsettings.json. ", CommandOptionType.NoValue); var versionOption = app.Option("-ver|--version", "Will output the current version of the utility.", CommandOptionType.NoValue); var templateFileNameOrDirectoryOption = app.Option("-t|--template <value>", "The template file name or path that you wish to render. If you choose aa path, ", CommandOptionType.SingleValue); var pathNameOption = app.Option("-p|--outpath <value>", "The template that you wish to render. This is required uniless you use the <OUTPUT_PATH> specifier in the template file.", CommandOptionType.SingleValue); var sourceConnectionStringOption = app.Option("-sc|--connection-string <optionvalue>", "Connection String pass via the appline. This parm is optional and this value will override the value in appsettings.json. ", CommandOptionType.SingleValue); var sourceSchemaFileNameOption = app.Option("-sf|--schema-file <optionvalue>", "Specify a schema json dump to perform the code generation (as opposed to a connection string). This parm is optional and parm is present, it will override the appsettings and the -sc app line parm", CommandOptionType.SingleValue); var configFileOption = app.Option("-cf|--configfile", "The configuration file this template render will use. This is optional, the default search path will be in the same path as this assembly of this applicaiton. ", CommandOptionType.SingleValue); var compareToConnectionStringOption = app.Option("-tc|--compare-connection-string <optionvalue>", "Connection String to compare to. This parm is optional. If it is present, This schema will be compared to either the -sc or -sf and the only the changes will be updated.", CommandOptionType.SingleValue); var compareToSchemaFileNameOption = app.Option("-tf|--compare-schema-file <optionvalue>", "OPTIONAL: Specify a compare schema json dump to perform the (as opposed to a compare connection string). This parm is optional and parm is present, it will override the -ts command line parameter and will be used to compare, affecting only those entities that have changed.", CommandOptionType.SingleValue); var schemaNameOption = app.Option("-sn|--schemaName", "the Name of the schema, a decent standard could be <DatabaseName>Entites.", CommandOptionType.SingleValue); var projectFileToModifyOption = app.Option("-pf|--project-file <optionvalue>", "Option to pass a project file that will be altered with the ouputpath that the template files will be written to. This only pertains to older version of a visual studio project file.", CommandOptionType.SingleValue); var templateFilterFileMasks = app.Option("-f|--filter <optionvalue>", "Option to ignore template file names (seperated by comma, wildcards are acceptable) for those runs where a path is sent through parm -t (or --template).", CommandOptionType.SingleValue); app.OnExecute(() => { var pfx = "EzDbCodeGen: "; if (versionOption.HasValue()) { var version_ = Assembly.GetAssembly(typeof(CodeGenerator)).GetName().Version; Console.WriteLine(version_); Environment.ExitCode = 0; Environment.Exit(Environment.ExitCode); return(Environment.ExitCode); } if (verboseOption.HasValue()) { AppSettings.Instance.VerboseMessages = verboseOption.HasValue(); } try { var sampleFilesPath = (sampleFilesOption.HasValue() ? sampleFilesOption.Value().ResolvePathVars() : "%THIS%".ResolvePathVars()); if (!sampleFilesPath.EndsWith(Path.DirectorySeparatorChar)) { sampleFilesPath += Path.DirectorySeparatorChar; } if (sampleFilesOption.HasValue()) { var workPath = Path.GetTempPath() + @"EzDbCodeGen\"; var appName = (appNameOption.HasValue() ? appNameOption.Value() : "MyApp"); Directory.Delete(workPath, true); Console.WriteLine(pfx + "Sample Files to be downloaded from https://github.com/rvegajr/ez-db-codegen-core to " + workPath); WebFileHelper.CurlGitRepoZip(workPath); if (Directory.Exists(workPath)) { var rootPath = workPath + @"ez-db-codegen-core-master\Src\EzDbCodeGen.Cli\"; WebFileHelper.CopyTo(@"Templates\SchemaRender.hbs", rootPath, sampleFilesPath); WebFileHelper.CopyTo(@"Templates\SchemaRenderAsFiles.hbs", rootPath, sampleFilesPath); WebFileHelper.CopyTo(@"Templates\SchemaRenderAsFilesNoOutput.hbs", rootPath, sampleFilesPath); WebFileHelper.CopyTo(@"ezdbcodegen.config.json", rootPath, sampleFilesPath); WebFileHelper.CopyTo(@"ezdbcodegen.ps1", rootPath, sampleFilesPath); WebFileHelper.CopyTo(@"ezdbcodegen.config.json", rootPath, sampleFilesPath, appName + ".config.json") .ReplaceAll("MyEntities", appName + "Entities"); WebFileHelper.CopyTo(@"ezdbcodegen.ps1", rootPath, sampleFilesPath, appName + ".codegen.ps1") .ReplaceAll("ezdbcodegen", appName);; WebFileHelper.CopyTo(@"readme.txt", rootPath, sampleFilesPath) .ReplaceAll("SuperApp", appName) .ReplaceAll("%PSPATH%", sampleFilesPath) ; } } var schemaName = "MySchema"; if (schemaNameOption.HasValue()) { schemaName = schemaNameOption.Value(); } if (sourceConnectionStringOption.HasValue()) { AppSettings.Instance.ConnectionString = sourceConnectionStringOption.Value().SettingResolution(); } if (configFileOption.HasValue()) { AppSettings.Instance.ConfigurationFileName = configFileOption.Value(); } var Errors = new StringBuilder(); var OutputPath = string.Empty; if ((!templateFileNameOrDirectoryOption.HasValue()) || (templateFileNameOrDirectoryOption.Value().Length == 0)) { Errors.AppendLine("TemplateName is missing or empty. "); } if ((pathNameOption.HasValue()) && (pathNameOption.Value().Length > 0)) { OutputPath = pathNameOption.Value(); } if ((!sourceSchemaFileNameOption.HasValue()) && (AppSettings.Instance.ConnectionString.Length == 0)) { Errors.AppendLine("ConnectionString and schemaFileName are both missing or empty. "); } if ((sourceSchemaFileNameOption.HasValue()) && (!File.Exists(sourceSchemaFileNameOption.Value()))) { Errors.AppendLine(string.Format("Schema file '{0}' was does not exists! ", sourceSchemaFileNameOption.Value())); } if (Errors.Length > 0) { if (sampleFilesOption.HasValue()) { Console.WriteLine("Sample files where generated... exiting"); Environment.ExitCode = 0; Environment.Exit(Environment.ExitCode); return(Environment.ExitCode); } throw new Exception(Errors.ToString()); } var TemplateFileNameOrPath = templateFileNameOrDirectoryOption.Value(); if (Path.GetPathRoot(TemplateFileNameOrPath).Length == 0) { TemplateFileNameOrPath = ("{ASSEMBLY_PATH}" + TemplateFileNameOrPath).ResolvePathVars(); } if (!( (File.Exists(TemplateFileNameOrPath)) || (Directory.Exists(TemplateFileNameOrPath) ))) { TemplateFileNameOrPath = ("{ASSEMBLY_PATH}" + TemplateFileNameOrPath).ResolvePathVars(); } if (!((File.Exists(TemplateFileNameOrPath)) || (Directory.Exists(TemplateFileNameOrPath)))) { throw new Exception(string.Format("Template not found in path {0}", TemplateFileNameOrPath)); } // get the file attributes for file or directory FileAttributes attr = File.GetAttributes(TemplateFileNameOrPath); if (attr.HasFlag(FileAttributes.Directory)) { pfx = "<List of templates>: "; //Since we know this is a directory, force it to end with a system path seperator TemplateFileNameOrPath = TemplateFileNameOrPath.PathEnds(); } else { pfx = Path.GetFileNameWithoutExtension(TemplateFileNameOrPath) + ": "; } Console.WriteLine("Performing Rendering of template " + pfx + "...."); if (AppSettings.Instance.VerboseMessages) { Console.WriteLine(pfx + "Template Path: " + TemplateFileNameOrPath); Console.WriteLine(pfx + "Path Name: " + pathNameOption.Value()); Console.WriteLine(pfx + "Config File Name: " + AppSettings.Instance.ConfigurationFileName); if (!sourceSchemaFileNameOption.HasValue()) { Console.WriteLine(pfx + "Source Connection String: " + AppSettings.Instance.ConnectionString); } if (sourceSchemaFileNameOption.HasValue()) { Console.WriteLine(pfx + "Source Schema File Name: " + sourceSchemaFileNameOption.Value()); } if (compareToSchemaFileNameOption.HasValue()) { Console.WriteLine(pfx + "Compare To Schema File Name: " + compareToSchemaFileNameOption.Value()); } if ((!compareToSchemaFileNameOption.HasValue()) && (compareToConnectionStringOption.HasValue())) { Console.WriteLine(pfx + "Compare To Connection String: " + compareToConnectionStringOption.Value()); } } var CodeGen = new CodeGenerator { SchemaName = schemaName, VerboseMessages = AppSettings.Instance.VerboseMessages, ConfigurationFileName = AppSettings.Instance.ConfigurationFileName, ProjectPath = ((projectFileToModifyOption.HasValue()) ? projectFileToModifyOption.Value() : ""), TemplateFileNameFilter = ((templateFilterFileMasks.HasValue()) ? templateFilterFileMasks.Value() : "") }; var version = Assembly.GetAssembly(typeof(CodeGenerator)).GetName().Version; Console.WriteLine(pfx + "Using CodeGenerator Version " + version); CodeGen.OnStatusChangeEventArgs += StatusChangeEventHandler; var returnCode = new ReturnCodes(); ITemplateInput Source = null; if (sourceSchemaFileNameOption.HasValue()) { Source = new TemplateInputFileSource(sourceSchemaFileNameOption.Value()); } else { Source = new TemplateInputDatabaseConnecton(AppSettings.Instance.ConnectionString); } ITemplateInput CompareTo = null; if (compareToSchemaFileNameOption.HasValue()) { CompareTo = new TemplateInputFileSource(compareToSchemaFileNameOption.Value()); } else if (compareToConnectionStringOption.HasValue()) { CompareTo = new TemplateInputDatabaseConnecton(compareToConnectionStringOption.Value()); } if (CompareTo == null) { Source.VerboseMessages = AppSettings.Instance.VerboseMessages; returnCode = CodeGen.ProcessTemplate(TemplateFileNameOrPath, Source, OutputPath); } else { Source.VerboseMessages = AppSettings.Instance.VerboseMessages; CompareTo.VerboseMessages = AppSettings.Instance.VerboseMessages; returnCode = CodeGen.ProcessTemplate(TemplateFileNameOrPath, Source, CompareTo, OutputPath); } Console.WriteLine("Render of template " + templateFileNameOrDirectoryOption.Value() + " Completed!"); Environment.ExitCode = (int)returnCode.Result; Environment.Exit(Environment.ExitCode); return(Environment.ExitCode); } catch (Exception ex) { Console.WriteLine("Could not render template. " + ex.Message); Console.WriteLine("Stack Trace:"); Console.WriteLine(ex.StackTrace); Environment.ExitCode = (int)ReturnCode.Error; Environment.Exit(Environment.ExitCode); return(Environment.ExitCode); } }); }
public void ConfigDb() { ITemplateInput template = new TemplateInputFileSource(SchemaFileName); var database = template.LoadSchema(Internal.AppSettings.Instance.Configuration); }