/// <summary> /// Performs the template compilation. /// </summary> /// /// <returns> /// <see langword="true"/> if the processing succeeded; <see langword="false"/> otherwise. /// </returns> public override bool Execute() { var host = new EngineHost(); var rootNamespace = RootNamespace; var projectRootFolder = Environment.CurrentDirectory; var newLine = Environment.NewLine; foreach (var templateFile in TemplateFiles) { var inputPath = templateFile.ItemSpec; Log.LogMessage (MessageImportance.Normal, "Compiling T4 template {0}", inputPath); try { CompileTemplate (host, projectRootFolder, inputPath, rootNamespace, newLine); } catch(Exception e) { Log.LogErrorFromException (e); } } foreach (CompilerError err in host.Errors) { Log.LogError ( "TemplateCompiler", err.ErrorNumber, null, err.FileName, err.Line, err.Column, 0, 0, err.ErrorText); } return !Log.HasLoggedErrors; }
void Generate(string input, string namespac, string className, string expectedOutput) { var host = new EngineHost(); string actualOutput; using (var writer = new StringWriter()) { Parent.TemplateCompiler.GenerateCode(host, input, namespac, className, writer); actualOutput = writer.ToString(); } Assert.AreEqual (0, host.Errors.Count); var output = StripHeader (actualOutput); Assert.AreEqual (expectedOutput, output); }
internal static void CompileTemplate(EngineHost host, string projectRootFolder, string inputPath, string rootNamespace, string newLine) { var className = Path.GetFileNameWithoutExtension (inputPath); host.TemplateFile = Path.GetFileName(inputPath); var targetFolder = Path.GetDirectoryName (inputPath); var absoluteInputPath = Path.Combine (projectRootFolder, inputPath); var inputText = File.ReadAllText (absoluteInputPath); var fullNamespace = rootNamespace; if (!String.IsNullOrEmpty(targetFolder)) { fullNamespace += "." + ConvertPathToNamespace(targetFolder); } var fileName = "{0}.Generated.cs".FormatInvariant (className); var relativeOutputPath = Path.Combine (targetFolder, fileName); var absoluteOutputPath = Path.Combine (projectRootFolder, relativeOutputPath); using (var writer = new StreamWriter (absoluteOutputPath, false, Encoding.UTF8)) { writer.NewLine = newLine; GenerateCode (host, inputText, fullNamespace, className, writer); } }