2016-05-10 03:40:59 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Net;
|
2016-05-11 02:24:15 +08:00
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Net.Sockets;
|
2016-05-10 03:40:59 +08:00
|
|
|
|
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
|
|
|
|
|
{
|
2017-03-06 07:59:10 +08:00
|
|
|
|
public static async Task UpdateApp()
|
2016-05-10 03:40:59 +08:00
|
|
|
|
{
|
2016-05-10 06:51:10 +08:00
|
|
|
|
|
2017-03-06 08:05:03 +08:00
|
|
|
|
var c = new WebClient { Proxy = Http.WebProxy() };
|
|
|
|
|
var d = new FileDownloader(c);
|
2016-05-10 06:51:10 +08:00
|
|
|
|
|
2016-05-10 03:40:59 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-03-06 08:05:03 +08:00
|
|
|
|
const string url = Infrastructure.Constant.Github;
|
2016-05-10 03:40:59 +08:00
|
|
|
|
// todo 5/9 the return value of UpdateApp() is NULL, fucking useless!
|
2017-03-06 08:05:03 +08:00
|
|
|
|
using (var m = await UpdateManager.GitHubUpdateManager(url, urlDownloader: d))
|
2016-05-10 03:40:59 +08:00
|
|
|
|
{
|
2017-03-06 08:05:03 +08:00
|
|
|
|
await m.UpdateApp();
|
2016-05-10 03:40:59 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-06 08:05:03 +08:00
|
|
|
|
catch (Exception e) when (e is HttpRequestException || e is WebException || e is SocketException)
|
2016-05-10 03:40:59 +08:00
|
|
|
|
{
|
2017-03-06 08:05:03 +08:00
|
|
|
|
Log.Exception("|Updater.UpdateApp|Network error", e);
|
|
|
|
|
|
2016-05-10 03:40:59 +08:00
|
|
|
|
}
|
2017-03-06 08:05:03 +08:00
|
|
|
|
catch (Exception e)
|
2016-05-10 03:40:59 +08:00
|
|
|
|
{
|
|
|
|
|
const string info = "Update.exe not found, not a Squirrel-installed app?";
|
2017-03-06 08:05:03 +08:00
|
|
|
|
if (e.Message == info)
|
2016-05-10 03:40:59 +08:00
|
|
|
|
{
|
2017-03-06 08:05:03 +08:00
|
|
|
|
Log.Error($"|Updater.UpdateApp|{info}");
|
2016-05-10 03:40:59 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-11 03:26:47 +08:00
|
|
|
|
|
2016-05-10 03:40:59 +08:00
|
|
|
|
public static async Task<string> NewVersion()
|
|
|
|
|
{
|
|
|
|
|
const string githubAPI = @"https://api.github.com/repos/wox-launcher/wox/releases/latest";
|
2016-05-11 03:26:47 +08:00
|
|
|
|
|
|
|
|
|
string response;
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-06-19 23:18:43 +08:00
|
|
|
|
response = await Http.Get(githubAPI);
|
2016-05-11 03:26:47 +08:00
|
|
|
|
}
|
|
|
|
|
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);
|
2016-05-11 03:26:47 +08:00
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
2016-05-10 03:40:59 +08:00
|
|
|
|
|
|
|
|
|
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);
|
2016-05-10 03:40:59 +08:00
|
|
|
|
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");
|
2016-05-10 03:40:59 +08:00
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-01-24 08:24:20 +08:00
|
|
|
|
Log.Warn("|Updater.NewVersion|Can't get response from Github API");
|
2016-05-10 03:40:59 +08:00
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int NumericVersion(string version)
|
|
|
|
|
{
|
|
|
|
|
var newVersion = version.Replace("v", ".").Replace(".", "").Replace("*", "");
|
|
|
|
|
return int.Parse(newVersion);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|