-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInstallHelper.cs
78 lines (64 loc) · 2.42 KB
/
InstallHelper.cs
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
using AzDeploy.Build.Models;
using Microsoft.Azure.Storage.Blob;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
namespace AzDeploy.Client
{
public abstract class InstallHelper
{
private readonly Uri _uri;
private readonly Version _localVersion;
private readonly string _installerExe;
public InstallHelper(Version localVerison, string account, string container, string installerExe)
{
_localVersion = localVerison;
_uri = GetBlobUri(account, container, installerExe);
_installerExe = installerExe;
}
public async Task<VersionCheck> GetVersionCheckAsync()
{
var blob = new CloudBlockBlob(_uri);
var remoteVersion = (await blob.ExistsAsync()) ?
Version.Parse(blob.Metadata[VersionCheck.VersionMetadata]) :
Version.Parse("0.0.0.0");
return new VersionCheck()
{
IsNew = (remoteVersion > _localVersion),
Version = remoteVersion
};
}
public async Task AutoInstallAsync()
{
var check = await GetVersionCheckAsync();
if (check.IsNew)
{
if (!PromptDownloadAndExit()) return;
var localFile = await DownloadInstallerAsync();
ProcessStartInfo psi = new ProcessStartInfo(localFile);
Process.Start(psi);
ExitApplication();
}
}
public async Task<string> DownloadInstallerAsync()
{
string localFile = GetDownloadFilename();
if (File.Exists(localFile)) File.Delete(localFile);
var blob = new CloudBlockBlob(_uri);
await blob.DownloadToFileAsync(localFile, FileMode.CreateNew);
return localFile;
}
protected abstract bool PromptDownloadAndExit();
protected abstract void ExitApplication();
protected virtual string GetDownloadFilename()
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), _installerExe);
}
private static Uri GetBlobUri(string account, string container, string fileName)
{
string blobName = $"https://{account}.blob.core.windows.net:443/{container}/{fileName}";
return new Uri(blobName);
}
}
}