public void Generate(CommandLineConfiguration cliConfig, VSProjectMetadata projectMetadata, Database database) { var entitiesDirectory = new DirectoryInfo($"{projectMetadata.BasePath}/{cliConfig.EntitiesFolder}"); var shouldGenerate = false; if (entitiesDirectory.Exists) { //Entities have already been created or files exist in the target folder. if (DirectoryHasFiles(entitiesDirectory.FullName)) { if (!cliConfig.ForceRecreate) { throw new CodeGenerationException("To be able to run the generation routine when you have previously generated code, you must set ForceRecreate to true or your target directories must be empty."); } else { shouldGenerate = true; //Delete existing files. foreach (var file in entitiesDirectory.EnumerateFiles()) { file.Delete(); } } } else { shouldGenerate = true; } } else { Directory.CreateDirectory($"{projectMetadata.BasePath}/{cliConfig.EntitiesFolder}"); shouldGenerate = true; } if (shouldGenerate) { foreach (var tableMeta in database.Tables) { var entityCode = GenerateEntity(cliConfig.EntitiesFolder, tableMeta, projectMetadata); WriteEntityFileToDisk($"{projectMetadata.BasePath}/{cliConfig.EntitiesFolder}/{tableMeta.FormattedTableName}.cs", entityCode); } } }
private string GenerateEntity(string entitiesFolder, Table tableMetadata, VSProjectMetadata projectMetadata) { var compilationUnit = SyntaxFactory.CompilationUnit(); var usings = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("System")); compilationUnit = compilationUnit.AddUsings(usings); var entityNamespace = SyntaxFactory.NamespaceDeclaration(SyntaxFactory.ParseName($"{projectMetadata.DefaultNamespace}.{entitiesFolder}")).NormalizeWhitespace(); var entityClass = SyntaxFactory.ClassDeclaration(tableMetadata.FormattedTableName) .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword)); foreach (var columnMeta in tableMetadata.Columns) { var columnType = columnMeta.Type.CSharpTypeString; var columnName = columnMeta.FormattedColumnName; var databaseType = columnMeta.Type.DatabaseType; var property = SyntaxFactory.ParseMemberDeclaration($"public {columnType} {columnName} {{ get; set; }}"); if (property != null && !string.IsNullOrWhiteSpace(columnType)) { entityClass = entityClass.AddMembers(property); } else { Console.WriteLine("Skipping Column Generation"); Console.WriteLine($"Table Column belongs to: {tableMetadata.TableName}. Column: {columnName}. Database type {databaseType} not mapped."); } } entityNamespace = entityNamespace.AddMembers(entityClass); compilationUnit = compilationUnit.AddMembers(entityNamespace); var code = compilationUnit .NormalizeWhitespace() .ToFullString(); return(code); }
public void Generate(CommandLineConfiguration cliConfig, VSProjectMetadata projectMetadata, Database database) { var mapperStringBuilder = new StringBuilder(); mapperStringBuilder.AppendLine(UsingStatements(projectMetadata.DefaultNamespace)); mapperStringBuilder.AppendLine(); mapperStringBuilder.AppendLine($"namespace {projectMetadata.DefaultNamespace}"); mapperStringBuilder.AppendLine("{"); mapperStringBuilder.AppendLine($" public static class {database.FormattedName}Mappers"); mapperStringBuilder.AppendLine(" {"); mapperStringBuilder.AppendLine(" public static void Setup()"); mapperStringBuilder.AppendLine(" {"); mapperStringBuilder.AppendLine(" SetTableMappers();"); mapperStringBuilder.AppendLine(" }"); mapperStringBuilder.AppendLine(); mapperStringBuilder.AppendLine(" private static void SetTableMappers()"); mapperStringBuilder.AppendLine(" {"); foreach (var table in database.Tables) { mapperStringBuilder.AppendLine(@$ " FluentMapper.Entity<{table.FormattedTableName}>().Table(" "{table.TableName}" ")"); //If no valid type mapping is available, don't generate. var columnsWithValidTypeMappings = table.Columns.Where(c => !string.IsNullOrWhiteSpace(c.Type.CSharpTypeString)); var lastColumnInList = columnsWithValidTypeMappings.Last(); foreach (var column in columnsWithValidTypeMappings) { if (column.IsPrimaryKey) { mapperStringBuilder.AppendLine($@" .Primary(c => c.{column.FormattedColumnName})"); } if (column.IsIdentity) { mapperStringBuilder.AppendLine($@" .Identity(c => c.{column.FormattedColumnName})"); } if (column.ColumnName == lastColumnInList.ColumnName) { mapperStringBuilder.Append($@" .Column(c => c.{column.FormattedColumnName}, ""{column.ColumnName}"")"); mapperStringBuilder.AppendLine(";"); mapperStringBuilder.AppendLine(); } else { mapperStringBuilder.AppendLine($@" .Column(c => c.{column.FormattedColumnName}, ""{column.ColumnName}"")"); } } } mapperStringBuilder.AppendLine(" }"); mapperStringBuilder.AppendLine(" }"); mapperStringBuilder.AppendLine("}"); var filePath = $"{projectMetadata.BasePath}/{database.FormattedName}Mappers.cs"; if (File.Exists(filePath)) { if (cliConfig.ForceRecreate) { WriteCodeFileToDisk(filePath, mapperStringBuilder.ToString()); } else { throw new CodeGenerationException("To be able to run the generation routine when you have previously generated code, you must set ForceRecreate to true or your target directories must be empty."); } } else { WriteCodeFileToDisk(filePath, mapperStringBuilder.ToString()); } }