Question

I have created a t4 template which creates typescript dto files based on my c# dtos. During the generation I also want to compile the produced ts files into js files and add them to the solution, just like web essentials does for me when I save a ts file.

My initial thought was to somehow trigger the save event;

EnvDTE.DTE dte = (EnvDTE.DTE) ((IServiceProvider) this.Host).GetService(typeof(EnvDTE.DTE));
dte.OpenFile(EnvDTE.Constants.vsViewKindAny, outputFilePath);

Save the file (to trigger the compile) and then close it again. But I can not get this to work.

Does anyone have an idea for me?

Was it helpful?

Solution

I ended up doing as proposed by Ryan Cavanaugh and just invoking the compiler. I used Olegs tips for creating more than one output for a template to add the generated files to the project and tt template.

I kicked off the compiler by the following code:

<#+ 
void CompileTypeScriptFile(string fullFileName)
{
    var process = new System.Diagnostics.Process
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = "tsc.exe",
            Arguments = "--target ES5 \"" + fullFileName + "\""

        }
    };
    process.Start();
    process.WaitForExit();
}
#>

OTHER TIPS

The right way to do that is to have a separate Build Target (or Task) that runs after the T4 template and compiles the output. T4 does not seem the right fit for this.

You can use T4Toolbox to associate your T4 file with the .ts files. It adds "Custom Tool Template" to the Visual Studio Properties Window and provides a custom tool called T4Toolbox.TemplatedFileGenerator that will transform the template automatically whenever the .ts file is saved. You can use the output management functionality of the T4Toolbox to add the generated .js file to the desired place in the solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top