Пример #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());
        }
Пример #2
0
        private void GenerateOptionMembers(ExternalType type,
                                           HashSet <string> skip, bool isWidget)
        {
            bool preserveMemberCase = type.Attributes.Any(x =>
                                                          x.Type == "System.Runtime.CompilerServices.PreserveMemberCaseAttribute");

            var options = GetOptionMembers(type, isWidget);

            foreach (var option in options.Values)
            {
                if (skip != null &&
                    skip.Contains(option.Name))
                {
                    continue;
                }

                var typeName = FormatterTypeGenerator.GetOptionTypeName(option.Type);

                sb.AppendLine();
                cw.Indented("public ");
                sb.Append(typeName);
                sb.Append(" ");

                string jsName     = option.Name;
                string optionName = option.Name;
                var    prop       = option as ExternalProperty;
                if (prop != null)
                {
                    jsName = GetPropertyScriptName(prop, preserveMemberCase);
                }
                else if (type.Origin == ExternalTypeOrigin.TS)
                {
                    var emo = option as ExternalMethod;
                    if (emo != null && emo.Arguments.Count == 1)
                    {
                        if (jsName.StartsWith("set_"))
                        {
                            jsName     = jsName.Substring(4);
                            optionName = optionName.Substring(4);
                        }

                        typeName = FormatterTypeGenerator.GetOptionTypeName(emo.Arguments[0].Type);
                    }
                }

                if (Char.IsLower(optionName[0]))
                {
                    if (optionName == "id")
                    {
                        optionName = "ID";
                    }
                    else
                    {
                        optionName = Char.ToUpperInvariant(optionName[0]) +
                                     optionName.Substring(1);
                    }
                }

                sb.AppendLine(optionName);

                cw.InBrace(() =>
                {
                    cw.Indented("get { return GetOption<");
                    sb.Append(typeName);
                    sb.Append(">(\"");
                    sb.Append(jsName);
                    sb.AppendLine("\"); }");
                    cw.Indented("set { SetOption(\"");
                    sb.Append(jsName);
                    sb.AppendLine("\", value); }");
                });
            }
        }