diff --git a/I18N Commander/DataModel/Setup.cs b/I18N Commander/DataModel/Setup.cs index ebbd986..d36d5be 100644 --- a/I18N Commander/DataModel/Setup.cs +++ b/I18N Commander/DataModel/Setup.cs @@ -1,9 +1,9 @@ -using DataModel.Database; +using System.Diagnostics; +using DataModel.Database; using DataModel.Database.Common; using DataModel.MigrationScripts; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; namespace DataModel; @@ -13,12 +13,10 @@ public static class Setup private const string DB_READ_WRITE_CREATE_MODE = "ReadWriteCreate"; private static string USED_DATA_FILE = string.Empty; - private static SetupMaintenance SETUP_MAINTENANCE = new(); + public static SetupMaintenance SETUP_MAINTENANCE = new(); public static string DataFile => Setup.USED_DATA_FILE; - public static SetupMaintenance Maintenance => Setup.SETUP_MAINTENANCE; - /// /// Tries to migrate the database. /// @@ -44,15 +42,18 @@ public static class Setup /// public static async Task ImportDataAndAddDatabase(this IServiceCollection serviceCollection, string path2JSONFile) { + Console.WriteLine($"Importing the data from the JSON file '{path2JSONFile}' into a new database."); var tempPath = Path.GetTempFileName(); - Setup.USED_DATA_FILE = tempPath; - Setup.SETUP_MAINTENANCE = new(path2JSONFile, true); + Console.WriteLine($"The temporary database file is: {tempPath}"); serviceCollection.AddDbContext(options => options.UseSqlite($"Filename={tempPath};Mode={DB_READ_WRITE_CREATE_MODE};"), ServiceLifetime.Transient); // Get the database service: await using var serviceProvider = serviceCollection.BuildServiceProvider(); var dbContext = serviceProvider.GetRequiredService(); + + Setup.USED_DATA_FILE = tempPath; + Setup.SETUP_MAINTENANCE = new(tempPath, true); // Migrate the database to create the tables etc.: await Setup.PerformDataMigration(dbContext); @@ -128,17 +129,29 @@ public static class Setup return dbContext; } - public readonly record struct SetupMaintenance(string PathToDataFile = "", bool RemoveTempDatabaseAfterwards = false) : IDisposable { public void Dispose() { if (!this.RemoveTempDatabaseAfterwards) return; - + + Console.WriteLine("Removing the temporary database file..."); try { - File.Delete(this.PathToDataFile); + var process = new Process + { + StartInfo = new() + { + FileName = "cmd.exe", + Arguments = $@"/C del /Q /F ""{Setup.SETUP_MAINTENANCE.PathToDataFile}""", + UseShellExecute = false, + CreateNoWindow = true, + } + }; + + process.Start(); + Console.WriteLine($"The temporary database file '{this.PathToDataFile}' has been removed."); } catch(Exception e) { diff --git a/I18N Commander/Processor/Version.cs b/I18N Commander/Processor/Version.cs index d6dce1e..6de87ef 100644 --- a/I18N Commander/Processor/Version.cs +++ b/I18N Commander/Processor/Version.cs @@ -2,5 +2,5 @@ public static class Version { - public static string Text => $"v0.8.2 (2023-02-11), .NET {Environment.Version}"; + public static string Text => $"v0.8.3 (2023-02-11), .NET {Environment.Version}"; } \ No newline at end of file diff --git a/I18N Commander/UI WinForms/Program.cs b/I18N Commander/UI WinForms/Program.cs index 3bedb40..68f2106 100644 --- a/I18N Commander/UI WinForms/Program.cs +++ b/I18N Commander/UI WinForms/Program.cs @@ -60,30 +60,31 @@ internal static class Program } }); - // Tear down the setup: - using var setupMaintenance = Setup.Maintenance; - // Get the host out of the DI system: - var host = builder.Build(); - - // Create a service scope: - using (var scope = host.Services.CreateScope()) + using (var host = builder.Build()) { - // Get a service provider: - SERVICE_PROVIDER = scope.ServiceProvider; - - // Set the service provider to the processor: - ProcessorMeta.ServiceProvider = SERVICE_PROVIDER; - - // Apply database migrations: - using (var database = SERVICE_PROVIDER.GetRequiredService()) - Setup.PerformDataMigration(database).Wait(); - - // Start the app: - do + // Create a service scope: + using (var scope = host.Services.CreateScope()) { - Application.Run(new Main()); - } while (Program.RestartMainApp); + // Get a service provider: + SERVICE_PROVIDER = scope.ServiceProvider; + + // Set the service provider to the processor: + ProcessorMeta.ServiceProvider = SERVICE_PROVIDER; + + // Apply database migrations: + using (var database = SERVICE_PROVIDER.GetRequiredService()) + Setup.PerformDataMigration(database).Wait(); + + // Start the app: + do + { + Application.Run(new Main()); + } while (Program.RestartMainApp); + } } + + // Tear down the setup: + AppDomain.CurrentDomain.ProcessExit += (sender, eventArgs) => Setup.SETUP_MAINTENANCE.Dispose(); } } \ No newline at end of file