Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Host.ResolvePath and relative paths

Tags:

t4

I have put my t4 template in a CodeTemplate\MyTemplateFolder folder but can't get it to work from there.

The problem is that I need to access a folder, called models, that is in the root of the project.

Ie:

Project Root Folder\CodeTemplates\MyTemplateFolder\MyTemplate.tt

and I need to access a file in:

Project Root Folder\Models

However, I can't figure out how to use this.Host.ResolvePath to get at the Models folder from my template.

Any suggestions?

Use of:

var relativePathModelsFolder = @"..\..\Models";
var path = this.Host.ResolvePath(relativePathModelsFolder);

gives me the absolute path to the root folder with ....\Models stuck on the end.

I don't seem to be able to traverse back up to the root, and then into my Models folder.

like image 234
awrigley Avatar asked Oct 24 '25 14:10

awrigley


2 Answers

This seems a simpler way to me:

({
<#
var projectPath = Host.ResolveAssemblyReference("$(ProjectDir)");
var viewPath = projectPath + "Views";

#>
   view:"<#=viewPath #>",
}
)
like image 188
WoofWoof88 Avatar answered Oct 26 '25 15:10

WoofWoof88


I have solve the issue by getting the project then getting the path then adding the folder:

Getting the project:

Project GetLibraryProject(string tier)
    {
        return VisualStudioHelper.GetAllProjects().Where(x => x.Name == tier).First();
    }

Getting the path:

string GetProjectPath(Project project)
{
    var pathProjectFile = project.FullName;
    return pathProjectFile.Replace(project.Name + ".csproj", "");
}

Putting it all together:

var project = GetLibraryProject("MyMvcApp.MyClassLibrary");
var path = GetProjectPath(project);
var myUsefulPath = path + @"MyFolderParent\MyFolder";
like image 28
awrigley Avatar answered Oct 26 '25 16:10

awrigley