mirror of
https://github.com/bloxstraplabs/bloxstrap.git
synced 2025-04-21 18:11:27 -07:00
feat(helpers/updater): re-write updater
This commit is contained in:
parent
4630b3c9bd
commit
fe1eb6cf3d
@ -210,7 +210,8 @@ namespace Bloxstrap
|
|||||||
|
|
||||||
#if !DEBUG
|
#if !DEBUG
|
||||||
if (!IsUninstall && !IsFirstRun)
|
if (!IsUninstall && !IsFirstRun)
|
||||||
Updater.CheckInstalledVersion();
|
Updater.CheckIsSuccessfulyUpdate();
|
||||||
|
Updater.CheckForUpdate();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
string commandLine = "";
|
string commandLine = "";
|
||||||
|
@ -7,8 +7,8 @@
|
|||||||
<UseWPF>true</UseWPF>
|
<UseWPF>true</UseWPF>
|
||||||
<UseWindowsForms>True</UseWindowsForms>
|
<UseWindowsForms>True</UseWindowsForms>
|
||||||
<ApplicationIcon>Bloxstrap.ico</ApplicationIcon>
|
<ApplicationIcon>Bloxstrap.ico</ApplicationIcon>
|
||||||
<Version>2.2.0</Version>
|
<Version>2.3.0</Version>
|
||||||
<FileVersion>2.2.0.0</FileVersion>
|
<FileVersion>2.3.0.0</FileVersion>
|
||||||
<ApplicationManifest>app.manifest</ApplicationManifest>
|
<ApplicationManifest>app.manifest</ApplicationManifest>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
@ -5,40 +5,66 @@ using System.Windows;
|
|||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using System.Net.Http;
|
||||||
|
|
||||||
using Bloxstrap.Properties;
|
using Bloxstrap.Properties;
|
||||||
using Bloxstrap.Views;
|
using Bloxstrap.Views;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Net;
|
||||||
|
|
||||||
namespace Bloxstrap.Helpers
|
namespace Bloxstrap.Helpers
|
||||||
{
|
{
|
||||||
public class Updater
|
public class Updater
|
||||||
{
|
{
|
||||||
public static void CheckInstalledVersion()
|
public static void CheckIsSuccessfulyUpdate()
|
||||||
{
|
{
|
||||||
if (Environment.ProcessPath is null || !File.Exists(Directories.Application) || Environment.ProcessPath == Directories.Application)
|
string updatePath = Path.Combine(Directories.Base, "Bloxstrap-Update-Version.exe");
|
||||||
|
|
||||||
|
if (!File.Exists(updatePath) || !File.Exists(Directories.Application))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// 2.0.0 downloads updates to <BaseFolder>/Updates so lol
|
var applicationInfo = new FileInfo(Directories.Application);
|
||||||
bool isAutoUpgrade = Environment.ProcessPath.StartsWith(Path.Combine(Directories.Base, "Updates")) || Environment.ProcessPath.StartsWith(Path.Combine(Directories.LocalAppData, "Temp"));
|
var updateInfo = new FileInfo(updatePath);
|
||||||
|
|
||||||
// if downloaded version doesn't match, replace installed version with downloaded version
|
if (applicationInfo.Length == updateInfo.Length)
|
||||||
FileVersionInfo currentVersionInfo = FileVersionInfo.GetVersionInfo(Environment.ProcessPath);
|
{
|
||||||
FileVersionInfo installedVersionInfo = FileVersionInfo.GetVersionInfo(Directories.Application);
|
App.ShowMessageBox("Successfully update Bloxstrap", MessageBoxImage.Information);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
App.ShowMessageBox("Failed to update Bloxstrap", MessageBoxImage.Error);
|
||||||
|
}
|
||||||
|
|
||||||
if (installedVersionInfo.ProductVersion == currentVersionInfo.ProductVersion)
|
|
||||||
|
File.Delete(updatePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async void CheckForUpdate()
|
||||||
|
{
|
||||||
|
using var httpClient = new HttpClient();
|
||||||
|
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3");
|
||||||
|
|
||||||
|
string url = $"https://api.github.com/repos/{App.ProjectRepository}/releases/latest";
|
||||||
|
var response = await httpClient.GetAsync(url);
|
||||||
|
var responseCOntent = await response.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
|
var jsonDoc = JsonDocument.Parse(responseCOntent);
|
||||||
|
|
||||||
|
var tagValue = jsonDoc.RootElement.GetProperty("tag_name").GetString().Replace("v", "");
|
||||||
|
|
||||||
|
if (tagValue == App.Version)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
MessageBoxResult result;
|
MessageBoxResult result;
|
||||||
|
|
||||||
// silently upgrade version if the command line flag is set or if we're launching from an auto update
|
if (App.IsUpgrade)
|
||||||
if (App.IsUpgrade || isAutoUpgrade)
|
|
||||||
{
|
{
|
||||||
result = MessageBoxResult.Yes;
|
result = MessageBoxResult.Yes;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
result = App.ShowMessageBox(
|
result = App.ShowMessageBox(
|
||||||
$"The version of {App.ProjectName} you've launched is different to the version you currently have installed.\nWould you like to upgrade your currently installed version?",
|
"Would you like to update to the latest version of Bloxstrap?",
|
||||||
MessageBoxImage.Question,
|
MessageBoxImage.Question,
|
||||||
MessageBoxButton.YesNo
|
MessageBoxButton.YesNo
|
||||||
);
|
);
|
||||||
@ -47,68 +73,35 @@ namespace Bloxstrap.Helpers
|
|||||||
if (result != MessageBoxResult.Yes)
|
if (result != MessageBoxResult.Yes)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// yes, this is EXTREMELY hacky, but the updater process that launched the
|
string fileName = "";
|
||||||
// new version may still be open and so we have to wait for it to close
|
|
||||||
int attempts = 0;
|
|
||||||
while (attempts < 10)
|
|
||||||
{
|
|
||||||
attempts++;
|
|
||||||
|
|
||||||
try
|
/// Why i didn't do it in short hand if else
|
||||||
|
if (IntPtr.Size == 8)
|
||||||
{
|
{
|
||||||
File.Delete(Directories.Application);
|
fileName = $"Bloxstrap-v{tagValue}-x64.exe";
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
catch (Exception)
|
else
|
||||||
{
|
{
|
||||||
if (attempts == 1)
|
fileName = $"Bloxstrap-v{tagValue}-x86.exe";
|
||||||
App.Logger.WriteLine("[Updater::CheckInstalledVersion] Waiting for write permissions to update version");
|
|
||||||
|
|
||||||
Thread.Sleep(500);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (attempts == 10)
|
string downloadUrl = $"https://github.com/pizzaboxer/bloxstrap/releases/download/v{tagValue}/{fileName}";
|
||||||
{
|
string downloadPath = Path.Combine(Directories.Base, "Bloxstrap-Update-Version.exe");
|
||||||
App.Logger.WriteLine("[Updater::CheckInstalledVersion] Failed to update! (Could not get write permissions after 5 seconds)");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
File.Copy(Environment.ProcessPath, Directories.Application);
|
WebClient client = new WebClient();
|
||||||
|
client.DownloadFile(downloadUrl, downloadPath);
|
||||||
|
App.Logger.WriteLine("[Updater::CheckForUpdate] Downloaded new bloxstrap version: " + downloadPath);
|
||||||
|
App.Logger.WriteLine("[Updater::CheckForUpdate] Restarting bloxstrap to update...");
|
||||||
|
|
||||||
Bootstrapper.Register();
|
/// Use ping command to wait 5 s before replace because it impossible to replace
|
||||||
|
/// file without closing it process.
|
||||||
|
ProcessStartInfo startInfo = new ProcessStartInfo();
|
||||||
|
startInfo.FileName = "cmd.exe";
|
||||||
|
startInfo.Arguments = $"/c ping 127.0.0.1 -n 5 > nul && copy /y {downloadPath} {Directories.Application}";
|
||||||
|
///startInfo.WindowStyle = ProcessWindowStyle.Hidden;
|
||||||
|
|
||||||
if (isAutoUpgrade)
|
Process.Start(startInfo);
|
||||||
{
|
|
||||||
NotifyIcon notification = new()
|
|
||||||
{
|
|
||||||
Icon = Resources.IconBloxstrap,
|
|
||||||
Text = "Bloxstrap",
|
|
||||||
Visible = true,
|
|
||||||
BalloonTipTitle = $"Bloxstrap has been upgraded to v{currentVersionInfo.ProductVersion}",
|
|
||||||
BalloonTipText = "Click here to see what's new in this version"
|
|
||||||
};
|
|
||||||
|
|
||||||
notification.BalloonTipClicked += (_, _) => Utilities.OpenWebsite($"https://github.com/{App.ProjectRepository}/releases/tag/v{currentVersionInfo.ProductVersion}");
|
|
||||||
notification.ShowBalloonTip(30);
|
|
||||||
|
|
||||||
Task.Run(() =>
|
|
||||||
{
|
|
||||||
Task.Delay(30000).Wait();
|
|
||||||
notification.Dispose();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else if (!App.IsQuiet)
|
|
||||||
{
|
|
||||||
App.ShowMessageBox(
|
|
||||||
$"{App.ProjectName} has been updated to v{currentVersionInfo.ProductVersion}",
|
|
||||||
MessageBoxImage.Information,
|
|
||||||
MessageBoxButton.OK
|
|
||||||
);
|
|
||||||
|
|
||||||
new MainWindow().ShowDialog();
|
|
||||||
App.Terminate();
|
App.Terminate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user