Skip to content

Commit

Permalink
fix: or-2313 embedded resources text template
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Lesage authored and janlesage committed Aug 23, 2024
1 parent a5de5c8 commit 65d671d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,28 @@
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="EmbeddedResourceTemplate.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>EmbeddedResourceTemplate.cs</LastGenOutput>
</None>
<None Update="EmbeddedResources.tt">
<Generator>TextTemplatingFilePreprocessor</Generator>
<LastGenOutput>EmbeddedResources.cs</LastGenOutput>
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>EmbeddedRiderResources.cs</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="templates\*.json" />
</ItemGroup>
<ItemGroup>
<Compile Update="EmbeddedResources.cs">
<Compile Update="EmbeddedResourceTemplate.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>EmbeddedResourceTemplate.tt</DependentUpon>
</Compile>
<Compile Update="EmbeddedRiderResources.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>EmbeddedResources.tt</DependentUpon>
<DependentUpon>EmbeddedRiderResources.tt</DependentUpon>
</Compile>
</ItemGroup>
<Import Project="..\..\.paket\Paket.Restore.targets" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace AssociationRegistry.Test.Acm.Api;
namespace AssociationRegistry.Test.Acm.Api;

public static class EmbeddedResources
{
public static class EmbeddedResources {
public const string templates_VerenigingenPerInszResponse_json = "templates.VerenigingenPerInszResponse.json";
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<#@ template debug="false" hostspecific="true" language="C#" #>
namespace AssociationRegistry.Test.Acm.Api;

<#@ template debug="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Xml.Linq" #>
public static class EmbeddedResources
{
<#@ import namespace="System.Linq" #>
public static class EmbeddedResources {
<#
static string GetRelativePath(string fromPath, string toPath)
string GetRelativePath(string fromPath, string toPath)
{
var fromUri = new Uri(fromPath);
var toUri = new Uri(toPath);
Expand All @@ -18,7 +17,18 @@ public static class EmbeddedResources
return relativePath.Replace(oldChar: '/', Path.DirectorySeparatorChar);
}

var csprojPath = Host.ResolvePath("AssociationRegistry.Test.Acm.Api.csproj"); // Adjust this to your file path
void WriteStaticResourceName(string resourceName)
{
// Convert the resource name to a valid C# identifier (if needed)
var validResourceName = new string(resourceName
.Select(ch => char.IsLetterOrDigit(ch) ? ch : '_')
.ToArray());

WriteLine($" public const string {validResourceName} = \"{resourceName.Replace(oldChar: '\\', newChar: '.').Replace(oldChar: '/', newChar: '.')}\";");
}

var currentDir = Directory.GetCurrentDirectory();
var csprojPath = Path.Combine(currentDir, "AssociationRegistry.Test.Acm.Api.csproj");
var xDocument = XDocument.Load(csprojPath);
var embeddedResourceElements = xDocument.Descendants("EmbeddedResource");

Expand All @@ -32,11 +42,10 @@ public static class EmbeddedResources
var directoryPath = includePath.Substring(0, lastDirectorySeparator);
var extension = Path.GetExtension(includePath);

var files = Directory.GetFiles(Host.ResolvePath(directoryPath), "*" + extension);
var files = Directory.GetFiles(directoryPath, "*" + extension);
foreach (var file in files)
{
var relativePath = GetRelativePath(csprojPath, file).Replace(oldChar: '\\', newChar: '.').Replace(oldChar: '/', newChar: '.');
WriteStaticResourceName(relativePath);
WriteStaticResourceName(file);
}
}
else // Direct path
Expand All @@ -46,14 +55,3 @@ public static class EmbeddedResources
}
#>
}
<#+
private void WriteStaticResourceName(string resourceName)
{
// Convert the resource name to a valid C# identifier (if needed)
var validResourceName = new string(resourceName
.Select(ch => char.IsLetterOrDigit(ch) ? ch : '_')
.ToArray());

WriteLine($" public const string {validResourceName} = \"{resourceName}\";");
}
#>

0 comments on commit 65d671d

Please sign in to comment.