diff --git a/Modules.Config/ConfigUtils.vb b/Modules.Config/ConfigUtils.vb new file mode 100644 index 00000000..aa93b406 --- /dev/null +++ b/Modules.Config/ConfigUtils.vb @@ -0,0 +1,92 @@ +Imports DigitalData.Modules.Logging + +Public Class ConfigUtils + Private _Logger As Logger + Private _File As Filesystem.File + + Private Const MIGRATE_DIRECTORY As String = "Migrate" + + + Public Sub New(LogConfig As LogConfig) + _Logger = LogConfig.GetLogger() + _File = New Filesystem.File(LogConfig) + End Sub + + 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 + + ' 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) + 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 + + For Each oPath In IO.Directory.EnumerateFiles(SourceDirectory, FilePattern) + Dim oFileInfo = New IO.FileInfo(oPath) + 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 + Next + + For Each oPath In IO.Directory.EnumerateFiles(SourceDirectory, FilePattern) + Dim oFileInfo = New IO.FileInfo(oPath) + 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) + + If oDirInfo.FullName = TargetDirectory Or oDirInfo.FullName = oMigrationDirectory Then + Continue For + End If + + Dim oNewDirectoryPath = IO.Path.Combine(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 + Next + + For Each oDirectoryPath In IO.Directory.EnumerateDirectories(SourceDirectory, "*", IO.SearchOption.TopDirectoryOnly) + Dim oDirInfo As New IO.DirectoryInfo(oDirectoryPath) + + If oDirInfo.FullName = TargetDirectory Or oDirInfo.FullName = oMigrationDirectory Then + Continue For + End If + + Dim oNewDirectoryPath = IO.Path.Combine(oMigrationDirectory, oDirInfo.Name) + Try + _File.CopyDirectory(oDirInfo.FullName, oNewDirectoryPath, True) + Catch ex As Exception + _Logger.Error(ex) + _Logger.Warn("Could not move directory [{0}] to migration directory [{1}]", oDirInfo.FullName, oNewDirectoryPath) + End Try + Next + End Sub +End Class