Skip to content

Commit

Permalink
DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikEJ committed Dec 29, 2023
1 parent 8b2b73a commit a085a7b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 111 deletions.
60 changes: 15 additions & 45 deletions src/GUI/RevEng.Core.60/ServiceProviderBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,62 +98,32 @@ public static IServiceCollection AddEfpt(this IServiceCollection serviceCollecti
switch (options.DatabaseType)
{
case DatabaseType.SQLServer:
case DatabaseType.SQLServerDacpac:
var provider = new SqlServerDesignTimeServices();
provider.ConfigureDesignTimeServices(serviceCollection);

if (options.DatabaseType == DatabaseType.SQLServer)
{
#if !CORE80
serviceCollection.AddSingleton<IDatabaseModelFactory, PatchedSqlServerDatabaseModelFactory>();
serviceCollection.AddSingleton<IDatabaseModelFactory, PatchedSqlServerDatabaseModelFactory>();
#endif
serviceCollection.AddSqlServerStoredProcedureDesignTimeServices();
serviceCollection.AddSqlServerFunctionDesignTimeServices();

if (options.UseSpatial)
{
var spatial = new SqlServerNetTopologySuiteDesignTimeServices();
spatial.ConfigureDesignTimeServices(serviceCollection);
serviceCollection.AddSqlServerStoredProcedureDesignTimeServices();
serviceCollection.AddSqlServerFunctionDesignTimeServices();
}

if (options.UseHierarchyId)
if (options.DatabaseType == DatabaseType.SQLServerDacpac)
{
var hierachyId = new SqlServerHierarchyIdDesignTimeServices();
hierachyId.ConfigureDesignTimeServices(serviceCollection);
}
serviceCollection.AddSingleton<IDatabaseModelFactory, SqlServerDacpacDatabaseModelFactory>(
provider => new SqlServerDacpacDatabaseModelFactory(new SqlServerDacpacDatabaseModelFactoryOptions
{
MergeDacpacs = options.MergeDacpacs,
}));

if (options.UseNodaTime)
{
var nodaTime = new SqlServerNodaTimeDesignTimeServices();
nodaTime.ConfigureDesignTimeServices(serviceCollection);
}
#if CORE80
serviceCollection.AddSingleton<IRelationalTypeMappingSource, SqlServerTypeMappingSource>(
provider => new RevEng.Core.SqlServerTypeMappingSource(
provider.GetService<TypeMappingSourceDependencies>(),
provider.GetService<RelationalTypeMappingSourceDependencies>(),
options.UseDateOnlyTimeOnly));
#endif
#if !CORE80
if (options.UseDateOnlyTimeOnly)
{
var dateOnlyTimeOnly = new SqlServerDateOnlyTimeOnlyDesignTimeServices();
dateOnlyTimeOnly.ConfigureDesignTimeServices(serviceCollection);
}
#endif
break;

case DatabaseType.SQLServerDacpac:
var dacProvider = new SqlServerDesignTimeServices();
dacProvider.ConfigureDesignTimeServices(serviceCollection);

serviceCollection.AddSingleton<IDatabaseModelFactory, SqlServerDacpacDatabaseModelFactory>(
provider => new SqlServerDacpacDatabaseModelFactory(new SqlServerDacpacDatabaseModelFactoryOptions
serviceCollection.AddSqlServerDacpacStoredProcedureDesignTimeServices(new SqlServerDacpacDatabaseModelFactoryOptions
{
MergeDacpacs = options.MergeDacpacs,
}));

serviceCollection.AddSqlServerDacpacStoredProcedureDesignTimeServices(new SqlServerDacpacDatabaseModelFactoryOptions
{
MergeDacpacs = options.MergeDacpacs,
});
});
}

if (options.UseSpatial)
{
Expand Down
71 changes: 39 additions & 32 deletions src/GUI/Shared/Handlers/ProcessLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,44 @@ public ProcessLauncher(Project project)
this.project = project;
}

public static async Task<string> RunProcessAsync(ProcessStartInfo startInfo)
{
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.CreateNoWindow = true;
startInfo.StandardOutputEncoding = Encoding.UTF8;
startInfo.StandardErrorEncoding = Encoding.UTF8;

var standardOutput = new StringBuilder();
var error = string.Empty;
using (var process = Process.Start(startInfo))
{
while (process != null && !process.HasExited)
{
standardOutput.Append(await process.StandardOutput.ReadToEndAsync());
}

if (process != null)
{
standardOutput.Append(await process.StandardOutput.ReadToEndAsync());
}

if (process != null)
{
error = await process.StandardError.ReadToEndAsync();
}
}

var result = standardOutput.ToString();
if (string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(error))
{
result = "Error:" + Environment.NewLine + error;
}

return result;
}

public Task<string> GetOutputAsync(string outputPath, string projectPath, GenerationType generationType, string contextName, string migrationIdentifier, string nameSpace)
{
return GetOutputInternalAsync(outputPath, projectPath, generationType, contextName, migrationIdentifier, nameSpace);
Expand Down Expand Up @@ -108,11 +146,6 @@ private async Task<string> GetOutputInternalAsync(string outputPath, string proj
{
FileName = Path.Combine(Path.GetDirectoryName(launchPath) ?? throw new InvalidOperationException(), "efpt.exe"),
Arguments = "\"" + outputPath + "\"",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
StandardOutputEncoding = Encoding.UTF8,
};

var outputs = " \"" + outputPath + "\" \"" + startupOutputPath + "\" ";
Expand Down Expand Up @@ -201,33 +234,7 @@ private async Task<string> GetOutputInternalAsync(string outputPath, string proj
// Ignore
}

var standardOutput = new StringBuilder();
var error = string.Empty;
using (var process = Process.Start(startInfo))
{
while (process != null && !process.HasExited)
{
standardOutput.Append(await process.StandardOutput.ReadToEndAsync());
}

if (process != null)
{
standardOutput.Append(await process.StandardOutput.ReadToEndAsync());
}

if (process != null)
{
error = await process.StandardError.ReadToEndAsync();
}
}

var result = standardOutput.ToString();
if (string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(error))
{
result = "Error:" + Environment.NewLine + error;
}

return result;
return await RunProcessAsync(startInfo);
}

private async Task<string> DropNetCoreFilesAsync()
Expand Down
35 changes: 1 addition & 34 deletions src/GUI/Shared/Handlers/ReverseEngineer/EfRevEngLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,40 +150,7 @@ public async Task<string> GetDgmlAsync(string connectionString, DatabaseType dat

private static async Task<string> RunProcessAsync(ProcessStartInfo startInfo)
{
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.CreateNoWindow = true;
startInfo.StandardOutputEncoding = Encoding.UTF8;
var standardOutput = new StringBuilder();
var error = string.Empty;

using (var process = Process.Start(startInfo))
{
while (process != null && !process.HasExited)
{
standardOutput.Append(await process.StandardOutput.ReadToEndAsync());
}

if (process != null)
{
standardOutput.Append(await process.StandardOutput.ReadToEndAsync());
}

if (process != null)
{
error = await process.StandardError.ReadToEndAsync();
}
}

var result = standardOutput.ToString();

if (string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(error))
{
result = "Error:" + Environment.NewLine + error;
}

return result;
return await ProcessLauncher.RunProcessAsync(startInfo);
}

private async Task<string> GetDgmlInternalAsync(string arguments)
Expand Down

0 comments on commit a085a7b

Please sign in to comment.