Skip to content

Commit

Permalink
Remove unneeded namespace, and add non-interactive mode which will fo…
Browse files Browse the repository at this point in the history
…rce overwrite the output
  • Loading branch information
prom3theu5 committed Jan 9, 2024
1 parent ddd9325 commit ede3c6d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 30 deletions.
31 changes: 21 additions & 10 deletions src/SimCube.DapperGenerator/Commands/Generate/GenerateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,7 @@ public override async Task<int> ExecuteAsync(CommandContext context, GenerateSet

var rootOutputDirectory = Path.Combine(Directory.GetCurrentDirectory(), settings.OutputDirectory);

if (Directory.Exists(rootOutputDirectory))
{
var prompt = $"[red]Directory '{rootOutputDirectory}' already exists, do you want to remove it first?[/]";
var clean = console.Confirm(prompt);

if (clean)
{
Directory.Delete(rootOutputDirectory, true);
}
}
HandleExistingDirectory(rootOutputDirectory, settings);

var generator = scope.ServiceProvider.GetRequiredKeyedService<ISourceGenerator>(GeneratorType.Poco.Name);

Expand Down Expand Up @@ -68,6 +59,26 @@ public override async Task<int> ExecuteAsync(CommandContext context, GenerateSet
return 0;
}

private void HandleExistingDirectory(string rootOutputDirectory, GenerateSettings input)
{
if (input.NonInteractive)
{
Directory.Delete(rootOutputDirectory, true);
return;
}

if (Directory.Exists(rootOutputDirectory))
{
var prompt = $"[red]Directory '{rootOutputDirectory}' already exists, do you want to remove it first?[/]";
var clean = console.Confirm(prompt);

if (clean)
{
Directory.Delete(rootOutputDirectory, true);
}
}
}

private static IReadOnlyCollection<TableInfo>? ExtractData(GenerateSettings settings, IDatabaseProvider provider)
{
IReadOnlyCollection<TableInfo> data = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ public sealed class GenerateSettings : CommandSettings
[CommandOption("--include-json")]
[Description("Include json property names when generating pocos.")]
public bool IncludeJsonProperties { get; set; }

[CommandOption("--non-interactive")]
[Description("Disable interactive mode. This will overwrite files without prompting.")]
public bool NonInteractive { get; set; }
}
58 changes: 38 additions & 20 deletions src/SimCube.DapperGenerator/Generators/DapperPocoSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,7 @@ protected override string GenClass(TableInfo properties, string className, Gener
{
_sourceBuilder.Clear();

_sourceBuilder.AppendLine("using System;");

if (config.IncludeJsonProperties)
{
_sourceBuilder.AppendLine("using System.Text.Json.Serialization;");
}

foreach (var relationship in properties.Columns.SelectMany(column => column.Relationship))
{
_sourceBuilder.AppendLine($"using {config.Namespace.Pascalize()}.{relationship.Schema.Pascalize()}.Entities;");
}

_sourceBuilder.AppendLine($"using {config.Namespace.Pascalize()}.{properties.Schema.Pascalize()}.Entities;");
AddImports(properties, config);

_sourceBuilder.AppendLine();
_sourceBuilder.AppendLine($"namespace {config.Namespace.Pascalize()}.{properties.Schema.Pascalize()}.Entities;");
Expand All @@ -42,6 +30,43 @@ protected override string GenClass(TableInfo properties, string className, Gener
_sourceBuilder.AppendLine($"public class {className}");
_sourceBuilder.AppendLine("{");

AddEachColumnAsProperty(properties, config);

RemoveLastLineBreak();

_sourceBuilder.AppendLine("}");
_sourceBuilder.AppendLine();

return _sourceBuilder.ToString();
}

private void AddImports(TableInfo properties, GenerateSettings config)
{
_sourceBuilder.AppendLine("using System;");

if (config.IncludeJsonProperties)
{
_sourceBuilder.AppendLine("using System.Text.Json.Serialization;");
}

AddRelatedNamespaceImports(properties, config);
}

private void AddRelatedNamespaceImports(TableInfo properties, GenerateSettings config)
{
foreach (var schema in properties.Columns.Where(x => x.Relationship is not null).SelectMany(column => column.Relationship).Select(x => x.Schema).ToHashSet())
{
if (schema.Equals(properties.Schema, StringComparison.OrdinalIgnoreCase))
{
continue;
}

_sourceBuilder.AppendLine($"using {config.Namespace.Pascalize()}.{schema.Pascalize()}.Entities;");
}
}

private void AddEachColumnAsProperty(TableInfo properties, GenerateSettings config)
{
foreach (var column in properties.Columns)
{
if (config.IncludeJsonProperties)
Expand All @@ -68,13 +93,6 @@ protected override string GenClass(TableInfo properties, string className, Gener

AppendRelationships(properties, column, config);
}

RemoveLastLineBreak();

_sourceBuilder.AppendLine("}");
_sourceBuilder.AppendLine();

return _sourceBuilder.ToString();
}

private void AppendRelationships(TableInfo properties, ColumnInfo column, GenerateSettings config)
Expand Down

0 comments on commit ede3c6d

Please sign in to comment.