public UpdaterModel GetNewFiles(FileInfo archivePath, UpdaterModel currentData) { _logger.LogDebug("{0}: Getting the new files for {1} with current data: {2}", nameof(UpdaterService), archivePath, currentData); var unpackedPath = Path.Combine(archivePath.Directory.FullName, currentData.NewVersionModel.VersionString); var unpackedDirectory = new DirectoryInfo(unpackedPath); if (!unpackedDirectory.Exists) { unpackedDirectory.Create(); ZipFile.ExtractToDirectory(archivePath.FullName, unpackedDirectory.FullName); unpackedDirectory = new DirectoryInfo(unpackedPath); } var newFiles = GetFiles(unpackedDirectory); return(new UpdaterModel { InstallPath = currentData.InstallPath, NewFiles = newFiles, NewVersionString = currentData.NewVersionString, NewVersionModel = currentData.NewVersionModel, OldFiles = currentData.OldFiles, OldVersion = currentData.OldVersion }); }
public bool Update(UpdaterModel updaterModel, IProgress <int> progress, CancellationToken cancellationToken) { _logger.LogDebug("{0}: Executing update with data {1}", nameof(UpdaterService), updaterModel); var basePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), Constants.SOLUTION_ENTRYPOINT_NAME); var newVersionPath = Path.Combine(basePath, Constants.SOLUTION_DOWNLOADS_NAME, updaterModel.NewVersionModel.VersionString); var tempPath = Path.Combine(basePath, Constants.SOLUTION_TEMP_NAME, updaterModel.OldVersion); var backupPath = Path.Combine(basePath, Constants.SOLUTION_BACKUP_NAME, updaterModel.OldVersion); var dbPath = Path.Combine(updaterModel.InstallPath, Constants.SOLUTION_DATABASE_NAME); var dbFileInfo = new FileInfo(dbPath); var backupDbPath = Path.Combine(backupPath, dbFileInfo.Name); if (!Directory.Exists(backupPath)) { Directory.CreateDirectory(backupPath); } _fileCopyService.CopyFilesFromToRecursively(updaterModel.InstallPath, tempPath); if (dbFileInfo.Exists) { File.Copy(dbPath, backupDbPath, true); } try { StopRunningService(); progress.Report(33); _fileCopyService.DeleteFolderAndFilesRecursively(updaterModel.InstallPath); progress.Report(66); _fileCopyService.CopyFilesFromToRecursively(newVersionPath, updaterModel.InstallPath); if (File.Exists(backupDbPath)) { File.Copy(backupDbPath, dbPath, true); } } catch (Exception ex) { _logger.LogError(ex, "{0}: Update failed.", nameof(UpdaterService)); _fileCopyService.DeleteFolderAndFilesRecursively(updaterModel.InstallPath); _fileCopyService.CopyFilesFromToRecursively(tempPath, updaterModel.InstallPath); return(false); } finally { _fileCopyService.DeleteFolderAndFilesRecursively(tempPath); progress.Report(100); } return(true); }