Skip to content

Commit

Permalink
Run T4 templates if targets are out-of-date (#953)
Browse files Browse the repository at this point in the history
  • Loading branch information
atifaziz authored Jan 29, 2023
1 parent 808874a commit 0f7f749
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
28 changes: 28 additions & 0 deletions MoreLinq/MoreLinq.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@

<ItemGroup>
<None Include="..\COPYING.txt" Pack="true" PackagePath="$(PackageLicenseFile)" />
<None Include="*.g.tt" />
<None Update="Aggregate.g.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>Aggregate.g.cs</LastGenOutput>
Expand Down Expand Up @@ -244,4 +245,31 @@
</Compile>
</ItemGroup>

<Target Name="_CollectTextTemplates">
<ItemGroup>
<TextTemplate Include="%(None.Identity)" Condition="'%(None.Generator)' == 'TextTemplatingFileGenerator'">
<LastGenOutput>%(None.LastGenOutput)</LastGenOutput>
</TextTemplate>
</ItemGroup>
</Target>

<Target Name="_TransformTextTemplate" Inputs="$(TextTemplate)" Outputs="$(TextTemplateOutput)">
<Exec Command="dotnet t4 -h &gt; /dev/null" IgnoreExitCode="True" Condition="'$(WINDIR)' == ''">
<Output TaskParameter="ExitCode" PropertyName="_TestExitCode" />
</Exec>
<Exec Command="dotnet t4 -h &gt; NUL" IgnoreExitCode="True" Condition="'$(WINDIR)' != ''">
<Output TaskParameter="ExitCode" PropertyName="_TestExitCode" />
</Exec>
<Exec Command="dotnet tool restore" Condition="$(_TestExitCode) != 0" />
<Message Text="dotnet t4 $(TextTemplate) -o $(TextTemplateOutput)" Importance="High" />
<Exec Command="dotnet t4 $(TextTemplate) -o $(TextTemplateOutput)" />
</Target>

<Target Name="TransformTextTemplates"
DependsOnTargets="_CollectTextTemplates">
<MSBuild Projects="$(MSBuildProjectFile)"
Targets="_TransformTextTemplate"
Properties="TextTemplate=%(TextTemplate.Identity);TextTemplateOutput=%(TextTemplate.LastGenOutput)" />
</Target>

</Project>
9 changes: 1 addition & 8 deletions MoreLinq/tt.cmd
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
@echo off
pushd "%~dp0"
for /f "tokens=*" %%f in ('dir /s /b *.tt') do (
echo>&2 dotnet t4 "%%f"
dotnet t4 "%%f" || goto :end
)
:end
popd
@dotnet build "%~dp0MoreLinq.csproj" -t:TransformTextTemplates %*
2 changes: 1 addition & 1 deletion MoreLinq/tt.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
set -e
cd "$(dirname "$0")"
find . -name "*.tt" -print0 | xargs -0 -t -L 1 sh -c '(dotnet t4 "$0" || exit 255)'
dotnet build -t:TransformTextTemplates "$@"
2 changes: 2 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ install:
- sh: export PATH="$HOME/.dotnet:$PATH"
before_build:
- dotnet --info
# Touch T4 templates to force code generation & validation
- touch MoreLinq/*.g.tt
build_script:
- pwsh: |
grep --extended-regexp '^[[:space:]]*using[[:space:]]+System\.Linq;' (dir -Recurse -File -Filter *Test.cs MoreLinq.Test)
Expand Down

0 comments on commit 0f7f749

Please sign in to comment.