Config: add config migration

This commit is contained in:
Jonathan Jenne 2020-12-17 16:58:38 +01:00
parent cd2bac4d6a
commit f4141aee40

View File

@ -12,12 +12,21 @@ Public Class ConfigUtils
_File = New Filesystem.File(LogConfig)
End Sub
Public Function TestMigrationNeeded(TargetDirectory As String) As Boolean
If IO.Directory.Exists(TargetDirectory) Then
Return False
Else
Return True
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
End If
_Logger.Debug("Creating TargetDirectory [{0}]", TargetDirectory)
' Create target directory
Try
IO.Directory.CreateDirectory(TargetDirectory)
@ -29,6 +38,7 @@ Public Class ConfigUtils
' 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
@ -37,18 +47,24 @@ Public Class ConfigUtils
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.NewBlock($"File {oFileInfo.Name}")
_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
Next
For Each oPath In IO.Directory.EnumerateFiles(SourceDirectory, FilePattern)
Dim oFileInfo = New IO.FileInfo(oPath)
_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
@ -60,33 +76,44 @@ Public Class ConfigUtils
For Each oDirectoryPath In IO.Directory.EnumerateDirectories(SourceDirectory, "*", IO.SearchOption.TopDirectoryOnly)
Dim oDirInfo As New IO.DirectoryInfo(oDirectoryPath)
_Logger.NewBlock($"Directory {oDirInfo.Name}")
_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
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)
_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, oNewDirectoryPath, True)
_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, oNewDirectoryPath)
_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