T4 Get Current Working Directory of Solution
Asked Answered
J

3

25

I am using T4 in Visual Studio 2010, and I want to iterate over the files in my solution, however I have found that T4 source generation works in a kind of a sandbox, and the current working directory is inside of the Visual Studio 10 directory in program files.

Is there a way to reference the solution the T4 file is in relativistically, so that it doesn't break the build, or works on some one else's box that doesn't have the same file structure etc?

Thanks

Jessamine answered 10/2, 2011 at 2:37 Comment(0)
B
41

You must set the hostspecific attribute to true like so:

<#@ template language="C#" hostspecific="True" #>

The ITextTemplatingEngineHost interface will give you the information you need.

<#= this.Host.ResolveParameterValue("-", "-", "projects") #>

I don't believe there is a way to reference the solution, but you can get the path in which your *.tt file is and from there get other files.

To load a file from a location relative to the text template, you can use this:

this.Host.ResolvePath("relative/path.txt")
Blaze answered 10/2, 2011 at 5:16 Comment(4)
Thanks Shiv! I ended up using this.Host.TemplateFile, which points directly to the template and ran from there.Jessamine
You can get the Solution directly by loading the DTE: #12952610Center
@PhilipPittle why i should this to prefer?Equipoise
@devi - I don't follow, what preference are you referring to? Shiv said "I don't believe there is a way to reference the Solution" and I commented "You can get the Solution directly by loading the DTE".Center
F
16

This is the method I use to get the solution base directory:

public string GetSolutionDirectory()
{
    var serviceProvider = this.Host as IServiceProvider;
    var dte = serviceProvider.GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
    return System.IO.Path.GetDirectoryName(dte.Solution.FullName);
}
Fifteenth answered 24/7, 2015 at 17:46 Comment(1)
One information I missed trying your approach was registering the assembly EnvDTE with <#@ assembly name="EnvDTE" #><# /* This assembly provides access to Visual Studio project properties. */ #> as indicated in Brandon S. answer. Sadly most people forget to mention imports/assembly definitions when they post code examples. Apart from that I find your answer just to be what I needed. Thx!Octodecimo
H
5

Here's the how to use the logic JCallico provided in a T4 template that creates an XML file:

<#@ template debug="false" hostspecific="true" language="C#" #><# /* hostspecific must be set to "true" in order to access Visual Studio project properties. */ #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Text" #>
<#@ output extension=".xml" #>
<#@ assembly name="EnvDTE" #><# /* This assembly provides access to Visual Studio project properties. */ #>
<#
    var serviceProvider = this.Host as IServiceProvider;
    var dte = serviceProvider.GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
    var solutionDirectory = System.IO.Path.GetDirectoryName(dte.Solution.FullName);
#>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <mySetting filePath="<#= solutionDirectory #>\MySubfolder\MyFile.exe" />
</configuration>

The XML attribute "filePath" will equal the Visual Studio's solution directory plus "\MySubfolder\MyFile.exe".

Hypotrachelium answered 4/12, 2015 at 17:12 Comment(1)
What does creating an XML file have to do with the original question? It appears you copy/pasted another answer and implemented it in a solution that doesn't have anything to do with the question.Cassation

© 2022 - 2024 — McMap. All rights reserved.