Hi
The example how to create multiple files using T4 templates you can found in Data Access Guidance Package (part of Service Factory). For example you can take a look at Templates\DAC\EntityFactory.t4 which generates one string for all business entities, but each class is separated by:
Code Snippet
// begin "filename"
here is the content
// end "filename"
This template is rub by CreateDataRepositoryFromDCProject or CreateDataRepositoryFromBEProject recipes (located in Recipes folder in files with the same name). The action which makes the "magic" is action called SplitAndAddItmesFromStringAction located in Actions\VisualStudio. Please take a look at splitRegex which is responsible for splitting generate string into separate classes.
Here is some code:
Regex:
Code Snippet
static readonly Regex splitRegex = new Regex(@"^// name ""(?<name>[^""]+)""\n^// begin ""(?<filename>[^""]+)"".?$\n(?<content>.*)^// end ""\k<filename>"".?$", RegexOptions.Multiline | RegexOptions.Singleline);
Here is code of a "Execute" method which splits generated string into separate files:
Code Snippet
const string FilenameGroups = "filename";
const string ContentGroups = "content";
const string FolderNameGroups = "name";
public override void Execute()
{
DTE vs = GetService<DTE>(true);
projectItems = new List<EnvDTE.ProjectItem>();
for(Match match = splitRegex.Match(content); match.Success; match = match.NextMatch())
{
string relativeFileName = match.Groups[FilenameGroups].Value;
string fileName = Path.GetFileName(relativeFileName);
string relativeTargetPath = Path.GetDirectoryName(relativeFileName);
... bottom is code which adds a file to a given project
}
}
Hope this clarifies your question. If not please let me know I will try to help.
Kind regards,
Lukasz