private void Load() { // find our file CompiledDustTemplate temp = null; FileInfo saveTo = new FileInfo(GetSaveToPath()); if (saveTo.Exists) { // load it temp = saveTo.FromJsonFile <CompiledDustTemplate>(); } // compare the hashes if (temp != null && !string.IsNullOrEmpty(temp.Source) && temp.SourceHash.Equals(this.SourceHash) && temp.Name.Equals(this.Name)) { // if they match set our compiled to that of temp this.Compiled = temp.Compiled; } else { Compile(); } }
/// <summary> /// Compiles all the dust templates in the specified directory returning the combined /// result as a string and each individual template in a list as an out parameter. /// </summary> /// <param name="directory"></param> /// <param name="templates"></param> /// <param name="fileSearchPattern"></param> /// <param name="searchOption"></param> /// <param name="templateNamePrefix"></param> /// <param name="logger"></param> /// <returns></returns> public static string CompileTemplates(this DirectoryInfo directory, out List <ICompiledTemplate> templates, string fileSearchPattern = "*.dust", SearchOption searchOption = SearchOption.TopDirectoryOnly, string templateNamePrefix = "", ILogger logger = null) { StringBuilder compiled = new StringBuilder(); logger = logger ?? Log.Default; templates = new List <ICompiledTemplate>(); if (directory.Exists) { logger.AddEntry("DustScript::Compiling Dust Directory: {0}", directory.FullName); FileInfo[] files = directory.GetFiles(fileSearchPattern, searchOption); foreach (FileInfo file in files) { string templateName = Path.GetFileNameWithoutExtension(file.Name); if (searchOption == SearchOption.AllDirectories) { if (file.Directory.FullName.Replace("\\", "/") != directory.FullName.Replace("\\", "/")) { string prefix = file.Directory.FullName.TruncateFront(directory.FullName.Length + 1).Replace("\\", "_") + "_"; templateName = prefix + templateName; } } string templateFullName = templateNamePrefix + templateName; ICompiledTemplate template = new CompiledDustTemplate(file.FullName, templateFullName); logger.AddEntry("DustScript::Starting Dust compile: fileName={0}, templateName={1}", file.FullName, templateFullName); compiled.Append(";\r\n"); // TODO: do Regex.Unescape(tempalte.Compiled) to prevent having to do it everywhere else that references the resulting "compiled.ToString()" // such as below in Render. Do it here and it shouldn't be necessary anywhere else. compiled.Append(template.Compiled); compiled.Append(";\r\n"); logger.AddEntry("DustScript::Finished Dust compile: fileName={0}, templateName={1}", file.FullName, templateFullName); templates.Add(template); } } return(compiled.ToString()); }