Hi,
It took me a lot of time to find good resources about T4 (even if there are no real alternatives built-in for visual studio). Here a small template to create multiple files easily...
I used following resources exists using archive.org:
kr,
Daniel
<#@
template debug="false" hostspecific="true" language="C#" #><#@
assembly name="System.Core" #><#@
import namespace="System.Linq" #><#@
import namespace="System.Text" #><#@
import namespace="System.IO" #><#@
import namespace="System.Collections.Generic" #><#@
output extension=".log" #><#
CleanOutputFolder();
string propertyName = "Prop";
Enumerable.Range(1,3).ToList().ForEach(id => {
StringBuilder builder = new StringBuilder();
builder.AppendLine("public class Data" + id.ToString("00"));
builder.AppendLine(" {");
Enumerable.Range(1,3).ToList().ForEach(propId => {
builder.AppendLine(" public int "+propertyName+propId+" { get; set; }");
});
builder.AppendLine(" }");
CreateClass("Data"+id.ToString("00")+".cs", builder.ToString());
});
#><#+
public void CleanOutputFolder()
{
string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
string outputFolder = Path.Combine(templateDirectory, "output/");
foreach(var file in Directory.GetFiles(outputFolder))
{
File.Delete(file);
}
}
public void SaveOutput(string outputFileName)
{
string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
string outputFilePath = Path.Combine(templateDirectory, "output/", outputFileName);
File.WriteAllText(outputFilePath, this.GenerationEnvironment.ToString());
this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length);
}
public void CreateClass(string fileName, string content)
{
#>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test
{
<#= content #>
}
<#+
SaveOutput(fileName);
}
#>
It took me a lot of time to find good resources about T4 (even if there are no real alternatives built-in for visual studio). Here a small template to create multiple files easily...
I used following resources exists using archive.org:
- https://web.archive.org/web/20160501025241/http:/www.olegsych.com/2008/03/how-to-generate-multiple-outputs-from-single-t4-template/
- https://web.archive.org/web/20160402073438/http://www.olegsych.com/2008/05/t4-architecture/
Further resources:
- https://resharper-plugins.jetbrains.com/packages/JLebosquain.ForTea/
- https://docs.microsoft.com/en-us/visualstudio/modeling/code-generation-and-t4-text-templates
- writing info in error window: Error("error"), Warning("warning")
kr,
Daniel
<#@
template debug="false" hostspecific="true" language="C#" #><#@
assembly name="System.Core" #><#@
import namespace="System.Linq" #><#@
import namespace="System.Text" #><#@
import namespace="System.IO" #><#@
import namespace="System.Collections.Generic" #><#@
output extension=".log" #><#
CleanOutputFolder();
string propertyName = "Prop";
Enumerable.Range(1,3).ToList().ForEach(id => {
StringBuilder builder = new StringBuilder();
builder.AppendLine("public class Data" + id.ToString("00"));
builder.AppendLine(" {");
Enumerable.Range(1,3).ToList().ForEach(propId => {
builder.AppendLine(" public int "+propertyName+propId+" { get; set; }");
});
builder.AppendLine(" }");
CreateClass("Data"+id.ToString("00")+".cs", builder.ToString());
});
#><#+
public void CleanOutputFolder()
{
string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
string outputFolder = Path.Combine(templateDirectory, "output/");
foreach(var file in Directory.GetFiles(outputFolder))
{
File.Delete(file);
}
}
public void SaveOutput(string outputFileName)
{
string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
string outputFilePath = Path.Combine(templateDirectory, "output/", outputFileName);
File.WriteAllText(outputFilePath, this.GenerationEnvironment.ToString());
this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length);
}
public void CreateClass(string fileName, string content)
{
#>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test
{
<#= content #>
}
<#+
SaveOutput(fileName);
}
#>
No comments:
Post a Comment