Skip to content

Commit

Permalink
Merge pull request #110 from mono/build-targets
Browse files Browse the repository at this point in the history
Add MSBuild targets
  • Loading branch information
mhutch authored Oct 27, 2022
2 parents 425ba47 + a92d22c commit 384e6af
Show file tree
Hide file tree
Showing 50 changed files with 2,823 additions and 155 deletions.
17 changes: 9 additions & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,19 @@ jobs:
run: dotnet restore -p:Configuration=${{ matrix.config }}

- name: Build
run: dotnet build --no-restore -c ${{ matrix.config }}
run: dotnet build Mono.TextTemplating.sln -c ${{ matrix.config }} --no-restore

- name: Test
run: dotnet test --no-build -c ${{ matrix.config }}
run: dotnet test --no-build -c ${{ matrix.config }} --logger trx --results-directory "TestResults-${{ matrix.os }}-${{ matrix.config }}"

# run: dotnet test -c ${{ matrix.config }} --no-build --blame-hang-timeout 5m --diag TestResults-${{ matrix.config }}-${{ matrix.os }}/Log/test_log.txt --logger trx --results-directory TestResults-${{ matrix.config }}-${{ matrix.os }}
# - name: Test diagnostics
# if: failure() && startsWith (matrix.os, 'windows')
# uses: actions/upload-artifact@v2
# with:
# name: test-results-${{ matrix.config }}-${{ matrix.os }}
# path: TestResults-${{ matrix.config }}-${{ matrix.os }}

- name: Upload Test Results
if: failure()
uses: actions/upload-artifact@v3
with:
name: "TestResults-${{ matrix.os }}-${{ matrix.config }}"
path: "TestResults-${{ matrix.os }}-${{ matrix.config }}"

- uses: actions/upload-artifact@v3
if: matrix.config == 'Release' && startsWith(matrix.os, 'windows')
Expand Down
21 changes: 19 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Authors>Mikayla Hutchinson</Authors>
<PackageTags>T4, templating</PackageTags>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageOutputPath>$(MSBuildThisFileDirectory)packages\$(Configuration)</PackageOutputPath>
<EnablePackageValidation>true</EnablePackageValidation>
</PropertyGroup>

<PropertyGroup>
<LangVersion>10.0</LangVersion>
<PackageValidationBaselineVersion>2.2.1</PackageValidationBaselineVersion>
<CheckEolTargetFramework>False</CheckEolTargetFramework>
<AnalysisLevel>latest-Recommended</AnalysisLevel>
<CheckEolTargetFramework>False</CheckEolTargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Nerdbank.GitVersioning" Version="3.4.255" PrivateAssets="all" />
</ItemGroup>
Expand All @@ -26,4 +31,16 @@
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All"/>
</ItemGroup>

<PropertyGroup>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)TextTemplating.snk</AssemblyOriginatorKeyFile>
<T4PublicKey>0024000004800000940000000602000000240000525341310004000001000100490b59506a03eb5fc5524722a6526eff804b7880a968a581a0bbb73cd6ab93c7b94a14150cb4ea40f610bebf607cea5e8a93ef25d124983300f4bdfe44859430a20a4ed2ac32cfd3a6f0aa12702df819b7799cc0fcf077eea706a27252d59a8a10e5164c2cdddd6680ca76b02ca244e83c1a8cc44b2691052b93ab30bcc613ad</T4PublicKey>
</PropertyGroup>

<ItemDefinitionGroup>
<InternalsVisibleTo>
<Key>$(T4PublicKey)</Key>
</InternalsVisibleTo>
</ItemDefinitionGroup>
</Project>
181 changes: 181 additions & 0 deletions Mono.TextTemplating.Build.Tests/MSBuildExecutionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
// 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.IO;
using System.Linq;

using Xunit;

namespace Mono.TextTemplating.Tests
{
public class MSBuildExecutionTests : IClassFixture<MSBuildFixture>
{
[Fact]
public void TransformExplicitWithArguments ()
{
using var ctx = new MSBuildTestContext ();
var project = ctx.LoadTestProject ("TransformTemplates");

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

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

instance.AssertSingleItem ("GeneratedTemplates", withFullPath: generated);
instance.AssertNoItems ("PreprocessedTemplates");
}

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

project.Restore ();

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

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

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

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

project.Restore ();

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

project.DirectoryPath["foo.txt"].AssertFileExists (false);

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

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

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

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

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

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

project.Restore ();

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

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

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

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

[Fact]
public void PreprocessOnDesignTimeBuild ()
{
using var ctx = new MSBuildTestContext ();
var project = ctx.LoadTestProject ("PreprocessTemplate")
.WithProperty ("DesignTimeBuild", "true")
.WithProperty ("SkipCompilerExecution", "true");

project.Restore ();

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

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

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

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

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"];

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

instance.GetItems ("GeneratedTemplates").AssertPaths (fooGenerated, barGenerated);
instance.AssertNoItems ("PreprocessedTemplates");
fooGenerated.AssertFileExists ();
}

ExecuteAndValidate ();

fooGenerated.AssertTextStartsWith ("Helper says Hello 2019!");
var fooWriteTime = new WriteTimeTracker (fooGenerated);
var barWriteTime = new WriteTimeTracker (barGenerated);

void AssertNoopBuild ()
{
ExecuteAndValidate ();
fooWriteTime.AssertSame ();
barWriteTime.AssertSame ();
}

AssertNoopBuild ();

// check touching a template causes rebuild of that file only
File.SetLastWriteTime (fooTemplate, DateTime.Now);
ExecuteAndValidate ();
fooWriteTime.AssertChanged ();
barWriteTime.AssertSame ();

AssertNoopBuild ();

// check touching the include causes rebuild of the file that uses it
File.SetLastWriteTime (includeFile, DateTime.Now);
ExecuteAndValidate ();
fooWriteTime.AssertChanged ();
barWriteTime.AssertSame ();

AssertNoopBuild ();

// check changing a parameter causes rebuild of both files
File.SetLastWriteTime (includeFile, DateTime.Now);
project.Project.GetItems ("T4Argument").Single (i => i.UnevaluatedInclude == "Year").SetMetadataValue ("Value", "2021");
ExecuteAndValidate ();
fooGenerated.AssertTextStartsWith ("Helper says Hello 2021!");
fooWriteTime.AssertChanged ();
barWriteTime.AssertChanged ();

AssertNoopBuild ();
}
}
}
Loading

0 comments on commit 384e6af

Please sign in to comment.