ProcessTemplate() публичный Метод

public ProcessTemplate ( string content, ITextTemplatingEngineHost host ) : string
content string
host ITextTemplatingEngineHost
Результат string
Пример #1
0
        public bool ProcessTemplate(string inputFileName, string inputContent, ref string outputFileName, out string outputContent)
        {
            errors.Clear();
            encoding = Encoding.UTF8;

            outputFile     = outputFileName;
            inputFile      = inputFileName;
            outputContent  = Engine.ProcessTemplate(inputContent, this);
            outputFileName = outputFile;

            return(!errors.HasErrors);
        }
Пример #2
0
        public bool ProcessTemplate(string inputFileName, string inputContent, ref string outputFileName, out string outputContent)
        {
            Errors.Clear();
            encoding = Utf8.BomlessEncoding;

            OutputFile     = outputFileName;
            TemplateFile   = inputFileName;
            outputContent  = Engine.ProcessTemplate(inputContent, this);
            outputFileName = OutputFile;

            return(!Errors.HasErrors);
        }
Пример #3
0
        /// <summary>
        /// Generates this project template to the specified output directory.
        /// </summary>
        /// <param name="outputDirectory">The output directory.</param>
        /// <param name="projectName">Name of the project.</param>
        /// <param name="projectGuid">The project unique identifier.</param>
        /// <param name="log">The log to output errors to.</param>
        /// <param name="options">The options arguments that will be made available through the Session property in each template.</param>
        /// <param name="generatedOutputFiles">The generated files.</param>
        /// <exception cref="System.ArgumentNullException">outputDirectory
        /// or
        /// projectName</exception>
        /// <exception cref="System.InvalidOperationException">FilePath cannot be null on this instance</exception>
        public void Generate(string outputDirectory, string projectName, Guid projectGuid, ILogger log, Dictionary<string, object> options = null, List<string> generatedOutputFiles = null )
        {
            if (outputDirectory == null) throw new ArgumentNullException("outputDirectory");
            if (projectName == null) throw new ArgumentNullException("projectName");
            if (log == null) throw new ArgumentNullException("log");
            if (FilePath == null) throw new InvalidOperationException("FilePath cannot be null on this instance");

            try
            {
                // Check Project template filepath
                var templateDirectory = new FileInfo(FilePath).Directory;
                if (templateDirectory == null || !templateDirectory.Exists)
                {
                    log.Error("Invalid ProjectTemplate directory [{0}]", FilePath);
                    return;
                }

                // Creates the output directory
                var directory = new DirectoryInfo(outputDirectory);
                if (!directory.Exists)
                {
                    directory.Create();
                }

                // Create expando object from options valid for the whole life of generating a project template
                var expandoOptions = new ExpandoObject();
                var expandoOptionsAsDictionary = (IDictionary<string, object>)expandoOptions;
                expandoOptionsAsDictionary["ProjectName"] = projectName;
                expandoOptionsAsDictionary["ProjectGuid"] = projectGuid;
                if (options != null)
                {
                    foreach (var option in options)
                    {
                        expandoOptionsAsDictionary[option.Key] = option.Value;
                    }
                }

                var engine = new TemplatingEngine();

                // In case this project template is dynamic, we need to generate its content first through T4
                if (IsDynamicTemplate)
                {
                    var content = File.ReadAllText(FilePath);
                    var host = new ProjectTemplatingHost(log, FilePath, templateDirectory.FullName, expandoOptions, Assemblies.Select(assembly => assembly.FullPath));
                    var newTemplateAsString = engine.ProcessTemplate(content, host);
                    Files.Clear();
                    using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(newTemplateAsString)))
                    {
                        var newTemplate = (ProjectTemplate)YamlSerializer.Deserialize(stream);
                        Files.AddRange(newTemplate.Files);
                    }
                }

                // Iterate on each files
                foreach (var fileItem in Files)
                {
                    if (fileItem.Source == null)
                    {
                        log.Warning("Invalid empty file item [{0}] with no source location", fileItem);
                        continue;
                    }
                    var sourceFilePath = System.IO.Path.Combine(templateDirectory.FullName, fileItem.Source);
                    var targetLocation = fileItem.Target ?? fileItem.Source;
                    if (Path.IsPathRooted(targetLocation))
                    {
                        log.Error("Invalid file item [{0}]. TargetLocation must be a relative path", fileItem);
                        continue;
                    }

                    var targetLocationExpanded = Expand(targetLocation, expandoOptionsAsDictionary, log);

                    // If this is a template file, turn template on by default
                    if (fileItem.IsTemplate)
                    {
                        var targetPath = Path.GetDirectoryName(targetLocationExpanded);
                        var targetFileName = Path.GetFileName(targetLocationExpanded);
                        targetLocationExpanded = targetPath != null ? Path.Combine(targetPath, targetFileName) : targetFileName;
                    }

                    var targetFilePath = Path.Combine(outputDirectory, targetLocationExpanded);
                    try
                    {
                        // Make sure that the target directory does exist
                        var targetDirectory = new FileInfo(targetFilePath).Directory;
                        if (!targetDirectory.Exists)
                        {
                            targetDirectory.Create();
                        }

                        bool fileGenerated = false;
                        if (fileItem.IsTemplate)
                        {
                            var content = File.ReadAllText(sourceFilePath);
                            var host = new ProjectTemplatingHost(log, sourceFilePath, templateDirectory.FullName, expandoOptions, Assemblies.Select(assembly => assembly.FullPath));
                            var newContent = engine.ProcessTemplate(content, host);
                            if (newContent != null)
                            {
                                fileGenerated = true;
                                File.WriteAllText(targetFilePath, newContent);
                            }
                        }
                        else
                        {
                            fileGenerated = true;
                            File.Copy(sourceFilePath, targetFilePath, true);
                        }

                        if (generatedOutputFiles != null && fileGenerated)
                        {
                            generatedOutputFiles.Add(targetFilePath);
                        }
                    }
                    catch (Exception ex)
                    {

                        log.Error("Unexpected exception while processing [{0}]", fileItem, ex);
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error("Unexpected exception while processing project template [{0}] to directory [{1}]", projectName, outputDirectory, ex);
            }
        }
Пример #4
0
        public string GeneratePart(string templatePathPart, ILogger log, Dictionary<string, object> options)
        {
            if (templatePathPart == null) throw new ArgumentNullException("templatePathPart");
            if (log == null) throw new ArgumentNullException("log");
            var expandoOptions = new ExpandoObject();
            var expandoOptionsAsDictionary = (IDictionary<string, object>)expandoOptions;
            foreach (var option in options)
            {
                expandoOptionsAsDictionary[option.Key] = option.Value;
            }

            var templateDirectory = new FileInfo(FilePath).Directory;
            var sourceFilePath = System.IO.Path.Combine(templateDirectory.FullName, templatePathPart);
            var content = File.ReadAllText(sourceFilePath);

            var engine = new TemplatingEngine();
            var host = new ProjectTemplatingHost(log, sourceFilePath, templateDirectory.FullName, expandoOptions, Assemblies.Select(assembly => assembly.FullPath));
            return engine.ProcessTemplate(content, host);            
        }