Flow.Launcher/Flow.Launcher.Core/Updater.cs

158 lines
5.9 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Net;
2016-05-10 18:24:15 +00:00
using System.Net.Http;
using System.Net.Sockets;
using System.Linq;
using System.Threading.Tasks;
2017-03-06 00:25:17 +00:00
using System.Windows;
2017-03-07 19:37:38 +00:00
using JetBrains.Annotations;
using Squirrel;
using Newtonsoft.Json;
2020-04-21 09:12:17 +00:00
using Flow.Launcher.Core.Resource;
using Flow.Launcher.Plugin.SharedCommands;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Http;
using Flow.Launcher.Infrastructure.Logger;
using System.IO;
2020-04-21 09:12:17 +00:00
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
2020-04-21 09:12:17 +00:00
namespace Flow.Launcher.Core
{
public class Updater
{
public string GitHubRepository { get; }
2017-03-26 23:08:56 +00:00
public Updater(string gitHubRepository)
{
GitHubRepository = gitHubRepository;
}
public async Task UpdateApp(IPublicAPI api , bool silentUpdate = true)
{
UpdateManager updateManager;
UpdateInfo newUpdateInfo;
2016-05-09 22:51:10 +00:00
if (!silentUpdate)
api.ShowMsg("Please wait...", "Checking for new update");
try
{
updateManager = await GitHubUpdateManager(GitHubRepository);
}
2017-03-06 00:05:03 +00:00
catch (Exception e) when (e is HttpRequestException || e is WebException || e is SocketException)
{
2017-04-05 12:59:16 +00:00
Log.Exception($"|Updater.UpdateApp|Please check your connection and proxy settings to api.github.com.", e);
2017-03-26 23:08:56 +00:00
return;
}
try
{
2017-03-26 23:08:56 +00:00
// UpdateApp CheckForUpdate will return value only if the app is squirrel installed
newUpdateInfo = await updateManager.CheckForUpdate().NonNull();
}
2017-03-26 23:08:56 +00:00
catch (Exception e) when (e is HttpRequestException || e is WebException || e is SocketException)
{
2017-04-05 12:59:16 +00:00
Log.Exception($"|Updater.UpdateApp|Check your connection and proxy settings to api.github.com.", e);
updateManager.Dispose();
2017-03-26 23:08:56 +00:00
return;
}
var newReleaseVersion = Version.Parse(newUpdateInfo.FutureReleaseEntry.Version.ToString());
var currentVersion = Version.Parse(Constant.Version);
Log.Info($"|Updater.UpdateApp|Future Release <{newUpdateInfo.FutureReleaseEntry.Formatted()}>");
2017-03-26 23:08:56 +00:00
if (newReleaseVersion <= currentVersion)
{
if (!silentUpdate)
2020-04-22 10:26:09 +00:00
MessageBox.Show("You already have the latest Flow Launcher version");
updateManager.Dispose();
return;
}
if (!silentUpdate)
api.ShowMsg("Update found", "Updating...");
try
{
await updateManager.DownloadReleases(newUpdateInfo.ReleasesToApply);
}
catch (Exception e) when (e is HttpRequestException || e is WebException || e is SocketException)
{
Log.Exception($"|Updater.UpdateApp|Check your connection and proxy settings to github-cloud.s3.amazonaws.com.", e);
updateManager.Dispose();
return;
}
await updateManager.ApplyReleases(newUpdateInfo);
if (DataLocation.PortableDataLocationInUse())
{
var targetDestination = updateManager.RootAppDirectory + $"\\app-{newReleaseVersion.ToString()}\\{DataLocation.PortableFolderName}";
2020-09-21 22:56:41 +00:00
FilesFolders.CopyAll(DataLocation.PortableDataPath, targetDestination);
if (!FilesFolders.VerifyBothFolderFilesEqual(DataLocation.PortableDataPath, targetDestination))
MessageBox.Show("Flow Launcher was not able to move your user profile data to the new update version. Please manually " +
$"move your profile data folder from {DataLocation.PortableDataPath} to {targetDestination}");
}
else
{
await updateManager.CreateUninstallerRegistryEntry();
}
var newVersionTips = NewVersinoTips(newReleaseVersion.ToString());
Log.Info($"|Updater.UpdateApp|Update success:{newVersionTips}");
2017-03-30 17:43:52 +00:00
// always dispose UpdateManager
updateManager.Dispose();
if (MessageBox.Show(newVersionTips, "New Update", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
UpdateManager.RestartApp(Constant.ApplicationFileName);
}
2017-03-06 01:30:47 +00:00
}
2017-03-07 19:37:38 +00:00
[UsedImplicitly]
private class GithubRelease
{
[JsonProperty("prerelease")]
2017-03-26 23:08:56 +00:00
public bool Prerelease { get; [UsedImplicitly] set; }
[JsonProperty("published_at")]
2017-03-26 23:08:56 +00:00
public DateTime PublishedAt { get; [UsedImplicitly] set; }
[JsonProperty("html_url")]
2017-03-26 23:08:56 +00: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-26 23:08:56 +00: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-26 23:08:56 +00: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 00:25:17 +00:00
public string NewVersinoTips(string version)
2017-03-06 00:25:17 +00:00
{
var translater = InternationalizationManager.Instance;
var tips = string.Format(translater.GetTranslation("newVersionTips"), version);
return tips;
}
}
}