PowerToys/Wox.Core/Updater.cs

125 lines
4.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Net;
2016-05-11 02:24:15 +08:00
using System.Net.Http;
using System.Net.Sockets;
using System.Linq;
using System.Threading.Tasks;
2017-03-06 08:25:17 +08:00
using System.Windows;
2017-03-08 03:37:38 +08:00
using JetBrains.Annotations;
using Squirrel;
using Newtonsoft.Json;
2017-03-06 08:25:17 +08:00
using Wox.Core.Resource;
using Wox.Infrastructure;
using Wox.Infrastructure.Http;
using Wox.Infrastructure.Logger;
namespace Wox.Core
{
public class Updater
{
public string GitHubRepository { get; }
2017-03-27 07:08:56 +08:00
public Updater(string gitHubRepository)
{
GitHubRepository = gitHubRepository;
}
public async Task UpdateApp()
{
2017-03-27 07:08:56 +08:00
UpdateManager m;
UpdateInfo u;
2016-05-10 06:51:10 +08:00
try
{
m = await GitHubUpdateManager(GitHubRepository);
}
2017-03-06 08:05:03 +08:00
catch (Exception e) when (e is HttpRequestException || e is WebException || e is SocketException)
{
2017-04-05 20:59:16 +08:00
Log.Exception($"|Updater.UpdateApp|Please check your connection and proxy settings to api.github.com.", e);
2017-03-27 07:08:56 +08:00
return;
}
try
{
2017-03-27 07:08:56 +08:00
// UpdateApp CheckForUpdate will return value only if the app is squirrel installed
u = await m.CheckForUpdate().NonNull();
}
2017-03-27 07:08:56 +08:00
catch (Exception e) when (e is HttpRequestException || e is WebException || e is SocketException)
{
2017-04-05 20:59:16 +08:00
Log.Exception($"|Updater.UpdateApp|Check your connection and proxy settings to api.github.com.", e);
2017-03-27 07:08:56 +08:00
m.Dispose();
return;
}
2017-03-27 07:08:56 +08:00
var fr = u.FutureReleaseEntry;
var cr = u.CurrentlyInstalledVersion;
Log.Info($"|Updater.UpdateApp|Future Release <{fr.Formatted()}>");
if (fr.Version > cr.Version)
{
try
{
2017-03-27 07:08:56 +08:00
await m.DownloadReleases(u.ReleasesToApply);
}
2017-03-27 07:08:56 +08:00
catch (Exception e) when (e is HttpRequestException || e is WebException || e is SocketException)
{
2017-04-05 20:59:16 +08:00
Log.Exception($"|Updater.UpdateApp|Check your connection and proxy settings to github-cloud.s3.amazonaws.com.", e);
2017-03-27 07:08:56 +08:00
m.Dispose();
return;
}
2017-03-27 07:08:56 +08:00
await m.ApplyReleases(u);
await m.CreateUninstallerRegistryEntry();
var newVersionTips = this.NewVersinoTips(fr.Version.ToString());
2017-03-27 07:08:56 +08:00
MessageBox.Show(newVersionTips);
2019-08-21 18:18:20 +08:00
Log.Info($"|Updater.UpdateApp|Update success:{newVersionTips}");
}
2017-03-31 01:43:52 +08:00
// always dispose UpdateManager
m.Dispose();
2017-03-06 09:30:47 +08:00
}
2017-03-08 03:37:38 +08:00
[UsedImplicitly]
private class GithubRelease
{
[JsonProperty("prerelease")]
2017-03-27 07:08:56 +08:00
public bool Prerelease { get; [UsedImplicitly] set; }
[JsonProperty("published_at")]
2017-03-27 07:08:56 +08:00
public DateTime PublishedAt { get; [UsedImplicitly] set; }
[JsonProperty("html_url")]
2017-03-27 07:08:56 +08:00
public string HtmlUrl { get; [UsedImplicitly] set; }
}
/// https://github.com/Squirrel/Squirrel.Windows/blob/master/src/Squirrel/UpdateManager.Factory.cs
private async Task<UpdateManager> GitHubUpdateManager(string repository)
{
var uri = new Uri(repository);
2017-03-27 07:08:56 +08:00
var api = $"https://api.github.com/repos{uri.AbsolutePath}/releases";
var json = await Http.Get(api);
var releases = JsonConvert.DeserializeObject<List<GithubRelease>>(json);
2017-03-27 07:08:56 +08:00
var latest = releases.Where(r => !r.Prerelease).OrderByDescending(r => r.PublishedAt).First();
var latestUrl = latest.HtmlUrl.Replace("/tag/", "/download/");
var client = new WebClient { Proxy = Http.WebProxy() };
var downloader = new FileDownloader(client);
var manager = new UpdateManager(latestUrl, urlDownloader: downloader);
return manager;
}
2017-03-06 08:25:17 +08:00
public string NewVersinoTips(string version)
2017-03-06 08:25:17 +08:00
{
var translater = InternationalizationManager.Instance;
var tips = string.Format(translater.GetTranslation("newVersionTips"), version);
return tips;
}
}
}