public virtual string GenerateSource(TemplateOptions options)
 {
     CodeCompileUnit c = GetCompileUnit(options);
     StringWriter sw = new StringWriter();
     Provider.GenerateCodeFromCompileUnit(c, sw, GeneratorOptions);
     return sw.ToString();
 }
Exemple #2
0
 public WebFormsVisitor(TemplateOptions o)
     : base(o)
 {
     _sb = new StringBuilder();
     _stack = new Stack<StringBuilder>();
     _partialMethod = Options.GetPartialRenderMethod();
 }
        public virtual Type GenerateType(TemplateOptions options)
        {
            AddReferences(options);
            if (options.OutputDebugFiles && SupportsDebug())
            {
                CompilerOptions.GenerateInMemory = false;
                CompilerOptions.IncludeDebugInformation = true;
                var directoryInfo = GetNHamlTempDirectoryInfo();
                var classFileInfo = GetClassFileInfo(directoryInfo, ClassName);
                using (var writer = classFileInfo.CreateText())
                {
                    writer.Write(GenerateSource(options));
                }

                //TODO: when we move to vs2010 fully this ebcomes redundant as it will load the debug info for an in memory assembly.
                var tempFileName = Path.GetTempFileName();
                var tempAssemblyName = new FileInfo(Path.Combine(directoryInfo.FullName, tempFileName + ".dll"));
                var tempSymbolsName = new FileInfo(Path.Combine(directoryInfo.FullName, tempFileName + ".pdb"));
                try
                {
                    CompilerOptions.OutputAssembly = tempAssemblyName.FullName;
                    CompilerResults = Provider.CompileAssemblyFromFile(CompilerOptions, classFileInfo.FullName);
                    if (ContainsErrors())
                    {
                        return null;
                    }

                    var assembly = Assembly.Load(File.ReadAllBytes(tempAssemblyName.FullName), File.ReadAllBytes(tempSymbolsName.FullName));
                    return ExtractType(ClassName, assembly);
                }
                finally
                {
                    if (tempAssemblyName.Exists)
                    {
                        tempAssemblyName.Delete();
                    }
                    if (tempSymbolsName.Exists)
                    {
                        tempSymbolsName.Delete();
                    }
                }
            }
            else
            {
                CompilerOptions.GenerateInMemory = true;
                CompilerOptions.IncludeDebugInformation = false;
                CodeCompileUnit c = GetCompileUnit(options);
                CompilerResults = Provider.CompileAssemblyFromDom(CompilerOptions,c);
                if (ContainsErrors())
                {
                    return null;
                }
                var assembly = CompilerResults.CompiledAssembly;
                return ExtractType(ClassName, assembly);
            }
        }
Exemple #4
0
        public TemplateEngine( TemplateOptions options )
        {
            Invariant.ArgumentNotNull( options, "options" );

            Options = options;

            _compiledTemplateCache = new Dictionary<string, CompiledTemplate>();

            NHamlConfigurationSection.UpdateTemplateOptions( Options );
            Options.TemplateBaseTypeChanged += (sender, args) => ClearCompiledTemplatesCache();
            Options.TemplateCompilerChanged += (sender, args) => ClearCompiledTemplatesCache();
        }
Exemple #5
0
        /// <summary>
        /// Fills the page/control/master metadata using default values and returns the node that contains the main metadata
        /// </summary>
        /// <param name="metadata">The metadata container of the document</param>
        /// <returns>The page declaration metadata container</returns>
        public static MetaNode FillAndGetPageDefinition(Dictionary<string, List<MetaNode>> metadata, TemplateOptions options)
        {
            MetaNode pagedefiniton = null;
            List<MetaNode> data;

            if (metadata.ContainsKey("contentplaceholder"))
            {
                pagedefiniton = new MetaNode("master");
            }
            else
            {
                pagedefiniton = new MetaNode("page");
            }

            if (metadata.TryGetValue("page", out data))
            {
                pagedefiniton = data[0];
            }
            if (metadata.TryGetValue("control", out data))
            {
                pagedefiniton = data[0];
            }
            if (metadata.TryGetValue("master", out data))
            {
                pagedefiniton = data[0];
            }

            if (pagedefiniton.Attributes.Find(x => x.Name == "Language") == null)
            {
                pagedefiniton.Attributes.Add(new AttributeNode("Language") { Value = new TextNode(new TextChunk("C#")) });
            }

            if (pagedefiniton.Attributes.Find(x => x.Name == "AutoEventWireup") == null)
            {
                pagedefiniton.Attributes.Add(new AttributeNode("AutoEventWireup") { Value = new TextNode(new TextChunk("true")) });
            }

            if (!metadata.ContainsKey(pagedefiniton.Name))
            {
                metadata[pagedefiniton.Name] = new List<MetaNode> { pagedefiniton };
            }

            return pagedefiniton;
        }
Exemple #6
0
 public CodeDomVisitor(TemplateOptions options)
     : this()
 {
     Options = options;
 }
Exemple #7
0
 public HtmlVisitor()
 {
     Options = new TemplateOptions();
 }
Exemple #8
0
 public HtmlVisitor(TemplateOptions options)
 {
     Options = options;
 }
 public virtual TemplateFactory Compile(TemplateOptions options)
 {
     return new TemplateFactory(GenerateType(options));
 }
        protected virtual CodeCompileUnit GetCompileUnit(TemplateOptions options)
        {
            var compileUnit = new CodeCompileUnit();
            var testNamespace = new CodeNamespace();
            compileUnit.Namespaces.Add(testNamespace);

            foreach (var import in options.Usings)
            {
                var namespaceImport = new CodeNamespaceImport(import);
                testNamespace.Imports.Add(namespaceImport);
            }

            var declaration = new CodeTypeDeclaration
            {
                Name = ClassName,
                IsClass = true,
            };

            CodeMemberMethod[] ctm = new CodeMemberMethod[Visitor.Methods.Count];
            Visitor.Methods.Values.CopyTo(ctm, 0);
            declaration.BaseTypes.Add(options.TemplateBaseType);
            declaration.Members.AddRange(ctm);
            declaration.Members.Add(Visitor.RunContentMethod);
            declaration.Members.Add(Visitor.ContainsContentMethod);

            testNamespace.Types.Add(declaration);
            return compileUnit;
        }
        protected virtual void AddReferences(TemplateOptions options)
        {
            CompilerOptions.ReferencedAssemblies.Clear();

            foreach (var assembly in options.References)
            {
                CompilerOptions.ReferencedAssemblies.Add(assembly);
            }
        }
 public virtual void SetDocument(TemplateOptions options, DocumentNode node, string className)
 {
     if (options != null)
     {
         Visitor.Options = options;
     }
     _node = node;
     Visitor.Visit(node);
     ClassName = className;
 }