Search code examples
c#visual-studio-2022envdtevsxvsixmanifest

Creating a Visual Studio 2022 (VSIX) Extension that can process T4 Text Templates


I have been trying to create a Visual Studio Extension (with Command) that can process Text Templates (T4).

I've been trying to follow this page, where the heading says "Passing parameter values to a template". I try copying the code underneath that heading into "Command" file and I get an error, regarding the dte variable. Can anyone please help me with this, as I'm completely new to Visual Studio Extensions? I'm not sure how to "Get a service provider" or what "DTE" even is and I think what make things more difficult is the fact that it is an async method.

Here is the code that I have in the Command file for the extension:

using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.TextTemplating;
using Microsoft.VisualStudio.TextTemplating.VSHost;
using System.IO;

namespace TestVSIXExtension
{
    [Command(PackageIds.MyCommand)]
    internal sealed class MyCommand : BaseCommand<MyCommand>
    {
        protected override async Task ExecuteAsync(OleMenuCmdEventArgs e)
        {
                // Get a service provider - how you do this depends on the context:
                IServiceProvider serviceProvider = dte; // or dslDiagram.Store, for example
                                                        // Get the text template service:
                ITextTemplating t4 = serviceProvider.GetService(typeof(STextTemplating)) as ITextTemplating;
                ITextTemplatingSessionHost host = t4 as ITextTemplatingSessionHost;
                // Create a Session in which to pass parameters:
                host.Session = host.CreateSession();
                // Add parameter values to the Session:
                session["TimesToRepeat"] = 5;
                // Process a text template:
                string result = t4.ProcessTemplate("MyTemplateFile.t4",
                  System.IO.File.ReadAllText("MyTemplateFile.t4"));
           
            await VS.MessageBox.ShowWarningAsync("TestVSIXExtension", "Button clicked");
        }
    }
}

Solution

  • Took hours to find the solution, but here it is. Shame there's not an updated tutorial on how to do this.

     using EnvDTE;
        using EnvDTE80;
        using Microsoft.VisualStudio.Shell;
        using Microsoft.VisualStudio.Shell.Interop;
        using Microsoft.VisualStudio.TextTemplating;
        using Microsoft.VisualStudio.TextTemplating.VSHost;
        using System.IO;
        
        namespace TestVSIXExtension
        {
            [Command(PackageIds.MyCommand)]
            internal sealed class MyCommand : BaseCommand<MyCommand>
            {
                protected override async Task ExecuteAsync(OleMenuCmdEventArgs e)
                {
                        // Get the text template service:
                        ITextTemplating t4 = await Package.GetServiceAsync(typeof(STextTemplating)) as ITextTemplating;
                        ITextTemplatingSessionHost host = t4 as ITextTemplatingSessionHost;
                        // Create a Session in which to pass parameters:
                        host.Session = host.CreateSession();
                        // Add parameter values to the Session:
                        session["TimesToRepeat"] = 5;
                        // Process a text template:
                        string result = t4.ProcessTemplate("MyTemplateFile.t4",
                          System.IO.File.ReadAllText("MyTemplateFile.t4"));
                   
                    await VS.MessageBox.ShowWarningAsync("TestVSIXExtension", "Button clicked");
                }
            }
        }