Skip to content

Commit

Permalink
Improve Path Sanitization (#161)
Browse files Browse the repository at this point in the history
* Move Path Sanitizer code to common .cs file

* Update ThisAssembly.Resources to use common .cs file code

* Test Sanitization Code

* Fix more path name issues

Closes #167

* Handle rare corner case ("/0/0/abc.csv")
  • Loading branch information
viceroypenguin authored Jan 25, 2023
1 parent 9589ddb commit 92a5fdc
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 23 deletions.
15 changes: 15 additions & 0 deletions src/PathSanitizer.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 1 addition & 1 deletion src/ThisAssembly.Resources/CSharp.sbntxt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/// => @"{{ $0.Path }}"
{{~ end ~}}
/// </summary>
public static partial class {{ $0.Name | string.replace "-" "_" | string.replace " " "_" }}
public static partial class {{ $0.Name }}
{
{{~ if $0.IsText ~}}
private static string text;
Expand Down
25 changes: 10 additions & 15 deletions src/ThisAssembly.Resources/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,24 @@ public static Area Load(string basePath, List<Resource> 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; }
};
record Resource(string Name, string? Comment, bool IsText, string Path);
9 changes: 5 additions & 4 deletions src/ThisAssembly.Resources/ResourcesGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 4 additions & 0 deletions src/ThisAssembly.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 1 addition & 3 deletions src/ThisAssembly.Tests/ThisAssembly.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
<Import Project="..\*\*.props" />

<ItemGroup>
<Compile Remove="..\EmbeddedResource.cs" />
<Compile Remove="..\GeneratorExtension.cs" />
<Compile Remove="..\IsExternalInit.cs" />
<Compile Remove="..\*.cs" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 92a5fdc

Please sign in to comment.