Skip to content

Commit

Permalink
Tried to improve check environment
Browse files Browse the repository at this point in the history
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
  • Loading branch information
atruskie committed Mar 11, 2019
1 parent ee00f00 commit b89a5d5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 22 deletions.
31 changes: 17 additions & 14 deletions src/Acoustics.Shared/AppConfigHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,32 @@ public static class AppConfigHelper
private static readonly string ExecutingAssemblyPath =
(Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly()).Location;

private static readonly IConfigurationSection SharedSettings;
private static readonly Lazy<IConfigurationSection> SharedSettings = new Lazy<IConfigurationSection>(() =>
{
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;
private static readonly bool IsMacOsXValue;

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<IConfigurationRoot> AppConfiguration { get; } = new Lazy<IConfigurationRoot>(() =>
new ConfigurationBuilder()
.AddJsonFile("AP.Settings.json")
.Build());

public static int DefaultTargetSampleRate => GetInt(DefaultTargetSampleRateKey);

Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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}'");

Expand Down
41 changes: 33 additions & 8 deletions src/AnalysisPrograms/CheckEnvironment.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="CheckEnvironment.cs" company="QutEcoacoustics">
// <copyright file="CheckEnvironment.cs" company="QutEcoacoustics">
// 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).
// </copyright>

Expand All @@ -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
{
Expand All @@ -25,10 +26,18 @@ public class CheckEnvironment

private void Execute(Arguments arguments)
{
List<Exception> errors = new List<Exception>();
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)
{
Expand All @@ -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(
Expand Down

0 comments on commit b89a5d5

Please sign in to comment.