For T4 templates that create metadata, service, and ViewModel classes (which I want to change), I need the ability to combine new templates with existing ones, as well as create new ones for new objects, so I back up the file (.BAK) before calling fileManager. StartNewFile ...
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name)) { var outputPath = Path.GetDirectoryName(Host.TemplateFile); var outputFile = entity.Name + ".ViewModel.cs"; var fileName = Path.Combine(outputPath, outputFile); if (File.Exists(fileName)) { var newName = fileName + ".BAK"; File.Move(fileName, newName); } fileManager.StartNewFile(outputFile);
and then after fileManager.Process I merge the .BAK file and the new one using SourceGear DiffMerge ...
fileManager.Process(); System.Diagnostics.Process p = new System.Diagnostics.Process(); foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name)) { var outputPath = Path.GetDirectoryName(Host.TemplateFile); var outputFile = entity.Name + ".ViewModel.cs"; var fileName = Path.Combine(outputPath, outputFile); var newName = fileName + ".BAK"; if (File.Exists(newName)) { String s = String.Format("-m -nosplash \"{0}\" \"{1}\" \"{2}\"", fileName, fileName, newName); p.StartInfo.Arguments = s; p.StartInfo.FileName = "C:\\Program Files (x86)\\SourceGear\\DiffMerge\\DiffMerge.exe"; p.StartInfo.Verb = "Open"; p.Start(); p.WaitForExit(); File.Delete(newName); } }
It works very well, and since DiffMerge is a GUI, I can handle conflicts, etc. before saving.
source share