Esempio n. 1
0
        internal static CachedTemplate GetCachedTemplate(string templateName, CompileAction action)
        {
            if (_cache.Count == 0)
            {
                CleanupOldTemplates();
            }

            CachedTemplate cachedTemplate = null;

            if (_cache.ContainsKey(templateName))
            {
                cachedTemplate = _cache[templateName];

                DateTime lastUpdate = File.GetLastWriteTime(templateName);

                // Has the file changed since we last cached it?
                if (lastUpdate > cachedTemplate.CompileTime)
                {
                    // Update it
                    cachedTemplate.CodeBuilder = Parser.ParseMarkup(templateName);

                    cachedTemplate.CompiledAssembly = null;
                    cachedTemplate.CompilerResults  = null;
                }

                if (action == CompileAction.Compile && cachedTemplate.CompiledAssembly == null)
                {
                    cachedTemplate.CompilerResults = Compiler.Compile(cachedTemplate.CodeBuilder);

                    if (!cachedTemplate.CompilerResults.Errors.HasErrors)
                    {
                        cachedTemplate.CompiledAssembly = cachedTemplate.CompilerResults.CompiledAssembly;
                    }
                }

                cachedTemplate.CompileTime = DateTime.Now;
            }
            else
            {
                cachedTemplate             = new CachedTemplate();
                cachedTemplate.Name        = templateName;
                cachedTemplate.CodeBuilder = Parser.ParseMarkup(templateName);

                if (action == CompileAction.Compile)
                {
                    CompilerResults results = Compiler.Compile(cachedTemplate.CodeBuilder);
                    cachedTemplate.CompiledAssembly = results.CompiledAssembly;
                    cachedTemplate.CompilerResults  = results;
                }

                cachedTemplate.CompileTime = DateTime.Now;
                _cache[templateName]       = cachedTemplate;
            }

            return(cachedTemplate);
        }
        internal static CachedTemplate GetCachedTemplate(string templateName, CompileAction action)
        {
            if (_cache.Count == 0)
            {
                CleanupOldTemplates();
            }

            CachedTemplate cachedTemplate = null;

            if (_cache.ContainsKey(templateName))
            {
                cachedTemplate = _cache[templateName];

                DateTime lastUpdate = File.GetLastWriteTime(templateName);

                // Has the file changed since we last cached it?
                if (lastUpdate > cachedTemplate.CompileTime)
                {
                    // Update it
                    cachedTemplate.CodeBuilder = Parser.ParseMarkup(templateName);

                    cachedTemplate.CompiledAssembly = null;
                    cachedTemplate.CompilerResults = null;
                }

                if (action == CompileAction.Compile && cachedTemplate.CompiledAssembly == null)
                {
                    cachedTemplate.CompilerResults = Compiler.Compile(cachedTemplate.CodeBuilder);

                    if (!cachedTemplate.CompilerResults.Errors.HasErrors)
                    {
                        cachedTemplate.CompiledAssembly = cachedTemplate.CompilerResults.CompiledAssembly;
                    }
                }

                cachedTemplate.CompileTime = DateTime.Now;
            }
            else
            {
                cachedTemplate = new CachedTemplate();
                cachedTemplate.Name = templateName;
                cachedTemplate.CodeBuilder = Parser.ParseMarkup(templateName);

                if (action == CompileAction.Compile)
                {
                    CompilerResults results = Compiler.Compile(cachedTemplate.CodeBuilder);
                    cachedTemplate.CompiledAssembly = results.CompiledAssembly;
                    cachedTemplate.CompilerResults = results;
                }

                cachedTemplate.CompileTime = DateTime.Now;
                _cache[templateName] = cachedTemplate;
            }

            return cachedTemplate;
        }
Esempio n. 3
0
        /// <summary>
        /// This method only parses the template so we can get to the header info.
        /// </summary>
        /// <param name="templateName">The template file path.</param>
        public void Parse(string templateName)
        {
            this._templateHeader = null;

            CachedTemplate cachedTemplate = TemplateCache.GetCachedTemplate(templateName, CompileAction.ParseOnly);

            _codeBuilder = cachedTemplate.CodeBuilder;
            _assembly    = cachedTemplate.CompiledAssembly;
            _results     = cachedTemplate.CompilerResults;
        }
Esempio n. 4
0
        /// <summary>
        /// Compiles a template, and set the current template information accordingly. Note that this does not execute the template.
        /// </summary>
        /// <param name="templateName">The template file path.</param>
        public void Compile(string templateName)
        {
            this._templateHeader = null;

            CachedTemplate cachedTemplate = TemplateCache.GetCachedTemplate(templateName, CompileAction.Compile);

            _codeBuilder = cachedTemplate.CodeBuilder;
            _assembly    = cachedTemplate.CompiledAssembly;
            _results     = cachedTemplate.CompilerResults;

            if (_results != null && _results.Errors.HasErrors)
            {
                throw new CompilerException(_results, this);
            }
        }