forked from Squirrel/Squirrel.Windows
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e062fa8
commit 1c91d99
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using Splat; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Net.Http.Formatting; | ||
using System.Net.Http.Headers; | ||
using System.Reflection; | ||
using System.Runtime.Serialization; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Squirrel | ||
{ | ||
public sealed partial class UpdateManager | ||
{ | ||
const string gitHubUrl = "https://api.github.com"; | ||
|
||
[DataContract] | ||
public class Release | ||
{ | ||
[DataMember(Name = "prerelease")] | ||
public bool Prerelease { get; set; } | ||
|
||
[DataMember(Name = "published_at")] | ||
public DateTime PublishedAt { get; set; } | ||
|
||
[DataMember(Name = "html_url")] | ||
public string HtmlUrl { get; set; } | ||
} | ||
|
||
public static async Task<UpdateManager> GitHubUpdateManager( | ||
string repoUrl, | ||
string applicationName = null, | ||
string rootDirectory = null, | ||
IFileDownloader urlDownloader = null, | ||
bool prerelease = false) | ||
{ | ||
var repoUri = new Uri(repoUrl); | ||
var userAgent = new ProductInfoHeaderValue("Squirrel", Assembly.GetExecutingAssembly().GetName().Version.ToString()); | ||
|
||
if (repoUri.Segments.Count() != 3) | ||
{ | ||
throw new Exception("Repo URL must be to the root URL of the repo e.g. https://github.com/myuser/myrepo"); | ||
} | ||
|
||
using (var client = new HttpClient() { BaseAddress = new Uri(gitHubUrl) }) | ||
{ | ||
client.DefaultRequestHeaders.UserAgent.Add(userAgent); | ||
var response = await client.GetAsync(String.Format("/repos{0}/releases", repoUri.PathAndQuery)); | ||
response.EnsureSuccessStatusCode(); | ||
|
||
var releases = await response.Content.ReadAsAsync<IEnumerable<Release>>(); | ||
var latestRelease = releases | ||
.Where(x => prerelease ? x.Prerelease : !x.Prerelease) | ||
.OrderByDescending(x => x.PublishedAt) | ||
.First(); | ||
|
||
var latestReleaseUrl = latestRelease.HtmlUrl.Replace("/tag/", "/download/"); | ||
|
||
return new UpdateManager(latestReleaseUrl, applicationName, rootDirectory, urlDownloader); | ||
} | ||
} | ||
} | ||
} |