Пример #1
0
        public ProjectionResult Generate(RazorPageData pageData)
        {
            ISource template = new StringSource(this.Options.TemplateCode);

            Projector projector = new Projector(template);

            foreach (RazorFragment fragment in this.MergeContent(pageData.Content))
            {
                switch (fragment)
                {
                case SqlFragment sql:
                    projector.Open("execute")
                    .WriteLine($"WriteLiteral({CSharp.Literal(sql.Text)});");
                    break;

                case CodeFragment code when code.CodeType == CSharpType.Statement:
                    projector.Open("execute").WritePragma(code);
                    break;

                case CodeFragment code when code.CodeType == CSharpType.Expression:
                    projector.Open("execute")
                    .Write("Write(")
                    .WritePragma(code)
                    .WriteLine(");");
                    break;
                }
            }

            if (pageData.SourceChecksum != null)
            {
                projector.Open("pragmachecksum")
                .Write($"#pragma checksum \"{pageData.SourceName}\" \"{{ff1816ec-aa5e-4d10-87f7-6f4963833460}}\" \"{pageData.SourceChecksum}\"");
            }

            if (pageData.Model?.Text != null)
            {
                projector.Open("model").WritePragma(pageData.Model);
            }
            else
            {
                projector.Open("model").Write("dynamic");
            }

            if (pageData.Result?.Text != null)
            {
                projector.Open("result").WritePragma(pageData.Result);
            }
            else
            {
                projector.Open("result").Write("object");
            }

            if (pageData.Class?.Text != null)
            {
                projector.Open("class").WritePragma(pageData.Class);
            }
            else if (this.Options.Class?.Text != null)
            {
                projector.Open("class").Write(this.Options.Class.Text);
            }
            else
            {
                projector.Open("class").Write("MyRazorPage");
            }

            if (pageData.Template?.Text != null)
            {
                projector.Open("template")
                .Write("[global::Jerrycurl.Mvc.Metadata.Annotations.Template(")
                .WritePragma(pageData.Template)
                .Write(")]");
            }

            foreach (InjectDirective inject in pageData.Injections ?? new InjectDirective[0])
            {
                if (string.IsNullOrWhiteSpace(inject.Type.Text) || string.IsNullOrWhiteSpace(inject.Variable?.Text))
                {
                    projector.WriteWarning(inject.Type, "Injection ignored. Please specify both type and variable name.");
                }
                else
                {
                    projector.Open("injectiondefs")
                    .WriteLine("[global::Jerrycurl.Mvc.Metadata.Annotations.Inject]")
                    .Write("public ")
                    .WritePragma(inject.Type)
                    .WritePragma(inject.Variable)
                    .WriteLine(" { get; set; }");
                }
            }

            foreach (InjectDirective project in pageData.Projections ?? new InjectDirective[0])
            {
                projector.Open("injectiondefs");

                if (string.IsNullOrWhiteSpace(project.Type?.Text) || string.IsNullOrWhiteSpace(project.Variable?.Text))
                {
                    projector.WriteWarning(project.Type, "Projection ignored. Please specify both type and variable name.");
                }
                else
                {
                    projector.WriteLine("[global::Jerrycurl.Mvc.Metadata.Annotations.Inject]")
                    .Write("public global::Jerrycurl.Mvc.Projections.IProjection<")
                    .WritePragma(project.Type)
                    .Write("> ")
                    .WritePragma(project.Variable)
                    .WriteLine(" { get; set; }");
                }
            }

            foreach (RazorFragment import in this.Options.Imports ?? new RazorFragment[0])
            {
                projector.Open("globalimports")
                .Write("using ")
                .WritePragma(import, terminate: true)
                .WriteLine();
            }

            foreach (RazorFragment import in pageData.Imports ?? new RazorFragment[0])
            {
                projector.Open("localimports")
                .Write("using ")
                .WritePragma(import, terminate: true)
                .WriteLine();
            }

            if (pageData.Namespace?.Text != null)
            {
                projector.Open("beginnamespace")
                .Write($"namespace ")
                .WritePragma(pageData.Namespace)
                .WriteLine(" {");

                projector.Open("endnamespace")
                .Write("}");
            }
            else if (!string.IsNullOrEmpty(this.Options.Namespace?.Text))
            {
                projector.Open("beginnamespace")
                .Write($"namespace ")
                .Write(this.Options.Namespace.Text)
                .WriteLine(" {");

                projector.Open("endnamespace")
                .Write("}");
            }

            return(projector.Generate());
        }