Skip to content

Instantly share code, notes, and snippets.

@mjul
Created May 23, 2013 16:00
Show Gist options
  • Select an option

  • Save mjul/5637174 to your computer and use it in GitHub Desktop.

Select an option

Save mjul/5637174 to your computer and use it in GitHub Desktop.
Visual Studio T4 template that invokes an external compiler on a file in the project to generate a C# code file.
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Diagnostics" #>
<#@ assembly name="EnvDTE" #>
<#@ import namespace="EnvDTE" #>
<#@ output extension=".cs" #>
<#
IServiceProvider serviceProvider = (IServiceProvider)this.Host;
DTE dte = serviceProvider.GetService(typeof(DTE)) as DTE;
var solutionRoot = new DirectoryInfo(dte.Solution.FullName).Parent.FullName;
var compiler = Path.Combine(solutionRoot, @"Compiler\bin\Debug\Compiler.exe");
var model = this.Host.ResolvePath("Model.dsl");
// Start the child process.
var p = new System.Diagnostics.Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = compiler;
p.StartInfo.Arguments = String.Format("\"{0}\"", model);
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
#>
<#= output #>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment