Esempio n. 1
0
        protected override void Visit(RazorDirectiveChunk chunk)
        {
            var tokensBlock = new CSharpBlock();

            using (_context.Builder.UseBlock(tokensBlock))
            {
                Accept(chunk.Children);
            }

            IRazorDirective directive;
            var             tokens = tokensBlock.Children.OfType <RazorDirectiveToken>().ToList();

            if (chunk.Descriptor.Type == RazorDirectiveDescriptorType.SingleLine)
            {
                directive = new RazorSingleLineDirective()
                {
                    Name   = chunk.Name,
                    Tokens = tokens,
                };
            }
            else
            {
                var directiveChildren = tokensBlock.Children.Except(tokens);
                var directiveBlock    = new RazorBlockDirective()
                {
                    Name   = chunk.Name,
                    Tokens = tokens,
                };

                directiveBlock.Children.AddRange(directiveChildren);
                directive = directiveBlock;
            }

            _context.Builder.Add(directive);
        }
Esempio n. 2
0
        protected override void Visit(InjectChunk chunk)
        {
            // TODO: Remove this InjectChunk handling point, make the inject directive more generic.
            var injectTokens = new List <RazorDirectiveToken>
            {
                new RazorDirectiveToken
                {
                    Descriptor = new RazorDirectiveTokenDescriptor {
                        Type = RazorDirectiveTokenType.Type
                    },
                    Value = chunk.TypeName,
                },
                new RazorDirectiveToken
                {
                    Descriptor = new RazorDirectiveTokenDescriptor {
                        Type = RazorDirectiveTokenType.Member
                    },
                    Value = chunk.MemberName,
                }
            };

            var injectDirective = new RazorSingleLineDirective()
            {
                Name   = "inject",
                Tokens = injectTokens,
            };

            _context.Builder.Add(injectDirective);
        }
Esempio n. 3
0
        private static IRazorDirective CreateInjectDirective(string typeName, string memberName)
        {
            var injectTokens = new List <RazorDirectiveToken>
            {
                new RazorDirectiveToken
                {
                    Descriptor = new RazorDirectiveTokenDescriptor {
                        Type = RazorDirectiveTokenType.Type
                    },
                    Value = typeName,
                },
                new RazorDirectiveToken
                {
                    Descriptor = new RazorDirectiveTokenDescriptor {
                        Type = RazorDirectiveTokenType.Member
                    },
                    Value = memberName,
                }
            };

            var injectDirective = new RazorSingleLineDirective
            {
                Name   = "inject",
                Tokens = injectTokens,
            };

            return(injectDirective);
        }
Esempio n. 4
0
        public CSharpSourceTree Execute(RazorCodeDocument document, CSharpSourceTree sourceTree)
        {
            var modelType = GetDeclaredModelType(sourceTree);
            var classInfo = document.GetClassName();

            if (modelType == null)
            {
                // Insert a model directive into the system so sub-systems can rely on the model being the class.
                var modelTokens = new List <RazorDirectiveToken>()
                {
                    new RazorDirectiveToken
                    {
                        Descriptor = new RazorDirectiveTokenDescriptor {
                            Type = RazorDirectiveTokenType.Type
                        },
                        Value = classInfo.Class,
                    }
                };
                var modelDirective = new RazorSingleLineDirective()
                {
                    Name   = "model",
                    Tokens = modelTokens,
                };
                sourceTree.Children.Insert(0, modelDirective);
                modelType = classInfo.Class;
            }

            // Inject properties needed to execute Razor pages.
            var classDeclaration = FindClassDeclaration(sourceTree);

            var viewDataType = $"global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<{modelType}>";

            classDeclaration.Children.Insert(0, new RenderStatement()
            {
                Code = $"public {modelType} Model => ViewData.Model;",
            });

            var viewDataProperty = new RenderStatement
            {
                Code = $"public new {viewDataType} ViewData => ({viewDataType})base.ViewData;"
            };

            classDeclaration.Children.Insert(0, viewDataProperty);

            var injectHtmlHelper = CreateInjectDirective($"Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<{modelType}>", "Html");

            sourceTree.Children.Insert(0, injectHtmlHelper);

            var injectLogger = CreateInjectDirective($"Microsoft.Extensions.Logging.ILogger<{classInfo.Class}>", "Logger");

            sourceTree.Children.Insert(0, injectLogger);

            return(sourceTree);
        }
Esempio n. 5
0
        protected override void Visit(ModelChunk chunk)
        {
            // TODO: Remove this ModelChunk handling point, make the model directive more generic.

            var modelTokens = new List <RazorDirectiveToken>()
            {
                new RazorDirectiveToken
                {
                    Descriptor = new RazorDirectiveTokenDescriptor {
                        Type = RazorDirectiveTokenType.Type
                    },
                    Value = chunk.ModelType,
                }
            };
            var modelDirective = new RazorSingleLineDirective()
            {
                Name   = "model",
                Tokens = modelTokens,
            };

            _context.Builder.Add(modelDirective);
        }