Skip to content

Commit

Permalink
DAB: Fix .dacpac support & add UiHint
Browse files Browse the repository at this point in the history
fixes #2439
  • Loading branch information
ErikEJ committed Jul 30, 2024
1 parent ed9c4a2 commit db4891d
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 17 deletions.
5 changes: 2 additions & 3 deletions src/Core/RevEng.Core.80/DataApiBuilderBuilder/DabBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public string GetDabConfigCmdFile()
{
case DatabaseType.Undefined:
break;
case DatabaseType.SQLServerDacpac:
case DatabaseType.SQLServer:
databaseType = "mssql";
break;
Expand All @@ -66,8 +67,6 @@ public string GetDabConfigCmdFile()
break;
case DatabaseType.Oracle:
break;
case DatabaseType.SQLServerDacpac:
break;
case DatabaseType.Firebird:
break;
default:
Expand Down Expand Up @@ -228,7 +227,7 @@ private DatabaseModel GetModelInternal()

var dbModelOptions = new DatabaseModelFactoryOptions(options.Tables.Where(t => t.ObjectType.HasColumns()).Select(m => m.Name), null);

var dbModel = dbModelFactory!.Create(options.ConnectionString, dbModelOptions);
var dbModel = dbModelFactory!.Create(options.Dacpac ?? options.ConnectionString, dbModelOptions);

return dbModel;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Core/efreveng60/TestFiles/dabtest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"ConnectionString": "Data Source=.\\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True;Encrypt=false",
"Dacpac": null,
"DatabaseType": 3,
"Dacpac": "C:\\Code\\Github\\EFCorePowerTools\\src\\Core\\NUnitTestCore\\Dacpac\\NorthwindViews.dacpac",
"DatabaseType": 8,
"ProjectPath": "C:\\Code\\Github\\EFCorePowerTools\\test\\ScaffoldingTester\\ScaffoldingTester5",
"Tables": [
{
Expand Down Expand Up @@ -145,4 +145,4 @@
"ObjectType": 2
}
]
}
}
37 changes: 30 additions & 7 deletions src/GUI/Shared/Handlers/DabBuilder/DabBuilderHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public async System.Threading.Tasks.Task BuildDabConfigAsync(Project project)
return;
}

var optionsPath = Path.Combine(Path.GetDirectoryName(project.FullPath), "dab-options.json");
var projectPath = Path.GetDirectoryName(project.FullPath);

var optionsPath = Path.Combine(projectPath, "dab-options.json");

var options = DataApiBuilderOptionsExtensions.TryRead(optionsPath);

Expand All @@ -53,11 +55,18 @@ public async System.Threading.Tasks.Task BuildDabConfigAsync(Project project)
options = new DataApiBuilderOptions();
}

var userOptions = ReverseEngineerUserOptionsExtensions.TryRead(optionsPath, projectPath);

if (userOptions == null)
{
userOptions = new ReverseEngineerUserOptions();
}

options.ProjectPath = Path.GetDirectoryName(project.FullPath);

DatabaseConnectionModel dbInfo = null;

if (!await ChooseDataBaseConnectionAsync(options))
if (!await ChooseDataBaseConnectionAsync(options, userOptions))
{
await VS.StatusBar.ClearAsync();
return;
Expand All @@ -82,12 +91,12 @@ public async System.Threading.Tasks.Task BuildDabConfigAsync(Project project)
}

options.ConnectionString = dbInfo.ConnectionString;
await SaveOptionsAsync(project, optionsPath, options);
await SaveOptionsAsync(project, optionsPath, options, userOptions);

await GenerateFilesAsync(optionsPath);

options.ConnectionString = null;
await SaveOptionsAsync(project, optionsPath, options);
await SaveOptionsAsync(project, optionsPath, options, userOptions);

Telemetry.TrackEvent("PowerTools.DabBuild");
}
Expand All @@ -104,7 +113,7 @@ public async System.Threading.Tasks.Task BuildDabConfigAsync(Project project)
}
}

private async Task<bool> ChooseDataBaseConnectionAsync(DataApiBuilderOptions options)
private async Task<bool> ChooseDataBaseConnectionAsync(DataApiBuilderOptions options, ReverseEngineerUserOptions userOptions)
{
var databaseList = await vsDataHelper.GetDataConnectionsAsync(package);

Expand Down Expand Up @@ -140,6 +149,11 @@ private async Task<bool> ChooseDataBaseConnectionAsync(DataApiBuilderOptions opt
new CodeGenerationItem { Key = (int)CodeGenerationMode.EFCore8, Value = "DAB" },
});

if (!string.IsNullOrEmpty(userOptions.UiHint))
{
psd.PublishUiHint(userOptions.UiHint);
}

psd.PublishSchemas(new List<SchemaInfo>());

var pickDataSourceResult = psd.ShowAndAwaitUserResponse(true);
Expand All @@ -149,6 +163,7 @@ private async Task<bool> ChooseDataBaseConnectionAsync(DataApiBuilderOptions opt
}

options.Dacpac = pickDataSourceResult.Payload.Connection?.FilePath;
userOptions.UiHint = pickDataSourceResult.Payload.UiHint;

if (pickDataSourceResult.Payload.Connection != null)
{
Expand Down Expand Up @@ -263,14 +278,17 @@ private async System.Threading.Tasks.Task GenerateFilesAsync(string optionsPath)

stopWatch.Stop();

await VS.Documents.OpenAsync(cmdPath);
if (File.Exists(cmdPath))
{
await VS.Documents.OpenAsync(cmdPath);
}

await VS.StatusBar.ShowMessageAsync(string.Format(ReverseEngineerLocale.ReverseEngineerCompleted, stopWatch.Elapsed.ToString(@"mm\:ss")));

Telemetry.TrackFrameworkUse(nameof(DabBuilderHandler), CodeGenerationMode.EFCore8);
}

private async System.Threading.Tasks.Task SaveOptionsAsync(Project project, string optionsPath, DataApiBuilderOptions options)
private async System.Threading.Tasks.Task SaveOptionsAsync(Project project, string optionsPath, DataApiBuilderOptions options, ReverseEngineerUserOptions userOptions)
{
if (File.Exists(optionsPath) && File.GetAttributes(optionsPath).HasFlag(FileAttributes.ReadOnly))
{
Expand All @@ -280,6 +298,11 @@ private async System.Threading.Tasks.Task SaveOptionsAsync(Project project, stri

if (!File.Exists(optionsPath + ".ignore"))
{
if (userOptions != null && !string.IsNullOrEmpty(userOptions.UiHint))
{
File.WriteAllText(optionsPath + ".user", userOptions.Write(Path.GetDirectoryName(project.FullPath)), Encoding.UTF8);
}

File.WriteAllText(optionsPath, options.Write(), Encoding.UTF8);

await project.AddExistingFilesAsync(new List<string> { optionsPath }.ToArray());
Expand Down
Binary file modified src/GUI/lib/efreveng80.exe.zip
Binary file not shown.
3 changes: 2 additions & 1 deletion test/Ef7Playground/Ef7Playground/dab-config.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dab init -c dab-config.json --database-type mssql --connection-string "@env('dab
@echo Adding tables
dab add "Album" --source "[dbo].[Album]" --fields.include "AlbumId,Title,ArtistId" --permissions "anonymous:*"
dab add "Artist" --source "[dbo].[Artist]" --fields.include "ArtistId,Name" --permissions "anonymous:*"
dab add "Customer" --source "[dbo].[Customer]" --fields.include "CustomerId,FirstName,LastName,Company,Address,City,State,Country,PostalCode,Phone,Fax,Email,SupportRepId" --permissions "anonymous:*"
dab add "Customer" --source "[dbo].[Customer]" --fields.include "CustomerId,FirstName,LastName,Company,Address,City,State,Country,SupportRepId" --permissions "anonymous:*"
dab add "Employee" --source "[dbo].[Employee]" --fields.include "EmployeeId,LastName,FirstName,Title,ReportsTo,BirthDate,HireDate,Address,City,State,Country,PostalCode,Phone,Fax,Email" --permissions "anonymous:*"
dab add "Genre" --source "[dbo].[Genre]" --fields.include "GenreId,Name" --permissions "anonymous:*"
dab add "Invoice" --source "[dbo].[Invoice]" --fields.include "InvoiceId,CustomerId,InvoiceDate,BillingAddress,BillingCity,BillingState,BillingCountry,BillingPostalCode,Total" --permissions "anonymous:*"
Expand Down Expand Up @@ -42,6 +42,7 @@ dab update Track --relationship Genre --target.entity Genre --cardinality one
dab update Genre --relationship Track --target.entity Track --cardinality many
dab update Track --relationship MediaType --target.entity MediaType --cardinality one
dab update MediaType --relationship Track --target.entity Track --cardinality many
@echo Adding stored procedures
@echo **
@echo ** run 'dab validate' to validate your configuration **
@echo ** run 'dab start' to start the development API host **
6 changes: 3 additions & 3 deletions test/Ef7Playground/Ef7Playground/dab-options.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"ConnectionString": null,
"Dacpac": null,
"DatabaseType": 3,
"ConnectionString": "Data Source=(local);Initial Catalog=Chinook;Integrated Security=true;",
"Dacpac": "C:\\Code\\Github\\EFCorePowerTools\\src\\Core\\NUnitTestCore\\Dacpac\\Chinook.dacpac",
"DatabaseType": 8,
"ProjectPath": "C:\\Code\\Github\\EFCorePowerTools\\test\\Ef7Playground\\Ef7Playground",
"Tables": [
{
Expand Down

0 comments on commit db4891d

Please sign in to comment.