Repository navigation
Expand file tree
/
Copy pathIUpdateManager.cs
More file actions
193 lines (170 loc) · 8.32 KB
/
Copy pathIUpdateManager.cs
File metadata and controls
193 lines (170 loc) · 8.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Win32;
using Splat;
using NuGet;
namespace Squirrel
{
[Flags]
public enum ShortcutLocation {
StartMenu = 1 << 0,
Desktop = 1 << 1,
Startup = 1 << 2,
/// <summary>
/// A shortcut in the application folder, useful for portable applications.
/// </summary>
AppRoot = 1 << 3
}
public interface IUpdateManager : IDisposable, IEnableLogger
{
/// <summary>
/// Fetch the remote store for updates and compare against the current
/// version to determine what updates to download.
/// </summary>
/// <param name="ignoreDeltaUpdates">Set this flag if applying a release
/// fails to fall back to a full release, which takes longer to download
/// but is less error-prone.</param>
/// <param name="progress">A Observer which can be used to report Progress -
/// will return values from 0-100 and Complete, or Throw</param>
/// <returns>An UpdateInfo object representing the updates to install.
/// </returns>
Task<UpdateInfo> CheckForUpdate(bool ignoreDeltaUpdates = false, Action<int> progress = null);
/// <summary>
/// Download a list of releases into the local package directory.
/// </summary>
/// <param name="releasesToDownload">The list of releases to download,
/// almost always from UpdateInfo.ReleasesToApply.</param>
/// <param name="progress">A Observer which can be used to report Progress -
/// will return values from 0-100 and Complete, or Throw</param>
/// <returns>A completion Observable - either returns a single
/// Unit.Default then Complete, or Throw</returns>
Task DownloadReleases(IEnumerable<ReleaseEntry> releasesToDownload, Action<int> progress = null);
/// <summary>
/// Take an already downloaded set of releases and apply them,
/// copying in the new files from the NuGet package and rewriting
/// the application shortcuts.
/// </summary>
/// <param name="updateInfo">The UpdateInfo instance acquired from
/// CheckForUpdate</param>
/// <param name="progress">A Observer which can be used to report Progress -
/// will return values from 0-100 and Complete, or Throw</param>
/// <returns>The path to the installed application (i.e. the path where
/// your package's contents ended up</returns>
Task<string> ApplyReleases(UpdateInfo updateInfo, Action<int> progress = null);
/// <summary>
/// Completely Installs a targeted app
/// </summary>
/// <param name="silentInstall">If true, don't run the app once install completes.</param>
/// <param name="progress">A Observer which can be used to report Progress -
/// will return values from 0-100 and Complete, or Throw</param>
/// <returns>Completion</returns>
Task FullInstall(bool silentInstall, Action<int> progress = null);
/// <summary>
/// Completely uninstalls the targeted app
/// </summary>
/// <returns>Completion</returns>
Task FullUninstall();
/// <summary>
/// Gets the currently installed version of the given executable, or if
/// not given, the currently running assembly
/// </summary>
/// <param name="executable">The executable to check, or null for this
/// executable</param>
/// <returns>The running version, or null if this is not a Squirrel
/// installed app (i.e. you're running from VS)</returns>
SemanticVersion CurrentlyInstalledVersion(string executable = null);
/// <summary>
/// Creates an entry in Programs and Features based on the currently
/// applied package
/// </summary>
/// <param name="uninstallCmd">The command to run to uninstall, usually update.exe --uninstall</param>
/// <param name="quietSwitch">The switch for silent uninstall, usually --silent</param>
/// <returns>The registry key that was created</returns>
Task<RegistryKey> CreateUninstallerRegistryEntry(string uninstallCmd, string quietSwitch);
/// <summary>
/// Creates an entry in Programs and Features based on the currently
/// applied package. Uses the built-in Update.exe to handle uninstall.
/// </summary>
/// <returns>The registry key that was created</returns>
Task<RegistryKey> CreateUninstallerRegistryEntry();
/// <summary>
/// Removes the entry in Programs and Features created via
/// CreateUninstallerRegistryEntry
/// </summary>
void RemoveUninstallerRegistryEntry();
/// <summary>
/// Create a shortcut on the Desktop / Start Menu for the given
/// executable. Metadata from the currently installed NuGet package
/// and information from the Version Header of the EXE will be used
/// to construct the shortcut folder / name.
/// </summary>
/// <param name="exeName">The name of the executable, relative to the
/// app install directory.</param>
/// <param name="locations">The locations to install the shortcut</param>
/// <param name="updateOnly">Set to false during initial install, true
/// during app update.</param>
/// <param name="programArguments">The arguments to code into the shortcut</param>
/// <param name="icon">The shortcut icon</param>
void CreateShortcutsForExecutable(string exeName, ShortcutLocation locations, bool updateOnly, string programArguments, string icon);
/// <summary>
/// Removes shortcuts created by CreateShortcutsForExecutable
/// </summary>
/// <param name="exeName">The name of the executable, relative to the
/// app install directory.</param>
/// <param name="locations">The locations to install the shortcut</param>
void RemoveShortcutsForExecutable(string exeName, ShortcutLocation locations);
}
public static class EasyModeMixin
{
public static async Task<ReleaseEntry> UpdateApp(this IUpdateManager This, Action<int> progress = null)
{
progress = progress ?? (_ => {});
This.Log().Info("Starting automatic update");
bool ignoreDeltaUpdates = false;
retry:
var updateInfo = default(UpdateInfo);
try {
updateInfo = await This.ErrorIfThrows(() => This.CheckForUpdate(ignoreDeltaUpdates, x => progress(x / 3)),
"Failed to check for updates");
await This.ErrorIfThrows(() =>
This.DownloadReleases(updateInfo.ReleasesToApply, x => progress(x / 3 + 33)),
"Failed to download updates");
await This.ErrorIfThrows(() =>
This.ApplyReleases(updateInfo, x => progress(x / 3 + 66)),
"Failed to apply updates");
await This.ErrorIfThrows(() =>
This.CreateUninstallerRegistryEntry(),
"Failed to set up uninstaller");
} catch {
if (ignoreDeltaUpdates == false) {
ignoreDeltaUpdates = true;
goto retry;
}
throw;
}
return updateInfo.ReleasesToApply.Any() ?
updateInfo.ReleasesToApply.MaxBy(x => x.Version).Last() :
default(ReleaseEntry);
}
public static void CreateShortcutForThisExe(this IUpdateManager This)
{
This.CreateShortcutsForExecutable(Path.GetFileName(
Assembly.GetEntryAssembly().Location),
ShortcutLocation.Desktop | ShortcutLocation.StartMenu,
Environment.CommandLine.Contains("squirrel-install") == false,
null, null);
}
public static void RemoveShortcutForThisExe(this IUpdateManager This)
{
This.RemoveShortcutsForExecutable(
Path.GetFileName(Assembly.GetEntryAssembly().Location),
ShortcutLocation.Desktop | ShortcutLocation.StartMenu);
}
}
}