Skip to content

Commit

Permalink
fix(T4.BuildTools): evaluate T4Transform/T4Preprocess Metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
T-Ernst committed Dec 11, 2024
1 parent 8c6b45a commit ba81adc
Show file tree
Hide file tree
Showing 15 changed files with 305 additions and 29 deletions.
135 changes: 128 additions & 7 deletions Mono.TextTemplating.Build.Tests/MSBuildExecutionTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Linq;
using Xunit;

Expand Down Expand Up @@ -35,7 +36,7 @@ public void TransformOnBuild ()

var instance = project.Build ("Build");

var generatedFilePath = project.DirectoryPath["foo.txt"].AssertTextStartsWith("Hello 2019!");
var generatedFilePath = project.DirectoryPath["foo.txt"].AssertTextStartsWith ("Hello 2019!");

instance.AssertSingleItem ("GeneratedTemplates", withFullPath: generatedFilePath);
instance.AssertNoItems ("PreprocessedTemplates");
Expand Down Expand Up @@ -71,6 +72,82 @@ public void PreprocessLegacy ()
instance.AssertNoItems ("GeneratedTemplates");
}

[Fact]
public void PreprocessLegacyWithExtension ()
{
using var ctx = new MSBuildTestContext ();
var project = ctx.LoadTestProject ("PreprocessTemplateWithExtension")
.WithProperty ("UseLegacyT4Preprocessing", "true");

var instance = project.Build ("TransformTemplates");

var generatedFilePath = project.DirectoryPath["foo.g.cs"].AssertTextStartsWith ("//--------");

instance.AssertSingleItem ("PreprocessedTemplates", generatedFilePath);
instance.AssertNoItems ("GeneratedTemplates");
}

[Fact]
public void PreprocessLegacyNamespaceFromRelativePath ()
{
using var ctx = new MSBuildTestContext ();
var project = ctx.LoadTestProject ("PreprocessTemplateFromRelativePath")
.WithProperty ("UseLegacyT4Preprocessing", "true");

var instance = project.Build ("TransformTemplates");

var generatedFilePath = project.DirectoryPath["Nested/Template/Folder/foo.cs"]
.AssertContainsText
(
StringComparison.Ordinal,
"namespace PreprocessTemplateFromRelativePath.Nested.Template.Folder {",
"partial class foo"
);

instance.AssertSingleItem ("PreprocessedTemplates", generatedFilePath);
instance.AssertNoItems ("GeneratedTemplates");
}

[Fact]
public void PreprocessLegacyMetadata ()
{
// Arrange
using var ctx = new MSBuildTestContext ();
var project = ctx.LoadTestProject ("PreprocessTemplateMetadata")
.WithProperty ("UseLegacyT4Preprocessing", "true");

var outputDirectory = project.DirectoryPath["Demo/Output/OutputDirectory.cs"];
var outputFileName = project.DirectoryPath["Demo/OutputFileNameTest.cs"];
var outputFileNameAndOutputDirectory = project.DirectoryPath["Demo/Output/OutputDirectoryAndFileNameTest.cs"];

// Act
var instance = project.Build ("TransformTemplates");

// Assert
outputDirectory.AssertContainsText
(
StringComparison.Ordinal,
"namespace PreprocessTemplateMetadata.Demo.Output {",
"partial class OutputDirectory"
);

outputFileName.AssertContainsText
(
StringComparison.Ordinal,
"namespace PreprocessTemplateMetadata.Demo {",
"partial class OutputFileNameTest"
);

outputFileNameAndOutputDirectory.AssertContainsText
(
StringComparison.Ordinal,
"namespace PreprocessTemplateMetadata.Demo.Output {",
"partial class OutputDirectoryAndFileNameTest"
);

instance.AssertNoItems ("GeneratedTemplates");
}

[Fact]
public void PreprocessOnBuild ()
{
Expand All @@ -93,6 +170,50 @@ public void PreprocessOnBuild ()
.AssertAssemblyContainsType ("PreprocessTemplate.foo");
}

[Fact]
public void PreprocessOnBuildWithExtension ()
{
using var ctx = new MSBuildTestContext ();
var project = ctx.LoadTestProject ("PreprocessTemplateWithExtension");

project.Restore ();

var instance = project.Build ("Build");
var objDir = project.DirectoryPath["obj", "Debug", "netstandard2.0"];

var generatedFilePath = instance.GetIntermediateDirFile ("TextTransform", "foo.g.cs")
.AssertTextStartsWith ("//--------");

instance.AssertSingleItem ("PreprocessedTemplates", generatedFilePath);
instance.AssertNoItems ("GeneratedTemplates");

instance.GetTargetPath ()
.AssertFileName ("PreprocessTemplateWithExtension.dll")
.AssertAssemblyContainsType ("PreprocessTemplateWithExtension.foo");
}

[Fact]
public void PreprocessOnBuildNamespaceFromRelativePath ()
{
using var ctx = new MSBuildTestContext ();
var project = ctx.LoadTestProject ("PreprocessTemplateFromRelativePath");

project.Restore ();

var instance = project.Build ("Build");
var objDir = project.DirectoryPath["obj", "Debug", "netstandard2.0"];

var generatedFilePath = instance.GetIntermediateDirFile ("TextTransform", "Nested", "Template", "Folder", "foo.cs")
.AssertTextStartsWith ("//--------");

instance.AssertSingleItem ("PreprocessedTemplates", generatedFilePath);
instance.AssertNoItems ("GeneratedTemplates");

instance.GetTargetPath ()
.AssertFileName ("PreprocessTemplateFromRelativePath.dll")
.AssertAssemblyContainsType ("PreprocessTemplateFromRelativePath.Nested.Template.Folder.foo");
}

[Fact]
public void PreprocessOnDesignTimeBuild ()
{
Expand Down Expand Up @@ -120,13 +241,13 @@ public void IncrementalTransform ()

project.Restore ();

var fooGenerated = project.DirectoryPath ["foo.txt"];
var fooTemplate = project.DirectoryPath ["foo.tt"];
var barGenerated = project.DirectoryPath ["bar.txt"];
var barTemplate = project.DirectoryPath ["bar.tt"];
var includeFile = project.DirectoryPath ["helper.ttinclude"];
var fooGenerated = project.DirectoryPath["foo.txt"];
var fooTemplate = project.DirectoryPath["foo.tt"];
var barGenerated = project.DirectoryPath["bar.txt"];
var barTemplate = project.DirectoryPath["bar.tt"];
var includeFile = project.DirectoryPath["helper.ttinclude"];

void ExecuteAndValidate()
void ExecuteAndValidate ()
{
var instance = project.Build ("TransformTemplates");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<#@ template language="C#" #>
Hello World
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$(TemplatingTargetsPath)\T4.BuildTools.props" />

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<T4Preprocess Include="./Nested/Template/Folder/foo.tt" />
<PackageReference Include="System.CodeDom" Version="5.0.0" />
</ItemGroup>

<Import Project="$(TemplatingTargetsPath)\T4.BuildTools.targets" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<#@ template language="C#" #>
Hello Item Metadata OutputDirectory
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<#@ template language="C#" #>
Hello Item Metadata OutputDirectory And OutputDFileName
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<#@ template language="C#" #>
Hello Item Metadata OutputDFileName
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$(TemplatingTargetsPath)\T4.BuildTools.props" />

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<T4Preprocess Include="OutputDirectory.tt">
<OutputDirectory>Demo/Output</OutputDirectory>
</T4Preprocess>
<T4Preprocess Include="OutputFileName.tt">
<OutputFileName>Demo/OutputFileNameTest.cs</OutputFileName>
</T4Preprocess>
<T4Preprocess Include="OutputDirectoryAndOutputFileName.tt">
<OutputDirectory>Demo/Output</OutputDirectory>
<OutputFileName>OutputDirectoryAndFileNameTest.cs</OutputFileName>
</T4Preprocess>
<PackageReference Include="System.CodeDom" Version="5.0.0" />
</ItemGroup>

<Import Project="$(TemplatingTargetsPath)\T4.BuildTools.targets" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$(TemplatingTargetsPath)\T4.BuildTools.props" />

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<T4Preprocess Include="foo.tt" />
<PackageReference Include="System.CodeDom" Version="5.0.0" />
</ItemGroup>

<Import Project="$(TemplatingTargetsPath)\T4.BuildTools.targets" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<#@ template language="C#" #>
<#@ output extension=".g.cs" #>
Hello World
2 changes: 2 additions & 0 deletions Mono.TextTemplating.Build/T4.BuildTools.targets
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<_T4PreprocessOnly>False</_T4PreprocessOnly>
<_T4PreprocessOnly Condition="'$(_T4TransformKind)'=='DesignTime' Or ('$(_T4TransformKind)'=='OnBuild' And '$(TransformOnBuild)'=='False')">True</_T4PreprocessOnly>
<_T4IntermediateTemplateOutputDir Condition="'$(_T4IntermediateTemplateOutputDir)'==''">$(IntermediateOutputPath)TextTransform\</_T4IntermediateTemplateOutputDir>
<_T4ProjectDirectory Condition="'$(_T4ProjectDirectory)'==''">$(MSBuildProjectDirectory)</_T4ProjectDirectory>
</PropertyGroup>

<TextTransform
Expand All @@ -90,6 +91,7 @@
PreprocessOnly="$(_T4PreprocessOnly)"
UseLegacyPreprocessingMode="$(UseLegacyT4Preprocessing)"
IntermediateDirectory="$(_T4IntermediateTemplateOutputDir)"
ProjectDirectory="$(_T4ProjectDirectory)"
TransformOutOfDateOnly="$(TransformOutOfDateOnly)"
PreprocessTargetRuntimeIdentifier="$(TargetFrameworkIdentifier)"
>
Expand Down
6 changes: 6 additions & 0 deletions Mono.TextTemplating.Build/TemplateBuildState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ public class TransformTemplate
public string OutputFile { get; set; }
[Key (2)]
public List<string> Dependencies { get; set; }
[Key (3)]
public bool IgnoreExtensionTemplateDirective { get; set; }

public bool IsStale (Func<string, DateTime?> getFileWriteTime, TaskLoggingHelper logger)
{
Expand Down Expand Up @@ -266,6 +268,10 @@ public class PreprocessedTemplate
public List<string> Dependencies { get; set; }
[Key (3)]
public List<string> References { get; set; }
[Key (4)]
public string Namespace { get; set; }
[Key (5)]
public bool IgnoreExtensionTemplateDirective { get; set; }

public bool IsStale (Func<string, DateTime?> getFileWriteTime, TaskLoggingHelper logger)
{
Expand Down
Loading

0 comments on commit ba81adc

Please sign in to comment.