PowerToys/Wox.Core/Updater.cs

113 lines
3.5 KiB
C#
Raw Normal View History

using System;
using System.Diagnostics;
using System.Net;
2016-05-11 02:24:15 +08:00
using System.Net.Http;
using System.Net.Sockets;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Squirrel;
using Wox.Infrastructure.Http;
using Wox.Infrastructure.Logger;
namespace Wox.Core
{
public static class Updater
{
[Conditional("RELEASE")]
public static async void UpdateApp()
{
2016-05-10 06:51:10 +08:00
2017-01-24 08:24:20 +08:00
var client = new WebClient { Proxy = Http.WebProxy() };
2016-05-10 06:51:10 +08:00
var downloader = new FileDownloader(client);
try
{
// todo 5/9 the return value of UpdateApp() is NULL, fucking useless!
2016-05-11 02:24:15 +08:00
using (
var updater =
2016-05-19 02:38:43 +08:00
await UpdateManager.GitHubUpdateManager(Infrastructure.Constant.Github, urlDownloader: downloader))
{
await updater.UpdateApp();
}
}
2016-05-11 02:24:15 +08:00
catch (HttpRequestException he)
{
2017-01-24 08:24:20 +08:00
Log.Exception("|Updater.UpdateApp|network error", he);
2016-05-11 02:24:15 +08:00
}
catch (WebException we)
{
2017-01-24 08:24:20 +08:00
Log.Exception("|Updater.UpdateApp|network error", we);
2016-05-11 02:24:15 +08:00
}
catch (SocketException sc)
{
2017-01-24 08:24:20 +08:00
Log.Exception("|Updater.UpdateApp|Socket exception happened, which method cause this exception??", sc);
}
catch (Exception exception)
{
const string info = "Update.exe not found, not a Squirrel-installed app?";
if (exception.Message == info)
{
2017-01-24 08:24:20 +08:00
Log.Warn($"|Updater.UpdateApp|{info}");
}
else
{
throw;
}
}
}
public static async Task<string> NewVersion()
{
const string githubAPI = @"https://api.github.com/repos/wox-launcher/wox/releases/latest";
string response;
try
{
2016-06-19 23:18:43 +08:00
response = await Http.Get(githubAPI);
}
catch (WebException e)
{
2017-01-24 08:24:20 +08:00
Log.Exception("|Updater.NewVersion|Can't connect to github api to check new version", e);
return string.Empty;
}
if (!string.IsNullOrEmpty(response))
{
JContainer json;
try
{
json = (JContainer)JsonConvert.DeserializeObject(response);
}
catch (JsonSerializationException e)
{
2017-01-24 08:24:20 +08:00
Log.Exception("|Updater.NewVersion|can't parse response", e);
return string.Empty;
}
var version = json?["tag_name"]?.ToString();
if (!string.IsNullOrEmpty(version))
{
return version;
}
else
{
2017-01-24 08:24:20 +08:00
Log.Warn("|Updater.NewVersion|Can't find tag_name from Github API response");
return string.Empty;
}
}
else
{
2017-01-24 08:24:20 +08:00
Log.Warn("|Updater.NewVersion|Can't get response from Github API");
return string.Empty;
}
}
public static int NumericVersion(string version)
{
var newVersion = version.Replace("v", ".").Replace(".", "").Replace("*", "");
return int.Parse(newVersion);
}
}
}