From 0763761c57f635b3a9c5b21e4e06d8c1b17befad Mon Sep 17 00:00:00 2001 From: Paul Betts Date: Fri, 8 Aug 2014 12:58:18 +0200 Subject: [PATCH] Get ApplyReleases building --- src/Squirrel/UpdateManager.ApplyReleases.cs | 62 +++++++++++++++++++- src/Squirrel/UpdateManager.cs | 64 ++++++--------------- 2 files changed, 77 insertions(+), 49 deletions(-) diff --git a/src/Squirrel/UpdateManager.ApplyReleases.cs b/src/Squirrel/UpdateManager.ApplyReleases.cs index 00f43ec1..1b6003ce 100644 --- a/src/Squirrel/UpdateManager.ApplyReleases.cs +++ b/src/Squirrel/UpdateManager.ApplyReleases.cs @@ -1,8 +1,12 @@ using System; using System.Collections.Generic; +using System.Diagnostics.Contracts; +using System.IO; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; +using NuGet; using Splat; namespace Squirrel @@ -11,6 +15,16 @@ namespace Squirrel { class ApplyReleases : IEnableLogger { + // TODO: Kill this entire concept + readonly FrameworkVersion appFrameworkVersion = FrameworkVersion.Net45; + + readonly string rootAppDirectory; + + public ApplyReleases(string rootAppDirectory) + { + this.rootAppDirectory = rootAppDirectory; + } + public async Task ApplyReleases(UpdateInfo updateInfo, Action progress = null) { progress = progress ?? (_ => { }); @@ -28,6 +42,25 @@ namespace Squirrel progress(100); } + public async Task FullUninstall(Version version = null) + { + version = version ?? new Version(255, 255, 255, 255); + this.Log().Info("Uninstalling version '{0}'", version); + + + // find all the old releases (and this one) + var directoriesToDelete = getOldReleases(version) + .Concat(new [] { getDirectoryForRelease(version) }) + .Where(d => d.Exists) + .Select(d => d.FullName); + + await directoriesToDelete.ForEachAsync(x => Utility.DeleteDirectoryWithFallbackToNextReboot(x)); + + if (!getReleases().Any()) { + await Utility.DeleteDirectoryWithFallbackToNextReboot(rootAppDirectory); + } + } + async Task installPackageToAppDir(UpdateInfo updateInfo, ReleaseEntry release) { var pkg = new ZipPackage(Path.Combine(updateInfo.PackageDirectory, release.Filename)); @@ -278,7 +311,34 @@ namespace Squirrel async Task updateLocalReleasesFile() { - await Task.Run(() => ReleaseEntry.BuildReleasesFile(PackageDirectory)); + await Task.Run(() => ReleaseEntry.BuildReleasesFile(Utility.PackageDirectoryForAppDir(rootAppDirectory))); + } + + IEnumerable getReleases() + { + var rootDirectory = new DirectoryInfo(rootAppDirectory); + + if (!rootDirectory.Exists) return Enumerable.Empty(); + + return rootDirectory.GetDirectories() + .Where(x => x.Name.StartsWith("app-", StringComparison.InvariantCultureIgnoreCase)); + } + + IEnumerable getOldReleases(Version version) + { + return getReleases() + .Where(x => x.Name.ToVersion() < version) + .ToArray(); + } + + static string getLocalAppDataDirectory() + { + return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); + } + + DirectoryInfo getDirectoryForRelease(Version releaseVersion) + { + return new DirectoryInfo(Path.Combine(rootAppDirectory, "app-" + releaseVersion)); } } } diff --git a/src/Squirrel/UpdateManager.cs b/src/Squirrel/UpdateManager.cs index a9b35ee6..6054e319 100644 --- a/src/Squirrel/UpdateManager.cs +++ b/src/Squirrel/UpdateManager.cs @@ -41,63 +41,36 @@ namespace Squirrel this.urlDownloader = urlDownloader ?? new FileDownloader(); } - public Task CheckForUpdate(bool ignoreDeltaUpdates, Action progress = null) + public async Task CheckForUpdate(bool ignoreDeltaUpdates, Action progress = null) { var checkForUpdate = new CheckForUpdates(rootAppDirectory); - return checkForUpdate.CheckForUpdate(Utility.LocalReleaseFileForAppDir(rootAppDirectory), updateUrlOrPath, ignoreDeltaUpdates, progress, urlDownloader); + + await acquireUpdateLock(); + return await checkForUpdate.CheckForUpdate(Utility.LocalReleaseFileForAppDir(rootAppDirectory), updateUrlOrPath, ignoreDeltaUpdates, progress, urlDownloader); } - public Task DownloadReleases(IEnumerable releasesToDownload, Action progress = null) + public async Task DownloadReleases(IEnumerable releasesToDownload, Action progress = null) { var downloadReleases = new DownloadReleases(rootAppDirectory); - return downloadReleases.DownloadReleases(updateUrlOrPath, releasesToDownload, progress, urlDownloader); + await acquireUpdateLock(); + + await downloadReleases.DownloadReleases(updateUrlOrPath, releasesToDownload, progress, urlDownloader); } - public Task ApplyReleases(UpdateInfo updateInfo, Action progress = null) + public async Task ApplyReleases(UpdateInfo updateInfo, Action progress = null) { - throw new NotImplementedException(); + var applyReleases = new ApplyReleases(rootAppDirectory); + await acquireUpdateLock(); + + await applyReleases.ApplyReleases(updateInfo, progress); } public async Task FullUninstall() { + var applyReleases = new ApplyReleases(rootAppDirectory); await acquireUpdateLock(); - await fullUninstall(); - } - IEnumerable getReleases() - { - var rootDirectory = new DirectoryInfo(rootAppDirectory); - - if (!rootDirectory.Exists) return Enumerable.Empty(); - - return rootDirectory.GetDirectories() - .Where(x => x.Name.StartsWith("app-", StringComparison.InvariantCultureIgnoreCase)); - } - - IEnumerable getOldReleases(Version version) - { - return getReleases() - .Where(x => x.Name.ToVersion() < version) - .ToArray(); - } - - async Task fullUninstall(Version version = null) - { - version = version ?? new Version(255, 255, 255, 255); - this.Log().Info("Uninstalling version '{0}'", version); - - - // find all the old releases (and this one) - var directoriesToDelete = getOldReleases(version) - .Concat(new [] { getDirectoryForRelease(version) }) - .Where(d => d.Exists) - .Select(d => d.FullName); - - await directoriesToDelete.ForEachAsync(x => Utility.DeleteDirectoryWithFallbackToNextReboot(x)); - - if (!getReleases().Any()) { - await Utility.DeleteDirectoryWithFallbackToNextReboot(rootAppDirectory); - } + await applyReleases.FullUninstall(); } public void Dispose() @@ -148,10 +121,5 @@ namespace Squirrel { return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); } - - DirectoryInfo getDirectoryForRelease(Version releaseVersion) - { - return new DirectoryInfo(Path.Combine(rootAppDirectory, "app-" + releaseVersion)); - } } -} +} \ No newline at end of file