From b89a5d59e58abc1aa3ff89a07d74b0f72de2d15b Mon Sep 17 00:00:00 2001 From: Anthony Truskinger Date: Mon, 11 Mar 2019 17:25:40 +1000 Subject: [PATCH] Tried to improve check environment It now delays execution of a few things in AppConfigHelper so we can do more checks before crashing. Check environment actually checks the mono version now, and multiple errors could happen as well to make the check-fix routine easier. Intended to help dx #225 --- src/Acoustics.Shared/AppConfigHelper.cs | 31 ++++++++++-------- src/AnalysisPrograms/CheckEnvironment.cs | 41 +++++++++++++++++++----- 2 files changed, 50 insertions(+), 22 deletions(-) diff --git a/src/Acoustics.Shared/AppConfigHelper.cs b/src/Acoustics.Shared/AppConfigHelper.cs index a3df47fe3..bc2e94621 100644 --- a/src/Acoustics.Shared/AppConfigHelper.cs +++ b/src/Acoustics.Shared/AppConfigHelper.cs @@ -38,7 +38,17 @@ public static class AppConfigHelper private static readonly string ExecutingAssemblyPath = (Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly()).Location; - private static readonly IConfigurationSection SharedSettings; + private static readonly Lazy SharedSettings = new Lazy(() => + { + var settings = AppConfiguration.Value.GetSection("appSettings"); + if (!settings.AsEnumerable().Any()) + { + throw new ConfigurationErrorsException("Could not read AP.Settings.json - no values were found in the config file"); + } + + return settings; + }); + private static readonly ILog Log = LogManager.GetLogger(nameof(AppConfigHelper)); private static readonly bool IsLinuxValue; private static readonly bool IsWindowsValue; @@ -46,21 +56,14 @@ public static class AppConfigHelper static AppConfigHelper() { - AppConfiguration = new ConfigurationBuilder() - .AddJsonFile("AP.Settings.json") - .Build(); - - SharedSettings = AppConfiguration.GetSection("appSettings"); - if (!SharedSettings.AsEnumerable().Any()) - { - throw new ConfigurationErrorsException("Could not read AP.Settings.json - no values were found in the config file"); - } - IsMono = Type.GetType("Mono.Runtime") != null; CheckOs(ref IsWindowsValue, ref IsLinuxValue, ref IsMacOsXValue); } - public static IConfigurationRoot AppConfiguration { get; } + public static Lazy AppConfiguration { get; } = new Lazy(() => + new ConfigurationBuilder() + .AddJsonFile("AP.Settings.json") + .Build()); public static int DefaultTargetSampleRate => GetInt(DefaultTargetSampleRateKey); @@ -166,7 +169,7 @@ public static DirectoryInfo AssemblyDir public static string GetString(string key) { - var value = SharedSettings[key]; + var value = SharedSettings.Value[key]; if (string.IsNullOrEmpty(value)) { @@ -333,7 +336,7 @@ private static string GetExeFile(string appConfigKey, bool required = true) key = appConfigKey; } - var path = SharedSettings[key]; + var path = SharedSettings.Value[key]; Log.Verbose($"Attempted to get exe path `{appConfigKey}`. Value: '{path}'"); diff --git a/src/AnalysisPrograms/CheckEnvironment.cs b/src/AnalysisPrograms/CheckEnvironment.cs index c3f1e6514..59efa8b58 100644 --- a/src/AnalysisPrograms/CheckEnvironment.cs +++ b/src/AnalysisPrograms/CheckEnvironment.cs @@ -1,4 +1,4 @@ -// +// // All code in this file and all associated files are the copyright and property of the QUT Ecoacoustics Research Group (formerly MQUTeR, and formerly QUT Bioacoustics Research Group). // @@ -9,13 +9,14 @@ namespace AnalysisPrograms using System.Linq; using System.Reflection; using System.Text; + using System.Text.RegularExpressions; using System.Threading.Tasks; using Acoustics.Shared; using Acoustics.Tools.Audio; - using AnalyseLongRecordings; + using AnalysisPrograms.AnalyseLongRecordings; + using AnalysisPrograms.Production.Arguments; using log4net; using McMaster.Extensions.CommandLineUtils; - using Production.Arguments; public class CheckEnvironment { @@ -25,10 +26,18 @@ public class CheckEnvironment private void Execute(Arguments arguments) { + List errors = new List(); Log.Info("Checking required executables can be found"); - // master audio utlility checks for available executables - var utility = new MasterAudioUtility(); + // master audio utility checks for available executables + try + { + var utility = new MasterAudioUtility(); + } + catch (Exception ex) + { + errors.Add(ex); + } if (AppConfigHelper.IsMono) { @@ -39,17 +48,33 @@ private void Execute(Arguments arguments) if (displayName != null) { var name = displayName.Invoke(null, null); - Log.Info($"Mono version is {name}, we require at least Mono 5.5"); + var version = Regex.Match(name as string, @".*(\d+\.\d+\.\d+\.\d+).*").Groups[1].Value; + Console.WriteLine(version); + if (new Version(version) > new Version(5, 5)) + { + Log.Success($"Your mono version {name} is greater than our required Mono version 5.5"); + } + else + { + errors.Add(new Exception($"Mono version is {name}, we require at least Mono 5.5")); + } } else { - Log.Warn("Could not check Mono version"); + errors.Add(new Exception("Could not check Mono version")); } } } // don't have much more to check at the current time - Log.Success("Valid environment"); + if (errors.Count == 0) + { + Log.Success("Valid environment"); + } + else + { + throw new AggregateException(errors.ToArray()); + } } [Command(