Skip to content

Commit

Permalink
Stabalized t4 template generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
DJGosnell committed Sep 4, 2024
1 parent c2e657b commit c7e89b2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
10 changes: 1 addition & 9 deletions src/DtronixCommon.Tests/DtronixCommon.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="..\t4.props" />
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
Expand All @@ -20,13 +20,5 @@
<ItemGroup>
<ProjectReference Include="..\DtronixCommon\DtronixCommon.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Mono.TextTemplating" Version="2.3.1" />
<TextTemplate Include="**\*.tt" />
<Generated Include="**\*.g.cs" />
</ItemGroup>
<Target Name="TextTemplateTransform" BeforeTargets="BeforeBuild">
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet t4 %(TextTemplate.Identity)" />
</Target>

</Project>
1 change: 1 addition & 0 deletions src/DtronixCommon.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
..\.github\workflows\dotnet.yml = ..\.github\workflows\dotnet.yml
t4.props = t4.props
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DtronixCommonBenchmarks", "DtronixCommonBenchmarks\DtronixCommonBenchmarks.csproj", "{48AA31E7-0494-4FBA-8CD7-C00BDB0CB9A9}"
Expand Down
14 changes: 3 additions & 11 deletions src/DtronixCommon/DtronixCommon.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net5.0;net6.0;net8.0;</TargetFrameworks>
<Import Project="..\t4.props" />
<PropertyGroup>
<TargetFrameworks>net8.0;</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Version>0.9.0.0</Version>
<Nullable>enable</Nullable>
Expand All @@ -27,16 +28,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Mono.TextTemplating" Version="2.3.1" PrivateAssets="All" />
<TextTemplate Include="**\*.tt" />
<Generated Include="**\*.g.cs" />
</ItemGroup>
<Target Name="TextTemplateTransform" BeforeTargets="BeforeBuild">
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet t4 %(TextTemplate.Identity)" />
</Target>
<Target Name="TextTemplateClean" AfterTargets="Clean">
<Delete Files="@(Generated)" />
</Target>
<ItemGroup>
<None Include="icon.png">
<Pack>True</Pack>
Expand Down
38 changes: 38 additions & 0 deletions src/t4.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<Project>
<ItemGroup>
<PackageReference Include="Mono.TextTemplating" Version="3.0.0" PrivateAssets="All" />
<TextTemplate Include="**\*.tt" />
<Generated Include="**\*.g.cs" />
</ItemGroup>
<Target Name="TextTemplateTransform" BeforeTargets="BeforeBuild" Inputs="@(TextTemplate)" Outputs="@(TextTemplate->'%(RelativeDir)%(Filename).cs')">
<!--
This is a crude hack to make Mono's T4 generator work at least somewhat sensibly
when working with include files or relative paths. Microsoft's T4 used MSBuild
syntax $(X) to reference environment variables, whilst Mono's version uses Environment
variable syntax (%x%).
dotnet <tool> is also sensitive to the current working directory with local tools
and cannot be used when the working directory changes.
-->
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet t4 -v %(TextTemplate.Identity) -o %(TextTemplate.RelativeDir)%(TextTemplate.Filename).cs -I=%(TextTemplate.RelativeDir)" EnvironmentVariables="ProjectDir=$(ProjectDir)" />

<!--
The generator may have added new files that are not included in the list of compiled items.
Those files would not be compiled in the same run that generated the files. So we have to
add them to the compile collection manually after the fact.
But adding files that had been added already also triggers an error (DuplicateItems) and
thus we have to first remove them before adding them again. Remove does nothing if the
item is not in the collection, and adding them afterwards ensures there is only one left.
-->
<ItemGroup>
<Compile Remove="%(TextTemplate.RelativeDir)%(TextTemplate.Filename).g.cs" />
<Compile Include="%(TextTemplate.RelativeDir)%(TextTemplate.Filename).g.cs" />
<FileWrites Include="%(TextTemplate.RelativeDir)%(TextTemplate.Filename).g.cs" />
<!-- For clean to work properly -->
</ItemGroup>
</Target>
<Target Name="TextTemplateClean" AfterTargets="Clean">
<Delete Files="@(Generated)" />
</Target>
</Project>

0 comments on commit c7e89b2

Please sign in to comment.