fix github update error handling #1266

This commit is contained in:
bao-qian 2017-03-27 00:08:56 +01:00
parent 6945eb548f
commit 128671f5e9
6 changed files with 77 additions and 48 deletions

View file

@ -18,77 +18,91 @@ namespace Wox.Core
{
public static class Updater
{
private static readonly Internationalization Translater = InternationalizationManager.Instance;
public static async Task UpdateApp()
{
UpdateManager m;
UpdateInfo u;
try
{
using (var m = await GitHubUpdateManager(Constant.Repository))
{
// UpdateApp CheckForUpdate will return value only if the app is squirrel installed
var e = await m.CheckForUpdate().NonNull();
var fe = e.FutureReleaseEntry;
var ce = e.CurrentlyInstalledVersion;
if (fe.Version > ce.Version)
{
var t = NewVersinoTips(fe.Version.ToString());
MessageBox.Show(t);
await m.DownloadReleases(e.ReleasesToApply);
await m.ApplyReleases(e);
await m.CreateUninstallerRegistryEntry();
Log.Info($"|Updater.UpdateApp|Future Release <{fe.Formatted()}>");
}
}
m = await GitHubUpdateManager(Constant.Repository);
}
catch (Exception e) when (e is HttpRequestException || e is WebException || e is SocketException)
{
Log.Exception("|Updater.UpdateApp|Network error", e);
var checkUpdatesFailed = Translater.GetTranslation("checkUpdatesFailed");
Log.Exception($"|Updater.UpdateApp|{checkUpdatesFailed}", e);
MessageBox.Show(checkUpdatesFailed);
return;
}
catch (Exception e)
{
const string info = "Update.exe not found, not a Squirrel-installed app?";
if (e.Message == info)
{
Log.Error($"|Updater.UpdateApp|{info}");
}
else
{
throw;
}
}
}
private static string NewVersinoTips(string version)
{
var translater = InternationalizationManager.Instance;
var tips = string.Format(translater.GetTranslation("newVersionTips"), version);
return tips;
try
{
// UpdateApp CheckForUpdate will return value only if the app is squirrel installed
u = await m.CheckForUpdate().NonNull();
}
catch (Exception e) when (e is HttpRequestException || e is WebException || e is SocketException)
{
var checkUpdatesFailed = Translater.GetTranslation("checkUpdatesFailed");
Log.Exception($"|Updater.UpdateApp|{checkUpdatesFailed}", e);
MessageBox.Show(checkUpdatesFailed);
m.Dispose();
return;
}
var fr = u.FutureReleaseEntry;
var cr = u.CurrentlyInstalledVersion;
Log.Info($"|Updater.UpdateApp|Future Release <{fr.Formatted()}>");
if (fr.Version > cr.Version)
{
try
{
await m.DownloadReleases(u.ReleasesToApply);
}
catch (Exception e) when (e is HttpRequestException || e is WebException || e is SocketException)
{
var downloadUpdatesFailed = Translater.GetTranslation("downloadUpdatesFailed");
Log.Exception($"|Updater.UpdateApp|{downloadUpdatesFailed}", e);
MessageBox.Show(downloadUpdatesFailed);
m.Dispose();
return;
}
await m.ApplyReleases(u);
await m.CreateUninstallerRegistryEntry();
m.Dispose();
var newVersionTips = Translater.GetTranslation("newVersionTips");
newVersionTips = string.Format(newVersionTips, fr.Version);
MessageBox.Show(newVersionTips);
Log.Info($"|Updater.UpdateApp|Update succeed:{newVersionTips}");
}
}
[UsedImplicitly]
private class GithubRelease
{
[JsonProperty("prerelease")]
public bool Prerelease { get; set; }
public bool Prerelease { get; [UsedImplicitly] set; }
[JsonProperty("published_at")]
public DateTime PublishedAt { get; set; }
public DateTime PublishedAt { get; [UsedImplicitly] set; }
[JsonProperty("html_url")]
public string HtmlUrl { get; set; }
public string HtmlUrl { get; [UsedImplicitly] set; }
}
/// https://github.com/Squirrel/Squirrel.Windows/blob/master/src/Squirrel/UpdateManager.Factory.cs
private static async Task<UpdateManager> GitHubUpdateManager(string repository)
{
var uri = new Uri(repository);
var api = $"https://api.github.com/{uri.AbsolutePath}/releases";
var api = $"https://api.github.com/repos{uri.AbsolutePath}/releases";
var json = await Http.Get(api);
var releases = JsonConvert.DeserializeObject<List<GithubRelease>>(json);
var latest = releases.Where(r => r.Prerelease).OrderByDescending(r => r.PublishedAt).First();
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() };

View file

@ -4,6 +4,7 @@ using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.UserSettings;
namespace Wox.Infrastructure.Http
@ -37,7 +38,6 @@ namespace Wox.Infrastructure.Http
}
}
/// <exception cref="WebException">Can't download file </exception>
public static void Download([NotNull] string url, [NotNull] string filePath)
{
var client = new WebClient { Proxy = WebProxy() };
@ -45,9 +45,9 @@ namespace Wox.Infrastructure.Http
client.DownloadFile(url, filePath);
}
/// <exception cref="WebException">Can't get response from http get </exception>
public static async Task<string> Get([NotNull] string url, string encoding = "UTF-8")
{
Log.Debug($"|Http.Get|Url <{url}>");
var request = WebRequest.CreateHttp(url);
request.Method = "GET";
request.Timeout = 1000;

View file

@ -27,7 +27,7 @@ namespace Wox.Infrastructure.Logger
#if DEBUG
var rule = new LoggingRule("*", LogLevel.Debug, target);
#else
var rule = new LoggingRule("*", LogLevel.Info, target);
var rule = new LoggingRule("*", LogLevel.Debug, target);
#endif
configuration.LoggingRules.Add(rule);
LogManager.Configuration = configuration;

View file

@ -84,7 +84,12 @@
<system:String x:Key="version">Version</system:String>
<system:String x:Key="about_activate_times">You have activated Wox {0} times</system:String>
<system:String x:Key="checkUpdates">Check for Updates</system:String>
<system:String x:Key="newVersionTips">New version {0} is available, please restart Wox</system:String>
<system:String x:Key="newVersionTips">New version {0} is available, please restart Wox.</system:String>
<system:String x:Key="checkUpdatesFailed">Check updates failed, please check your connection and proxy settings to api.github.com.</system:String>
<system:String x:Key="downloadUpdatesFailed">
Download updates failed, please check your connection and proxy settings to github-cloud.s3.amazonaws.com,
or go to https://github.com/Wox-launcher/Wox/releases to download updates manually.
</system:String>
<system:String x:Key="releaseNotes">Release Notes:</system:String>
<!--Action Keyword Setting Dialog-->

View file

@ -84,7 +84,12 @@
<system:String x:Key="version">版本</system:String>
<system:String x:Key="about_activate_times">你已经激活了Wox {0} 次</system:String>
<system:String x:Key="checkUpdates">检查更新</system:String>
<system:String x:Key="newVersionTips">发现新版本 {0} , 请重启 wox</system:String>
<system:String x:Key="newVersionTips">发现新版本 {0} , 请重启 wox。</system:String>
<system:String x:Key="checkUpdatesFailed">检查更新失败,请检查你到 api.github.com 的网络连接和代理设置。</system:String>
<system:String x:Key="downloadUpdatesFailed">
下载更新失败,请检查你到 github-cloud.s3.amazonaws.com, 的网络连接和代理设置,
或去 https://github.com/Wox-launcher/Wox/releases 手动下载更新。
</system:String>
<system:String x:Key="releaseNotes">更新说明:</system:String>
<!--Action Keyword 设置对话框-->

View file

@ -80,7 +80,12 @@
<system:String x:Key="version">版本</system:String>
<system:String x:Key="about_activate_times">您已經啟動了 Wox {0} 次</system:String>
<system:String x:Key="checkUpdates">檢查更新</system:String>
<system:String x:Key="newVersionTips">發現有新版本 {0} , 請重新啟動 Wox</system:String>
<system:String x:Key="newVersionTips">發現有新版本 {0} 請重新啟動 Wox。</system:String>
<system:String x:Key="checkUpdatesFailed">检查更新失败,请检查你到 api.github.com 的网络连接和代理设置。</system:String>
<system:String x:Key="downloadUpdatesFailed">
下载更新失败,请检查你到 github-cloud.s3.amazonaws.com, 的网络连接和代理设置,
或去 https://github.com/Wox-launcher/Wox/releases 手动下载更新。
</system:String>
<system:String x:Key="releaseNotes">更新說明:</system:String>
<!--Action Keyword 設定對話框-->