From eb5a80c3e811ee5264a4356a00e52e47c8c36113 Mon Sep 17 00:00:00 2001 From: Paul Betts Date: Tue, 26 Aug 2014 15:37:50 -0700 Subject: [PATCH] Initial hack at an Install action --- src/Update/Program.cs | 56 +++++++++++++++++++++++++++++++++++--- src/Update/Update.csproj | 28 +++++++++++++++++++ src/Update/packages.config | 4 +++ 3 files changed, 84 insertions(+), 4 deletions(-) diff --git a/src/Update/Program.cs b/src/Update/Program.cs index 673ba9cb..d0b7aa7b 100644 --- a/src/Update/Program.cs +++ b/src/Update/Program.cs @@ -1,12 +1,15 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.IO; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; using Mono.Options; +using Squirrel; -namespace Update +namespace Squirrel.Update { enum UpdateAction { Unset = 0, Install, Uninstall, Download, Update, @@ -33,10 +36,10 @@ namespace Update "Manages Squirrel packages", "", "Commands", - { "install=", "Install the app specified by the RELEASES file", v => { updateAction = UpdateAction.Install; target = v; } }, + { "install=", "Install the app whose package is in the specified directory", v => { updateAction = UpdateAction.Install; target = v; } }, { "uninstall", "Uninstall the app the same dir as Update.exe", v => updateAction = UpdateAction.Uninstall}, { "download=", "Download the releases specified by the URL and write new results to stdout as JSON", v => { updateAction = UpdateAction.Download; target = v; } }, - { "update", "Update the application to the latest remote version", v => updateAction = UpdateAction.Update }, + { "update=", "Update the application to the latest remote version specified by URL", v => { updateAction = UpdateAction.Update; target = v; } }, "", "Options:", { "h|?|help", "Display Help and exit", _ => ShowHelp() }, @@ -49,13 +52,58 @@ namespace Update ShowHelp(); } + switch (updateAction) { + case UpdateAction.Install: + Install(silentInstall, target).Wait(); + break; + case UpdateAction.Update: + Update(target).Wait(); + break; + } + return 0; } - static void ShowHelp() + public static async Task Install(bool silentInstall, string sourceDirectory = null) + { + sourceDirectory = sourceDirectory ?? Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + var releasesPath = Path.Combine(sourceDirectory, "RELEASES"); + + if (!File.Exists(releasesPath)) { + var nupkgs = (new DirectoryInfo(sourceDirectory)).GetFiles() + .Where(x => x.Name.EndsWith(".nupkg", StringComparison.OrdinalIgnoreCase)) + .Select(x => ReleaseEntry.GenerateFromFile(x.FullName)); + + ReleaseEntry.WriteReleaseFile(nupkgs, releasesPath); + } + + var ourAppName = ReleaseEntry.ParseReleaseFile(releasesPath).First().PackageName; + using (var mgr = new UpdateManager(sourceDirectory, ourAppName, FrameworkVersion.Net45)) { + await mgr.FullInstall(silentInstall); + } + } + + public static async Task Update(string updateUrl, string appName = null) + { + appName = appName ?? getAppNameFromDirectory(); + + using (var mgr = new UpdateManager(updateUrl, appName, FrameworkVersion.Net45)) { + var updateInfo = await mgr.CheckForUpdate(progress: x => Console.WriteLine(x / 3)); + await mgr.DownloadReleases(updateInfo.ReleasesToApply, x => Console.WriteLine(33 + x / 3)); + await mgr.ApplyReleases(updateInfo, x => Console.WriteLine(66 + x / 3)); + } + } + + public static void ShowHelp() { opts.WriteOptionDescriptions(Console.Out); Environment.Exit(0); } + + static string getAppNameFromDirectory(string path = null) + { + path = path ?? Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + return (new DirectoryInfo(path)).Name; + } } } diff --git a/src/Update/Update.csproj b/src/Update/Update.csproj index d72d49ca..413178fa 100644 --- a/src/Update/Update.csproj +++ b/src/Update/Update.csproj @@ -32,6 +32,28 @@ 4 + + ..\..\packages\Microsoft.Web.Xdt.2.1.1\lib\net40\Microsoft.Web.XmlTransform.dll + + + ..\..\packages\Mono.Cecil.0.9.5.4\lib\net40\Mono.Cecil.dll + + + ..\..\packages\Mono.Cecil.0.9.5.4\lib\net40\Mono.Cecil.Mdb.dll + + + ..\..\packages\Mono.Cecil.0.9.5.4\lib\net40\Mono.Cecil.Pdb.dll + + + ..\..\packages\Mono.Cecil.0.9.5.4\lib\net40\Mono.Cecil.Rocks.dll + + + False + ..\..\packages\NuGet.Core.2.8.2\lib\net40-Client\NuGet.Core.dll + + + ..\..\packages\Splat.1.4.0\lib\Net45\Splat.dll + @@ -49,6 +71,12 @@ + + + {1436e22a-fe3c-4d68-9a85-9e74df2e6a92} + Squirrel + +