private static TemplateRenderingResult GetTemplateOutput(Project project, IFileSystem fileSystem, string template, string projectRelativeOutputPath, Hashtable model, bool force)
{
var templateFullPath = FindTemplateAssertExists(project, fileSystem, template);
var templateContent = fileSystem.ReadAllText(templateFullPath);
templateContent = PreprocessTemplateContent(templateContent);
// Create the T4 host and engine
using (var host = new DynamicTextTemplatingEngineHost { TemplateFile = templateFullPath }) {
var t4Engine = GetT4Engine();
// Make it possible to reference the same assemblies that your project references
// using <@ Assembly @> directives.
host.AddFindableAssembly(FindProjectAssemblyIfExists(project));
foreach (dynamic reference in ((dynamic)project.Object).References) {
if ((!string.IsNullOrEmpty(reference.Path)) && (!reference.AutoReferenced))
host.AddFindableAssembly(reference.Path);
}
string projectRelativeOutputPathWithExtension = null;
ProjectItem existingOutputProjectItem = null;
if (!string.IsNullOrEmpty(projectRelativeOutputPath)) {
// Respect the <#@ Output Extension="..." #> directive
projectRelativeOutputPathWithExtension = projectRelativeOutputPath + GetOutputExtension(host, t4Engine, templateContent);
// Resolve the output path and ensure it doesn't already exist (unless "Force" is set)
var outputDiskPath = Path.Combine(project.GetFullPath(), projectRelativeOutputPathWithExtension);
existingOutputProjectItem = project.GetProjectItem(projectRelativeOutputPathWithExtension);
if (existingOutputProjectItem != null)
outputDiskPath = existingOutputProjectItem.GetFullPath();
if ((!force) && fileSystem.FileExists(outputDiskPath)) {
return new TemplateRenderingResult(projectRelativeOutputPathWithExtension) { SkipBecauseFileExists = true };
}
}
// Convert the incoming Hashtable to a dynamic object with properties for each of the Hashtable entries
host.Model = DynamicViewModel.FromObject(model);
// Run the text transformation
var templateOutput = t4Engine.ProcessTemplate(templateContent, host);
return new TemplateRenderingResult(projectRelativeOutputPathWithExtension) {
Content = templateOutput,
Errors = host.Errors,
ExistingProjectItemToOverwrite = existingOutputProjectItem,
};
}
}