public static async Task StartUpdate(Action successAction) { logger.Info("Starting update"); var updateDirectoryPath = GetUpdateDirectory(); var sourcePath = Path.Combine(versionBinaryPath, versionBinaryFileName); var destPath = Path.Combine(updateDirectoryPath, versionBinaryFileName); var taskCancellationTokenSource = new ReportingCancellationTokenSource("Update"); SynchronizationContext.SetSynchronizationContext(new SynchronizationContext()); var taskScheduler = TaskScheduler.FromCurrentSynchronizationContext(); Console.WriteLine("Waiting to enter for debug"); Console.ReadLine(); try { await Task.Factory .StartNew(() => Helpers.CreateDirectory(updateDirectoryPath, taskCancellationTokenSource), taskCancellationTokenSource.Token, TaskCreationOptions.None, taskScheduler) .ContinueWith(task => Helpers.CleanDirectory(updateDirectoryPath, taskCancellationTokenSource, null), taskCancellationTokenSource.Token, TaskContinuationOptions.OnlyOnRanToCompletion, taskScheduler) .ContinueWith(task => Helpers.DownloadFile(sourcePath, destPath, taskCancellationTokenSource), taskCancellationTokenSource.Token, TaskContinuationOptions.OnlyOnRanToCompletion, taskScheduler) .ContinueWith(task => Helpers.UnpackFile(destPath, taskCancellationTokenSource), taskCancellationTokenSource.Token, TaskContinuationOptions.OnlyOnRanToCompletion, taskScheduler) .ContinueWith(task => RunUpdater(taskCancellationTokenSource), taskCancellationTokenSource.Token, TaskContinuationOptions.OnlyOnRanToCompletion, taskScheduler) .ContinueWith(task => successAction(), taskCancellationTokenSource.Token, TaskContinuationOptions.OnlyOnRanToCompletion, taskScheduler); } catch (OperationCanceledException) { Console.WriteLine("Update operation was cancelled"); } }
internal static void CreateDirectory(string destination, ReportingCancellationTokenSource taskCancellationTokenSource) { try { if (destination == null) { throw new IOException("Directory path cannot be null"); } if (Directory.Exists(destination)) { logger.Info(string.Format("Update directory with path {0} exists", destination)); return; } logger.Info(string.Format("Creating directory with path {0}", destination)); Directory.CreateDirectory(destination); } catch (IOException ex) { var message = string.Format("Cannot create directory on a path {0}", destination); logger.Error(message); logger.Error(ex.Message); CancelTask(taskCancellationTokenSource); } logger.Info(string.Format("Directory creation finished succesfully")); }
public static void UpdateApplication(bool startApp = true) { logger.Info("Updater: Starting application update"); var retriesCount = 10; while (retriesCount > 0 && MainAppRunning()) { retriesCount--; var message = "HomeCalc application still running, waiting 5 seconds"; logger.Info(message); Thread.Sleep(5000); } if (MainAppRunning()) { logger.Info("Cannot stop HomeCalc application, exiting..."); return; } var updateFolder = AppDomain.CurrentDomain.BaseDirectory.Trim(Path.DirectorySeparatorChar); var appFolder = Directory.GetParent(updateFolder).FullName; logger.Info("Current updater application running directory: {0}", updateFolder); logger.Info("Current updater parent directory: {0}", appFolder); var cancellationTokenSource = new ReportingCancellationTokenSource(); logger.Info("Starting application folder cleanup"); Helpers.CleanDirectory(appFolder, cancellationTokenSource, new [] { "Update", "Debug", "Release" }); if (cancellationTokenSource.Token.IsCancellationRequested) { var message = "Application folder cleanup failed. Exiting..."; logger.Error(message); return; } logger.Info("Starting application files copying"); Helpers.CopyAllFiles(updateFolder, appFolder, cancellationTokenSource, new List <string> { "HomeCalc.zip", "HomeCalcUpdate.zip" }); if (cancellationTokenSource.Token.IsCancellationRequested) { var message = "Application files copying failed. Exiting..."; logger.Error(message); return; } if (startApp) { var appExePath = Path.Combine(appFolder, "HomeCalc.View.exe"); logger.Info("Starting application: {0}", appExePath); try { Process.Start(appExePath); } catch (Exception ex) { logger.Error(ex.Message); } } }
public static void UpdateApplication(bool startApp = true) { logger.Info("Updater: Starting application update"); var retriesCount = 10; while (retriesCount > 0 && MainAppRunning()) { retriesCount--; var message = "HomeCalc application still running, waiting 5 seconds"; logger.Info(message); Thread.Sleep(5000); } if (MainAppRunning()) { logger.Info("Cannot stop HomeCalc application, exiting..."); return; } var updateFolder = AppDomain.CurrentDomain.BaseDirectory.Trim(Path.DirectorySeparatorChar); var appFolder = Directory.GetParent(updateFolder).FullName; logger.Info("Current updater application running directory: {0}", updateFolder); logger.Info("Current updater parent directory: {0}", appFolder); var cancellationTokenSource = new ReportingCancellationTokenSource(); logger.Info("Starting application folder cleanup"); Helpers.CleanDirectory(appFolder, cancellationTokenSource, new []{"Update", "Debug", "Release"}); if (cancellationTokenSource.Token.IsCancellationRequested) { var message = "Application folder cleanup failed. Exiting..."; logger.Error(message); return; } logger.Info("Starting application files copying"); Helpers.CopyAllFiles(updateFolder, appFolder, cancellationTokenSource, new List<string> { "HomeCalc.zip", "HomeCalcUpdate.zip" }); if (cancellationTokenSource.Token.IsCancellationRequested) { var message = "Application files copying failed. Exiting..."; logger.Error(message); return; } if (startApp) { var appExePath = Path.Combine(appFolder, "HomeCalc.View.exe"); logger.Info("Starting application: {0}", appExePath); try { Process.Start(appExePath); } catch (Exception ex) { logger.Error(ex.Message); } } }
public static void CopyAllFiles(string source, string destination, ReportingCancellationTokenSource tokenSource, List <string> exclusions = null) { if (exclusions == null) { exclusions = new List <string>(); } try { logger.Info(string.Format("Starting copying files from {0} to {1}", source, destination)); var itemsToCopy = Directory.EnumerateFileSystemEntries(source); foreach (var itemPath in itemsToCopy) { var itemName = Path.GetFileName(itemPath); if (exclusions.Contains(itemName)) { logger.Info("File {0} found in exclusion list, skipping", itemName); continue; } var destinationItemPath = Path.Combine(destination, itemName); logger.Info("Preparing to copy file {0} to {1}", itemPath, destinationItemPath); if (File.Exists(destinationItemPath)) { File.SetAttributes(destinationItemPath, FileAttributes.Normal); } var attributes = File.GetAttributes(itemPath); if (attributes.HasFlag(FileAttributes.Directory)) { logger.Info("Copying directory {0}", itemName); Directory.CreateDirectory(destinationItemPath); CopyAllFiles(itemPath, destinationItemPath, tokenSource); } else { logger.Info("Copying file {0}", itemName); File.Copy(itemPath, destinationItemPath, true); } } } catch (IOException ex) { var message = "File copying failed"; logger.Error(message); logger.Error(ex.Message); CancelTask(tokenSource); } logger.Info(string.Format("File copying finished succesfully")); }
public static void DownloadFile(string source, string destination, ReportingCancellationTokenSource tokenSource) { try { logger.Info(string.Format("Starting file download from URL: {0} to directory: {1}", source, destination)); if (File.Exists(destination)) { File.Delete(destination); } var webClient = new WebClient(); status.Post("Завантаження оновлення триває"); //status.StartProgress(); //webClient.DownloadProgressChanged += (o, e) => //{ // status.UpdateProgress(e.ProgressPercentage); //}; webClient.DownloadFile(source, destination); } catch (IOException ex) { //status.StopProgress(); var message = string.Format("Cannot download file to path {0} : file exists and cannot be deleted", destination); logger.Error(message); logger.Error(ex.Message); CancelTask(tokenSource); } catch (WebException ex) { //status.StopProgress(); var message = "File download failed"; logger.Error(message); logger.Error(ex.Message); CancelTask(tokenSource); } finally { status.StopProgress(); } logger.Info(string.Format("File download finished succesfully")); }
public static void UnpackFile(string filePath, ReportingCancellationTokenSource tokenSource, string destination = null) { try { if (destination == null) { destination = Directory.GetParent(filePath).FullName; } logger.Info(string.Format("Starting file {0} unpack to path {1}", filePath, destination)); ZipFile.ExtractToDirectory(filePath, destination); } catch (IOException ex) { var message = string.Format("Cannot unpack file on a path {0}", filePath); logger.Error(message); logger.Error(ex.Message); CancelTask(tokenSource); } logger.Info(string.Format("File unpack finished succesfully")); }
public static void CleanDirectory(string path, ReportingCancellationTokenSource tokenSource, string[] exceptions = null) { try { var message = ""; if (exceptions == null) { message = string.Format("Starting folder {0} cleanup", path); } else { message = string.Format("Starting folder {0} cleanup, exceptions: {1}", path, string.Join("; ", exceptions)); } logger.Info(message); if (!Directory.Exists(path)) { logger.Info("Cleanup folder {0} missing, exiting cleanup", path); } Directory.EnumerateFileSystemEntries(path).ToList().ForEach(item => { var fileName = Path.GetFileName(item); logger.Info("***********************"); logger.Info("Preparing to delete {0}", fileName); if (exceptions != null) { if (exceptions.Contains(fileName)) { logger.Info("Skipping {0} as exception", fileName); return; } else { logger.Info("File {0} not found in exceptions \"{1}\", deleting", fileName, string.Join("; ", exceptions)); } } var fileAttributes = File.GetAttributes(item); File.SetAttributes(item, FileAttributes.Normal); if (fileAttributes.HasFlag(FileAttributes.Directory)) { logger.Info("Deleting folder {0}", item); Directory.Delete(item, true); } else { logger.Info("Deleting file {0}", item); File.Delete(item); } }); } catch (IOException ex) { var message = "Directory cleanup failed"; logger.Error(message); logger.Error(ex.Message); CancelTask(tokenSource); } catch (UnauthorizedAccessException ex) { var message = "Directory cleanup failed: you have no access permission"; logger.Error(message); logger.Error(ex.Message); CancelTask(tokenSource); } logger.Info(string.Format("Directory cleanup finished succesfully")); }
public static void CopyAllFiles(string source, string destination, ReportingCancellationTokenSource tokenSource, List<string> exclusions = null) { if (exclusions == null) { exclusions = new List<string>(); } try { logger.Info(string.Format("Starting copying files from {0} to {1}", source, destination)); var itemsToCopy = Directory.EnumerateFileSystemEntries(source); foreach (var itemPath in itemsToCopy) { var itemName = Path.GetFileName(itemPath); if (exclusions.Contains(itemName)) { logger.Info("File {0} found in exclusion list, skipping", itemName); continue; } var destinationItemPath = Path.Combine(destination, itemName); logger.Info("Preparing to copy file {0} to {1}", itemPath, destinationItemPath); if (File.Exists(destinationItemPath)) { File.SetAttributes(destinationItemPath, FileAttributes.Normal); } var attributes = File.GetAttributes(itemPath); if (attributes.HasFlag(FileAttributes.Directory)) { logger.Info("Copying directory {0}", itemName); Directory.CreateDirectory(destinationItemPath); CopyAllFiles(itemPath, destinationItemPath, tokenSource); } else { logger.Info("Copying file {0}", itemName); File.Copy(itemPath, destinationItemPath, true); } } } catch (IOException ex) { var message = "File copying failed"; logger.Error(message); logger.Error(ex.Message); CancelTask(tokenSource); } logger.Info(string.Format("File copying finished succesfully")); }