Ejemplo n.º 1
0
        public override void VisitUsingDirective(UsingDirectiveSyntax node)
        {
            if (node.ChildNodes().Any(x => x is NameEqualsSyntax))
            {
                var nameNode  = node.ChildNodes().FirstOrDefault(x => x is NameEqualsSyntax);
                var alternate = nameNode.ChildNodes().FirstOrDefault().ToString();
                var ns        = node.ChildNodes().LastOrDefault().ToString();

                UsingNamespaces.Add(new NamespaceInfo
                {
                    Namespace = ns,
                    Alternate = alternate,
                });
            }
            else
            {
                var ns = node.ChildNodes().FirstOrDefault().ToString();
                UsingNamespaces.Add(new NamespaceInfo
                {
                    Namespace = ns,
                    Alternate = string.Empty,
                });
            }

            base.VisitUsingDirective(node);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Add using namespace.
 /// </summary>
 /// <param name="namespace">namespace</param>
 protected void AddUsingNamespace(string @namespace)
 {
     if (UsingNamespaces != null && !UsingNamespaces.Contains(@namespace))
     {
         UsingNamespaces.Add(@namespace);
     }
 }
Ejemplo n.º 3
0
 public virtual bool Use(AST.Namespace ns, bool abi)
 {
     if (!abi && !UsingNamespaces.Contains(ns))
     {
         UsingNamespaces.Add(ns);
     }
     else if (abi && !UsingABINamespaces.Contains(ns))
     {
         UsingABINamespaces.Add(ns);
     }
     else
     {
         return(false);
     }
     return(true);
 }
Ejemplo n.º 4
0
 public void WriteUsingStatement(string @namespace)
 {
     UsingNamespaces.Add(@namespace);
 }
Ejemplo n.º 5
0
        public SortedDictionary <string, string> GenerateCode()
        {
            var codeByType = new Dictionary <Type, string>();
            var sb         = new StringBuilder();
            var cw         = new Serenity.Reflection.CodeWriter(sb, 4);

            foreach (var type in this.Assembly.GetTypes())
            {
                if (type.GetCustomAttribute(typeof(Serenity.ComponentModel.FormScriptAttribute)) == null)
                {
                    continue;
                }

                var ns = DoGetNamespace(type);

                sb.Clear();
                cw.Indented("public partial class ");
                sb.Append(DoGetTypeName(type));
                sb.AppendLine(" : PrefixedContext");
                cw.InBrace(delegate
                {
                    cw.Indented("public ");
                    sb.Append(DoGetTypeName(type));
                    sb.AppendLine("(string idPrefix) : base(idPrefix) {}");
                    sb.AppendLine();

                    foreach (var item in Serenity.PropertyGrid.PropertyItemHelper.GetPropertyItemsFor(type))
                    {
                        var editorType        = item.EditorType;
                        string widgetTypeName = null;
                        foreach (var rootNamespace in RootNamespaces)
                        {
                            string wn = rootNamespace + "." + editorType;
                            if (WidgetTypes.Contains(wn))
                            {
                                widgetTypeName = wn;
                                UsingNamespaces.Add(rootNamespace);
                                break;
                            }

                            wn += "Editor";
                            if (WidgetTypes.Contains(wn))
                            {
                                widgetTypeName = wn;
                                UsingNamespaces.Add(rootNamespace);
                                break;
                            }
                        }

                        if (widgetTypeName == null)
                        {
                            var wn = editorType;
                            if (!WidgetTypes.Contains(editorType))
                            {
                                wn = editorType + "Editor";
                            }

                            if (WidgetTypes.Contains(wn))
                            {
                                var idx = wn.LastIndexOf(".");
                                if (idx >= 0)
                                {
                                    UsingNamespaces.Add(wn.Substring(0, idx));
                                    widgetTypeName = wn.Substring(idx + 1);
                                }
                                else
                                {
                                    widgetTypeName = wn;
                                }
                            }
                        }

                        if (widgetTypeName == null)
                        {
                            continue;
                        }

                        if (widgetTypeName.StartsWith(ns + "."))
                        {
                            widgetTypeName = widgetTypeName.Substring(ns.Length + 1);
                        }
                        else
                        {
                            foreach (var rn in RootNamespaces)
                            {
                                if (widgetTypeName.StartsWith(rn + "."))
                                {
                                    widgetTypeName = widgetTypeName.Substring(rn.Length + 1);
                                }
                            }
                        }

                        cw.Indented("public ");
                        sb.Append(widgetTypeName);
                        sb.Append(" ");
                        sb.Append(item.Name);
                        sb.Append(" { get { return ById<");
                        sb.Append(widgetTypeName);
                        sb.Append(">(\"");
                        sb.Append(item.Name);
                        sb.AppendLine("\"); } }");
                    }
                });

                codeByType[type] = sb.ToString();
                sb.Clear();
            }

            var ordered     = codeByType.Keys.OrderBy(x => DoGetNamespace(x)).ThenBy(x => x.Name);
            var byNameSpace = ordered.ToLookup(x => DoGetNamespace(x));

            var result = new SortedDictionary <string, string>();

            foreach (var ns in byNameSpace.ToArray().OrderBy(x => x.Key))
            {
                foreach (var type in ns)
                {
                    sb.Clear();
                    sb.AppendLine();
                    cw.Indented("namespace ");
                    sb.AppendLine(ns.Key);
                    cw.InBrace(delegate
                    {
                        foreach (var usingNamespace in UsingNamespaces)
                        {
                            cw.Indented("using ");
                            sb.Append(usingNamespace);
                            sb.AppendLine(";");
                        }

                        sb.AppendLine();

                        int i = 0;

                        {
                            if (i++ > 0)
                            {
                                sb.AppendLine();
                            }

                            cw.IndentedMultiLine(codeByType[type].TrimEnd());
                        }
                    });

                    var filename = ns.Key + "." + DoGetTypeName(type) + ".cs";

                    foreach (var rn in RootNamespaces)
                    {
                        if (filename.StartsWith(rn + "."))
                        {
                            filename = filename.Substring(rn.Length + 1);
                        }
                    }

                    result.Add(filename, sb.ToString());
                }
            }

            return(result);
        }