Skip to content

Commit

Permalink
Fix some SonarCloud warnings (#1138)
Browse files Browse the repository at this point in the history
* r

* hdr

* sc

* StyleCop.Analyzers
  • Loading branch information
StefH authored Jul 22, 2024
1 parent 6ab1a6f commit 6055b0d
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 28 deletions.
2 changes: 2 additions & 0 deletions examples/WireMock.Net.Client/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright © WireMock.Net

using System;
using System.Net.Http.Headers;
using System.Text;
Expand Down
2 changes: 2 additions & 0 deletions examples/WireMock.Net.Console.NET5/__admin/mappings/1.cs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
// Copyright © WireMock.Net

// C# Hello
2 changes: 2 additions & 0 deletions examples/WireMock.Net.Console.NET6/__admin/mappings/1.cs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
// Copyright © WireMock.Net

// C# Hello
9 changes: 5 additions & 4 deletions hdr.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ function Write-Header ($file) {
$content = Get-Content $file -Raw # Using -Raw to read the entire file as a single string
$filename = Split-Path -Leaf $file

# Check if the file content starts with the auto-generated line
if ($content.TrimStart().StartsWith("// <auto-generated>")) {
Write-Host "Skipping auto-generated file: $filename"
if ($content.TrimStart().StartsWith($header.Trim())) {
# Write-Host "Skipping existing: $filename"
} elseif ($content.TrimStart().StartsWith("// <auto-generated>")) {
# Write-Host "Skipping auto-generated file: $filename"
} else {
# If not an auto-generated file, prepend the header
Write-Host "Prepend the header for file: $filename"
Set-Content $file $header
Add-Content $file $content -NoNewline # Writing back to the file without an extra newline
}
Expand Down
4 changes: 4 additions & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<!-- <PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> -->
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace WireMock.Transformers.Handlebars;

interface IHandlebarsContext : ITransformerContext
internal interface IHandlebarsContext : ITransformerContext
{
IHandlebars Handlebars { get; }
}
Original file line number Diff line number Diff line change
@@ -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
namespace WireMock.Transformers.Handlebars;

internal static class WireMockHandlebarsHelpers
{
internal static class WireMockHandlebarsHelpers
public static void Register(IHandlebars handlebarsContext, IFileSystemHandler fileSystemHandler)
{
public static void Register(IHandlebars handlebarsContext, IFileSystemHandler fileSystemHandler)
// Register https://github.com/StefH/Handlebars.Net.Helpers
HandlebarsHelpers.Register(handlebarsContext, o =>
{
// 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)
{
o.CustomHelperPaths = new string[]
if (!string.IsNullOrEmpty(path))
{
Directory.GetCurrentDirectory()
#if !NETSTANDARD1_3
, Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
#endif
customHelperPaths.Add(path!);
}
.Distinct()
.ToList();
}
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) }
};
});
}
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
}
}
8 changes: 4 additions & 4 deletions src/WireMock.Net/Util/StringUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ namespace WireMock.Util;
internal static class StringUtils
{
private static readonly string[] ValidUriSchemes =
{
[
"ftp://",
"http://",
"https://"
};
];

private static readonly Func<string, (bool IsConverted, object ConvertedValue)>[] ConversionsFunctions =
{
[
s => bool.TryParse(s, out var result) ? (true, result) : (false, s),
s => int.TryParse(s, out var result) ? (true, result) : (false, s),
s => long.TryParse(s, out var result) ? (true, result) : (false, s),
Expand All @@ -36,7 +36,7 @@ internal static class StringUtils

return (false, s);
}
};
];

public static (bool IsConverted, object ConvertedValue) TryConvertToKnownType(string value)
{
Expand Down

0 comments on commit 6055b0d

Please sign in to comment.