Fix some SonarCloud warnings (#1138)

* r

* hdr

* sc

* StyleCop.Analyzers
This commit is contained in:
Stef Heyenrath
2024-07-22 21:24:30 +02:00
committed by GitHub
parent 6ab1a6fd13
commit 6055b0df1a
8 changed files with 64 additions and 33 deletions

View File

@@ -1,38 +1,58 @@
// Copyright © WireMock.Net
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using HandlebarsDotNet;
using HandlebarsDotNet.Helpers;
using HandlebarsDotNet.Helpers.Helpers;
using WireMock.Handlers;
namespace WireMock.Transformers.Handlebars
{
internal static class WireMockHandlebarsHelpers
{
public static void Register(IHandlebars handlebarsContext, IFileSystemHandler fileSystemHandler)
{
// Register https://github.com/StefH/Handlebars.Net.Helpers
HandlebarsHelpers.Register(handlebarsContext, o =>
{
o.CustomHelperPaths = new string[]
{
Directory.GetCurrentDirectory()
#if !NETSTANDARD1_3
, Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
#endif
}
.Distinct()
.ToList();
namespace WireMock.Transformers.Handlebars;
o.CustomHelpers = new Dictionary<string, IHelpers>
internal static class WireMockHandlebarsHelpers
{
public static void Register(IHandlebars handlebarsContext, IFileSystemHandler fileSystemHandler)
{
// Register https://github.com/StefH/Handlebars.Net.Helpers
HandlebarsHelpers.Register(handlebarsContext, o =>
{
var paths = new List<string>
{
Directory.GetCurrentDirectory(),
GetBaseDirectory(),
};
#if !NETSTANDARD1_3_OR_GREATER
void Add(string? path, ICollection<string> customHelperPaths)
{
if (!string.IsNullOrEmpty(path))
{
{ "File", new FileHelpers(handlebarsContext, fileSystemHandler) }
};
});
}
customHelperPaths.Add(path!);
}
}
Add(Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location), paths);
Add(Path.GetDirectoryName(Assembly.GetCallingAssembly().Location), paths);
Add(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), paths);
Add(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule?.FileName), paths);
#endif
o.CustomHelperPaths = paths;
o.CustomHelpers = new Dictionary<string, IHelpers>
{
{ "File", new FileHelpers(handlebarsContext, fileSystemHandler) }
};
});
}
private static string GetBaseDirectory()
{
#if NETSTANDARD1_3_OR_GREATER || NET6_0_OR_GREATER
return AppContext.BaseDirectory.TrimEnd(Path.DirectorySeparatorChar);
#else
return AppDomain.CurrentDomain.BaseDirectory.TrimEnd(Path.DirectorySeparatorChar);
#endif
}
}