Example #1
0
        public string GenerateCode()
        {
            var codeByType = new Dictionary<string, string>();
            var sb = new StringBuilder();
            var cw = new Serenity.Reflection.CodeWriter(sb, 4);

            foreach (var key in EditorTypes.Keys)
            {
                var ns = DoGetNamespace(key);

                var editorInfo = EditorTypes[key];

                sb.Clear();
                cw.Indented("public partial class ");
                var type = DoGetTypeName(key);
                sb.Append(type);

                // yes it's ugly, but backward compatible
                if (editorInfo.Options.Count >= lookupEditorBaseOptions.Length &&
                    lookupEditorBaseOptions.All(x => editorInfo.Options.ContainsKey(x)))
                {
                    sb.AppendLine(" : LookupEditorBaseAttribute");
                    foreach (var x in lookupEditorBaseOptions)
                        editorInfo.Options.Remove(x);
                }
                else
                    sb.AppendLine(" : CustomEditorAttribute");

                cw.InBrace(delegate
                {
                    cw.Indented("public const string Key = \"");
                    sb.Append(key);
                    sb.AppendLine("\";");
                    sb.AppendLine();

                    cw.Indented("public ");
                    sb.Append(type);
                    sb.AppendLine("()");
                    cw.IndentedLine("    : base(Key)");
                    cw.IndentedLine("{");
                    cw.IndentedLine("}");
                    var opt = editorInfo.Options.Keys.ToList();
                    opt.Sort();

                    foreach (var item in opt)
                    {
                        var option = editorInfo.Options[item];
                        var typeName = FormatterTypeGenerator.GetOptionTypeName(option.Type);

                        sb.AppendLine();
                        cw.Indented("public ");
                        sb.Append(typeName);
                        sb.Append(" ");
                        sb.AppendLine(item);
                        cw.InBrace(() =>
                        {
                            string propName = item.Substring(0, 1).ToLowerInvariant() + item.Substring(1);
                            if (item == "ID")
                                propName = "id";
                            cw.Indented("get { return GetOption<");
                            sb.Append(typeName);
                            sb.Append(">(\"");
                            sb.Append(propName);
                            sb.AppendLine("\"); }");
                            cw.Indented("set { SetOption(\"");
                            sb.Append(propName);
                            sb.AppendLine("\", value); }");
                        });
                    }
                });

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

            sb.Clear();
            sb.AppendLine();

            foreach (var ns in UsingNamespaces)
            {
                cw.Indented("using ");
                sb.Append(ns);
                sb.AppendLine(";");
            }

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

            foreach (var ns in byNameSpace.ToArray().OrderBy(x => x.Key))
            {
                sb.AppendLine();
                cw.Indented("namespace ");
                sb.AppendLine(ns.Key);
                cw.InBrace(delegate
                {
                    int i = 0;
                    foreach (var type in ns)
                    {
                        if (i++ > 0)
                            sb.AppendLine();

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

            return sb.ToString();
        }
        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 assembly in Assemblies)
            foreach (var type in assembly.GetTypes())
            {
                var formScriptAttribute = type.GetCustomAttribute<FormScriptAttribute>();
                if (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("[InlineConstant] public const string FormKey = \"");
                    sb.Append(formScriptAttribute.Key);
                    sb.AppendLine("\";");
                    sb.AppendLine();
                    
                    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";
                        string widgetTypeName = null;
                        foreach (var rootNamespace in RootNamespaces)
                        {
                            string wn = rootNamespace + "." + editorType;
                            if (WidgetTypes.Contains(wn))
                            {
                                widgetTypeName = wn;
                                break;
                            }

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

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

                            if (WidgetTypes.Contains(wn))
                                widgetTypeName = wn;
                        }

                        if (widgetTypeName == null)
                            continue;

                        var fullName = widgetTypeName;

                        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(" { ");
                        sb.Append("[InlineCode(\"{this}.w('");
                        sb.Append(item.Name);
                        sb.Append("', ");
                        sb.Append(fullName);
                        sb.AppendLine(")\")] get; private set; }");
                    }
                });

                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.ToArray().OrderBy(x => x))
                        {
                            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;
        }
        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 assembly in Assemblies)
            {
                foreach (var type in assembly.GetTypes())
                {
                    var formScriptAttribute = type.GetCustomAttribute <FormScriptAttribute>();
                    if (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("[InlineConstant] public const string FormKey = \"");
                        sb.Append(formScriptAttribute.Key);
                        sb.AppendLine("\";");
                        sb.AppendLine();

                        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";
                            string widgetTypeName = null;
                            foreach (var rootNamespace in RootNamespaces)
                            {
                                string wn = rootNamespace + "." + editorType;
                                if (WidgetTypes.Contains(wn))
                                {
                                    widgetTypeName = wn;
                                    break;
                                }

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

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

                                if (WidgetTypes.Contains(wn))
                                {
                                    widgetTypeName = wn;
                                }
                            }

                            if (widgetTypeName == null)
                            {
                                continue;
                            }

                            var fullName = widgetTypeName;

                            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(" { ");
                            sb.Append("[InlineCode(\"{this}.w('");
                            sb.Append(item.Name);
                            sb.Append("', ");
                            sb.Append(fullName);
                            sb.AppendLine(")\")] get; private set; }");
                        }
                    });

                    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.ToArray().OrderBy(x => x))
                        {
                            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);
        }
Example #4
0
        public string GenerateCode()
        {
            var codeByType = new Dictionary <string, string>();
            var sb         = new StringBuilder();
            var cw         = new Serenity.Reflection.CodeWriter(sb, 4);

            foreach (var key in FormatterTypes.Keys)
            {
                var ns = DoGetNamespace(key);

                var formatterInfo = FormatterTypes[key];

                sb.Clear();
                cw.Indented("public partial class ");
                var type = DoGetTypeName(key);
                sb.Append(type);
                sb.AppendLine(" : CustomFormatterAttribute");
                cw.InBrace(delegate
                {
                    cw.Indented("public const string Key = \"");
                    sb.Append(key);
                    sb.AppendLine("\";");
                    sb.AppendLine();

                    cw.Indented("public ");
                    sb.Append(type);
                    sb.AppendLine("()");
                    cw.IndentedLine("    : base(Key)");
                    cw.IndentedLine("{");
                    cw.IndentedLine("}");

                    var opt = formatterInfo.Options.Keys.ToList();
                    opt.Sort();

                    foreach (var item in opt)
                    {
                        var option   = formatterInfo.Options[item];
                        var typeName = GetOptionTypeName(option.Type);

                        sb.AppendLine();
                        cw.Indented("public ");
                        sb.Append(typeName);
                        sb.Append(" ");
                        sb.AppendLine(item);
                        cw.InBrace(() =>
                        {
                            string propName = item.Substring(0, 1).ToLowerInvariant() + item.Substring(1);
                            if (item == "ID")
                            {
                                propName = "id";
                            }
                            cw.Indented("get { return GetOption<");
                            sb.Append(typeName);
                            sb.Append(">(\"");
                            sb.Append(propName);
                            sb.AppendLine("\"); }");
                            cw.Indented("set { SetOption(\"");
                            sb.Append(propName);
                            sb.AppendLine("\", value); }");
                        });
                    }
                });

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

            sb.Clear();
            sb.AppendLine();

            foreach (var ns in UsingNamespaces)
            {
                cw.Indented("using ");
                sb.Append(ns);
                sb.AppendLine(";");
            }

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

            foreach (var ns in byNameSpace.ToArray().OrderBy(x => x.Key))
            {
                sb.AppendLine();
                cw.Indented("namespace ");
                sb.AppendLine(ns.Key);
                cw.InBrace(delegate
                {
                    int i = 0;
                    foreach (var type in ns)
                    {
                        if (i++ > 0)
                        {
                            sb.AppendLine();
                        }

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

            return(sb.ToString());
        }
Example #5
0
        public string GenerateCode()
        {
            var codeByType = new Dictionary <string, string>();
            var sb         = new StringBuilder();
            var cw         = new Serenity.Reflection.CodeWriter(sb, 4);

            foreach (var key in EditorTypes.Keys)
            {
                var ns = DoGetNamespace(key);

                var editorKey  = key;
                var editorInfo = EditorTypes[key];
                foreach (var rn in RootNamespaces)
                {
                    if (key.StartsWith(rn + "."))
                    {
                        editorKey = editorKey.Substring(rn.Length + 1);
                        break;
                    }
                }

                if (editorKey.EndsWith("Editor"))
                {
                    editorKey = editorKey.Substring(0, editorKey.Length - "Editor".Length);
                }

                sb.Clear();
                cw.Indented("public partial class ");
                var type = DoGetTypeName(key);
                sb.Append(type);
                sb.AppendLine(" : CustomEditorAttribute");
                cw.InBrace(delegate
                {
                    cw.Indented("public ");
                    sb.Append(type);
                    sb.AppendLine("()");
                    cw.Indented("    : base(\"");
                    sb.Append(editorKey);
                    sb.AppendLine("\")");
                    cw.IndentedLine("{");
                    cw.IndentedLine("}");
                    var opt = editorInfo.Options.Keys.ToList();
                    opt.Sort();

                    foreach (var item in opt)
                    {
                        var option         = editorInfo.Options[item];
                        var typeName       = option.Type;
                        var nullablePrefix = "System.Nullable`1";
                        bool nullable      = option.Type.StartsWith(nullablePrefix);
                        if (nullable)
                        {
                            typeName = typeName.Substring(nullablePrefix.Length + 1, typeName.Length - nullablePrefix.Length - 2);
                        }

                        var systemType = Type.GetType(typeName);
                        if (systemType == null)
                        {
                            typeName = "object";
                        }
                        else if (typeName.StartsWith("System."))
                        {
                            typeName = typeName.Substring(7);
                        }

                        sb.AppendLine();
                        cw.Indented("public ");
                        sb.Append(typeName);
                        //if (nullable) //Attribute name argument nullable problem
                        //    sb.Append("?");
                        sb.Append(" ");
                        sb.AppendLine(item);
                        cw.InBrace(() =>
                        {
                            string propName = item.Substring(0, 1).ToLowerInvariant() + item.Substring(1);
                            if (item == "ID")
                            {
                                propName = "id";
                            }
                            cw.Indented("get { return GetOption<");
                            sb.Append(typeName);
                            //if (nullable) //Attribute name argument nullable problem
                            //    sb.Append("?");
                            sb.Append(">(\"");
                            sb.Append(propName);
                            sb.AppendLine("\"); }");
                            cw.Indented("set { SetOption(\"");
                            sb.Append(propName);
                            sb.AppendLine("\", value); }");
                        });
                    }
                });

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

            sb.Clear();
            sb.AppendLine();

            foreach (var ns in UsingNamespaces)
            {
                cw.Indented("using ");
                sb.Append(ns);
                sb.AppendLine(";");
            }

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

            foreach (var ns in byNameSpace.ToArray().OrderBy(x => x.Key))
            {
                sb.AppendLine();
                cw.Indented("namespace ");
                sb.AppendLine(ns.Key);
                cw.InBrace(delegate
                {
                    int i = 0;
                    foreach (var type in ns)
                    {
                        if (i++ > 0)
                        {
                            sb.AppendLine();
                        }

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

            return(sb.ToString());
        }
Example #6
0
        public 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.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.Web.PropertyEditor.PropertyEditorHelper.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)
                        {
                            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();
            }

            sb.Clear();
            sb.AppendLine();

            foreach (var ns in UsingNamespaces)
            {
                cw.Indented("using ");
                sb.Append(ns);
                sb.AppendLine(";");
            }

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

            foreach (var ns in byNameSpace.ToArray().OrderBy(x => x.Key))
            {
                sb.AppendLine();
                cw.Indented("namespace ");
                sb.AppendLine(ns.Key);
                cw.InBrace(delegate
                {
                    int i = 0;
                    foreach (var type in ns)
                    {
                        if (i++ > 0)
                        {
                            sb.AppendLine();
                        }

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

            return(sb.ToString());
        }