diff --git a/Wox.Core/Updater.cs b/Wox.Core/Updater.cs index 53a67278eb..47e72d16a6 100644 --- a/Wox.Core/Updater.cs +++ b/Wox.Core/Updater.cs @@ -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 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>(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() }; diff --git a/Wox.Infrastructure/Http/Http.cs b/Wox.Infrastructure/Http/Http.cs index fc93d930a5..d79f3481fc 100644 --- a/Wox.Infrastructure/Http/Http.cs +++ b/Wox.Infrastructure/Http/Http.cs @@ -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 } } - /// Can't download file 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); } - /// Can't get response from http get public static async Task 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; diff --git a/Wox.Infrastructure/Logger/Log.cs b/Wox.Infrastructure/Logger/Log.cs index 48691866c1..c428c5c134 100644 --- a/Wox.Infrastructure/Logger/Log.cs +++ b/Wox.Infrastructure/Logger/Log.cs @@ -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; diff --git a/Wox/Languages/en.xaml b/Wox/Languages/en.xaml index 47e434cd9f..ddc40b2d4f 100644 --- a/Wox/Languages/en.xaml +++ b/Wox/Languages/en.xaml @@ -84,7 +84,12 @@ Version You have activated Wox {0} times Check for Updates - New version {0} is available, please restart Wox + New version {0} is available, please restart Wox. + Check updates failed, please check your connection and proxy settings to api.github.com. + + 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. + Release Notes: diff --git a/Wox/Languages/zh-cn.xaml b/Wox/Languages/zh-cn.xaml index cbad578255..7332046b99 100644 --- a/Wox/Languages/zh-cn.xaml +++ b/Wox/Languages/zh-cn.xaml @@ -84,7 +84,12 @@ 版本 你已经激活了Wox {0} 次 检查更新 - 发现新版本 {0} , 请重启 wox + 发现新版本 {0} , 请重启 wox。 + 检查更新失败,请检查你到 api.github.com 的网络连接和代理设置。 + + 下载更新失败,请检查你到 github-cloud.s3.amazonaws.com, 的网络连接和代理设置, + 或去 https://github.com/Wox-launcher/Wox/releases 手动下载更新。 + 更新说明: diff --git a/Wox/Languages/zh-tw.xaml b/Wox/Languages/zh-tw.xaml index 1c9a62031b..61ca37344f 100644 --- a/Wox/Languages/zh-tw.xaml +++ b/Wox/Languages/zh-tw.xaml @@ -80,7 +80,12 @@ 版本 您已經啟動了 Wox {0} 次 檢查更新 - 發現有新版本 {0} , 請重新啟動 Wox + 發現有新版本 {0}, 請重新啟動 Wox。 + 检查更新失败,请检查你到 api.github.com 的网络连接和代理设置。 + + 下载更新失败,请检查你到 github-cloud.s3.amazonaws.com, 的网络连接和代理设置, + 或去 https://github.com/Wox-launcher/Wox/releases 手动下载更新。 + 更新說明: