-
Notifications
You must be signed in to change notification settings - Fork 0
/
GitHubUpdateManager.cs
128 lines (102 loc) · 3.59 KB
/
GitHubUpdateManager.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
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
using GitHubUpdateManager.Extensions;
using GitHubUpdateManager.Library;
using GitHubUpdateManager.Model;
using Octokit;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace GitHubUpdateManager
{
public class GitHubUpdateManager
{
private Library.CustomOctokit.GitHubClient github;
private string RepositoryName { get; set; }
private string RepositoryOwnerName { get; set; }
private string ReleaseVersionPattern { get; set; }
public List<RepositoryRelease> Updates { get; private set; }
public IApplicationUpdater ApplicationUpdater { get; set; }
public GitHubUpdateManager(string repositoryName, string repositoryOwnerName, string releaseVersionPattern)
{
RepositoryName = repositoryName;
RepositoryOwnerName = repositoryOwnerName;
ReleaseVersionPattern = releaseVersionPattern;
github = new Library.CustomOctokit.GitHubClient(new ProductHeaderValue(RepositoryName));
Updates = new List<RepositoryRelease>();
}
public async Task CheckForReleasesAsync(Version currentVersion, bool allowPrereleases = false)
{
Updates.Clear();
var releases = await github.Releases.GetPaginatedAsync(RepositoryOwnerName, RepositoryName, 1, 10);
foreach (var release in releases)
{
if (!allowPrereleases && release.Prerelease)
{
continue;
}
var assets = await github.Releases.GetAllAssets(RepositoryOwnerName, RepositoryName, release.Id);
var update = new RepositoryRelease(release, new List<ReleaseAsset>(assets), ReleaseVersionPattern);
Updates.Add(update);
}
NotifyUpdatesAvailable(currentVersion);
}
public async Task DownloadReleaseAssetsAsync(RepositoryRelease update, string downloadLocation)
{
await Task.Run(new Action(() => {
if (update.Assets.Count == 0)
{
return;
}
var releaseFolderName = $@"{update.ID}_{update.Name}";
var downloadReleaseLocationPath = $"{downloadLocation}/{releaseFolderName}";
update.AssetsDownloadLocation = downloadReleaseLocationPath;
foreach (var asset in update.Assets)
{
var id = asset.Id;
var filename = asset.Name;
var url = asset.BrowserDownloadUrl;
var downloadAssetFilePath = $@"{downloadReleaseLocationPath }/{id}_{filename}";
var request = (HttpWebRequest)WebRequest.Create(url);
FileSystemExtensions.EnsureDirectory(downloadAssetFilePath);
using (var response = (HttpWebResponse)request.GetResponse())
using (var responseStream = response.GetResponseStream())
using (var fileStream = new FileStream(downloadAssetFilePath, System.IO.FileMode.Create, FileAccess.Write))
{
responseStream.Seek(0, SeekOrigin.Begin);
responseStream.CopyTo(fileStream);
}
var downloadedAsset = new DownloadedReleaseAsset(asset, downloadAssetFilePath);
update.AssetsDownloaded.Add(downloadedAsset);
}
}));
NotifyUpdateDownloaded(update);
}
private void NotifyUpdatesAvailable(Version currentVersion)
{
if (ApplicationUpdater == null)
{
return;
}
var relevantUpdates = new List<RepositoryRelease>();
foreach (var update in Updates)
{
if (update.Version > currentVersion)
{
relevantUpdates.Add(update);
}
}
if (relevantUpdates.Count > 0)
{
ApplicationUpdater.UpdatesAvailable(relevantUpdates);
}
}
private void NotifyUpdateDownloaded(RepositoryRelease update)
{
ApplicationUpdater?.UpdateDownloaded(update);
}
}
}