Coder Social home page Coder Social logo

t4's Introduction

Mono.TextTemplating

Build NuGet version (dotnet-t4)

T4 templates are a simple general-purpose way to use C# to generate any kind of text or code files.

Mono.TextTemplating started out as an open-source reimplementation of the Visual Studio T4 text templating engine, but has since evolved to have many improvements over the original, including support for C# 10 and .NET 6.

The dotnet-t4 tool can be used either to process T4 templates directly, or preprocess them into runtime template classes that can be included in your app and processed at runtime.

$ dotnet tool install -g dotnet-t4
$ echo "<#@ parameter name='Name' #>Hello <#=Name#>" | t4 -o - -p:Name=World
Hello World

To learn more, see the dotnet-t4 readme.

For advanced use, the engine itself is available as a package called Mono.TextTemplating that can be embedded in an app. For details, see the Mono.TextTemplating package readme.

t4's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

t4's Issues

Strong name validation failed

Using 1.3.1 from NuGet package.
I get weird errors because the assembly doens't seem to be properly signed:

sn -vf %userprofile%\.nuget\packages\mono.texttemplating\1.3.1\lib\net45\Mono.TextTemplating.dll

Microsoft (R) .NET Framework Strong Name Utility  Version 4.0.30319.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Failed to verify assembly -- Strong name validation failed.

Relative paths do not work in the include directive

The issue is that relative paths do not work for the include directive. This issue makes the command line argument -I useless because if the include directives file path must be an absolute path, it will not use the provided included paths.

Run command:

t4 TestTemplate.tt

TestTemplate.tt

<#@ template #>
<#@ include file="./TemplateInclude.tt" #>

TemplateInclude.tt

Template Include Text

Results in:

System.ArgumentNullException

System.ArgumentNullException: Value cannot be null.
Parameter name: path1
   at System.IO.Path.Combine(String path1, String path2)
   at Mono.TextTemplating.TemplateGenerator.ResolvePath(String path) in C:\projects\t4\Mono.TextTemplating\Mono.TextTemplating\TemplateGenerator.cs:line 240
   at Mono.TextTemplating.TemplateGenerator.LoadIncludeText(String requestFileName, String& content, String& location) in C:\projects\t4\Mono.TextTemplating\Mono.TextTemplating\TemplateGenerator.cs:line 327
   at Mono.TextTemplating.TemplateGenerator.Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost.LoadIncludeText(String requestFileName, String& content, String& location) in C:\projects\t4\Mono.TextTemplating\Mono.TextTemplating\TemplateGenerator.cs:line 355
   at Mono.TextTemplating.ParsedTemplate.Import(ITextTemplatingEngineHost host, Directive includeDirective, String relativeToDirectory) in C:\projects\t4\Mono.TextTemplating\Mono.TextTemplating\ParsedTemplate.cs:line 174
   at Mono.TextTemplating.ParsedTemplate.Parse(ITextTemplatingEngineHost host, Tokeniser tokeniser, Boolean parseIncludes, Boolean isImport) in C:\projects\t4\Mono.TextTemplating\Mono.TextTemplating\ParsedTemplate.cs:line 141
   at Mono.TextTemplating.ParsedTemplate.Parse(ITextTemplatingEngineHost host, Tokeniser tokeniser, Boolean parseIncludes) in C:\projects\t4\Mono.TextTemplating\Mono.TextTemplating\ParsedTemplate.cs:line 82
   at Mono.TextTemplating.ParsedTemplate.Parse(ITextTemplatingEngineHost host, Tokeniser tokeniser) in C:\projects\t4\Mono.TextTemplating\Mono.TextTemplating\ParsedTemplate.cs:line 78
   at Mono.TextTemplating.ParsedTemplate.FromText(String content, ITextTemplatingEngineHost host) in C:\projects\t4\Mono.TextTemplating\Mono.TextTemplating\ParsedTemplate.cs:line 71
   at Mono.TextTemplating.TextTransform.MainInternal(String[] args) in C:\projects\t4\dotnet-t4\TextTransform.cs:line 181
   at Mono.TextTemplating.TextTransform.Main(String[] args) in C:\projects\t4\dotnet-t4\TextTransform.cs:line 42

This should not result in an exception, it should result in an error that tells the user that the file TemplateInclude.tt wasn't found.

templates.zip

CscCodeCompiler Performance on dotnet core

I'm running Mono.TextTemplating on Windows dotnet core and noticed performance issues.

Event with the most simple template ("hi") TemplateGenerator.ProcessTemplate takes and avarage of 500ms

The culprit seems to be
CscCodeCompiler.CompileFile where most of the processing time is lost

C:\Program Files\dotnet\dotnet "C:\Program Files\dotnet\sdk\3.0.100\Roslyn\bincore\csc.dll" -nologo -noconfig "@C:\Users\username\AppData\Local\Temp\tmp828D.tmp\response.rsp"

var process = ProcessUtils.StartProcess (psi, outWriter, errWriter, token);

Compilation hang-up

It's great having this project available, as I'm more comfortable for generating templates that output to html files.

But, for the last day or so, I've been stuck to a point where nothing was happening with the parser; it did not matter if I was using the latest nuget package, or the latest source.

After a step-by-step debug session, I've reached this loc where the execution halted without any error.

I'm using the library inside a web project, maybe it has something to do with this? I'm not sure, but here is a snippet of the code from the project as I make use of it (maybe i'm doing something wrong)

Exception: Could not find any valid runtime

Steps to Reproduce

On Windows, install t4 as a global tool:

> dotnet tool install --global dotnet-t4
You can invoke the tool using the following command: t4
Tool 'dotnet-t4' (version '2.0.1') was successfully installed.

Next, run the tool against the test.tt file:

t4 test.tt

Expected

Expected the template to be processed successfully.

Actual Result

Instead a run-time exception occurs with the error message saying, “Could not find any valid runtime”:

System.Exception: Could not find any valid runtime
   at Mono.TextTemplating.TemplatingEngine.CompileCode2(IEnumerable`1 references, TemplateSettings settings, CodeCompileUnit ccu) in C:\projects\t4\Mono.TextTemplating\Mono.TextTemplating\TemplatingEngine.cs:line 209
   at Mono.TextTemplating.TemplatingEngine.CompileTemplateInternal(ParsedTemplate pt, String content, ITextTemplatingEngineHost host) in C:\projects\t4\Mono.TextTemplating\Mono.TextTemplating\TemplatingEngine.cs:line 173
   at Mono.TextTemplating.TemplatingEngine.CompileTemplate(ParsedTemplate pt, String content, ITextTemplatingEngineHost host) in C:\projects\t4\Mono.TextTemplating\Mono.TextTemplating\TemplatingEngine.cs:line 147
   at Mono.TextTemplating.ToolTemplateGenerator.ProcessTemplate(ParsedTemplate pt, String inputFile, String inputContent, String& outputFile) in C:\projects\t4\dotnet-t4\ToolTemplateGenerator.cs:line 57
   at Mono.TextTemplating.TextTransform.MainInternal(String[] args) in C:\projects\t4\dotnet-t4\TextTransform.cs:line 194
   at Mono.TextTemplating.TextTransform.Main(String[] args) in C:\projects\t4\dotnet-t4\TextTransform.cs:line 42

More Information

dotnet --info says:

.NET Core SDK (reflecting any global.json):
 Version:   2.2.101
 Commit:    236713b0b7

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.17134
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\2.2.101\

Host (useful for support):
  Version: 2.2.0
  Commit:  1249f08fed

.NET Core SDKs installed:
  1.1.8 [C:\Program Files\dotnet\sdk]
  1.1.9 [C:\Program Files\dotnet\sdk]
  1.1.10 [C:\Program Files\dotnet\sdk]
  1.1.11 [C:\Program Files\dotnet\sdk]
  2.1.101 [C:\Program Files\dotnet\sdk]
  2.1.103 [C:\Program Files\dotnet\sdk]
  2.1.104 [C:\Program Files\dotnet\sdk]
  2.1.200 [C:\Program Files\dotnet\sdk]
  2.1.201 [C:\Program Files\dotnet\sdk]
  2.1.202 [C:\Program Files\dotnet\sdk]
  2.1.300 [C:\Program Files\dotnet\sdk]
  2.1.400 [C:\Program Files\dotnet\sdk]
  2.1.402 [C:\Program Files\dotnet\sdk]
  2.1.403 [C:\Program Files\dotnet\sdk]
  2.1.500 [C:\Program Files\dotnet\sdk]
  2.2.101 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 1.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.1.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.1.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

PCL compatibility

Hi,

Is there any chance to get this working with PCL libs in profile 44 (commonly used in Xamarin projects with iOS and Android support)?

Right now the following error appears on installation:

[Install] Install failed (project: MyMd.Mobile.Core, package: Mono.TextTemplating v1.3.1)
Could not install package 'Mono.TextTemplating 1.3.1'. You are trying to install this package into a project that targets '.NETPortable,Version=v4.6,Profile=Profile44', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

I guess I'm not the only person seeking for this :-)

Cheers,
Marek

Debug steps for project-tool install?

Previously was getting hung up on confusing errors but I managed to get it to theoretically install without error, but it doesn't appear to be recognized as a dotnet command as t4, t4-project or t4-project-tool.

Semi new to manual install vs nuget but installed as
dotnet add package dotnet-t4-project-tool --version 2.0.5
In my proj directory, but

PS ~> dotnet t4
No executable found matching command "dotnet-t4"
PS ~> dotnet t4-tool
No executable found matching command "dotnet-t4-tool"
PS ~> dotnet t4-project-tool
No executable found matching command "dotnet-t4-project-tool"

I feel like I'm just failing on the syntax for use, because dotnet says it exists

Project 'proj' has the following package references
   [netcoreapp2.2]:
   Top-level Package                                          Requested   Resolved
   > AspNet.Security.OpenIdConnect.Server                     2.0.0       2.0.0
   > dotnet-t4-project-tool                                   2.0.5       2.0.5

Trying to install it via nuget gives
'dotnet-t4-project-tool 2.0.5' has a package type 'DotNetCliTool' that is not supported by project

Support net core

I made some changes to work with the net core, I did some tests with version 2.1 and it worked for my context, I would like to use the official library with support for the net core, below the link of the repository with the changes, maybe it helps in something.

https://github.com/ErliSoares/t4

The biggest change was the compilation of the template, which I had to use roslyn which has support for all platforms, windows, linux...

PlatformNotSupportedException with dotnet-t4-project-tool

I have the global tool installed:

T4 text template processor version 2.0.2+gbf09da0ad0

Using it, this command works:

t4 -o tmp.cs -p:NAME=tmp -p:CONV=Cdecl -p:KIND=dynamic provider.tt

And I have a project dir with this in the csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="dotnet-t4-project-tool" Version="2.0.3" />
  </ItemGroup>
</Project>

And after a dotnet restore to install the local tool, dotnet t4 yields:

 T4 text template processor version 2.0.3+gd133b088d6

But if I run the same command as above, except with "dotnet " prepended, I get:

System.PlatformNotSupportedException: Operation is not supported on this platform.
   at Microsoft.CSharp.CSharpCodeGenerator.FromFileBatch(CompilerParameters options, String[] fileNames)
   at Microsoft.CSharp.CSharpCodeGenerator.System.CodeDom.Compiler.ICodeCompiler.CompileAssemblyFromDomBatch(CompilerParameters options, CodeCompileUnit[] ea)
   at Mono.TextTemplating.TemplatingEngine.CompileCode(IEnumerable`1 references, TemplateSettings settings, CodeCompileUnit ccu)
   at Mono.TextTemplating.TemplatingEngine.CompileTemplateInternal(ParsedTemplate pt, String content, ITextTemplatingEngineHost host)
   at Mono.TextTemplating.TemplatingEngine.CompileTemplate(ParsedTemplate pt, String content, ITextTemplatingEngineHost host)
   at Mono.TextTemplating.ToolTemplateGenerator.ProcessTemplate(ParsedTemplate pt, String inputFile, String inputContent, String& outputFile)
   at Mono.TextTemplating.TextTransform.MainInternal(String[] args)
   at Mono.TextTemplating.TextTransform.Main(String[] args)

dotnet --version says 2.1.500

The problem seems reproducible on Windows and Mac.

ERROR Metadata file 'System.Core' could not be found

I have a pretty simple T4 file like this:

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ output extension=".cs" #>
<#
    string versionFile = Host.ResolvePath("version.txt");
    string version = File.ReadLines(versionFile).First();
#>
using System.Reflection;

[assembly: AssemblyVersion("<#= version #>")]
[assembly: AssemblyFileVersion("<#= version #>")]

When I try to transform it with Mono.TextTransform, I get the following error:

> C:\My\Solution [master ≡]> . "C:\Users\thomas\.nuget\packages\Mono.TextTransform\1.0.0\tools\TextTransform.exe" Properties\VersionInfo.tt -out Properties\VersionInfo.cs
Processing 'Properties\VersionInfo.tt' failed.
(0,0): ERROR Metadata file 'System.Core' could not be found

Allow templates to depend on NuGets instead of assembly names

Currently tt templates can declare an assembly reference via a directive. This is resolved via the host, and typically resolves into the host appdomain or framework.

It would be less fragile to allow templates to declare NuGet dependencies instead.

System.IO.FileNotFoundException

Hello,

I am getting this issue running this command :
t4 -r=C:\WorkingArea\rdxnoninstrument3\rdxnoninstrument\source\nwm.dx.rdxnoninstrument.message\bin\Debug\netcoreapp2.1\nwm.dx.rdxnoninstrument.message.dll Schema\Types\GraphTypes.t4

T4 text template processor version 2.0.4+gaf154e27e2

This file exist :
C:\WorkingArea\rdxnoninstrument3\rdxnoninstrument\source\nwm.dx.rdxnoninstrument.message\bin\Debug\netcoreapp2.1\nwm.dx.rdxnoninstrument.message.dll

ERROR: Error running transform: System.Reflection.TargetInvocationException: Exc
eption has been thrown by the target of an invocation. ---> System.IO.FileNotFou
ndException: Could not load file or assembly 'nwm.dx.rdxnoninstrument.message, V
ersion=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find th
e file specified. ---> System.IO.FileNotFoundException: Could not load the speci
fied file.
at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingEvent(AssemblyName a
ssemblyName)
at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingResolvingEvent(IntPt
r gchManagedAssemblyLoadContext, AssemblyName assemblyName)
--- End of inner exception stack trace ---
at Microsoft.VisualStudio.TextTemplating.GeneratedTextTransformation6da4a4bd.
TransformText()
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments,
Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invoke
Attr, Binder binder, Object[] parameters, CultureInfo culture)
at Mono.TextTemplating.CompiledTemplate.Process() in /Users/mikayla/code/t4/M
ono.TextTemplating/Mono.TextTemplating/CompiledTemplate.cs:line 119

otherwise you get a different message with a random file

ERROR: Metadata file 'C:\WorkingArea\rdxnoninstrument3\rdxnoninstrument\source\n
wm.dx.rdxnoninstrument.message\bin\Debug\netcoreapp2.1\dummy.dll' could not be found

RuntimeInfo FindHighestVersionedDirectory fails with preview SDKs

Currently System.Version is used for "sorting" installed .NET Core SDKs , this to be able to find latest installed. Issue with that is Version.TryParse won't work for i.e. 3.0.0-preview6-27804-01 and you end up an error like this

- Could not find any valid runtime
-    at Mono.TextTemplating.TemplatingEngine.CompileCode(IEnumerable`1 references, TemplateSettings settings, CodeCompileUnit ccu) in /Users/mikayla/code/t4/Mono.TextTemplating/Mono.TextTemplating/TemplatingEngine.cs:line 210
-    at Mono.TextTemplating.TemplatingEngine.CompileTemplateInternal(ParsedTemplate pt, String content, ITextTemplatingEngineHost host, TemplateSettings settings) in /Users/mikayla/code/t4/Mono.TextTemplating/Mono.TextTemplating/TemplatingEngine.cs:line 183
-    at Mono.TextTemplating.ToolTemplateGenerator.ProcessTemplate(ParsedTemplate pt, String inputFile, String inputContent, String& outputFile, TemplateSettings settings) in /Users/mikayla/code/t4/dotnet-t4/ToolTemplateGenerator.cs:line 64
-    at Mono.TextTemplating.TextTransform.MainInternal(String[] args) in /Users/mikayla/code/t4/dotnet-t4/TextTransform.cs:line 214
-    at Mono.TextTemplating.TextTransform.Main(String[] args) in /Users/mikayla/code/t4/dotnet-t4/TextTransform.cs:line 42

@mhutch I will submit a PR fully aware not having buy-in for, just as a suggestion on how to solve it and as I have already the code after debugging the issue :)

Code where issue is:

static string FindHighestVersionedDirectory (string parentFolder, Func<string, bool> validate)
{
string bestMatch = null;
var bestVersion = new Version(0, 0, 0);
foreach (var dir in Directory.EnumerateDirectories (parentFolder)) {
var name = Path.GetFileName (dir);
if (Version.TryParse (name, out var version) && version.Build >= 0) {
if (version > bestVersion && (validate == null || validate (dir))) {
bestVersion = version;
bestMatch = dir;
}
}
}
return bestMatch;

Error when adding parameter directive

Tried to compile a template in Unity.

<#@ parameter type="System.String" name="json_input" #>

and I get the following:

(0,0): ERROR Error running transform: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NotImplementedException: The requested feature is not implemented.
  at System.Runtime.Remoting.Messaging.CallContext.LogicalGetData (System.String name) [0x00000] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Remoting.Messaging/CallContext.cs:84 
  at Microsoft.VisualStudio.TextTemplating.GeneratedTextTransformation22471636.Initialize () [0x00087] in C:\Users\Nikos\AppData\Local\Temp\7b5907dd\65f03029.0.cs:64 
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x000d0] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222 
  --- End of inner exception stack trace ---
  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x000eb] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232 
  at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115 
  at Mono.TextTemplating.CompiledTemplate.Process () [0x0011a] in C:\Users\Nikos\Documents\GitHub\t4\Mono.TextTemplating\Mono.TextTemplating\CompiledTemplate.cs:118 
UnityEngine.Debug:LogError(Object)
TextTemplating.TextTemplateGenerator:CompileTemplate(CodeGenerationTask) (at Assets/Editor/UnityHelpers/TextTemplateGenerator/TextTemplateGenerator.cs:40)
CustomEditorWizard:GenerateCustomEditor(Type) (at Assets/Editor/UnityHelpers/CreateCustomEditor.cs:113)
CustomEditorWizard:OnGUI() (at Assets/Editor/UnityHelpers/CreateCustomEditor.cs:82)
UnityEditor.DockArea:OnGUI()

PS: I didn't add any parameters on the generator instance directly. So basically it should have been null in the template scope. Don't know if that disrupts anything.

When can we expect 2.2 Nuget packages

Thanks for amazing work 👏 👏

Just wanted know the ETA for 2.2 Nuget package. Because latest nuget as of now doesn't work well with external assembly references. I've tried debugging the issue with the latest code and seems to be working fine.

It would be great if we can get a preview version (2.2.0-preview-0014-gc50153e6f1) to nuget feed at least.

Cannot resolve .NET Core 3.0 assemblies

Repro

  1. Save the following to TextTemplate1.tt
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Text.Json" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>

public class TextTemplateClass
{
}
  1. Run t4 TextTemplate1.tt

Expected
System.Text.Json assembly is discoverable via the shared runtime installation on the machine

Actual
Processing '.\TextTemplate1.tt' failed.
ERROR: Metadata file 'System.Text.Json.dll' could not be found

Is this project the future of T4

I spent some time trying to figure out if Microsoft had open-sourced the T4 engine or not when I found this. As mono is now part of Microsoft, will this be the "vNext" of T4, or will both projects move forward in parallel?

Improvements request

With the Microsoft T4 engine not on NuGet, a lot of project templates are made more difficult because of insufficient or overly specific (e.g. XDT) transforms. But a lot of projects need it. I often write PowerShell cmdlets where the module manifest (.psd1 file) benefits from transforms such as short commit ID or version.

I would like to offer help improving this one and wondering if you'd accept changes to put this in line with modern projects' needs. This project is a great start and alternative to Microsoft T4.

  • Change to use Roslyn compilation (dup of issue #6).
  • Change to dotnet toolset and target net45 or netstandard1.1 (or whatever is required) for broad support.
  • Build nupkg similar to the official T4 MSBuild support to support transformations during build.

Global tool dotnet-t4 fails on System.IO operations

We use System.IO frequently with T4 templates, but the System.IO doesn't seem to be handled correctly

A simple template like this one (taken from docs https://docs.microsoft.com/en-us/visualstudio/modeling/t4-import-directive?view=vs-2017 )

<#@ import namespace="System.IO" #>
<#
   string fileContent = File.ReadAllText("C:\x.txt"); // System.IO.File
#>
The file contains: <#=  fileContent #>

Will generate

- (3,25): ERROR: The name 'File' does not exist in the current context

Not compatible with dotnet core 3.0.0

I tried dotnet-t4 on mac with dotnet 3.0.0 installed and while it could be installed, it failed to run (see below for details).

dotnet tool install -g dotnet-t4
You can invoke the tool using the following command: t4
Tool 'dotnet-t4' (version '2.0.5') was successfully installed.

echo "<#@ parameter name='Name' #>Hello <#=Name#>" | t4 -o - -p:Name=World
It was not possible to find any compatible framework version
The specified framework 'Microsoft.NETCore.App', version '2.1.0' was not found.

  • The following frameworks were found:
    3.0.0 at [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

You can resolve the problem by installing the specified framework and/or SDK.

BUG: Path (or arg string value) ending in "\" appears to prevent input file discovery

dotnet t4 -r="Microsoft.AspNetCore.Hosting.Abstractions.dll" -P="C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.hosting.abstractions\2.2.0\lib\netstandard2.0\" CodeGen/bmc.ttx
dotnet : No input file specified.

The above has a trailing slash in the -P= path

dotnet t4 -r="Microsoft.AspNetCore.Hosting.Abstractions.dll" -P="C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.hosting.abstractions\2.2.0\lib\netstandard2.0" CodeGen/bmc.ttx
dotnet : Processing 'CodeGen/bmc.ttx' failed.

Simplify passing parameters to template engine

Currently the most idiomatic ways to pass parameters to a template are either to have a specific host subclass with properties for the template to access, or for the template to declare properties with <#@ parameter name="<name>" type="<type>" #> directives and then to pass the values in on the host's Session dictionary. This is how dotnet-t4 handles property values passed in on the commandline.

This design comes from the days when the template instance lived in an AppDomain and accessed the host over remoting, and it minimized unnecessary copying of objects across the remoting barrier. However it's cumbersome both for the host and for the template author. It would be good to have a simpler system for modern templates.

This issue is for discussing and designing possible approaches.

HowTo: Use this in a VS 2019 netstandard2.0 project with a .tt file set to TextTemplatingFilePreprocessor

Sorry if this is a newbie question...I have been using the old VS T4 templating tools and we are moving to netstandard and .NET Core. In VS T4 we would create a .tt flie setting the custom tool property for the file to TextTemplatingFilePreprocessor and pass in parameters to it via a template.Session variable that is a Dictionary<string, object> and call the TransformText method to get the transformed text. How do we use Mono.TextTemplating to do this same thing within VS?

Is there a sample netstandard2.0 project that uses this in this fashion we can see for reference? That has .tt files set to TextTemplatingFilePreprocessor or Mono.TextTemplating's version of this? Or is there a different way we do this...with some a project example on how to use it?

Type Environment exists in two assemblies

I've installed the tool in a .Net Core 3.0 project. I'm using environment variables to build a credentials class.

I get the following error: /var/folders/qf/59827vqj0jz819fcpsr0dwtm0000gn/T/tmp6w9d0b.tmp/Credentials.tt(6,106): ERROR: The type 'Environment' exists in both 'System.Runtime.Extensions, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'

From this code:

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
namespace SosBackend {
	public static class Credentials {
		public static string DbUser = "<#= Environment.GetEnvironmentVariable("DB_USER") ?? "" #>";
		public static string DbPassword = "<#= Environment.GetEnvironmentVariable("DB_PASSWORD") ?? "" #>";
		public static string DbHost = "<#= Environment.GetEnvironmentVariable("DB_HOST") ?? "" #>";
		public static string DbDb = "<#= Environment.GetEnvironmentVariable("DB_DB") ?? "" #>";
		public static string DbPort = "<#= Environment.GetEnvironmentVariable("DB_PORT") ?? "1433" #>";
	}
}

I've tried ading both assemblies listed in assembly and import directives, with them not being found.

ArgumentNullException Thrown on Build

I'm trying to make my Visual Studio T4 templates compatible with Visual Studio for Mac by installing and running the t4 command line tool in the build process but I get this error:

TransformAlliOS:
    dotnet tool install -g dotnet-t4
    Tool 'dotnet-t4' is already installed.
    The command "dotnet tool install -g dotnet-t4" exited with code 1.
    t4 /Users/adaptmac/Documents/AdaptSource/src/xivic/adapt.presentation.xamarinforms/adapt.presentation.xivic.ios/Assets.xcassets/attachment.imageset/Contents.tt
    Processing '/Users/adaptmac/Documents/AdaptSource/src/xivic/adapt.presentation.xamarinforms/adapt.presentation.xivic.ios/Assets.xcassets/attachment.imageset/Contents.tt' failed.
EXEC : error : Error running transform: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentNullException: Value cannot be null. [/Users/adaptmac/Documents/AdaptSource/src/xivic/adapt.presentation.xamarinforms/adapt.presentation.xivic.ios/adapt.presentation.xivic.ios.csproj] [/Users/adaptmac/Documents/AdaptSource/src/xivic/adapt.presentation.xamarinforms/adapt.presentation.xivic.ios/adapt.presentation.xivic.ios.csproj]
    Parameter name: path1
       at System.IO.Path.Combine(String path1, String path2)
       at Mono.TextTemplating.TemplateGenerator.ResolvePath(String path) in C:\projects\t4\Mono.TextTemplating\Mono.TextTemplating\TemplateGenerator.cs:line 240
       at Mono.TextTemplating.TemplateGenerator.Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost.ResolvePath(String path) in C:\projects\t4\Mono.TextTemplating\Mono.TextTemplating\TemplateGenerator.cs:line 380
       at Microsoft.VisualStudio.TextTemplating.GeneratedTextTransformation8c16d7c.TransformText()
       --- End of inner exception stack trace ---
       at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at Mono.TextTemplating.CompiledTemplate.Process() in C:\projects\t4\Mono.TextTemplating\Mono.TextTemplating\CompiledTemplate.cs:line 119
/Users/adaptmac/Documents/AdaptSource/src/xivic/adapt.presentation.xamarinforms/adapt.presentation.xivic.ios/iOS.Images.targets(71,9): error MSB3073: The command "t4 /Users/adaptmac/Documents/AdaptSource/src/xivic/adapt.presentation.xamarinforms/adapt.presentation.xivic.ios/Assets.xcassets/attachment.imageset/Contents.tt" exited with code 1. [/Users/adaptmac/Documents/AdaptSource/src/xivic/adapt.presentation.xamarinforms/adapt.presentation.xivic.ios/adapt.presentation.xivic.ios.csproj] [/Users/adaptmac/Documents/AdaptSource/src/xivic/adapt.presentation.xamarinforms/adapt.presentation.xivic.ios/adapt.presentation.xivic.ios.csproj]
  Done Building Project "/Users/adaptmac/Documents/AdaptSource/src/xivic/adapt.presentation.xamarinforms/adapt.presentation.xivic.ios/adapt.presentation.xivic.ios.csproj" (TransformAlliOS target(s)) -- FAILED.

These templates are only here because we don't have time to scale and write a manifest for every image in our Xamarin.iOS application.

You'll notice that the t4 command line tool is invoked in an Exec task calling msbuild.exe instead of using the MSBuild task. This is because the templates (along with generated, re-scaled images) are copied to each image folder and then included with a wildcard that needs to be re-evaluated. This re-evaluation is optimised away when using the MSBuild task.

I've included a binary log for further information that may help:
msbuild-binary-log.zip

Adding Assembly reference leads to error missing reference

I am trying to build a tt file which has references to other Assemblies.
So I tried to provide the Assembly via command line (-r) which got ignored. Then I changed the assembly to point to the file within the tt file, see below code.

<#@ Assembly Name="/root/.nuget/packages/system.data.datasetextensions/4.5.0/lib/netstandard2.0/System.Data.DataSetExtensions.dll" #>
<#@ Import Namespace="System.Data" #>
.... some code
DataSet dsProcedures = new DataSet();		
..... more code

But I am getting the error

ERROR: The type or namespace name 'DataSet' could not be found (are you missing a using directive or an assembly reference?)

Some more info.
I am using docker with the following Dockerfile

FROM microsoft/dotnet:sdk

RUN dotnet tool install -g dotnet-t4
ENV PATH="/root/.dotnet/tools:${PATH}"

Before starting the t4 command I am doing a dotnet restore on my project so that all dependencies are loaded.

Within Visual Studio I can build the tt files without changing the assembly name so I just have <#@ Assembly Name="System.Data" #>

Want we want to achive is a simple ci with a docker container to automate our build.
Maybe this is related to #21 ?

System.ArgumentNullException when using inheritance in template

Hi there,

I' using the T4 commandline tool and I can't get template inheritance working. When I run the following template without inheritance is works just fine.

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="C:\Projects\Framework\Binaries\Baffling.dll" #>
<#@ assembly name="C:\Projects\Framework\Binaries\Mono.TextTemplating.dll" #>
<#@ import namespace="Baffling" #>
<#@ import namespace="Mono.TextTemplating" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".txt" #>
Have a nice day!

The result is a text file containing nothing more than 'Have a nice day!'.

But when I add the 'inherits' attribute like this

<#@ template debug="false" hostspecific="false" language="C#" inherits="Baffling.TransformBase" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="C:\Projects\Framework\Binaries\Baffling.dll" #>
<#@ assembly name="C:\Projects\Framework\Binaries\Mono.TextTemplating.dll" #>
<#@ import namespace="Baffling" #>
<#@ import namespace="Mono.TextTemplating" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".txt" #>
Have a nice day!

I get the following error:

C:\Baffling>T4 Plain.tt
System.ArgumentNullException: Value cannot be null.
Parameter name: type
   at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
   at Mono.TextTemplating.CompiledTemplate.Load(CompilerResults results, String fullName) in /Users/mikayla/code/t4/Mono.TextTemplating/Mono.TextTemplating/CompiledTemplate.cs:line 63
   at Mono.TextTemplating.TemplatingEngine.CompileTemplateInternal(ParsedTemplate pt, String content, ITextTemplatingEngineHost host, TemplateSettings settings) in /Users/mikayla/code/t4/Mono.TextTemplating/Mono.TextTemplating/TemplatingEngine.cs:line 203
   at Mono.TextTemplating.ToolTemplateGenerator.ProcessTemplate(ParsedTemplate pt, String inputFile, String inputContent, String& outputFile, TemplateSettings settings) in /Users/mikayla/code/t4/dotnet-t4/ToolTemplateGenerator.cs:line 64
   at Mono.TextTemplating.TextTransform.MainInternal(String[] args) in /Users/mikayla/code/t4/dotnet-t4/TextTransform.cs:line 214
   at Mono.TextTemplating.TextTransform.Main(String[] args) in /Users/mikayla/code/t4/dotnet-t4/TextTransform.cs:line 42

The TransformBase class looks like this:

using Microsoft.VisualStudio.TextTemplating;

namespace Baffling
{
    public class TransformBase : TextTransformation
    {
        public override string TransformText()
        {
            return "No you have a nice day!";
        }
    }
}

If anyone could shed some light on this issue I would be most gratefull.

Regards,
Gerben ten wolde

output extension directive

It seems that using a directive like

<#@ output extension=".whatever" #>

and treating the template with texttransform.exe template.tt does not generate the correct output file, it just creates a file with a .txt extension.

Add MSBuild targets package

Create a package that contains MSBuild targets for transforming on build. It should work on both Mono and .NET Core.

Options help: Expected interaction between -r and -P

As far as I'm reading the --help my expectation would be that -r= wants a .dll, and -P= sets a directory it should exist in. (Part B: Is this a depth search or target directory only? IE can I specify the NugetFallbackFolder ?)

I'm currently trying to load the assembly Microsoft.AspNetCore.Hosting.Abstractions.dll which resides in C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.hosting.abstractions\2.2.0\lib\netstandard2.0

Unfortunately, supplying -r="Microsoft.AspNetCore.Hosting.Abstractions.dll" -P="C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.hosting.abstractions\2.2.0\lib\netstandard2.0" does not appear to locate it.

The following error is output

Error running transform: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.FileNotFoundException: Could not 
load file or assembly 'Microsoft.AspNetCore.Hosting.Abstractions, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

Using reflection assembly loading does work and will prevent this error (and then it's just the next in the line of external assemblies that I need to load), so I have a work around, and it's theoretically about the same amount of work of figuring out the full set of assemblies needed and their relevant paths, but I'd like to better understand the tool.

I don't know if these are supposed to be interconnected with the T4 <#@ assembly Name="...dll" #> directives or not, but I've had no luck in that department either (also amateur T4 composer, so that could be an absolute point of failure)

Specifying a parameter with an equal sign in it

I am having trouble specifying a parameter with an equal sign in it.

Example: t4 something.tt -o:something.cs /p:MyParam="testing="

This appears to be an issue with this: https://github.com/mono/mono/blob/master/mcs/class/Mono.Options/Mono.Options/Options.cs#L570

Any suggestions on how to allow this as we have values with equal signs in them?

A full example: echo "<#@ parameter name='Name' #>Hello <#=Name#>" | t4 -o - -p:Name='The World=Awesome'

Expected output: Hello The World = Awesome
Actual output: Hello The World

Thanks!

Is there any support for transforming T4 files on every build?

In one of my projects, I'd like to avoid committing the generated C# code to the repository. So I'm wondering if there is any kind of support for something like the TransformOnBuild property as described in this SO answer? That specific solution isn't great since it requires having both VS and the VS SDK installed. That same SO topic also has a mention of this project but that would require knowing exactly where the project tool gets installed. (It also doesn't look like that project supports .NET Core.)

What I'm looking for would ideally be the ability to just add something like the following to my project file:

  <DotNetCliToolReference Include="dotnet-t4-project-tool" Version="2.0.5" />
  <ItemGroup>
    <None Include="SomeTemplate.tt">
      <Generator>TextTemplatingFileGenerator</Generator>
      <TransformOnBuild>true</TransformOnBuild>
    </None>
    <Compile Include="SomeTemplate.cs">
      <DependentUpon>SomeTemplate.tt</DependentUpon>
    </Compile>
  </ItemGroup>

Pass object to template

Are there any examples of using T4?

I want to pass an object from my application to a template.

Tell me, please, how can I do this?

Modernize host and directive interfaces

The host, directive and session interfaces in the Microsoft.VisualStudio.TextTemplating namespace are pretty clunky, for example they return object-typed values without any generic helpers to assist in the casts. We should deprecate these and implement a new modern API for directives and for templates to access the host. In general abstract classes are better than interfaces as we can add/deprecate members much more easily. We can also have shims for code that uses the old API.

The ITextTemplatingEngine interface can be eliminated, as there's no need to abstract out the engine itself.

The members on IRecognizeHostSpecific can be moved to virtual members on the new DirectiveProcessor abstract class.

We could also consider putting the new directive/host/session APIs in a separate assembly for templates and directives to reference.

Template compilation fails due to missing core references

Step to reproduce

git clone https://github.com/mono/t4.git t4
cd t4
git checkout v2.0.1
git pull https://github.com/atifaziz/t4.git issue-29
dotnet run -p dotnet-t4 -- dotnet-t4\test.tt

Note that we need to merge the patch from PR #30 that addresses issue #29 otherwise the run-time is not found.

Expected

Expected the template to compile successfully.

Actual Results

Instead, it fails due to errors about core references not being found:

C:\Program Files\dotnet\sdk\2.2.101\Sdks\NuGet.Build.Tasks.Pack\build\NuGet.Build.Tasks.Pack.targets(202,5): warning NU5125: The 'licenseUrl' element will be deprecated. Consider using the 'license' element instead. [A:\Mono.TextTempalting\dotnet-t4\dotnet-t4.csproj]
Processing '.\dotnet-t4\test.tt' failed.
ERROR: Source file 'A:\Mono.TextTempalting\Files\dotnet\shared\Microsoft.NETCore.App\2.1.6\System.Private.Uri.dll' could not be found.
ERROR: Source file 'A:\Mono.TextTempalting\Files\dotnet\shared\Microsoft.NETCore.App\2.0.6\System.Private.CoreLib.dll' could not be found.
ERROR: Source file 'A:\Mono.TextTempalting\Files\dotnet\shared\Microsoft.NETCore.App\2.0.6\netstandard.dll' could not be found.
ERROR: Source file 'A:\Mono.TextTempalting\Files\dotnet\shared\Microsoft.NETCore.App\2.0.6\mscorlib.dll' could not be found.
ERROR: Source file 'A:\Mono.TextTempalting\Files\dotnet\shared\Microsoft.NETCore.App\2.0.6\System.Xml.dll' could not be found.
ERROR: Source file 'A:\Mono.TextTempalting\Files\dotnet\shared\Microsoft.NETCore.App\2.0.6\System.Runtime.dll' could not be found.

The problem seems to be due to malformed paths to the run-time.

More Information

dotnet --info says:

.NET Core SDK (reflecting any global.json):
 Version:   2.2.101
 Commit:    236713b0b7

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.17134
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\2.2.101\

Host (useful for support):
  Version: 2.2.0
  Commit:  1249f08fed

.NET Core SDKs installed:
  1.1.8 [C:\Program Files\dotnet\sdk]
  1.1.9 [C:\Program Files\dotnet\sdk]
  1.1.10 [C:\Program Files\dotnet\sdk]
  1.1.11 [C:\Program Files\dotnet\sdk]
  2.1.101 [C:\Program Files\dotnet\sdk]
  2.1.103 [C:\Program Files\dotnet\sdk]
  2.1.104 [C:\Program Files\dotnet\sdk]
  2.1.200 [C:\Program Files\dotnet\sdk]
  2.1.201 [C:\Program Files\dotnet\sdk]
  2.1.202 [C:\Program Files\dotnet\sdk]
  2.1.300 [C:\Program Files\dotnet\sdk]
  2.1.400 [C:\Program Files\dotnet\sdk]
  2.1.402 [C:\Program Files\dotnet\sdk]
  2.1.403 [C:\Program Files\dotnet\sdk]
  2.1.500 [C:\Program Files\dotnet\sdk]
  2.2.101 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 1.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.1.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.1.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

Any CLI option fails with “No input file specified.”

Steps to Reproduce

On Windows, install t4 as a global tool:

> dotnet tool install --global dotnet-t4
You can invoke the tool using the following command: t4
Tool 'dotnet-t4' (version '2.0.1') was successfully installed.

Next, run the tool against the test.tt file with the -o option to emit the result to standard output:

t4 -o=- test.tt

Expected

Expected the template to be processed successfully and the result to be emitted to standard output.

Actual Result

Instead, it fails with the error:

No input file specified.

This happens for any command-line option, not just -o.

More Information

dotnet --info says:

.NET Core SDK (reflecting any global.json):
 Version:   2.2.101
 Commit:    236713b0b7

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.17134
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\2.2.101\

Host (useful for support):
  Version: 2.2.0
  Commit:  1249f08fed

.NET Core SDKs installed:
  1.1.8 [C:\Program Files\dotnet\sdk]
  1.1.9 [C:\Program Files\dotnet\sdk]
  1.1.10 [C:\Program Files\dotnet\sdk]
  1.1.11 [C:\Program Files\dotnet\sdk]
  2.1.101 [C:\Program Files\dotnet\sdk]
  2.1.103 [C:\Program Files\dotnet\sdk]
  2.1.104 [C:\Program Files\dotnet\sdk]
  2.1.200 [C:\Program Files\dotnet\sdk]
  2.1.201 [C:\Program Files\dotnet\sdk]
  2.1.202 [C:\Program Files\dotnet\sdk]
  2.1.300 [C:\Program Files\dotnet\sdk]
  2.1.400 [C:\Program Files\dotnet\sdk]
  2.1.402 [C:\Program Files\dotnet\sdk]
  2.1.403 [C:\Program Files\dotnet\sdk]
  2.1.500 [C:\Program Files\dotnet\sdk]
  2.2.101 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 1.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.1.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.1.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

System.Runtime.Remoting not supported in .net Standard 2.0

Cannot invoke template in .net Standard library due to generated to containing: "System.Runtime.Remoting".
The generated template (*.cs) does not compile due to the Remoting reference.

Any suggestions?

TT: TextTemplatingFilePreprocessor
Assembly: .net Standard Library 2.0

Can't resolve dependencies in dotnet core

I'm using the latest preview version of Mono.TextTemplating (2.2.0-preview-0012-ga253e82592) and I've run into an issue importing a library that has its own dependencies. Since I'm still getting used to the dotnet core way of handling libraries, I'll just go ahead and post all the information I think might be relevant:

  • My program (executable) that uses Mono.TextTemplating for its templates targets netcoreapp3.1.
  • My library (the one I'm trying to import into a T4 template) targets netcoreapp3.1. Everything has been working smoothly for months, before I added dependencies to this library.
  • My library now has a dependency, specifically Newtonsoft.Json (version 12.0.3). Since adding this dependency, I can no longer import my library. I get the following error messages if I try:
error : error : Error running transform: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
error :  ---> System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'. Could not find or load a specific file. (0x80131621)
error : File name: 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' 
error :  ---> System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'.
error :    at System.Runtime.Loader.AssemblyLoadContext.LoadFromPath(IntPtr ptrNativeAssemblyLoadContext, String ilPath, String niPath, ObjectHandleOnStack retAssembly)
error :    at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath)
error :    at System.Reflection.Assembly.LoadFrom(String assemblyFile)
error :    at System.Reflection.Assembly.LoadFromResolveHandler(Object sender, ResolveEventArgs args) [C:\
error :    at System.Runtime.Loader.AssemblyLoadContext.InvokeResolveEvent(ResolveEventHandler eventHandler, RuntimeAssembly assembly, String name)
error :    at System.Runtime.Loader.AssemblyLoadContext.OnAssemblyResolve(RuntimeAssembly assembly, String assemblyFullName)
error :    at Nucleus.Model.Deserialize(String json)
error :    at Microsoft.VisualStudio.TextTemplating39cae511.GeneratedTextTransformation.TransformText()
error :    --- End of inner exception stack trace ---
error :    at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
error :    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
error :    at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
error :    at Mono.TextTemplating.CompiledTemplate.Process()

I have made a few attempts of making the error go away, by making sure Newtonsoft.Json is in my nuget cache and also by placing Newtonsoft.Json.dll in the same folder as my library dll. It makes no difference.

Last year I wrote a dotnet core app that could load plugins (dlls) dynamically. If I recall correctly, I had a similar issue with my own app, that I resolved by using the AssemblyDependencyResolver added in 3.0 (https://docs.microsoft.com/en-us/dotnet/core/tutorials/creating-app-with-plugin-support). Is it possible that a similar change should be implemented in T4, or am I missing an easier solution?

Mono.TextTemplating.Roslyn package not published to nuget.org

Mono.TextTemplating.Roslyn is a nice addition to Mono.TextTemplating to use the In-process Roslyn compiler, but unfortunately it cannot be found on nuget.org yet because there there hasn't been a new release in a while. The latest one dates June 13th 2019.

Is it possible to have it included in the next release and published?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.