From f2ba4338595d22fe3131622e5f15d30cf33f18a2 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Tue, 28 May 2024 11:57:53 -0400 Subject: [PATCH] Merge VersionTool into FrontendTool --- CHANGELIST.md | 1 + MPF.Core/VersionTool.cs | 99 ------------------------ MPF.Frontend/FrontendTool.cs | 96 +++++++++++++++++++++++ MPF.Frontend/SubmissionGenerator.cs | 2 +- MPF.Frontend/ViewModels/MainViewModel.cs | 4 +- 5 files changed, 100 insertions(+), 102 deletions(-) delete mode 100644 MPF.Core/VersionTool.cs diff --git a/CHANGELIST.md b/CHANGELIST.md index 4c521afc..687dc3cc 100644 --- a/CHANGELIST.md +++ b/CHANGELIST.md @@ -143,6 +143,7 @@ - Move InfoTool to root of Core - Create FrontendTool and move some methods to it - Clear out InfoTool and remove +- Merge VersionTool into FrontendTool ### 3.1.9a (2024-05-21) diff --git a/MPF.Core/VersionTool.cs b/MPF.Core/VersionTool.cs deleted file mode 100644 index 7fae6ba4..00000000 --- a/MPF.Core/VersionTool.cs +++ /dev/null @@ -1,99 +0,0 @@ -using System; -using System.Reflection; - -namespace MPF.Core -{ - public static class VersionTool - { - /// - /// Check for a new MPF 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() - { - try - { - // Get current assembly version - var assemblyVersion = Assembly.GetEntryAssembly()?.GetName()?.Version; - if (assemblyVersion == null) - return (false, "Assembly version could not be determined", null); - - string version = $"{assemblyVersion.Major}.{assemblyVersion.Minor}.{assemblyVersion.Build}"; - - // Get the latest tag from GitHub - var (tag, url) = GetRemoteVersionAndUrl(); - bool different = version != tag && tag != null; - - 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); - } - catch (Exception ex) - { - return (false, ex.ToString(), null); - } - } - - /// - /// Get the current informational version formatted as a string - /// - public static string? GetCurrentVersion() - { - try - { - var assembly = Assembly.GetEntryAssembly(); - if (assembly == null) - return null; - - var assemblyVersion = Attribute.GetCustomAttribute(assembly, typeof(AssemblyInformationalVersionAttribute)) as AssemblyInformationalVersionAttribute; - return assemblyVersion?.InformationalVersion; - } - catch (Exception ex) - { - return ex.ToString(); - } - } - - /// - /// Get the latest version of MPF from GitHub and the release URL - /// - private static (string? tag, string? url) GetRemoteVersionAndUrl() - { -#if NET20 || NET35 || NET40 - // Not supported in .NET Frameworks 2.0, 3.5, or 4.0 - return (null, null); -#else - using var hc = new System.Net.Http.HttpClient(); -#if NET452 - System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12; -#endif - - // TODO: Figure out a better way than having this hardcoded... - string url = "https://api.github.com/repos/SabreTools/MPF/releases/latest"; - var message = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, url); - message.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0"); - var latestReleaseJsonString = hc.SendAsync(message)?.ConfigureAwait(false).GetAwaiter().GetResult() - .Content?.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult(); - if (latestReleaseJsonString == null) - return (null, null); - - var latestReleaseJson = Newtonsoft.Json.Linq.JObject.Parse(latestReleaseJsonString); - if (latestReleaseJson == null) - return (null, null); - - var latestTag = latestReleaseJson["tag_name"]?.ToString(); - var releaseUrl = latestReleaseJson["html_url"]?.ToString(); - - return (latestTag, releaseUrl); -#endif - } - } -} diff --git a/MPF.Frontend/FrontendTool.cs b/MPF.Frontend/FrontendTool.cs index 99603ef7..d997a6a1 100644 --- a/MPF.Frontend/FrontendTool.cs +++ b/MPF.Frontend/FrontendTool.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Linq; +using System.Reflection; using System.Text; using SabreTools.RedumpLib.Data; @@ -522,5 +523,100 @@ namespace MPF.Frontend } #endregion + + #region Versioning + + /// + /// Check for a new MPF 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() + { + try + { + // Get current assembly version + var assemblyVersion = Assembly.GetEntryAssembly()?.GetName()?.Version; + if (assemblyVersion == null) + return (false, "Assembly version could not be determined", null); + + string version = $"{assemblyVersion.Major}.{assemblyVersion.Minor}.{assemblyVersion.Build}"; + + // Get the latest tag from GitHub + var (tag, url) = GetRemoteVersionAndUrl(); + bool different = version != tag && tag != null; + + 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); + } + catch (Exception ex) + { + return (false, ex.ToString(), null); + } + } + + /// + /// Get the current informational version formatted as a string + /// + public static string? GetCurrentVersion() + { + try + { + var assembly = Assembly.GetEntryAssembly(); + if (assembly == null) + return null; + + var assemblyVersion = Attribute.GetCustomAttribute(assembly, typeof(AssemblyInformationalVersionAttribute)) as AssemblyInformationalVersionAttribute; + return assemblyVersion?.InformationalVersion; + } + catch (Exception ex) + { + return ex.ToString(); + } + } + + /// + /// Get the latest version of MPF from GitHub and the release URL + /// + private static (string? tag, string? url) GetRemoteVersionAndUrl() + { +#if NET20 || NET35 || NET40 + // Not supported in .NET Frameworks 2.0, 3.5, or 4.0 + return (null, null); +#else + using var hc = new System.Net.Http.HttpClient(); +#if NET452 + System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12; +#endif + + // TODO: Figure out a better way than having this hardcoded... + string url = "https://api.github.com/repos/SabreTools/MPF/releases/latest"; + var message = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, url); + message.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0"); + var latestReleaseJsonString = hc.SendAsync(message)?.ConfigureAwait(false).GetAwaiter().GetResult() + .Content?.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult(); + if (latestReleaseJsonString == null) + return (null, null); + + var latestReleaseJson = Newtonsoft.Json.Linq.JObject.Parse(latestReleaseJsonString); + if (latestReleaseJson == null) + return (null, null); + + var latestTag = latestReleaseJson["tag_name"]?.ToString(); + var releaseUrl = latestReleaseJson["html_url"]?.ToString(); + + return (latestTag, releaseUrl); +#endif + } + + #endregion } } \ No newline at end of file diff --git a/MPF.Frontend/SubmissionGenerator.cs b/MPF.Frontend/SubmissionGenerator.cs index 4521093c..f34d95c7 100644 --- a/MPF.Frontend/SubmissionGenerator.cs +++ b/MPF.Frontend/SubmissionGenerator.cs @@ -368,7 +368,7 @@ namespace MPF.Frontend }, DumpingInfo = new DumpingInfoSection() { - FrontendVersion = VersionTool.GetCurrentVersion(), + FrontendVersion = FrontendTool.GetCurrentVersion(), DumpingProgram = GetDumpingProgramFromProcessor(processor), }, }; diff --git a/MPF.Frontend/ViewModels/MainViewModel.cs b/MPF.Frontend/ViewModels/MainViewModel.cs index 5a72c317..78fe721e 100644 --- a/MPF.Frontend/ViewModels/MainViewModel.cs +++ b/MPF.Frontend/ViewModels/MainViewModel.cs @@ -772,7 +772,7 @@ namespace MPF.Frontend.ViewModels /// public (bool, string, string?) CheckForUpdates() { - (bool different, string message, var url) = VersionTool.CheckForNewVersion(); + (bool different, string message, var url) = FrontendTool.CheckForNewVersion(); SecretLogLn(message); if (url == null) @@ -795,7 +795,7 @@ namespace MPF.Frontend.ViewModels + $"{Environment.NewLine}" + $"{Environment.NewLine}Thanks to everyone who has supported this project!" + $"{Environment.NewLine}" - + $"{Environment.NewLine}Version {VersionTool.GetCurrentVersion()}"; + + $"{Environment.NewLine}Version {FrontendTool.GetCurrentVersion()}"; SecretLogLn(aboutText); return aboutText; }