diff --git a/DICUI.Avalonia/MainWindow.axaml.cs b/DICUI.Avalonia/MainWindow.axaml.cs index f1769342..cbb5f672 100644 --- a/DICUI.Avalonia/MainWindow.axaml.cs +++ b/DICUI.Avalonia/MainWindow.axaml.cs @@ -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 /// 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); } /// diff --git a/DICUI.Library/Utilities/Tools.cs b/DICUI.Library/Utilities/Tools.cs new file mode 100644 index 00000000..059d0e54 --- /dev/null +++ b/DICUI.Library/Utilities/Tools.cs @@ -0,0 +1,48 @@ +using System; +using System.Reflection; +using DICUI.Web; + +namespace DICUI.Utilities +{ + public class Tools + { + /// + /// Check for a new DICUI version + /// + /// + /// Bool representing if the values are different. + /// String representing the message to display the the user. + /// String representing the new release URL. + /// + 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); + } + } + + /// + /// Get the current assembly version formatted as a string + /// + private static string GetCurrentVersion() + { + var assemblyVersion = Assembly.GetEntryAssembly().GetName().Version; + return $"{assemblyVersion.Major}.{assemblyVersion.Minor}" + (assemblyVersion.Build != 0 ? $".{assemblyVersion.Build}" : string.Empty); + } + + } +} diff --git a/DICUI.Library/Web/RedumpWebClient.cs b/DICUI.Library/Web/RedumpWebClient.cs index bb71c276..1a09290b 100644 --- a/DICUI.Library/Web/RedumpWebClient.cs +++ b/DICUI.Library/Web/RedumpWebClient.cs @@ -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; } + /// + /// Get the latest version of DICUI from GitHub and the release URL + /// + 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); + } + /// /// Create a new SubmissionInfo object based on a disc page /// diff --git a/DICUI/Windows/MainWindow.xaml.cs b/DICUI/Windows/MainWindow.xaml.cs index fe814d90..d397ffe1 100644 --- a/DICUI/Windows/MainWindow.xaml.cs +++ b/DICUI/Windows/MainWindow.xaml.cs @@ -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()