Skip to content

Commit

Permalink
Attempts to fix faulty OS detection
Browse files Browse the repository at this point in the history
  • Loading branch information
atruskie committed Jun 20, 2018
1 parent 28edc79 commit 1641de9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 35 deletions.
95 changes: 61 additions & 34 deletions src/Acoustics.Shared/AppConfigHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@ namespace Acoustics.Shared

public static class AppConfigHelper
{
private static readonly KeyValueConfigurationCollection SharedSettings;

private static readonly ILog Log = LogManager.GetLogger(nameof(AppConfigHelper));

private static readonly string ExecutingAssemblyPath =
(Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly()).Location;

public static readonly string ExecutingAssemblyDirectory = Path.GetDirectoryName(ExecutingAssemblyPath);

public const string DefaultTargetSampleRateKey = "DefaultTargetSampleRate";

public static int DefaultTargetSampleRate => GetInt(DefaultTargetSampleRateKey);
Expand All @@ -45,12 +36,63 @@ public static class AppConfigHelper

public const string StandardDateFormatUtcWithFractionalSeconds = "yyyyMMdd-HHmmss.FFFZ";

public static readonly string ExecutingAssemblyDirectory = Path.GetDirectoryName(ExecutingAssemblyPath);

private static readonly KeyValueConfigurationCollection SharedSettings;

private static readonly ILog Log = LogManager.GetLogger(nameof(AppConfigHelper));

private static readonly string ExecutingAssemblyPath =
(Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly()).Location;

private static readonly bool IsLinuxValue;
private static readonly bool IsWindowsValue;
private static readonly bool IsMacOsXValue;

static AppConfigHelper()
{
ExeConfigurationFileMap exeConfigurationFileMap = new ExeConfigurationFileMap();
exeConfigurationFileMap.ExeConfigFilename = Path.Combine(ExecutingAssemblyDirectory, "AP.Settings.Config");
var sharedConfig = ConfigurationManager.OpenMappedExeConfiguration(exeConfigurationFileMap, ConfigurationUserLevel.None);
SharedSettings = sharedConfig.AppSettings.Settings;

IsMono = Type.GetType("Mono.Runtime") != null;
CheckOs(ref IsWindowsValue, ref IsLinuxValue, ref IsMacOsXValue);

}

/// <summary>
/// Adapted from https://stackoverflow.com/a/38795621/224512
/// </summary>
private static void CheckOs(ref bool isWindows, ref bool isLinux, ref bool isMacOsX)
{
string windir = Environment.GetEnvironmentVariable("windir");
if (!string.IsNullOrEmpty(windir) && windir.Contains(@"\") && Directory.Exists(windir))
{
isWindows = true;
}
else if (File.Exists(@"/proc/sys/kernel/ostype"))
{
string osType = File.ReadAllText(@"/proc/sys/kernel/ostype");
if (osType.StartsWith("Linux", StringComparison.OrdinalIgnoreCase))
{
// Note: Android gets here too
isLinux = true;
}
else
{
throw new PlatformNotSupportedException(osType);
}
}
else if (File.Exists(@"/System/Library/CoreServices/SystemVersion.plist"))
{
// Note: iOS gets here too
isMacOsX = true;
}
else
{
throw new PlatformNotSupportedException("Unkown platform");
}
}

public static string FileDateFormatUtc
Expand Down Expand Up @@ -281,29 +323,13 @@ public static DirectoryInfo AssemblyDir
}
}

public static bool IsMono
{
get
{
return Type.GetType("Mono.Runtime") != null;
}
}
public static bool IsMono { get; }

public static bool IsLinux
{
get
{
return Environment.OSVersion.Platform == PlatformID.Unix;
}
}
public static bool IsLinux => IsLinuxValue;

public static bool IsMacOsX
{
get
{
return Environment.OSVersion.Platform == PlatformID.MacOSX;
}
}
public static bool IsWindows => IsWindowsValue;

public static bool IsMacOsX => IsMacOsXValue;

public static string GetString(string key)
{
Expand Down Expand Up @@ -517,14 +543,15 @@ private static string GetExeFile(string appConfigKey)
{
string path = null;
string key = null;
if (IsLinux)

if (IsMacOsX)
{
key = appConfigKey + "Linux";
key = appConfigKey + "MacOsX";
path = GetString(key);
}
else if (IsMacOsX)
else if (IsLinux)
{
key = appConfigKey + "MacOsX";
key = appConfigKey + "Linux";
path = GetString(key);
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/Acoustics.Tools/Acoustics.Tools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
<Link>AP.Settings.config</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="app.config">
<None Include="App.config">
<SubType>Designer</SubType>
</None>
<None Include="packages.config" />
Expand Down

0 comments on commit 1641de9

Please sign in to comment.