Consolidate version checking

This commit is contained in:
Matt Nadareski
2020-09-10 10:53:47 -07:00
parent e5c9591d64
commit dd6f7b0794
4 changed files with 76 additions and 60 deletions

View File

@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
@@ -13,7 +12,6 @@ using BurnOutSharp;
using DICUI.Data;
using DICUI.Utilities;
using DICUI.Web;
using Newtonsoft.Json.Linq;
namespace DICUI.Avalonia
{
@@ -599,36 +597,13 @@ namespace DICUI.Avalonia
/// </summary>
private void CheckForUpdatesMenuItemClick(object sender, RoutedEventArgs e)
{
// Get the current internal version
var assemblyVersion = Assembly.GetEntryAssembly().GetName().Version;
string version = $"{assemblyVersion.Major}.{assemblyVersion.Minor}" + (assemblyVersion.Build != 0 ? $".{assemblyVersion.Build}" : string.Empty);
(bool different, string message, string url) = Tools.CheckForNewVersion();
// Get the latest tag from GitHub
using (var client = new RedumpWebClient())
{
client.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0";
// If we have a new version, put it in the clipboard
if (different)
Application.Current.Clipboard.SetTextAsync(url);
// TODO: Figure out a better way than having this hardcoded...
string url = "https://api.github.com/repos/SabreTools/DICUI/releases/latest";
string latestReleaseJsonString = client.DownloadString(url);
var latestReleaseJson = JObject.Parse(latestReleaseJsonString);
string latestTag = latestReleaseJson["tag_name"].ToString();
string releaseUrl = latestReleaseJson["html_url"].ToString();
bool different = version != latestTag;
string message = $"Local version: {version}"
+ $"{Environment.NewLine}Remote version: {latestTag}"
+ (different
? $"{Environment.NewLine}The update URL has been added copied to your clipboard"
: $"{Environment.NewLine}You have the newest version!");
// If we have a new version, put it in the clipboard
if (different)
Application.Current.Clipboard.SetTextAsync(releaseUrl);
MessageBox.Show(this, message, "Version Update Check", MessageBoxButtons.Ok);
}
MessageBox.Show(this, message, "Version Update Check", MessageBoxButtons.Ok);
}
/// <summary>

View File

@@ -0,0 +1,48 @@
using System;
using System.Reflection;
using DICUI.Web;
namespace DICUI.Utilities
{
public class Tools
{
/// <summary>
/// Check for a new DICUI version
/// </summary>
/// <returns>
/// Bool representing if the values are different.
/// String representing the message to display the the user.
/// String representing the new release URL.
/// </returns>
public static (bool different, string message, string url) CheckForNewVersion()
{
// Get current assembly version
string version = GetCurrentVersion();
// Get the latest tag from GitHub
using (var client = new RedumpWebClient())
{
(string tag, string url) = client.GetRemoteVersionAndUrl();
bool different = version != tag;
string message = $"Local version: {version}"
+ $"{Environment.NewLine}Remote version: {tag}"
+ (different
? $"{Environment.NewLine}The update URL has been added copied to your clipboard"
: $"{Environment.NewLine}You have the newest version!");
return (different, message, url);
}
}
/// <summary>
/// Get the current assembly version formatted as a string
/// </summary>
private static string GetCurrentVersion()
{
var assemblyVersion = Assembly.GetEntryAssembly().GetName().Version;
return $"{assemblyVersion.Major}.{assemblyVersion.Minor}" + (assemblyVersion.Build != 0 ? $".{assemblyVersion.Build}" : string.Empty);
}
}
}

View File

@@ -7,6 +7,7 @@ using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using DICUI.Utilities;
using Newtonsoft.Json.Linq;
namespace DICUI.Web
{
@@ -354,6 +355,23 @@ namespace DICUI.Web
return true;
}
/// <summary>
/// Get the latest version of DICUI from GitHub and the release URL
/// </summary>
public (string tag, string url) GetRemoteVersionAndUrl()
{
Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0";
// TODO: Figure out a better way than having this hardcoded...
string url = "https://api.github.com/repos/SabreTools/DICUI/releases/latest";
string latestReleaseJsonString = DownloadString(url);
var latestReleaseJson = JObject.Parse(latestReleaseJsonString);
string latestTag = latestReleaseJson["tag_name"].ToString();
string releaseUrl = latestReleaseJson["html_url"].ToString();
return (latestTag, releaseUrl);
}
/// <summary>
/// Create a new SubmissionInfo object based on a disc page
/// </summary>

View File

@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using WinForms = System.Windows.Forms;
@@ -11,7 +10,6 @@ using BurnOutSharp;
using DICUI.Data;
using DICUI.Utilities;
using DICUI.Web;
using Newtonsoft.Json.Linq;
namespace DICUI.Windows
{
@@ -264,36 +262,13 @@ namespace DICUI.Windows
private void CheckForUpdatesClick(object sender, RoutedEventArgs e)
{
// Get the current internal version
var assemblyVersion = Assembly.GetEntryAssembly().GetName().Version;
string version = $"{assemblyVersion.Major}.{assemblyVersion.Minor}" + (assemblyVersion.Build != 0 ? $".{assemblyVersion.Build}" : string.Empty);
(bool different, string message, string url) = Tools.CheckForNewVersion();
// Get the latest tag from GitHub
using (var client = new RedumpWebClient())
{
client.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0";
// If we have a new version, put it in the clipboard
if (different)
Clipboard.SetText(url);
// TODO: Figure out a better way than having this hardcoded...
string url = "https://api.github.com/repos/SabreTools/DICUI/releases/latest";
string latestReleaseJsonString = client.DownloadString(url);
var latestReleaseJson = JObject.Parse(latestReleaseJsonString);
string latestTag = latestReleaseJson["tag_name"].ToString();
string releaseUrl = latestReleaseJson["html_url"].ToString();
bool different = version != latestTag;
string message = $"Local version: {version}"
+ $"{Environment.NewLine}Remote version: {latestTag}"
+ (different
? $"{Environment.NewLine}The update URL has been added copied to your clipboard"
: $"{Environment.NewLine}You have the newest version!");
// If we have a new version, put it in the clipboard
if (different)
Clipboard.SetText(releaseUrl);
MessageBox.Show(message, "Version Update Check", MessageBoxButton.OK, different ? MessageBoxImage.Exclamation : MessageBoxImage.Information);
}
MessageBox.Show(message, "Version Update Check", MessageBoxButton.OK, different ? MessageBoxImage.Exclamation : MessageBoxImage.Information);
}
public void OnOptionsUpdated()