Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a project's directory from within T4?

Tags:

.net

mono

t4

I've been messing around with T4 support in Mono and noticed a very cumbersome thing. The current directory when running T4 templates is the home directory. I need to be able to read a few files from the current project's directory but I'm at a loss for how to.

<#@ template language="C#v3.5" #>
<#@ output extension="txt" #>

<#=System.IO.Directory.GetCurrentDirectory() #>

yields

/home/earlz

where I want it to yield something like

/home/earlz/MyProject

How do I overcome this problem?

Also, I tried the hostspecific and Host.ResolvePath, but I got a NotImplementedException

like image 623
Earlz Avatar asked Feb 25 '23 12:02

Earlz


2 Answers

Ok, think I've figured it out. You just have to actually resolve paths yourself. To get the directory of the template file(which is good enough for me) just use this:

<#= System.IO.Path.GetDirectoryName(Host.TemplateFile) #>
like image 170
Earlz Avatar answered Mar 06 '23 18:03

Earlz


I fixed ResolvePath after your older question, along with a ton of other improvements. The fix should be in MonoDevelop 2.6, though you could try building from master.

like image 31
Mikayla Hutchinson Avatar answered Mar 06 '23 16:03

Mikayla Hutchinson