Skip to content

Commit 94c42bf

Browse files
committed
Migrate CLI to Spectre.Console and hosting
1 parent 6a2b055 commit 94c42bf

21 files changed

Lines changed: 628 additions & 242 deletions

File tree

Directory.Packages.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
<PackageVersion Include="FluentValidation" Version="12.1.0" />
1313
<PackageVersion Include="Humanizer.Core" Version="3.0.10" />
1414
<PackageVersion Include="Injectio" Version="5.1.0" />
15-
<PackageVersion Include="McMaster.Extensions.CommandLineUtils" Version="5.1.0" />
1615
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="5.3.0" />
1716
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.7" />
1817
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="10.0.7" />
@@ -23,6 +22,7 @@
2322
<PackageVersion Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="10.0.8" />
2423
<PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="10.0.8" />
2524
<PackageVersion Include="Microsoft.Extensions.Configuration.UserSecrets" Version="10.0.8" />
25+
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="10.0.9" />
2626
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.8" />
2727
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="18.6.0" />
2828
<PackageVersion Include="MinVer" Version="7.0.0" />
@@ -40,6 +40,7 @@
4040
<PackageVersion Include="Scrutor" Version="6.1.0" />
4141
<PackageVersion Include="Serilog.Extensions.Hosting" Version="10.0.0" />
4242
<PackageVersion Include="Serilog.Sinks.Console" Version="6.1.1" />
43+
<PackageVersion Include="Spectre.Console.Cli" Version="0.55.0" />
4344
<PackageVersion Include="Swashbuckle.AspNetCore" Version="10.0.1" />
4445
<PackageVersion Include="xunit" Version="2.9.3" />
4546
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.5" />

docs/commands.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ The `initialize` command is used to create the configuration yaml file and optio
1010
Usage: efg initialize [options]
1111

1212
Options:
13-
--help Show help information
14-
-p <Provider> Database provider to reverse engineer
15-
-c <ConnectionString> Database connection string to reverse engineer
16-
--id <UserSecretsId> The user secret ID to use.
17-
--name <ConnectionName> The user secret configuration name.
18-
-d <directory> The root working directory
19-
-f <file> The options file name
13+
-h, --help Show help information
14+
-p, --provider <PROVIDER> Database provider to reverse engineer
15+
-c, --connection-string <CONNECTION_STRING>
16+
Database connection string to reverse engineer
17+
--id <USER_SECRETS_ID> The user secret ID to use.
18+
--name <CONNECTION_NAME> The user secret configuration name.
19+
-d, --directory <DIRECTORY> The root working directory
20+
-f, --file <FILE> The options file name
2021

2122
Example:
2223

@@ -31,15 +32,16 @@ The `generate` command creates source code files from a database schema. Runnin
3132
Usage: efg generate [options]
3233

3334
Options:
34-
--help Show help information
35-
-p <Provider> Database provider to reverse engineer
36-
-c <ConnectionString> Database connection string to reverse engineer
37-
--extensions Include query extensions in generation
38-
--models Include view models in generation
39-
--mapper Include object mapper in generation
40-
--validator Include model validation in generation
41-
-d <directory> The root working directory
42-
-f <file> The options file name
35+
-h, --help Show help information
36+
-p, --provider <PROVIDER> Database provider to reverse engineer
37+
-c, --connection-string <CONNECTION_STRING>
38+
Database connection string to reverse engineer
39+
--extensions Include query extensions in generation
40+
--models Include view models in generation
41+
--mapper Include object mapper in generation
42+
--validator Include model validation in generation
43+
-d, --directory <DIRECTORY> The root working directory
44+
-f, --file <FILE> The options file name
4345

4446
Example:
4547

sample/Tracker/Tracker.Core/Data/Entities/Audit.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ public Audit()
122122
[System.ComponentModel.DataAnnotations.Schema.DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Computed)]
123123
public byte[] RowVersion { get; set; } = null!;
124124

125+
/// <summary>
126+
/// Gets or sets the property value representing column 'Attributes'.
127+
/// </summary>
128+
/// <value>
129+
/// The property value representing column 'Attributes'.
130+
/// </value>
131+
[System.ComponentModel.DataAnnotations.Schema.Column("Attributes", TypeName = "StringList")]
132+
public string? Attributes { get; set; }
133+
125134
#endregion
126135

127136
#region Generated Relationships

sample/Tracker/Tracker.Core/Data/Mapping/AuditMap.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ public void Configure(Microsoft.EntityFrameworkCore.Metadata.Builders.EntityType
8484
.HasColumnType("TIMESTAMP")
8585
.ValueGeneratedOnAddOrUpdate();
8686

87+
builder.Property(t => t.Attributes)
88+
.HasColumnName("Attributes")
89+
.HasColumnType("StringList");
90+
8791
// relationships
8892
#endregion
8993
}
@@ -121,6 +125,8 @@ public readonly struct Columns
121125
public const string UpdatedBy = "UpdatedBy";
122126
/// <summary>Column Name constant for property <see cref="TrackerGenerator.Core.Data.Entities.Audit.RowVersion" /></summary>
123127
public const string RowVersion = "RowVersion";
128+
/// <summary>Column Name constant for property <see cref="TrackerGenerator.Core.Data.Entities.Audit.Attributes" /></summary>
129+
public const string Attributes = "Attributes";
124130
}
125131
#endregion
126132
}

sample/Tracker/Tracker.Core/Domain/Audit/Models/AuditCreateModel.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ public partial class AuditCreateModel
5151
/// </value>
5252
public string Username { get; set; } = null!;
5353

54+
/// <summary>
55+
/// Gets or sets the property value for 'Attributes'.
56+
/// </summary>
57+
/// <value>
58+
/// The property value for 'Attributes'.
59+
/// </value>
60+
public string? Attributes { get; set; }
61+
5462
#endregion
5563

5664
}

sample/Tracker/Tracker.Core/Domain/Audit/Models/AuditReadModel.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ public partial class AuditReadModel
5151
/// </value>
5252
public string Username { get; set; } = null!;
5353

54+
/// <summary>
55+
/// Gets or sets the property value for 'Attributes'.
56+
/// </summary>
57+
/// <value>
58+
/// The property value for 'Attributes'.
59+
/// </value>
60+
public string? Attributes { get; set; }
61+
5462
#endregion
5563

5664
}

sample/Tracker/Tracker.Core/Domain/Audit/Models/AuditUpdateModel.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ public partial class AuditUpdateModel
5151
/// </value>
5252
public string Username { get; set; } = null!;
5353

54+
/// <summary>
55+
/// Gets or sets the property value for 'Attributes'.
56+
/// </summary>
57+
/// <value>
58+
/// The property value for 'Attributes'.
59+
/// </value>
60+
public string? Attributes { get; set; }
61+
5462
#endregion
5563

5664
}

src/EntityFrameworkCore.Generator.Core/ModelGenerator.cs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ public EntityContext Generate(GeneratorOptions options, DatabaseModel databaseMo
5555
entityContext.ContextNamespace = contextNamespace;
5656
entityContext.ContextBaseClass = contextBaseClass;
5757

58-
var tables = databaseModel.Tables;
59-
60-
foreach (var table in tables)
58+
foreach (var table in databaseModel.Tables)
6159
{
6260
if (IsIgnored(table, _options.Database.Exclude.Tables))
6361
{
@@ -74,20 +72,40 @@ public EntityContext Generate(GeneratorOptions options, DatabaseModel databaseMo
7472
GetModels(entity);
7573
}
7674

75+
foreach (var view in databaseModel.Views)
76+
{
77+
if (IsIgnored(view, _options.Database.Exclude.Tables))
78+
{
79+
_logger.LogDebug(" Skipping View : {schema}.{name}", view.Schema, view.Name);
80+
continue;
81+
}
82+
83+
_logger.LogDebug(" Processing View : {schema}.{name}", view.Schema, view.Name);
84+
85+
_options.Variables.Set(VariableConstants.TableSchema, ToLegalName(view.Schema));
86+
_options.Variables.Set(VariableConstants.TableName, ToLegalName(view.Name));
87+
88+
var entity = GetEntity(entityContext, view);
89+
GetModels(entity);
90+
}
91+
7792
_options.Variables.Remove(VariableConstants.TableName);
7893
_options.Variables.Remove(VariableConstants.TableSchema);
7994

8095
return entityContext;
8196
}
8297

8398

84-
private Entity GetEntity(EntityContext entityContext, Table tableSchema, bool processRelationships = true, bool processMethods = true)
99+
private Entity GetEntity(EntityContext entityContext, RelationBase relationSchema, bool processRelationships = true, bool processMethods = true)
85100
{
86-
var entity = entityContext.Entities.ByTable(tableSchema)
87-
?? CreateEntity(entityContext, tableSchema);
101+
var entity = entityContext.Entities.ByTable(relationSchema.Name, relationSchema.Schema)
102+
?? CreateEntity(entityContext, relationSchema);
88103

89104
if (!entity.Properties.IsProcessed)
90-
CreateProperties(entity, tableSchema);
105+
CreateProperties(entity, relationSchema);
106+
107+
if (relationSchema is not Table tableSchema)
108+
return entity;
91109

92110
if (processRelationships && !entity.Relationships.IsProcessed)
93111
CreateRelationships(entityContext, entity, tableSchema);
@@ -744,9 +762,12 @@ private static string ToLegalName(string? name)
744762
}
745763

746764

747-
private static bool IsIgnored(Table table, IEnumerable<MatchOptions> exclude)
765+
private static bool IsIgnored(RelationBase? relation, IEnumerable<MatchOptions> exclude)
748766
{
749-
var name = table.QualifiedName;
767+
if (relation is null)
768+
return true;
769+
770+
var name = relation.QualifiedName;
750771
var includeExpressions = Enumerable.Empty<MatchOptions>();
751772
var excludeExpressions = exclude ?? [];
752773

0 commit comments

Comments
 (0)