Ejemplo n.º 1
0
        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)
                });
            }
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        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());
        }