Repository navigation
Expand file tree
/
Copy pathUpdateManager.cs
More file actions
217 lines (179 loc) · 8.29 KB
/
Copy pathUpdateManager.cs
File metadata and controls
217 lines (179 loc) · 8.29 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.AccessControl;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Win32;
using NuGet;
using Splat;
namespace Squirrel
{
public sealed partial class UpdateManager : IUpdateManager, IEnableLogger
{
readonly string rootAppDirectory;
readonly string applicationName;
readonly IFileDownloader urlDownloader;
readonly string updateUrlOrPath;
readonly FrameworkVersion appFrameworkVersion;
IDisposable updateLock;
public UpdateManager(string urlOrPath,
string applicationName,
FrameworkVersion appFrameworkVersion,
string rootDirectory = null,
IFileDownloader urlDownloader = null)
{
Contract.Requires(!String.IsNullOrEmpty(urlOrPath));
Contract.Requires(!String.IsNullOrEmpty(applicationName));
updateUrlOrPath = urlOrPath;
this.applicationName = applicationName;
this.appFrameworkVersion = appFrameworkVersion;
this.rootAppDirectory = Path.Combine(rootDirectory ?? getLocalAppDataDirectory(), applicationName);
this.urlDownloader = urlDownloader ?? new FileDownloader();
}
public async Task<UpdateInfo> CheckForUpdate(bool ignoreDeltaUpdates = false, Action<int> progress = null)
{
var checkForUpdate = new CheckForUpdateImpl(rootAppDirectory);
await acquireUpdateLock();
return await checkForUpdate.CheckForUpdate(Utility.LocalReleaseFileForAppDir(rootAppDirectory), updateUrlOrPath, ignoreDeltaUpdates, progress, urlDownloader);
}
public async Task DownloadReleases(IEnumerable<ReleaseEntry> releasesToDownload, Action<int> progress = null)
{
var downloadReleases = new DownloadReleasesImpl(rootAppDirectory);
await acquireUpdateLock();
await downloadReleases.DownloadReleases(updateUrlOrPath, releasesToDownload, progress, urlDownloader);
}
public async Task<string> ApplyReleases(UpdateInfo updateInfo, Action<int> progress = null)
{
var applyReleases = new ApplyReleasesImpl(rootAppDirectory);
await acquireUpdateLock();
return await applyReleases.ApplyReleases(updateInfo, false, progress);
}
public async Task FullInstall(bool silentInstall = false)
{
var updateInfo = await CheckForUpdate();
await DownloadReleases(updateInfo.ReleasesToApply);
var applyReleases = new ApplyReleasesImpl(rootAppDirectory);
await acquireUpdateLock();
await applyReleases.ApplyReleases(updateInfo, silentInstall);
}
public async Task FullUninstall()
{
var applyReleases = new ApplyReleasesImpl(rootAppDirectory);
await acquireUpdateLock();
await applyReleases.FullUninstall();
}
const string uninstallRegSubKey = @"Software\Microsoft\Windows\CurrentVersion\Uninstall";
public async Task<RegistryKey> CreateUninstallerRegistryEntry(string uninstallCmd, string quietSwitch)
{
var releaseContent = File.ReadAllText(Path.Combine(rootAppDirectory, "packages", "RELEASES"), Encoding.UTF8);
var releases = ReleaseEntry.ParseReleaseFile(releaseContent);
var latest = releases.OrderByDescending(x => x.Version).First();
// Download the icon and PNG => ICO it. If this doesn't work, who cares
var pkgPath = Path.Combine(rootAppDirectory, "packages", latest.Filename);
var zp = new ZipPackage(pkgPath);
var targetPng = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".png");
var targetIco = Path.Combine(rootAppDirectory, "app.ico");
var key = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Default)
.CreateSubKey(uninstallRegSubKey + "\\" + applicationName, RegistryKeyPermissionCheck.ReadWriteSubTree);
try {
var wc = new WebClient();
await wc.DownloadFileTaskAsync(zp.IconUrl, targetPng);
using (var fs = new FileStream(targetIco, FileMode.Create)) {
if (zp.IconUrl.AbsolutePath.EndsWith("ico")) {
var bytes = File.ReadAllBytes(targetPng);
fs.Write(bytes, 0, bytes.Length);
} else {
using (var bmp = (Bitmap)Image.FromFile(targetPng))
using (var ico = Icon.FromHandle(bmp.GetHicon())) {
ico.Save(fs);
}
}
key.SetValue("DisplayIcon", targetIco, RegistryValueKind.String);
}
} catch(Exception ex) {
this.Log().InfoException("Couldn't write uninstall icon, don't care", ex);
} finally {
File.Delete(targetPng);
}
var stringsToWrite = new[] {
new { Key = "DisplayName", Value = zp.Description ?? zp.Summary },
new { Key = "DisplayVersion", Value = zp.Version.ToString() },
new { Key = "InstallDate", Value = DateTime.Now.ToString("yyyymmdd") },
new { Key = "InstallLocation", Value = rootAppDirectory },
new { Key = "Publisher", Value = zp.Authors.First() },
new { Key = "QuietUninstallString", Value = String.Format("{0} {1}", uninstallCmd, quietSwitch) },
new { Key = "UninstallString", Value = uninstallCmd },
};
var dwordsToWrite = new[] {
new { Key = "EstimatedSize", Value = (int)((new FileInfo(pkgPath)).Length / 1024) },
new { Key = "NoModify", Value = 1 },
new { Key = "NoRepair", Value = 1 },
new { Key = "Language", Value = 0x0409 },
};
foreach (var kvp in stringsToWrite) {
key.SetValue(kvp.Key, kvp.Value, RegistryValueKind.String);
}
foreach (var kvp in dwordsToWrite) {
key.SetValue(kvp.Key, kvp.Value, RegistryValueKind.DWord);
}
return key;
}
public void RemoveUninstallerRegistryEntry()
{
var key = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Default)
.OpenSubKey(uninstallRegSubKey, true);
key.DeleteSubKeyTree(applicationName);
}
public string RootAppDirectory {
get { return rootAppDirectory; }
}
public void Dispose()
{
var disp = Interlocked.Exchange(ref updateLock, null);
if (disp != null) {
disp.Dispose();
}
}
~UpdateManager()
{
if (updateLock != null) {
throw new Exception("You must dispose UpdateManager!");
}
}
Task<IDisposable> acquireUpdateLock()
{
if (updateLock != null) return Task.FromResult(updateLock);
return Task.Run(() => {
// TODO: We'll bring this back later
var key = Utility.CalculateStreamSHA1(new MemoryStream(Encoding.UTF8.GetBytes(rootAppDirectory)));
var theLock = Disposable.Create(() => { });
/*
IDisposable theLock;
try {
theLock = RxApp.InUnitTestRunner() ?
Disposable.Empty : new SingleGlobalInstance(key, 2000);
} catch (TimeoutException) {
throw new TimeoutException("Couldn't acquire update lock, another instance may be running updates");
}
*/
var ret = Disposable.Create(() => {
theLock.Dispose();
updateLock = null;
});
updateLock = ret;
return ret;
});
}
static string getLocalAppDataDirectory()
{
return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
}
}
}