1

How can I run T4 files in a project via plugin? Any hint / clue? I'm finding the EnvDte documentation to especially scarce.

I got to this solution because I need to run my T4 templates on build events. I don't want to manually run custom tools everytime something changes. I canNOT run it from command line, because the T4 scripts have reference to DTE objects, and when run from cmd line it just barfs out.

I also considered using Macros, but Macros aren't available on VS2012 plus there is a bug on my local machine that prevents me from firing up the Macro IDE. So this is not a good short/long term solution.

The best solution seems write own plugin to run the T4 file on build. This is as far as I got:

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    _applicationObject.Events.BuildEvents.OnBuildBegin += customBuildHander;

}

void customBuildHander(vsBuildScope Scope, vsBuildAction Action)
        {
            if (Scope == vsBuildScope.vsBuildScopeProject)
            {
                //Need to run steps 1 thru 3 here!
            }
        }

I need to fit the following routines in the above code somehow:

  1. Find all the T4 files in a project
  2. Run T4 on the TT files, while passing the DTE object so they are accessible from the T4 files
  3. Also declare environmental variables such as $(ProjectDir) so they can be read in the T4 script <-- I don't know if this comes with the DTE or is a seperate process.

Please help!

Alwyn
  • 8,079
  • 12
  • 59
  • 107

1 Answers1

2

You should probably invoke the IDE command TransformAllTemplates using the DTE infrastructure to run a command rather than doing your own traversal if you're just going to run them all anyway.

GarethJ
  • 6,496
  • 32
  • 42