示例#1
0
        private static TemplateRenderingResult GetTemplateOutput(Project project, IFileSystem fileSystem, string template, string projectRelativeOutputPath, Hashtable model, bool force)
        {
            var templateFullPath = FindTemplateAssertExists(project, fileSystem, template);
            var templateContent  = fileSystem.ReadAllText(templateFullPath);

            templateContent = PreprocessTemplateContent(templateContent);

            // Create the T4 host and engine
            using (var host = new DynamicTextTemplatingEngineHost {
                TemplateFile = templateFullPath
            }) {
                var t4Engine = GetT4Engine();

                // Make it possible to reference the same assemblies that your project references
                // using <@ Assembly @> directives.
                host.AddFindableAssembly(FindProjectAssemblyIfExists(project));
                foreach (dynamic reference in ((dynamic)project.Object).References)
                {
                    if ((!string.IsNullOrEmpty(reference.Path)) && (!reference.AutoReferenced))
                    {
                        host.AddFindableAssembly(reference.Path);
                    }
                }

                string      projectRelativeOutputPathWithExtension = null;
                ProjectItem existingOutputProjectItem = null;
                if (!string.IsNullOrEmpty(projectRelativeOutputPath))
                {
                    // Respect the <#@ Output Extension="..." #> directive
                    projectRelativeOutputPathWithExtension = projectRelativeOutputPath + GetOutputExtension(host, t4Engine, templateContent);

                    // Resolve the output path and ensure it doesn't already exist (unless "Force" is set)
                    var outputDiskPath = Path.Combine(project.GetFullPath(), projectRelativeOutputPathWithExtension);
                    existingOutputProjectItem = project.GetProjectItem(projectRelativeOutputPathWithExtension);
                    if (existingOutputProjectItem != null)
                    {
                        outputDiskPath = existingOutputProjectItem.GetFullPath();
                    }
                    if ((!force) && fileSystem.FileExists(outputDiskPath))
                    {
                        return(new TemplateRenderingResult(projectRelativeOutputPathWithExtension)
                        {
                            SkipBecauseFileExists = true
                        });
                    }
                }

                // Convert the incoming Hashtable to a dynamic object with properties for each of the Hashtable entries
                host.Model = DynamicViewModel.FromObject(model);

                // Run the text transformation
                var templateOutput = t4Engine.ProcessTemplate(templateContent, host);
                return(new TemplateRenderingResult(projectRelativeOutputPathWithExtension)
                {
                    Content = templateOutput,
                    Errors = host.Errors,
                    ExistingProjectItemToOverwrite = existingOutputProjectItem,
                });
            }
        }
        static string ProcessTemplate(string exampleTemplateName, DynamicViewModel model)
        {
            var templateQualifiedFileName = ExampleTemplates.GetPath(exampleTemplateName);

            if (!File.Exists(templateQualifiedFileName))
            {
                throw new FileNotFoundException("File not found: " + exampleTemplateName);
            }

            // Read the text template.
            string input = File.ReadAllText(templateQualifiedFileName);

            // Eliminate Inherits="DynamicTransform" from template directive
            input = Regex.Replace(input, @"\<\#\@\s*\bTemplate\b(.*?\b)?Inherits=""DynamicTransform""\s*(.*?)\#\>", @"<#@ Template $1 $2 #>", RegexOptions.Singleline | RegexOptions.IgnoreCase);

            // Append "Model" property (all example templates are C#)
            input += ModelPropertyClassFeatures.ModelPropertySourceForLanguage["cs"];

            // Transform the text template.
            using (var host = new DynamicTextTemplatingEngineHost {
                TemplateFile = templateQualifiedFileName,
                Model = model
            }) {
                string output = new Engine().ProcessTemplate(input, host);
                if (host.Errors.HasErrors)
                {
                    throw new TemplateProcessingErrorException(host.Errors);
                }
                return(output);
            }
        }
示例#3
0
        private static string GetOutputExtension(DynamicTextTemplatingEngineHost host, ITextTemplatingEngine t4Engine, string templateContent)
        {
            string ignoredLanguage;

            string[] ignoreReferences;
            t4Engine.PreprocessTemplate(templateContent, host, "DummyClass", "DummyNamespace", out ignoredLanguage, out ignoreReferences);
            return(host.FileExtension ?? string.Empty);
        }