rdjnl / texttemplatingcore Goto Github PK
View Code? Open in Web Editor NEWT4 Text Templating with .NET 6
License: Other
T4 Text Templating with .NET 6
License: Other
Hello,
Thank you so much for this library which allows us to write T4 files for the .Net core assemblies. Since debugging is not available with this library, I was trying to find out the issues with our template files which used to work with old gen engine by adding Try.. Catch sections, but it doesn't appear to be honouring those and always outputting 'ErrorGeneratingOutput' in the generated file which doesn't help much in diagnosing the issues. I have added a simple template here which deliberately creates an exception here:
<#@ template hostspecific="false" debug="false" language="C#" #>
<#@ output extension=".generated.txt" encoding="utf-8" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Text.Json" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Text.Json" #>
<#@ import namespace="System.Text.Json.Serialization" #>
<# BuildTests("Jack"); #>
<#+
void BuildTests(string param)
{
try
{
#>
Hello <#= param #>
<#+
var a = 10/0; //To get an exception
#>
Number: <#= a #>
<#+
}
catch(Exception ex)
{
#>
Error: <#= ex.ToString() #>
<#+
}
}
#>
Any advice is much appreciated which would allow us to find and fix the issues with our template files.
Kind regards,
Joby
Hello RdJNL,
we have a bigger library which should be transformed by T4 in combination with T4Toolbox (the output of multiple input files goes inti multiple output files). Now we had to update our project therefore I try to use your solution for .Net Core. It seems to run but after a while I get following error:
Something went wrong processing the template 'C:\src\SIS-PhoneApp_maui\Sources\Bizerba.Sis.PhoneApp.Database.Common\Generation\Generator.tt': System.TimeoutException: The TemplateExecute process did not respond within 60 seconds. Aborting operation.
at RdJNL.TextTemplatingCore.TextTemplatingCoreLib.TextTemplatingHelper.RunExecute(String inputFileName, String coreInputFile, String coreOutputFile, String[] references, TemplateError[]& errors)
at RdJNL.TextTemplatingCore.TextTemplatingCoreLib.TextTemplatingHelper.ExecuteTemplate(String inputFileName, String templateCode, String[] references, TemplateError[]& errors)
at RdJNL.TextTemplatingCore.TextTemplatingFileGeneratorCore.TextTemplatingFileGeneratorCore.ProcessTemplate(String inputFileName, String inputFileContent, ITextTemplating processor, IVsHierarchy hierarchy)
Is there a possibility to rise that timeout or to see where it hangs?
Can you release a .NET 5 version? I get errors trying to load a .NET 5 dll into it from System.Runtime
Hi,
As I noticed today, the latest VS Update seems to mess up the used assemblies.
Following error is displayed now, when I try to run the custom tool.
Something went wrong executing the template in .NET Core: Error:
An assembly specified in the application dependencies manifest (TemplateExecute.deps.json) was not found:
package: 'System.Text.Json', version: '4.7.1'
path: 'lib/netcoreapp3.0/System.Text.Json.dll'
Currently this problem appeared on Microsoft Visual Studio Professional 2019
Version 16.8.3
As i found int the release notes here
".NET 5.0.1 insertion into Visual Studio v 16.8.3" was included in the last update.
So this could be a potential reason.
Edit: I'm using it with .net Core 3.1 ;-)
For now it is only supporting $(SolutionDir)
and $(ProjectDir)
. It will be great to have support for $(TargetPath)
.
Would be nice to have a support for dotnet 7 and 8 for this extension since MS doesn't seem to care about fixing templating for VS.
Are there any plans on adding the support for mentioned, especially since dotnet 8 is LTS?
For a time, I have my fork with net7 implemented, but not sure if you need a pull request from it.
Thanks for great work and very useful extension.
Just wanted to leave a note of appreciation - I came across this project from .Net Core issue 2000 and it has put an end to messy devenv.config binding redirects.
The only thing I noticed which was quick to fix is that using the <@ assembly
directive takes a path relative to the TT file. With vanilla T4 the path is relative to the solution. For example if we had the folder structure:
MyApp/myApp.sln
MyApp/src/Tests/MyTestProject/myGenerated.tt
MyApp/src/SomeLibrary/bin/debug/net5.0/SomeLibrary.dll
and are writing the template in the context of myGenerated.tt, in vanilla T4 we would use:
<#@ assembly name="src/SomeLibrary/bin/debug/net5.0/SomeLibrary.dll" #>
in TextTemplatingCore we use:
<#@ assembly name="../../SomeLibrary/bin/debug/net5.0/SomeLibrary.dll" #>
Might be worth calling that out in the docs (or not, I'm not your boss)
Thanks again for a great tool
thanks!
We recently migrated from .net framework 4.8 to .net 6. We've found our existing .tt files do not generate.
We have added this extension and attempted to run but get the following errors:
Severity Code Description Project File Line Suppression State
Error Something went wrong executing the template in .NET Core: System.IO.FileNotFoundException: Could not load file or assembly 'd:\Repos\Hvcc.Ips\Src\Server\Hvcc.Ips.Host\Entities\Autogenerated\Microsoft.VisualStudio.Interop.dll'. The system cannot find the file specified.
File name: 'd:\Repos\Hvcc.Ips\Src\Server\Hvcc.Ips.Host\Entities\Autogenerated\Microsoft.VisualStudio.Interop.dll'
at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath)
at RdJNL.TemplateExecute.LibraryLoadContext.LoadLibrary(String library) in C:\Dev\TextTemplatingCore\TemplateExecute\LibraryLoadContext.cs:line 24
at RdJNL.TemplateExecute.Program.Main(String[] args) in C:\Dev\TextTemplatingCore\TemplateExecute\Program.cs:line 68 Hvcc.Ips.Host d:\Repos\Hvcc.Ips\Src\Server\Hvcc.Ips.Host\Entities\Autogenerated\Entities.tt 1
I did manage to find a package to obtain the Microsoft.VisualStudio.Interop.dll, but then found it needed to be copied into several places in our solution. Which then gave us a new error below:
Severity Code Description Project File Line Suppression State
Error Something went wrong executing the template in .NET Core: System.IO.FileNotFoundException: Could not load file or assembly 'd:\Repos\Hvcc.Ips\Src\Server\Hvcc.Ips.Host\Entities\Autogenerated%VSAPPIDDIR%EntityFramework.dll'. The system cannot find the file specified.
File name: 'd:\Repos\Hvcc.Ips\Src\Server\Hvcc.Ips.Host\Entities\Autogenerated%VSAPPIDDIR%EntityFramework.dll'
at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath)
at RdJNL.TemplateExecute.LibraryLoadContext.LoadLibrary(String library) in C:\Dev\TextTemplatingCore\TemplateExecute\LibraryLoadContext.cs:line 24
at RdJNL.TemplateExecute.Program.Main(String[] args) in C:\Dev\TextTemplatingCore\TemplateExecute\Program.cs:line 68 Hvcc.Ips.Host d:\Repos\Hvcc.Ips\Src\Server\Hvcc.Ips.Host\Entities\Autogenerated\Entities.tt 1
Any ideas?
Thanks,
Craig.
Please add 'how to run' manual
Hello I have an excel spreadsheet with two cols
NameCol
TypeCol
How can I template this excel sheet to a simple C# POCO model class with the name and type
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xml.XDocument" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Xml.Linq" #>
<#@ output extension=".txt" #>
<#
string output;
output = new XElement("Test").ToString();
#>
<#= output #>
Got error "error CS1069: The type name 'XElement' could not be found in the namespace 'System.Xml.Linq'. This type has been forwarded to assembly 'System.Private.Xml.Linq, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' Consider adding a reference to that assembly."
I have a .tt file that has a t4 file
In the t4 file I have <#@ assembly name="$(TargetDir)...
I tried to use a exe file for conversion it returns this error
Something went wrong executing the template in .NET Core: System.IO.FileNotFoundException: Could not load file or assembly
...\$(TargetDir)\ ...
Microsoft Visual Studio Enterprise 2022
Version 17.6.3
VisualStudio.17.Release/17.6.3+33801.468
Microsoft .NET Framework
Version 4.8.09032
Text Templating File Generator .NET 6 v1.2.0
<#@ template hostSpecific="true" language="C#" debug="true"
#><#@ output extension=".cstxt"
#><#@ assembly name="mscorlib"
#><#@ assembly name="System"
#><#@ assembly name="System.ComponentModel"
#><#@ assembly name="System.ComponentModel.DataAnnotations"
#><#@ assembly name="System.Private.Xml"
#><#@ assembly name="System.Xml.ReaderWriter"
#><#@ assembly name="System.Xml.Linq"
#><#@ assembly name="$(UserProfile)\.nuget\packages\microsoft.visualstudio.texttemplating\17.5.33428.366\lib\net472\Microsoft.VisualStudio.TextTemplating.dll"
#><#@ assembly name="$(UserProfile)\.nuget\packages\Microsoft.VisualStudio.Interop\17.6.36389\lib\net6.0\Microsoft.VisualStudio.Interop.dll"
#><#@ assembly name="$(UserProfile)\.nuget\packages\microsoft.visualstudio.texttemplating.interfaces\17.5.33428.366\lib\net472\Microsoft.VisualStudio.TextTemplating.Interfaces.dll"
#><#@ assembly name="$(UserProfile)\.nuget\packages\microsoft.visualstudio.texttemplating.interfaces.15.0\16.10.31320.204\lib\net45\Microsoft.VisualStudio.TextTemplating.Interfaces.15.0.dll"
#><#@ assembly name="$(SolutionDir)Core\MyProject.T4s\bin\Debug\net7.0\MyProject.T4s.dll"
#><#@ import namespace="System"
#><#@ import namespace="System.CodeDom.Compiler"
#><#@ import namespace="System.Collections.Generic"
#><#@ import namespace="System.ComponentModel.DataAnnotations"
#><#@ import namespace="System.Configuration"
#><#@ import namespace="System.Diagnostics"
#><#@ import namespace="System.IO"
#><#@ import namespace="System.Linq"
#><#@ import namespace="System.Linq.Expressions"
#><#@ import namespace="System.Reflection"
#><#@ import namespace="System.Runtime.Serialization"
#><#@ import namespace="System.Text"
#><#@ import namespace="System.Text.RegularExpressions"
#><#@ import namespace="System.Xml"
#><#@ import namespace="MyProject.T4s"
#><#
void SaveOutputNoDot(string typeName, string fileNameModifier)
{
if (!string.IsNullOrWhiteSpace(typeName)) { fileNameModifier = typeName + fileNameModifier; }
var templateDirectory = Path.GetDirectoryName(this.Host.TemplateFile);
var outputFilePath = Path.Combine(templateDirectory, fileNameModifier);
if (fileNameModifier.Contains("\\"))
{
var dirs = fileNameModifier.Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
var builtPath = string.Empty;
for (var i = 0; i < dirs.Length - 1; i++)
{
builtPath += (builtPath == string.Empty ? string.Empty : "\\") + dirs[i];
var tempDirPath = Path.Combine(templateDirectory, builtPath);
if (!Directory.Exists(tempDirPath))
{
Directory.CreateDirectory(tempDirPath);
}
}
}
System.IO.File.WriteAllText(outputFilePath, this.GenerationEnvironment.ToString());
this.GenerationEnvironment = new StringBuilder();
}
var g = new MyGenerator();
g.Execute(
writeFn: content => Write(content),
saveFn: name => SaveOutputNoDot(name, string.Empty));
I get 2 errors:
(97,70): error CS0234: The type or namespace name 'ITextTemplatingEngineHost' does not exist in the namespace 'Microsoft.VisualStudio.TextTemplating' (are you missing an assembly reference?)
(101,63): error CS0234: The type or namespace name 'ITextTemplatingEngineHost' does not exist in the namespace 'Microsoft.VisualStudio.TextTemplating' (are you missing an assembly reference?)
However, my T4 file is less than 80 lines long, so it's not the T4 file itself. Double-clicking the errors don't point to any spot in my solution (or does anything afaict).
If I remove the Microsoft.VisualStudio.* dll assemblies, I get the following error:
Something went wrong executing the template in .NET Core: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.Interop, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
File name: 'Microsoft.VisualStudio.Interop, Culture=neutral, PublicKeyToken=null'
at System.Reflection.RuntimeAssembly.InternalLoad(ObjectHandleOnStack assemblyName, ObjectHandleOnStack requestingAssembly, StackCrawlMarkHandle stackMark, Boolean throwOnFileNotFound, ObjectHandleOnStack assemblyLoadContext, ObjectHandleOnStack retAssembly)
at System.Reflection.RuntimeAssembly.InternalLoad(AssemblyName assemblyName, RuntimeAssembly requestingAssembly, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, AssemblyLoadContext assemblyLoadContext)
at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyName(AssemblyName assemblyName)
at RdJNL.TemplateExecute.LibraryLoadContext.LoadLibrary(String library) in D:\Dev\Projects\TextTemplatingCore\TemplateExecute\LibraryLoadContext.cs:line 28
at RdJNL.TemplateExecute.Program.Main(String[] args) in D:\Dev\Projects\TextTemplatingCore\TemplateExecute\Program.cs:line 66
Hi,
First of all, Thank You very much for amazing tool which solving all issues reagarding tt file in .net 6. I noticed that You tool has problem only with
<#@ import namespace="Microsoft.Extensions.Configuration" #>
and I've got error:
Error (21,21): error CS0234: The type or namespace name 'Extensions' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) Client.tt 21
So, my question is: how solve it this error.. I suppose that maybe TextTemplating source code will be improved or do You have any suggestion ?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.