From 7a6537d52998fcbe0f318882ce3e3d3d155cf539 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Fri, 29 Sep 2023 13:14:39 +0200 Subject: [PATCH] Config: add MigrateAppConfig --- Config/ConfigUtils.vb | 205 ++++++++++++++++++++++++------------------ 1 file changed, 120 insertions(+), 85 deletions(-) diff --git a/Config/ConfigUtils.vb b/Config/ConfigUtils.vb index 2de1f66b..e9fce55a 100644 --- a/Config/ConfigUtils.vb +++ b/Config/ConfigUtils.vb @@ -1,4 +1,5 @@ -Imports DigitalData.Modules.Base +Imports System.IO +Imports DigitalData.Modules.Base Imports DigitalData.Modules.Logging Public Class ConfigUtils @@ -6,6 +7,7 @@ Public Class ConfigUtils Private _File As FilesystemEx Private Const MIGRATE_DIRECTORY As String = "Migrate" + Private Const USER_CONFIG As String = "UserConfig.xml" Public Sub New(LogConfig As LogConfig) @@ -21,98 +23,131 @@ Public Class ConfigUtils End If End Function - Public Sub MigrateConfig(SourceDirectory As String, TargetDirectory As String, Optional FilePattern As String = "*.*") - If IO.Directory.Exists(TargetDirectory) Then - _Logger.Warn("Config Migration aborted because new config directory [{0}] already exists!", TargetDirectory) - Exit Sub + Public Function MigrateAppDataConfig(pUserAppDataPath As String, pProductName As String, pOldProductName As String) + Dim oNewDirPath = pUserAppDataPath + Dim oOldDirPath = oNewDirPath.Replace(pProductName, pOldProductName) + Dim oNewFilePath = Path.Combine(oNewDirPath, USER_CONFIG) + Dim oOldFilePath = Path.Combine(oOldDirPath, USER_CONFIG) + + ' If there is already a new config, exit. + If File.Exists(oNewFilePath) Then + Return True End If - _Logger.Debug("Creating TargetDirectory [{0}]", TargetDirectory) - ' Create target directory - Try - IO.Directory.CreateDirectory(TargetDirectory) - Catch ex As Exception - _Logger.Warn("Config Migration aborted because new config directory [{0}] could not be created!", TargetDirectory) - _Logger.Error(ex) - Exit Sub - End Try + ' If there is no old config, exit. + If Not File.Exists(oOldFilePath) Then + Return True + End If - ' Create Migration directory - Dim oMigrationDirectory = IO.Path.Combine(SourceDirectory, MIGRATE_DIRECTORY) - _Logger.Debug("Creating MigrationDirectory [{0}]", oMigrationDirectory) Try - IO.Directory.CreateDirectory(oMigrationDirectory) + If Not Directory.Exists(oNewDirPath) Then + Directory.CreateDirectory(oNewDirPath) + End If + + _Logger.Info("Migrating Config from [{0}] to [{1}]", pOldProductName, pProductName) + File.Copy(oOldFilePath, oNewFilePath) + Return True + Catch ex As Exception - _Logger.Warn("Config Migration aborted because migration directory [{0}] could not be created!", oMigrationDirectory) + _Logger.Warn("Error while Migrating Config") _Logger.Error(ex) - Exit Sub - End Try + Return False - ' Copy individual files from top level directory - For Each oPath In IO.Directory.EnumerateFiles(SourceDirectory, FilePattern) - Dim oFileInfo = New IO.FileInfo(oPath) - - _Logger.Debug("Processing file [{0}]", oFileInfo.Name) - - _Logger.Debug("Copying [{0}] to TargetDirectory..", oFileInfo.Name) - ' Copy to target directory - Try - IO.File.Copy(oPath, IO.Path.Combine(TargetDirectory, oFileInfo.Name)) - Catch ex As Exception - _Logger.Warn("Could not move old config file {0} to new config location {1}", oFileInfo.Name, TargetDirectory) - _Logger.Error(ex) - End Try - - _Logger.Debug("Moving [{0}] to MigrationDirectory..", oFileInfo.Name) - ' Move to migration directory - Try - IO.File.Move(oPath, IO.Path.Combine(oMigrationDirectory, oFileInfo.Name)) - Catch ex As Exception - _Logger.Warn("Could not move old config file {0} to migration directory {1}", oFileInfo.Name, oMigrationDirectory) - _Logger.Error(ex) - End Try - Next - - For Each oDirectoryPath In IO.Directory.EnumerateDirectories(SourceDirectory, "*", IO.SearchOption.TopDirectoryOnly) - Dim oDirInfo As New IO.DirectoryInfo(oDirectoryPath) - - _Logger.Debug("Processing directory [{0}]", oDirInfo.Name) - - ' Don't copy TargetDirectory if subpath of SourceDirectory or if MigrationDirectory - If oDirInfo.FullName = TargetDirectory Or oDirInfo.FullName = oMigrationDirectory Then - _Logger.Debug("Directory [{0}] should not be copied. Skipping.", oDirInfo.Name) - Continue For - End If + End Try + End Function - ' Copy directory to TargetDirectory - Dim oNewDirectoryPath = IO.Path.Combine(TargetDirectory, oDirInfo.Name) - _Logger.Debug("Copying [{0}] to TargetDirectory..", oDirInfo.Name) - Try - _File.CopyDirectory(oDirInfo.FullName, oNewDirectoryPath, True) - Catch ex As Exception - _Logger.Warn("Could not move directory [{0}] to new path [{1}]", oDirInfo.FullName, oNewDirectoryPath) - _Logger.Error(ex) - End Try - - _Logger.Debug("Copying [{0}] to MigrationDirectory..", oDirInfo.Name) - ' Copy directory to MigrationDirectory - Dim oMigrationDirectoryPath = IO.Path.Combine(oMigrationDirectory, oDirInfo.Name) - Try - _File.CopyDirectory(oDirInfo.FullName, oMigrationDirectoryPath, True) - Catch ex As Exception - _Logger.Error(ex) - _Logger.Warn("Could not move directory [{0}] to migration directory [{1}]", oDirInfo.FullName, oMigrationDirectoryPath) - End Try - - _Logger.Debug("Deleting [{0}]..", oDirInfo.Name) - ' Delete directory - Try - IO.Directory.Delete(oDirInfo.FullName, True) - Catch ex As Exception - _Logger.Error(ex) - _Logger.Warn("Could not delete directory [{0}]", oDirInfo.FullName) - End Try - Next + Public Sub MigrateConfig(SourceDirectory As String, TargetDirectory As String, Optional FilePattern As String = "*.*") + 'If IO.Directory.Exists(TargetDirectory) Then + ' _Logger.Warn("Config Migration aborted because new config directory [{0}] already exists!", TargetDirectory) + ' Exit Sub + 'End If + + '_Logger.Debug("Creating TargetDirectory [{0}]", TargetDirectory) + '' Create target directory + 'Try + ' IO.Directory.CreateDirectory(TargetDirectory) + 'Catch ex As Exception + ' _Logger.Warn("Config Migration aborted because new config directory [{0}] could not be created!", TargetDirectory) + ' _Logger.Error(ex) + ' Exit Sub + 'End Try + + '' Create Migration directory + 'Dim oMigrationDirectory = IO.Path.Combine(SourceDirectory, MIGRATE_DIRECTORY) + '_Logger.Debug("Creating MigrationDirectory [{0}]", oMigrationDirectory) + 'Try + ' IO.Directory.CreateDirectory(oMigrationDirectory) + 'Catch ex As Exception + ' _Logger.Warn("Config Migration aborted because migration directory [{0}] could not be created!", oMigrationDirectory) + ' _Logger.Error(ex) + ' Exit Sub + 'End Try + + '' Copy individual files from top level directory + 'For Each oPath In IO.Directory.EnumerateFiles(SourceDirectory, FilePattern) + ' Dim oFileInfo = New IO.FileInfo(oPath) + + ' _Logger.Debug("Processing file [{0}]", oFileInfo.Name) + + ' _Logger.Debug("Copying [{0}] to TargetDirectory..", oFileInfo.Name) + ' ' Copy to target directory + ' Try + ' IO.File.Copy(oPath, IO.Path.Combine(TargetDirectory, oFileInfo.Name)) + ' Catch ex As Exception + ' _Logger.Warn("Could not move old config file {0} to new config location {1}", oFileInfo.Name, TargetDirectory) + ' _Logger.Error(ex) + ' End Try + + ' _Logger.Debug("Moving [{0}] to MigrationDirectory..", oFileInfo.Name) + ' ' Move to migration directory + ' Try + ' IO.File.Move(oPath, IO.Path.Combine(oMigrationDirectory, oFileInfo.Name)) + ' Catch ex As Exception + ' _Logger.Warn("Could not move old config file {0} to migration directory {1}", oFileInfo.Name, oMigrationDirectory) + ' _Logger.Error(ex) + ' End Try + 'Next + + 'For Each oDirectoryPath In IO.Directory.EnumerateDirectories(SourceDirectory, "*", IO.SearchOption.TopDirectoryOnly) + ' Dim oDirInfo As New IO.DirectoryInfo(oDirectoryPath) + + ' _Logger.Debug("Processing directory [{0}]", oDirInfo.Name) + + ' ' Don't copy TargetDirectory if subpath of SourceDirectory or if MigrationDirectory + ' If oDirInfo.FullName = TargetDirectory Or oDirInfo.FullName = oMigrationDirectory Then + ' _Logger.Debug("Directory [{0}] should not be copied. Skipping.", oDirInfo.Name) + ' Continue For + ' End If + + ' ' Copy directory to TargetDirectory + ' Dim oNewDirectoryPath = IO.Path.Combine(TargetDirectory, oDirInfo.Name) + ' _Logger.Debug("Copying [{0}] to TargetDirectory..", oDirInfo.Name) + ' Try + ' _File.CopyDirectory(oDirInfo.FullName, oNewDirectoryPath, True) + ' Catch ex As Exception + ' _Logger.Warn("Could not move directory [{0}] to new path [{1}]", oDirInfo.FullName, oNewDirectoryPath) + ' _Logger.Error(ex) + ' End Try + + ' _Logger.Debug("Copying [{0}] to MigrationDirectory..", oDirInfo.Name) + ' ' Copy directory to MigrationDirectory + ' Dim oMigrationDirectoryPath = IO.Path.Combine(oMigrationDirectory, oDirInfo.Name) + ' Try + ' _File.CopyDirectory(oDirInfo.FullName, oMigrationDirectoryPath, True) + ' Catch ex As Exception + ' _Logger.Error(ex) + ' _Logger.Warn("Could not move directory [{0}] to migration directory [{1}]", oDirInfo.FullName, oMigrationDirectoryPath) + ' End Try + + ' _Logger.Debug("Deleting [{0}]..", oDirInfo.Name) + ' ' Delete directory + ' Try + ' IO.Directory.Delete(oDirInfo.FullName, True) + ' Catch ex As Exception + ' _Logger.Error(ex) + ' _Logger.Warn("Could not delete directory [{0}]", oDirInfo.FullName) + ' End Try + 'Next End Sub End Class