3

I am trying to generate code using T4, but I am unable to run my TextTemplate programmatically using net 3.5.

All links which are supposed to explain how to do it are dead This This

Generating a code file by saving the TextTemplate works as expected.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Ronijo
  • 71
  • 1
  • 8
  • Still seems to be valid: https://stackoverflow.com/a/6415032/2598770 – Rand Random Aug 24 '17 at 14:52
  • Alternative location: `C:\Program Files (x86)\Common Files\microsoft shared\TextTemplating\14.0` – BurnsBA Aug 24 '17 at 14:56
  • @RandRandom The actual links in the answers do not work. The post with 4 upvotes expects the user to have something installed which cannot be an option if I want the generation to work independently. – Ronijo Aug 24 '17 at 15:04
  • 1
    Chances are high that it doesnt need to be installed and the .exe can work standalone. Include it into your project (copy&paste) and publish/share/install it with your app. – Rand Random Aug 24 '17 at 15:06
  • If you need to run the T4 when building the project or solution, you should look for AutoT4 – Mauro Sampietro Aug 24 '17 at 15:10
  • @rand random i will try that when I get back to it tomorrow and report the results – Ronijo Aug 24 '17 at 15:15
  • what XML file do you mean? – Rand Random Aug 25 '17 at 15:18
  • @RandRandom I tried to run .tt file through TextTemplating.exe and it didn't work. I tried it exactly as in the linked example, but I only saw a flash of cmd and nothing happened. I made sure the .tt file is valid. Any ideas? – Ronijo Aug 25 '17 at 15:25
  • @RandRandom My bad, I was talking about the .tt file which I tried to run. When I saved the file manually, it generated the .cs output file as expected. It did not work when I tried to run the code, or drag it on top of the .exe. – Ronijo Aug 25 '17 at 15:26

1 Answers1

7

Update:

There is a newer version available than was mentioned originally see here the announcement: https://devblogs.microsoft.com/dotnet/t4-command-line-tool-for-dotnet/

It is located in

{VS_INSTALL_PATH}\Common7\IDE\TextTransformCore.exe.


Based on your comments.

I have tested the approach and did the following:

Add the TextTransform.exe into your project file (either with Add Existing Item, Copy and Paste it into your Project Folder and then reference it, or what ever)

Create a new .tt file, mine has the following content

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".txt" #>
<#
var dateTimeNow = DateTime.Now.ToString();
#>
<#=dateTimeNow#>

(Just a simple .txt file will be generated with a DateTime to see that its working)

Declare both files - TextTemplate1.tt and TextTransform.exe - as Copy always in its property Copy to Output Directory.

Now have the following code somewhere, I did it in Main

static void Main(string[] args)
{
    File.Delete("TextTemplate1.txt"); //delete the existing file, to make sure the code does what its supposed to do
    Thread.Sleep(1000); //wait for filesystem to do its job

    var proc = new Process
    {
        StartInfo =
        {
            FileName = "TextTransform.exe",
            Arguments = "TextTemplate1.tt"
        }
    };

    proc.Start();
    proc.WaitForExit();
}

(Droped the path, so its relative to the executing .exe - needs to be in same directory)

And with that you should successfully get this output

Everything works as expected, need more info to help you find your problem.

My TextTransform.exe file:

Rand Random
  • 7,300
  • 10
  • 40
  • 88
  • I tried to find out what is happening. I did like you presented in your example and I was thrown this exception https://pastebin.com/t8Wq5gnu . When I excluded the exe from the visual studio project, when I ran it again, it flashed cmd and exited the program. Could there be something wrong with my TextTransform.exe? – Ronijo Aug 25 '17 at 16:10
  • @Ronijo Check your paths the exception clearly says ``The system cannot find the file specified``, use full paths where you KNOW the files are located there and there. – Rand Random Aug 25 '17 at 16:12
  • @Ronijo ``Could there be something wrong with my TextTransform.exe?`` - doubt it – Rand Random Aug 25 '17 at 16:15
  • I have tried to specify full paths, even tried TextTransform on the command line and getting a reply "Error: Exception has been thrown by the target of an invocation.". I made sure I did everything as in the example but nothing. I seriously do not understand what is wrong. – Ronijo Aug 25 '17 at 16:31
  • Can you share a simple project, upload it some where? FYI, I am only online for another hour, than I am gone for the weekend. – Rand Random Aug 25 '17 at 16:32
  • https://drive.google.com/file/d/0B_FbQk83sgZ1ckhfNU9Wa3lTMnM/view?usp=drivesdk Here's a fresh project. I just added the exe and the Texttemplate.tt to bin/debug to try out if it made a difference. – Ronijo Aug 25 '17 at 16:41
  • Seems to really be the case that your TextTransform.exe does something differently than mine. – Rand Random Aug 25 '17 at 16:49
  • Updated my answer. – Rand Random Aug 25 '17 at 16:50
  • Btw, the error about ``The system cannot find the file specified,`` maybe due to the fact, that you didnt specify ``Copy Always`` on ``TextTransform.exe`` – Rand Random Aug 25 '17 at 16:51
  • http://i.imgur.com/1ufnG4x.png Random yes, figured that out changed it – Ronijo Aug 25 '17 at 16:52
  • Did you take your .exe from ``C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE`` , that one doesnt work for me as well. – Rand Random Aug 25 '17 at 16:54
  • You seem to have to use the one over there ``C:\Program Files (x86)\Common Files\Microsoft Shared\TextTemplating\14.0`` – Rand Random Aug 25 '17 at 16:55
  • T:\SomeName\Software\VisualStudio2017\Common7\IDE as I was unable to find one from anywhere else when performing a search, – Ronijo Aug 25 '17 at 16:58
  • And after I performed more thorough search, I found the version recommended from C:\Program Files (x86)\Common Files\microsoft shared\TextTemplating\14.0 and It worked perfectly. I am sorry for the hassle and thanks for helping anyway. – Ronijo Aug 25 '17 at 17:04
  • No Problem, glad I could help – Rand Random Aug 25 '17 at 17:05
  • You question doesn't specify the scenario you are generating for. If you need to run on a machine without Visual Studio installed, then you'll need to copy a bunch more files than just texttransform.exe, as that is just a small shim over the top of DLLs installed by Visual Studio. – GarethJ Aug 26 '17 at 00:37
  • This no longer works with the TextTransform file that comes with VS 2019. If you copy the .exe file to your solution and then run it, it crashes with errors. But if you run it directly from the "Microsoft Visual Studio\2019\Professional\Common7\IDE" directory, then it works. Looks like there are extra dependencies that are in that directory. – HamsterWithPitchfork Dec 02 '20 at 10:21
  • Also, if you run the latest TextTransform from VS 2019 directory, then you can also pass the path to .tt files, they no longer need to be in the same directory as the .exe file. I have several .tt files in many subfolders and I can easily run them by feeding the path to .exe – HamsterWithPitchfork Dec 02 '20 at 10:26