Skip to content

Commit

Permalink
(cake-buildGH-2410) T4 Global tool support
Browse files Browse the repository at this point in the history
* Adds t4 & t4.exe for tool resolution
* Adds Properties & Class to TextTransformSettings
* Intrgration tests for TransformTemplate
* Fixes cake-build#2410
  • Loading branch information
devlead committed Dec 22, 2018
1 parent 6a2a2b2 commit 2b50fe5
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 9 deletions.
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using Cake.Common.Tests.Fixtures.Tools.TextTransform;
using Cake.Core;
using Cake.Testing;
Expand Down Expand Up @@ -211,6 +212,38 @@ public void Should_Add_Include_Directory_If_Provided()
// Then
Assert.Equal("-I \"/Working/Transforms\" \"/Working/Test.tt\"", result.Args);
}

[Fact]
public void Should_Add_Class_If_Provided()
{
// Given
var fixture = new TextTransformFixture();
fixture.Settings.Class = "HelloWorld";

// When
var result = fixture.Run();

// Then
Assert.Equal("-class=\"HelloWorld\" \"/Working/Test.tt\"", result.Args);
}

[Fact]
public void Should_Add_Properties_If_Provided()
{
// Given
var fixture = new TextTransformFixture();
fixture.Settings.Properties = new Dictionary<string, string>()
{
{ "FirstName", "John" },
{ "LastName", "Doe" }
};

// When
var result = fixture.Run();

// Then
Assert.Equal("-p:\"FirstName\"=\"John\" -p:\"LastName\"=\"Doe\" \"/Working/Test.tt\"", result.Args);
}
}
}
}
26 changes: 19 additions & 7 deletions src/Cake.Common/Tools/TextTransform/TextTransformRunner.cs
Expand Up @@ -71,6 +71,23 @@ private ProcessArgumentBuilder GetArguments(FilePath sourceFilePath, TextTransfo
builder.AppendQuoted(settings.IncludeDirectory.MakeAbsolute(_environment).FullPath);
}

if (settings.Class != null)
{
builder.AppendSwitchQuoted(
"-class",
"=",
settings.Class);
}

if (settings.Properties != null)
{
foreach (var property in settings.Properties)
{
builder.Append(
$"-p:{property.Key.Quote()}={property.Value.Quote()}");
}
}

builder.AppendQuoted(sourceFilePath.MakeAbsolute(_environment).FullPath);

return builder;
Expand All @@ -89,12 +106,7 @@ private ProcessArgumentBuilder GetArguments(FilePath sourceFilePath, TextTransfo
IProcessRunner processRunner,
IToolLocator tools) : base(fileSystem, environment, processRunner, tools)
{
if (environment == null)
{
throw new ArgumentNullException(nameof(environment));
}

_environment = environment;
_environment = environment ?? throw new ArgumentNullException(nameof(environment));
}

/// <summary>
Expand All @@ -112,7 +124,7 @@ protected override string GetToolName()
/// <returns>The tool executable name.</returns>
protected override IEnumerable<string> GetToolExecutableNames()
{
return new[] { "TextTransform.exe" };
return new[] { "TextTransform.exe", "t4", "t4.exe" };
}
}
}
27 changes: 26 additions & 1 deletion src/Cake.Common/Tools/TextTransform/TextTransformSettings.cs
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using Cake.Core.IO;
using Cake.Core.Tooling;

Expand Down Expand Up @@ -29,7 +30,7 @@ public sealed class TextTransformSettings : ToolSettings
public FilePath OutputFile { get; set; }

/// <summary>
/// Gets or sets namespace that is used for compiling the text template.
/// Gets or sets the namespace that is used for compiling the text template.
/// </summary>
/// <value>
/// The namespace.
Expand All @@ -48,5 +49,29 @@ public sealed class TextTransformSettings : ToolSettings
/// The reference path.
/// </value>
public DirectoryPath ReferencePath { get; set; }

/// <summary>
/// Gets or sets the class name used for converting T4 template into a C# class that can be compiled into your app and executed at runtime.
/// </summary>
/// <value>
/// The class name.
/// </value>
/// <remarks>
/// Requires T4 text template processor version 2 or newer.
/// </remarks>
public string Class { get; set; }

/// <summary>
/// Gets or sets properties passes to the template's Session dictionary.
/// </summary>
/// <value>
/// The properties dictionary.
/// </value>
/// <remarks>
/// Requires T4 text template processor version 2 or newer.
/// These can also be accessed using strongly typed properties
/// (declared with &lt;#@ parameter name="&lt;name&gt;" type="&lt;type&gt;" #&gt; directives.)
/// </remarks>
public IDictionary<string, string> Properties { get; set; } = new Dictionary<string, string>();
}
}
@@ -0,0 +1,45 @@
#load "./../../../utilities/xunit.cake"
#load "./../../../utilities/paths.cake"

Task("Cake.Common.Tools.TextTransform.TextTransformAliases.Setup")
.Does(() =>
{
var t4Path = Context.Tools.Resolve("t4") ?? Context.Tools.Resolve("t4.exe");
if (t4Path == null)
{
DotNetCoreTool(null, "tool", "install --tool-path ./tools dotnet-t4");
}
});

Task("Cake.Common.Tools.TextTransform.TextTransformAliases.TransformTemplate")
.IsDependentOn("Cake.Common.Tools.TextTransform.TextTransformAliases.Setup")
.Does(() =>
{
// Given
var path = Paths.Resources.Combine("./Cake.Common/Tools/TextTransform");
var file = path.CombineWithFilePath("./HelloWorld.tt");
var targetPath = Paths.Temp.Combine("./Cake.Common.Tools.TextTransform.TextTransformAliases/TransformTemplate");
var targetFile = targetPath.CombineWithFilePath("HelloWorld.txt");
EnsureDirectoryExist(targetPath);
var settings = new TextTransformSettings {
OutputFile = targetFile,
Properties = {
["FirstName"] = "John",
["LastName"] = "Doe"
}
};
var expect = "Hello John Doe!";
// When
TransformTemplate(file, settings);
var result = System.IO.File.ReadAllText(targetFile.FullPath);
Assert.Equal(expect, result);
});


Task("Cake.Common.Tools.TextTransform.TextTransformAliases")
.IsDependentOn("Cake.Common.Tools.TextTransform.TextTransformAliases.Setup")
.IsDependentOn("Cake.Common.Tools.TextTransform.TextTransformAliases.TransformTemplate");
4 changes: 3 additions & 1 deletion tests/integration/build.cake
Expand Up @@ -25,6 +25,7 @@
#load "./Cake.Common/Tools/Cake/CakeAliases.cake"
#load "./Cake.Common/Tools/DotNetCore/DotNetCoreAliases.cake"
#load "./Cake.Common/Tools/NuGet/NuGetAliases.cake"
#load "./Cake.Common/Tools/TextTransform/TextTransformAliases.cake"
#load "./Cake.Core/Scripting/AddinDirective.cake"
#load "./Cake.Core/Scripting/DefineDirective.cake"
#load "./Cake.Core/Scripting/LoadDirective.cake"
Expand Down Expand Up @@ -71,7 +72,8 @@ Task("Cake.Common")
.IsDependentOn("Cake.Common.Text.TextTransformationAliases")
.IsDependentOn("Cake.Common.Tools.Cake.CakeAliases")
.IsDependentOn("Cake.Common.Tools.DotNetCore.DotNetCoreAliases")
.IsDependentOn("Cake.Common.Tools.NuGet.NuGetAliases");
.IsDependentOn("Cake.Common.Tools.NuGet.NuGetAliases")
.IsDependentOn("Cake.Common.Tools.TextTransform.TextTransformAliases");

Task("Run-All-Tests")
.IsDependentOn("Setup-Tests")
Expand Down
@@ -0,0 +1,3 @@
<#@ parameter name='FirstName' #>
<#@ parameter name='LastName' #>
Hello <#=FirstName#> <#=LastName#>!

0 comments on commit 2b50fe5

Please sign in to comment.