Skip to content

Commit

Permalink
Use GitHub url for UpdateManager
Browse files Browse the repository at this point in the history
  • Loading branch information
BarryThePenguin committed May 15, 2015
1 parent e062fa8 commit 1c91d99
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Squirrel/Squirrel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Xml.Linq" />
<Reference Include="Microsoft.CSharp" />
Expand Down Expand Up @@ -106,6 +108,7 @@
<Compile Include="UpdateInfo.cs" />
<Compile Include="UpdateManager.CheckForUpdates.cs" />
<Compile Include="UpdateManager.cs" />
<Compile Include="UpdateManager.Factory.cs" />
<Compile Include="Utility.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
65 changes: 65 additions & 0 deletions src/Squirrel/UpdateManager.Factory.cs
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);
}
}
}
}

0 comments on commit 1c91d99

Please sign in to comment.