public string GetTemplateSourceCode(string name) { string sourceCode = File.ReadAllText(GetTemplateFileName(name)); var host = T4Generator as ITextTemplatingEngineHost; ParsedTemplate pt = ParsedTemplate.FromText(sourceCode, T4Generator); if (pt.Errors.HasErrors) { host.LogErrors(pt.Errors); return(null); } TemplateSettings settings = TemplatingEngine.GetSettings(T4Generator, pt); var ccu = TemplatingEngine.GenerateCompileUnit(host, sourceCode, pt, settings); var opts = new CodeGeneratorOptions(); using (var writer = new StringWriter()) { settings.Provider.GenerateCodeFromCompileUnit(ccu, writer, opts); return(writer.ToString()); } }
static void Generate(TemplateGenerator host, ProjectFile file, out string outputFile) { outputFile = null; string content; try { content = File.ReadAllText(file.FilePath); } catch (IOException ex) { host.Errors.Add(new CompilerError { ErrorText = GettextCatalog.GetString("Could not read input file '{0}':\n{1}", file.FilePath, ex) }); return; } var pt = ParsedTemplate.FromText(content, host); if (pt.Errors.HasErrors) { host.Errors.AddRange(pt.Errors); return; } var settings = TemplatingEngine.GetSettings(host, pt); if (pt.Errors.HasErrors) { host.Errors.AddRange(pt.Errors); return; } outputFile = file.FilePath.ChangeExtension(settings.Provider.FileExtension); settings.Name = settings.Provider.CreateValidIdentifier(file.FilePath.FileNameWithoutExtension); settings.Namespace = CustomToolService.GetFileNamespace(file, outputFile); settings.IncludePreprocessingHelpers = string.IsNullOrEmpty(settings.Inherits); settings.IsPreprocessed = true; var ccu = TemplatingEngine.GenerateCompileUnit(host, content, pt, settings); host.Errors.AddRange(pt.Errors); if (pt.Errors.HasErrors) { return; } try { using (var writer = new StreamWriter(outputFile, false, System.Text.Encoding.UTF8)) { settings.Provider.GenerateCodeFromCompileUnit(ccu, writer, new CodeGeneratorOptions()); } } catch (IOException ex) { host.Errors.Add(new CompilerError { ErrorText = GettextCatalog.GetString("Could not write output file '{0}':\n{1}", outputFile, ex) }); } }
public void DefaultLanguage() { DummyHost host = new DummyHost(); string template = @"<#= DateTime.Now #>"; ParsedTemplate pt = ParsedTemplate.FromText(template, host); Assert.AreEqual(0, host.Errors.Count); TemplateSettings settings = TemplatingEngine.GetSettings(host, pt); Assert.AreEqual(settings.Language, "C#"); }
public void DefaultLanguage() { var host = new DummyHost(); string template = @"<#= DateTime.Now #>"; var pt = ParsedTemplate.FromTextInternal(template, host); Assert.Empty(host.Errors); TemplateSettings settings = TemplatingEngine.GetSettings(host, pt); Assert.Equal("C#", settings.Language); }
internal static void GenerateCode(ITextTemplatingEngineHost host, string content, string namespac, string name, TextWriter writer) { ParsedTemplate pt = ParsedTemplate.FromText(content, host); if (pt.Errors.HasErrors) { host.LogErrors(pt.Errors); return; } TemplateSettings settings = TemplatingEngine.GetSettings(host, pt); if (name != null) { settings.Name = name; } if (namespac != null) { settings.Namespace = namespac; } if (pt.Errors.HasErrors) { host.LogErrors(pt.Errors); return; } var ccu = TemplatingEngine.GenerateCompileUnit(host, pt, settings); if (pt.Errors.HasErrors) { host.LogErrors(pt.Errors); return; } var opts = new System.CodeDom.Compiler.CodeGeneratorOptions(); settings.Provider.GenerateCodeFromCompileUnit(ccu, writer, opts); }
static string GenerateCode(ITextTemplatingEngineHost host, string content, string name, string generatorNewline) { var pt = ParsedTemplate.FromTextInternal(content, host); if (pt.Errors.HasErrors) { host.LogErrors(pt.Errors); return(null); } TemplateSettings settings = TemplatingEngine.GetSettings(host, pt); if (name != null) { settings.Namespace = name; } if (pt.Errors.HasErrors) { host.LogErrors(pt.Errors); return(null); } var ccu = TemplatingEngine.GenerateCompileUnit(host, content, pt, settings); if (pt.Errors.HasErrors) { host.LogErrors(pt.Errors); return(null); } var opts = new CodeGeneratorOptions(); using var writer = new StringWriter() { NewLine = generatorNewline }; settings.Provider.GenerateCodeFromCompileUnit(ccu, writer, opts); return(writer.ToString()); }
private static TemplateGenerationResult GenerateTemplate( string templateFile, DeserializedLarancaFile larancaFile, string folder, string collectorTypesAsm, string genUtilsAsm, string newtonsoftAsm, string projFile) { var generator = new ToolTemplateGenerator(); var inputFile = templateFile; string inputContent; try { inputContent = File.ReadAllText(inputFile); } catch (IOException ex) { Console.Error.WriteLine("Could not read input file '" + inputFile + "':\n" + ex); return(new TemplateGenerationResult() { StatusCode = 1 }); } if (inputContent.Length == 0) { Console.Error.WriteLine("Input is empty"); return(new TemplateGenerationResult() { StatusCode = 1 }); } var pt = ParsedTemplate.FromText(inputContent, generator); var larnacaPropertiesExtractResult = TemplateLarnacaProperties.Extract(pt); if (larnacaPropertiesExtractResult.Fail()) { Console.Error.WriteLine($"Failed to parse larnaca propertsions of template: {templateFile}. {larnacaPropertiesExtractResult.StatusMessage}"); } var settings = TemplatingEngine.GetSettings(generator, pt); settings.Log = Console.Out; if (pt.Errors.Count > 0) { foreach (var currentError in pt.Errors) { var currentCompilerError = (CompilerError)currentError; if (currentCompilerError.IsWarning) { Console.WriteLine(currentCompilerError.ToString()); } else { Console.Error.WriteLine(currentCompilerError.ToString()); generator.Errors.Add(currentCompilerError); } } } var outputFile = inputFile; if (Path.HasExtension(outputFile)) { var dir = Path.GetDirectoryName(outputFile); var fn = Path.GetFileNameWithoutExtension(outputFile); outputFile = Path.Combine(dir, fn + (settings.Extension ?? ".txt")); } else { outputFile = outputFile + (settings.Extension ?? ".txt"); } HashSet <string> assemblyNamesToRemove = new HashSet <string>(new[] { Path.GetFileName(collectorTypesAsm), Path.GetFileName(genUtilsAsm), Path.GetFileName(newtonsoftAsm), }, StringComparer.OrdinalIgnoreCase); //fix template assemblies path foreach (var x in settings.Assemblies.ToArray()) { if (assemblyNamesToRemove.Contains(Path.GetFileName(x))) { settings.Assemblies.Remove(x); } else { settings.Assemblies.Add(FixPath(x, folder)); } } settings.Assemblies.Add(collectorTypesAsm); settings.Assemblies.Add(genUtilsAsm); settings.Assemblies.Add(newtonsoftAsm); string outputContent = null; if (!generator.Errors.HasErrors) { var larnacaDirective = pt.Directives.FirstOrDefault(d => d.Name.Equals("larnaca", StringComparison.OrdinalIgnoreCase)); if (larancaFile?.DatabaseMeta != null) { generator.AddParameter(null, null, "dbMeta", larancaFile.DatabaseMeta); } generator.AddParameter(null, null, "projFile", projFile); outputContent = generator.ProcessTemplate(pt, inputFile, inputContent, ref outputFile, settings); } if (!generator.Errors.HasErrors) { try { if (outputFile.EndsWith(".g.g.cs", StringComparison.OrdinalIgnoreCase)) { outputFile = outputFile.Substring(0, outputFile.Length - ".g.g.cs".Length) + ".g.cs"; } File.WriteAllText(outputFile, outputContent, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false)); if (outputFile.EndsWith(".cs", StringComparison.OrdinalIgnoreCase)) { if (larnacaPropertiesExtractResult.Data?.DoNotCompile ?? false) { return(new TemplateGenerationResult()); } else { return(new TemplateGenerationResult() { CSFileToCompile = outputFile }); } } else if (outputFile.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase)) { if ((larnacaPropertiesExtractResult.Data?.Type ?? ETemplateType.undefined) == ETemplateType.Analysis) { return(new TemplateGenerationResult() { AnalysisProjectFileToBuild = outputFile }); } else { return(new TemplateGenerationResult()); } } else { return(new TemplateGenerationResult()); } } catch (IOException ex) { Console.Error.WriteLine("Could not write output file '" + outputFile + "':\n" + ex); return(new TemplateGenerationResult() { StatusCode = 1 }); } } else { Console.Error.WriteLine(inputFile == null ? "Processing failed." : $"Processing '{inputFile}' failed."); return(new TemplateGenerationResult() { StatusCode = 1 }); } }