Exemplo n.º 1
0
        public override void GenerateSourceCode(IEnumerable <IList <Chunk> > viewTemplates, IEnumerable <IList <Chunk> > allResources)
        {
            var globalSymbols = new Dictionary <string, object>();

            var writer = new StringWriter();
            var source = new SourceWriter(writer);

            // debug symbols not adjusted until the matching-directive issue resolved
            source.AdjustDebugSymbols = false;

            var usingGenerator     = new UsingNamespaceVisitor(source);
            var baseClassGenerator = new BaseClassVisitor {
                BaseClass = BaseClass
            };
            var globalsGenerator = new GlobalMembersVisitor(source, globalSymbols, NullBehaviour);

            // needed for proper vb functionality
            source.WriteLine("Option Infer On");

            usingGenerator.UsingNamespace("Microsoft.VisualBasic");
            foreach (var ns in UseNamespaces ?? new string[0])
            {
                usingGenerator.UsingNamespace(ns);
            }

            usingGenerator.UsingAssembly("Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
            foreach (var assembly in UseAssemblies ?? new string[0])
            {
                usingGenerator.UsingAssembly(assembly);
            }

            foreach (var resource in allResources)
            {
                usingGenerator.Accept(resource);
            }

            foreach (var resource in allResources)
            {
                baseClassGenerator.Accept(resource);
            }

            var viewClassName = "View" + GeneratedViewId.ToString("n");

            if (string.IsNullOrEmpty(TargetNamespace))
            {
                ViewClassFullName = viewClassName;
            }
            else
            {
                ViewClassFullName = TargetNamespace + "." + viewClassName;

                source.WriteLine();
                source.WriteLine(string.Format("Namespace {0}", TargetNamespace));
            }

            source.WriteLine();

            if (Descriptor != null)
            {
                // [SparkView] attribute
                source.WriteLine("<Global.Spark.SparkViewAttribute( _");
                if (TargetNamespace != null)
                {
                    source.WriteFormat("    TargetNamespace:=\"{0}\", _", TargetNamespace).WriteLine();
                }
                source.WriteLine("    Templates := New String() { _");
                source.Write("      ").Write(string.Join(", _\r\n      ",
                                                         Descriptor.Templates.Select(
                                                             t => "\"" + t + "\"").ToArray()));
                source.WriteLine("    })> _");
            }

            // public class ViewName : BasePageType
            source
            .Write("Public Class ")
            .WriteLine(viewClassName)
            .Write("    Inherits ")
            .WriteLine(baseClassGenerator.BaseClassTypeName)
            .AddIndent();

            source.WriteLine();
            source.WriteLine(string.Format("    Private Shared ReadOnly _generatedViewId As Global.System.Guid = New Global.System.Guid(\"{0:n}\")", GeneratedViewId));


            source
            .WriteLine("    Public Overrides ReadOnly Property GeneratedViewId() As Global.System.Guid")
            .WriteLine("      Get")
            .WriteLine("        Return _generatedViewId")
            .WriteLine("      End Get")
            .WriteLine("    End Property");

            if (Descriptor != null && Descriptor.Accessors != null)
            {
                //TODO: correct this
                foreach (var accessor in Descriptor.Accessors)
                {
                    source.WriteLine();
                    source.Write("    public ").WriteLine(accessor.Property);
                    source.Write("    { get { return ").Write(accessor.GetValue).WriteLine("; } }");
                }
            }

            // properties and macros
            foreach (var resource in allResources)
            {
                globalsGenerator.Accept(resource);
            }

            // public void RenderViewLevelx()
            int renderLevel = 0;

            foreach (var viewTemplate in viewTemplates)
            {
                source.WriteLine();
                EditorBrowsableStateNever(source, 4);
                source
                .WriteLine("Private Sub RenderViewLevel{0}()", renderLevel)
                .AddIndent();
                var viewGenerator = new GeneratedCodeVisitor(source, globalSymbols, NullBehaviour);
                viewGenerator.Accept(viewTemplate);
                source
                .RemoveIndent()
                .WriteLine("End Sub");
                ++renderLevel;
            }

            // public void RenderView()
            source.WriteLine();
            EditorBrowsableStateNever(source, 4);
            source
            .WriteLine("Public Overrides Sub Render()")
            .AddIndent();
            for (var invokeLevel = 0; invokeLevel != renderLevel; ++invokeLevel)
            {
                if (invokeLevel != renderLevel - 1)
                {
                    source
                    .WriteLine("Using OutputScope()")
                    .AddIndent()
                    .WriteLine("RenderViewLevel{0}()", invokeLevel)
                    .WriteLine("Content(\"view\") = Output")
                    .RemoveIndent()
                    .WriteLine("End Using");
                }
                else
                {
                    source
                    .WriteLine("RenderViewLevel{0}()", invokeLevel);
                }
            }
            source
            .RemoveIndent()
            .WriteLine("End Sub");


            source
            .RemoveIndent()
            .WriteLine("End Class");

            if (!string.IsNullOrEmpty(TargetNamespace))
            {
                source.WriteLine("End Namespace");
            }

            SourceCode     = source.ToString();
            SourceMappings = source.Mappings;
        }
Exemplo n.º 2
0
        public override void GenerateSourceCode(IEnumerable <IList <Chunk> > viewTemplates, IEnumerable <IList <Chunk> > allResources)
        {
            var globalSymbols = new Dictionary <string, object>();

            var writer = new StringWriter();
            var source = new SourceWriter(writer);

            var usingGenerator     = new UsingNamespaceVisitor(source);
            var baseClassGenerator = new BaseClassVisitor {
                BaseClass = BaseClass
            };
            var globalsGenerator = new GlobalMembersVisitor(source, globalSymbols, NullBehaviour);



            // using <namespaces>;
            foreach (var ns in UseNamespaces ?? new string[0])
            {
                usingGenerator.UsingNamespace(ns);
            }

            foreach (var assembly in UseAssemblies ?? new string[0])
            {
                usingGenerator.UsingAssembly(assembly);
            }

            foreach (var resource in allResources)
            {
                usingGenerator.Accept(resource);
            }

            foreach (var resource in allResources)
            {
                baseClassGenerator.Accept(resource);
            }

            var viewClassName = "View" + GeneratedViewId.ToString("n");

            if (string.IsNullOrEmpty(TargetNamespace))
            {
                ViewClassFullName = viewClassName;
            }
            else
            {
                ViewClassFullName = TargetNamespace + "." + viewClassName;

                source
                .WriteLine()
                .WriteLine(string.Format("namespace {0}", TargetNamespace))
                .WriteLine("{").AddIndent();
            }

            source.WriteLine();

            if (Descriptor != null)
            {
                // [SparkView] attribute
                source.WriteLine("[global::Spark.SparkViewAttribute(");
                if (TargetNamespace != null)
                {
                    source.WriteFormat("    TargetNamespace=\"{0}\",", TargetNamespace).WriteLine();
                }
                source.WriteLine("    Templates = new string[] {");
                source.Write("      ").WriteLine(string.Join(",\r\n      ",
                                                             Descriptor.Templates.Select(
                                                                 t => "\"" + SparkViewAttribute.ConvertToAttributeFormat(t) + "\"").ToArray()));
                source.WriteLine("    })]");
            }

            // public class ViewName : BasePageType
            source
            .Write("public class ")
            .Write(viewClassName)
            .Write(" : ")
            .WriteCode(baseClassGenerator.BaseClassTypeName)
            .WriteLine();
            source.WriteLine("{").AddIndent();

            source.WriteLine();
            EditorBrowsableStateNever(source, 4);
            source.WriteLine("private static System.Guid _generatedViewId = new System.Guid(\"{0:n}\");", GeneratedViewId);
            source.WriteLine("public override System.Guid GeneratedViewId");
            source.WriteLine("{ get { return _generatedViewId; } }");

            if (Descriptor != null && Descriptor.Accessors != null)
            {
                foreach (var accessor in Descriptor.Accessors)
                {
                    source.WriteLine();
                    source.Write("public ").WriteLine(accessor.Property);
                    source.Write("{ get { return ").Write(accessor.GetValue).WriteLine("; } }");
                }
            }

            // properties and macros
            foreach (var resource in allResources)
            {
                globalsGenerator.Accept(resource);
            }

            // public void RenderViewLevelx()
            int renderLevel = 0;

            foreach (var viewTemplate in viewTemplates)
            {
                source.WriteLine();
                EditorBrowsableStateNever(source, 4);
                source.WriteLine(string.Format("private void RenderViewLevel{0}()", renderLevel));
                source.WriteLine("{").AddIndent();
                var viewGenerator = new GeneratedCodeVisitor(source, globalSymbols, NullBehaviour);
                viewGenerator.Accept(viewTemplate);
                source.RemoveIndent().WriteLine("}");
                ++renderLevel;
            }

            // public void RenderView()

            source.WriteLine();
            EditorBrowsableStateNever(source, 4);
            source.WriteLine("public override void Render()");
            source.WriteLine("{").AddIndent();
            for (var invokeLevel = 0; invokeLevel != renderLevel; ++invokeLevel)

            {
                if (invokeLevel != renderLevel - 1)
                {
                    source.WriteLine("using (OutputScope()) {{DelegateFirstRender(RenderViewLevel{0}); Content[\"view\"] = Output;}}", invokeLevel);
                }
                else
                {
                    if (renderLevel <= 1)
                    {
                        source.WriteLine("        DelegateFirstRender(RenderViewLevel{0});", invokeLevel);
                    }
                    else
                    {
                        source.WriteLine("        RenderViewLevel{0}();", invokeLevel);
                    }
                }
            }
            source.RemoveIndent().WriteLine("}");

            // end class
            source.RemoveIndent().WriteLine("}");

            if (!string.IsNullOrEmpty(TargetNamespace))
            {
                source.RemoveIndent().WriteLine("}");
            }

            SourceCode     = source.ToString();
            SourceMappings = source.Mappings;
        }
        public override void GenerateSourceCode(IEnumerable <IList <Chunk> > viewTemplates, IEnumerable <IList <Chunk> > allResources)
        {
            Dictionary <string, object> globalSymbols = new Dictionary <string, object>();
            StringWriter          writer   = new StringWriter();
            SourceWriter          output   = new SourceWriter(writer);
            UsingNamespaceVisitor visitor  = new UsingNamespaceVisitor(output);
            BaseClassVisitor      visitor2 = new BaseClassVisitor {
                BaseClass = base.BaseClass
            };
            GlobalMembersVisitor visitor3 = new GlobalMembersVisitor(output, globalSymbols, base.NullBehaviour);

            foreach (string str in base.UseNamespaces ?? ((IEnumerable <string>) new string[0]))
            {
                visitor.UsingNamespace(str);
            }
            foreach (string str2 in base.UseAssemblies ?? ((IEnumerable <string>) new string[0]))
            {
                visitor.UsingAssembly(str2);
            }
            foreach (IList <Chunk> list in allResources)
            {
                visitor.Accept(list);
            }
            foreach (IList <Chunk> list2 in allResources)
            {
                visitor2.Accept(list2);
            }
            string str3 = "View" + base.GeneratedViewId.ToString("n");

            if (string.IsNullOrEmpty(base.TargetNamespace))
            {
                base.ViewClassFullName = str3;
            }
            else
            {
                base.ViewClassFullName = base.TargetNamespace + "." + str3;
                output.WriteLine().WriteLine(string.Format("namespace {0}", base.TargetNamespace)).WriteLine("{").AddIndent();
            }
            output.WriteLine();
            if (base.Descriptor != null)
            {
                output.WriteLine("[global::Spark.SparkViewAttribute(");
                if (base.TargetNamespace != null)
                {
                    output.WriteFormat("    TargetNamespace=\"{0}\",", new object[] { base.TargetNamespace }).WriteLine();
                }
                output.WriteLine("    Templates = new string[] {");
                output.Write("      ").WriteLine(string.Join(",\r\n      ", (from t in base.Descriptor.Templates select "\"" + SparkViewAttribute.ConvertToAttributeFormat(t) + "\"").ToArray <string>()));
                output.WriteLine("    })]");
            }
            output.Write("public class ").Write(str3).Write(" : ").WriteCode(visitor2.BaseClassTypeName).WriteLine();
            output.WriteLine("{").AddIndent();
            output.WriteLine();
            EditorBrowsableStateNever(output, 4);
            output.WriteLine("private static System.Guid _generatedViewId = new System.Guid(\"{0:n}\");", new object[] { base.GeneratedViewId });
            output.WriteLine("public override System.Guid GeneratedViewId");
            output.WriteLine("{ get { return _generatedViewId; } }");
            if ((base.Descriptor != null) && (base.Descriptor.Accessors != null))
            {
                foreach (SparkViewDescriptor.Accessor accessor in base.Descriptor.Accessors)
                {
                    output.WriteLine();
                    output.Write("public ").WriteLine(accessor.Property);
                    output.Write("{ get { return ").Write(accessor.GetValue).WriteLine("; } }");
                }
            }
            foreach (IList <Chunk> list3 in allResources)
            {
                visitor3.Accept(list3);
            }
            int num = 0;

            foreach (IList <Chunk> list4 in viewTemplates)
            {
                output.WriteLine();
                EditorBrowsableStateNever(output, 4);
                output.WriteLine(string.Format("private void RenderViewLevel{0}()", num));
                output.WriteLine("{").AddIndent();
                new GeneratedCodeVisitor(output, globalSymbols, base.NullBehaviour).Accept(list4);
                output.RemoveIndent().WriteLine("}");
                num++;
            }
            output.WriteLine();
            EditorBrowsableStateNever(output, 4);
            output.WriteLine("public override void Render()");
            output.WriteLine("{").AddIndent();
            for (int i = 0; i != num; i++)
            {
                if (i != (num - 1))
                {
                    output.WriteLine("using (OutputScope()) {{RenderViewLevel{0}(); Content[\"view\"] = Output;}}", new object[] { i });
                }
                else
                {
                    output.WriteLine("        RenderViewLevel{0}();", new object[] { i });
                }
            }
            output.RemoveIndent().WriteLine("}");
            output.RemoveIndent().WriteLine("}");
            if (!string.IsNullOrEmpty(base.TargetNamespace))
            {
                output.RemoveIndent().WriteLine("}");
            }
            base.SourceCode     = output.ToString();
            base.SourceMappings = output.Mappings;
        }
Exemplo n.º 4
0
        public override void GenerateSourceCode(IEnumerable <IList <Chunk> > viewTemplates, IEnumerable <IList <Chunk> > allResources)
        {
            Dictionary <string, object> globalSymbols = new Dictionary <string, object>();
            StringWriter writer = new StringWriter();
            SourceWriter output = new SourceWriter(writer)
            {
                AdjustDebugSymbols = false
            };
            UsingNamespaceVisitor visitor  = new UsingNamespaceVisitor(output);
            BaseClassVisitor      visitor2 = new BaseClassVisitor {
                BaseClass = base.BaseClass
            };
            GlobalMembersVisitor visitor3 = new GlobalMembersVisitor(output, globalSymbols, base.NullBehaviour);

            output.WriteLine("Option Infer On");
            visitor.UsingNamespace("Microsoft.VisualBasic");
            foreach (string str in base.UseNamespaces ?? ((IEnumerable <string>) new string[0]))
            {
                visitor.UsingNamespace(str);
            }
            visitor.UsingAssembly("Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
            foreach (string str2 in base.UseAssemblies ?? ((IEnumerable <string>) new string[0]))
            {
                visitor.UsingAssembly(str2);
            }
            foreach (IList <Chunk> list in allResources)
            {
                visitor.Accept(list);
            }
            foreach (IList <Chunk> list2 in allResources)
            {
                visitor2.Accept(list2);
            }
            string str3 = "View" + base.GeneratedViewId.ToString("n");

            if (string.IsNullOrEmpty(base.TargetNamespace))
            {
                base.ViewClassFullName = str3;
            }
            else
            {
                base.ViewClassFullName = base.TargetNamespace + "." + str3;
                output.WriteLine();
                output.WriteLine(string.Format("Namespace {0}", base.TargetNamespace));
            }
            output.WriteLine();
            if (base.Descriptor != null)
            {
                output.WriteLine("<Global.Spark.SparkViewAttribute( _");
                if (base.TargetNamespace != null)
                {
                    output.WriteFormat("    TargetNamespace:=\"{0}\", _", new object[] { base.TargetNamespace }).WriteLine();
                }
                output.WriteLine("    Templates := New String() { _");
                output.Write("      ").Write(string.Join(", _\r\n      ", (from t in base.Descriptor.Templates select "\"" + SparkViewAttribute.ConvertToAttributeFormat(t) + "\"").ToArray <string>()));
                output.WriteLine("    })> _");
            }
            output.Write("Public Class ").WriteLine(str3).Write("    Inherits ").WriteLine((string)visitor2.BaseClassTypeName).AddIndent();
            output.WriteLine();
            output.WriteLine(string.Format("    Private Shared ReadOnly _generatedViewId As Global.System.Guid = New Global.System.Guid(\"{0:n}\")", base.GeneratedViewId));
            output.WriteLine("    Public Overrides ReadOnly Property GeneratedViewId() As Global.System.Guid").WriteLine("      Get").WriteLine("        Return _generatedViewId").WriteLine("      End Get").WriteLine("    End Property");
            if ((base.Descriptor != null) && (base.Descriptor.Accessors != null))
            {
                foreach (SparkViewDescriptor.Accessor accessor in base.Descriptor.Accessors)
                {
                    output.WriteLine();
                    output.Write("    public ").WriteLine(accessor.Property);
                    output.Write("    { get { return ").Write(accessor.GetValue).WriteLine("; } }");
                }
            }
            foreach (IList <Chunk> list3 in allResources)
            {
                visitor3.Accept(list3);
            }
            int num = 0;

            foreach (IList <Chunk> list4 in viewTemplates)
            {
                output.WriteLine();
                EditorBrowsableStateNever(output, 4);
                output.WriteLine("Private Sub RenderViewLevel{0}()", new object[] { num }).AddIndent();
                new GeneratedCodeVisitor(output, globalSymbols, base.NullBehaviour).Accept(list4);
                output.RemoveIndent().WriteLine("End Sub");
                num++;
            }
            output.WriteLine();
            EditorBrowsableStateNever(output, 4);
            output.WriteLine("Public Overrides Sub Render()").AddIndent();
            for (int i = 0; i != num; i++)
            {
                if (i != (num - 1))
                {
                    output.WriteLine("Using OutputScope()").AddIndent().WriteLine("RenderViewLevel{0}()", new object[] { i }).WriteLine("Content(\"view\") = Output").RemoveIndent().WriteLine("End Using");
                }
                else
                {
                    output.WriteLine("RenderViewLevel{0}()", new object[] { i });
                }
            }
            output.RemoveIndent().WriteLine("End Sub");
            output.RemoveIndent().WriteLine("End Class");
            if (!string.IsNullOrEmpty(base.TargetNamespace))
            {
                output.WriteLine("End Namespace");
            }
            base.SourceCode     = output.ToString();
            base.SourceMappings = output.Mappings;
        }