diff --git a/src/PathSanitizer.cs b/src/PathSanitizer.cs new file mode 100644 index 00000000..992cda7b --- /dev/null +++ b/src/PathSanitizer.cs @@ -0,0 +1,15 @@ +using System.Text.RegularExpressions; + +static class PathSanitizer +{ + static readonly Regex invalidCharsRegex = new(@"\W"); + public static string Sanitize(string path, string parent) + { + var partStr = invalidCharsRegex.Replace(path, "_"); + if (char.IsDigit(partStr[0])) + partStr = "_" + partStr; + if (partStr == parent) + partStr = "_" + partStr; + return partStr; + } +} \ No newline at end of file diff --git a/src/ThisAssembly.Resources/CSharp.sbntxt b/src/ThisAssembly.Resources/CSharp.sbntxt index 88649266..eeee15a4 100644 --- a/src/ThisAssembly.Resources/CSharp.sbntxt +++ b/src/ThisAssembly.Resources/CSharp.sbntxt @@ -16,7 +16,7 @@ /// => @"{{ $0.Path }}" {{~ end ~}} /// - public static partial class {{ $0.Name | string.replace "-" "_" | string.replace " " "_" }} + public static partial class {{ $0.Name }} { {{~ if $0.IsText ~}} private static string text; diff --git a/src/ThisAssembly.Resources/Model.cs b/src/ThisAssembly.Resources/Model.cs index 55894abe..da189b71 100644 --- a/src/ThisAssembly.Resources/Model.cs +++ b/src/ThisAssembly.Resources/Model.cs @@ -27,29 +27,24 @@ public static Area Load(string basePath, List resources, string rootAr var parts = basePath.Split(new[] { "\\", "/" }, StringSplitOptions.RemoveEmptyEntries); var end = resources.Count == 1 ? ^1 : ^0; + var parent = "Resources"; foreach (var part in parts.AsSpan()[..end]) { - var partStr = SanitizePart(part); + var partStr = PathSanitizer.Sanitize(part, parent); + parent = partStr; + area.NestedArea = new Area(partStr); area = area.NestedArea; } - area.Resources = resources; + area.Resources = resources + .Select(r => r with + { + Name = PathSanitizer.Sanitize(r.Name, parent), + }); return root; } - - static readonly Regex invalidCharsRegex = new(@"\W"); - static string SanitizePart(string? part) - { - var partStr = invalidCharsRegex.Replace(part, "_"); - if (char.IsDigit(partStr[0])) - partStr = "_" + partStr; - return partStr; - } } [DebuggerDisplay("{Name}")] -record Resource(string Name, string? Comment, bool IsText) -{ - public string? Path { get; set; } -}; \ No newline at end of file +record Resource(string Name, string? Comment, bool IsText, string Path); \ No newline at end of file diff --git a/src/ThisAssembly.Resources/ResourcesGenerator.cs b/src/ThisAssembly.Resources/ResourcesGenerator.cs index 722db5ad..cc5e8160 100644 --- a/src/ThisAssembly.Resources/ResourcesGenerator.cs +++ b/src/ThisAssembly.Resources/ResourcesGenerator.cs @@ -76,10 +76,11 @@ static void GenerateSource( var name = group.Count() == 1 ? Path.GetFileNameWithoutExtension(f.resourceName) : Path.GetExtension(f.resourceName)[1..]; - return new Resource(name, f.comment, isText) - { - Path = f.resourceName, - }; + return new Resource( + Name: name, + Comment: f.comment, + IsText: isText, + Path: f.resourceName); }) .ToList(); diff --git a/src/ThisAssembly.Tests/Tests.cs b/src/ThisAssembly.Tests/Tests.cs index 1339e52d..4a0c2d01 100644 --- a/src/ThisAssembly.Tests/Tests.cs +++ b/src/ThisAssembly.Tests/Tests.cs @@ -56,5 +56,9 @@ public void CanUseByteResource() [Fact] public void CanUseSameNameDifferentExtensions() => Assert.NotNull(ThisAssembly.Resources.Content.Swagger.swagger_ui.css.GetBytes()); + + [Fact] + public void CanUseFileInvalidCharacters() + => Assert.NotNull(ThisAssembly.Resources.webhook_data.Text); } } diff --git a/src/ThisAssembly.Tests/ThisAssembly.Tests.csproj b/src/ThisAssembly.Tests/ThisAssembly.Tests.csproj index ba067473..a1b93ed9 100644 --- a/src/ThisAssembly.Tests/ThisAssembly.Tests.csproj +++ b/src/ThisAssembly.Tests/ThisAssembly.Tests.csproj @@ -12,9 +12,7 @@ - - - +