Esempio n. 1
0
        public CSharpSourceTree Execute(RazorCodeDocument document, RazorChunkTree chunkTree)
        {
            var builder = new SourceTreeBuilder();
            var context = new CSharpSourceLoweringContext
            {
                Builder        = builder,
                Host           = _host,
                SourceFileName = document.Source.Filename
            };

            var checksum = new Checksum
            {
                FileName = document.Source.Filename,
                Guid     = Sha1AlgorithmId,
                Bytes    = document.GetChecksumBytes()
            };

            builder.Add(checksum);

            var classInfo = document.GetClassName();

            using (builder.BuildBlock <NamespaceDeclaration>(declaration => declaration.Namespace = classInfo.Namespace))
            {
                AddNamespaceImports(chunkTree, context);

                using (builder.BuildBlock <ViewClassDeclaration>(
                           declaration =>
                {
                    declaration.Accessor = "public";
                    declaration.Name = classInfo.Class;

                    var baseTypeVisitor = new BaseTypeVisitor();
                    baseTypeVisitor.Accept(chunkTree.Root);
                    declaration.BaseTypeName = baseTypeVisitor.BaseTypeName ?? _host.DefaultBaseClass;
                }))
                {
                    new TagHelperFieldDeclarationVisitor(context).Accept(chunkTree.Root);
                    new FunctionsDirectiveVisitor(context).Accept(chunkTree.Root);

                    using (builder.BuildBlock <ExecuteMethodDeclaration>(
                               declaration =>
                    {
                        declaration.Accessor = "public";
                        declaration.Modifiers = new[] { "override", "async" };
                        declaration.ReturnTypeName = typeof(Task).FullName;
                        declaration.Name = _host.GeneratedClassContext.ExecuteMethodName;
                    }))
                    {
                        new ExecuteMethodBodyVisitor(context).Accept(chunkTree.Root);
                    }
                }
            }

            return(builder.Root);
        }
        public static void SetChunkTree(this RazorCodeDocument document, RazorChunkTree chunkTree)
        {
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            if (chunkTree == null)
            {
                throw new ArgumentNullException(nameof(chunkTree));
            }

            document.Items[typeof(RazorChunkTree)] = chunkTree;
        }
Esempio n. 3
0
        private void AddNamespaceImports(RazorChunkTree chunkTree, CSharpSourceLoweringContext context)
        {
            var defaultImports = _host.NamespaceImports;

            foreach (var import in defaultImports)
            {
                var importSource = new ImportNamespace
                {
                    Namespace = import
                };
                context.Builder.Add(importSource);
            }

            new UsingDirectiveVisitor(defaultImports, context).Accept(chunkTree.Root);
        }