From 2d366141a96c07f367331e12fad1abf39b881b1f Mon Sep 17 00:00:00 2001 From: Jordan Wages Date: Sat, 5 Jan 2019 20:39:52 -0600 Subject: [PATCH] Add non-connection failsafe Added a try/catch as a failsafe for non-operational networks and any other errors involving using Octokit to check for updates on github. Fixes #3. --- butterflow-ui/MainWindow.xaml.cs | 13 ++--- butterflow-ui/OctokitWrapper.cs | 85 +++++++++++++++++------------- butterflow-ui/butterflow-ui.csproj | 4 +- butterflow-ui/packages.config | 1 - 4 files changed, 56 insertions(+), 47 deletions(-) diff --git a/butterflow-ui/MainWindow.xaml.cs b/butterflow-ui/MainWindow.xaml.cs index c9dfade..0248dee 100644 --- a/butterflow-ui/MainWindow.xaml.cs +++ b/butterflow-ui/MainWindow.xaml.cs @@ -53,17 +53,18 @@ namespace butterflow_ui InitializeComponent(); // Check for updates. - if (OctokitWrapper.CurrentVersionStatus == OctokitWrapper.VersionStatus.behind) + if (OctokitWrapper.CurrentVersionStatus == OctokitWrapper.VersionStatus.Behind) { var updateMessageBoxResult = MessageBox.Show(string.Format("{0} {1}", Localization.Localization.BehindVersionStatusDescription, Localization.Localization.BehindVersionQuestion), Localization.Localization.UpdateAvailableLabel, MessageBoxButton.YesNo, MessageBoxImage.Information); // If the user wants to update now, take them to the latest release on github and close this window. - if(updateMessageBoxResult == MessageBoxResult.Yes) + if (updateMessageBoxResult == MessageBoxResult.Yes) { Process.Start("https://github.com/wagesj45/butterflow-ui/releases/latest"); this.Close(); } } + } #region Methods @@ -113,8 +114,8 @@ namespace butterflow_ui this.OptionsConfiguration.Height = e.Value; break; case ButterflowWrapper.ButterflowOutputType.Progress: - // This case doesn't need to be considered since we're binding the progress bar's value to a property on the butterflow wrapper. - // We may use this in the future, though. + // This case doesn't need to be considered since we're binding the progress bar's value to a property on the butterflow wrapper. + // We may use this in the future, though. default: break; } @@ -357,7 +358,7 @@ namespace butterflow_ui { var button = sender as Button; - if(button != null) + if (button != null) { if (button.Tag.GetType() == typeof(Guid)) { @@ -385,7 +386,7 @@ namespace butterflow_ui { var binaryFormatter = new BinaryFormatter(); var file = binaryFormatter.Deserialize(ofd.OpenFile()); - if(file is OptionsConfigurationFile) + if (file is OptionsConfigurationFile) { this.OptionsConfiguration.LoadFile((OptionsConfigurationFile)file); } diff --git a/butterflow-ui/OctokitWrapper.cs b/butterflow-ui/OctokitWrapper.cs index 2b7f116..df16ada 100644 --- a/butterflow-ui/OctokitWrapper.cs +++ b/butterflow-ui/OctokitWrapper.cs @@ -22,7 +22,7 @@ namespace butterflow_ui private const string REGEX_VERSION = @"(?\d+ ?)\.(?\d+ ?)\.(?\d+ ?)"; /// The version status of the current installation. - private static VersionStatus versionStatus = VersionStatus.unknown; + private static VersionStatus versionStatus = VersionStatus.Unknown; #endregion @@ -46,13 +46,13 @@ namespace butterflow_ui { switch (CurrentVersionStatus) { - case VersionStatus.current: + case VersionStatus.Current: return Localization.Localization.CurrentVersionStatusDescription; - case VersionStatus.behind: + case VersionStatus.Behind: return Localization.Localization.BehindVersionStatusDescription; - case VersionStatus.custom: + case VersionStatus.Custom: return Localization.Localization.CustomVersionStatusDescription; - case VersionStatus.unknown: + case VersionStatus.Unknown: default: return Localization.Localization.UnknownVersionStatusDescription; } @@ -77,44 +77,53 @@ namespace butterflow_ui /// The current version status. private static VersionStatus GetVersionStatus() { - var interpreter = new InputInterpreter(); - var client = new GitHubClient(new ProductHeaderValue("butterflow-ui")); - var releases = client.Repository.Release.GetAll("wagesj45", "butterflow-ui").Result; - - if (releases.Any()) + try { - var latest = releases.First(); - decimal latestMajor = 0, latestMinor = 0, latestPatch = 0, currentMajor = 0, currentMinor = 0, currentPatch = 0; + var interpreter = new InputInterpreter(); + var client = new GitHubClient(new ProductHeaderValue("butterflow-ui")); + var releases = client.Repository.Release.GetAll("wagesj45", "butterflow-ui").Result; - var regex = new Regex(REGEX_VERSION); - foreach (Match match in regex.Matches(latest.TagName)) + if (releases.Any()) { - latestMajor = interpreter.ComputeExpression(match.Groups["Major"].Value); - latestMinor = interpreter.ComputeExpression(match.Groups["Minor"].Value); - latestPatch = interpreter.ComputeExpression(match.Groups["Patch"].Value); - } + var latest = releases.First(); + decimal latestMajor = 0, latestMinor = 0, latestPatch = 0, currentMajor = 0, currentMinor = 0, currentPatch = 0; - foreach (Match match in regex.Matches(Assembly.GetExecutingAssembly().GetName().Version.ToString())) - { - currentMajor = interpreter.ComputeExpression(match.Groups["Major"].Value); - currentMinor = interpreter.ComputeExpression(match.Groups["Minor"].Value); - currentPatch = interpreter.ComputeExpression(match.Groups["Patch"].Value); - } + var regex = new Regex(REGEX_VERSION); + foreach (Match match in regex.Matches(latest.TagName)) + { + latestMajor = interpreter.ComputeExpression(match.Groups["Major"].Value); + latestMinor = interpreter.ComputeExpression(match.Groups["Minor"].Value); + latestPatch = interpreter.ComputeExpression(match.Groups["Patch"].Value); + } - if (latestMajor == currentMajor && latestMinor == currentMinor && latestPatch == currentPatch) - { - return VersionStatus.current; - } + foreach (Match match in regex.Matches(Assembly.GetExecutingAssembly().GetName().Version.ToString())) + { + currentMajor = interpreter.ComputeExpression(match.Groups["Major"].Value); + currentMinor = interpreter.ComputeExpression(match.Groups["Minor"].Value); + currentPatch = interpreter.ComputeExpression(match.Groups["Patch"].Value); + } - if (latestMajor >= currentMajor && latestMinor >= currentMinor && latestPatch >= currentPatch) - { - return VersionStatus.behind; - } + if (latestMajor == currentMajor && latestMinor == currentMinor && latestPatch == currentPatch) + { + return VersionStatus.Current; + } - return VersionStatus.custom; + if (latestMajor >= currentMajor && latestMinor >= currentMinor && latestPatch >= currentPatch) + { + return VersionStatus.Behind; + } + + return VersionStatus.Custom; + } + } + catch(Exception e) + { + //There was an issue connecting to Github. This could be caused by a missing network connection. + //We can safely ignore an error in this process and proceed, falling through to the default connection + //value of Unknown. } - return VersionStatus.unknown; + return VersionStatus.Unknown; } #endregion @@ -125,13 +134,13 @@ namespace butterflow_ui public enum VersionStatus { /// The current version is up to date with the github repository. - current, + Current, /// The current version is behind the github repository and should be updated. - behind, + Behind, /// The current version is ahead of the github repository, or is a custom version of butterflow-ui that cannot be compared to the github repository. - custom, + Custom, /// Github failed to respond with the current version. This could be because of rate limits or a network failure. - unknown + Unknown } #endregion diff --git a/butterflow-ui/butterflow-ui.csproj b/butterflow-ui/butterflow-ui.csproj index 0f6d6ec..470547d 100644 --- a/butterflow-ui/butterflow-ui.csproj +++ b/butterflow-ui/butterflow-ui.csproj @@ -48,6 +48,8 @@ packages\Octokit.0.31.0\lib\net45\Octokit.dll + + @@ -60,8 +62,6 @@ 4.0 - - diff --git a/butterflow-ui/packages.config b/butterflow-ui/packages.config index a05de6f..9f4f4ae 100644 --- a/butterflow-ui/packages.config +++ b/butterflow-ui/packages.config @@ -2,5 +2,4 @@ - \ No newline at end of file